-
In analogy with http://forum.espruino.com/conversations/302392/ I coded a "drawString" override that introduces proper line spacing in Strings witch "\n" newlines in it. In this case I have chosen for 5 pixels of space between each line..
Rather new to Espruino so maybe not the best solution, but it works for me.
Hopefully Gordon will come up with a structural solution.
Example code:
// override drawString function to allocate line spacing g._ds = g.drawString; g.drawString = function(s,x,y) { s=s.toString(); sa=s.split("\n"); print(sa); spacing=5+g.getFontHeight(); sa.forEach(function(line){ g._ds(line,x,y); y+=spacing; }); }; g.clear(); g.setFontVector(20); g.setColor(1,1,1); g.drawString("Temp:\nBattery:%\nCharging\nUptime: 0.00\nFreeFlash: \nFreeStor:",0,30);
And the resulting output:
-
As can be seen in the picture the beginletters of the lines (T B C) have zero spacing.
And in the case of "accents and descenders" the characters in successive line overlap.
I am too using g.clearRect to clear the result of a g.drawString and would expect to clear all pixels of drawString characters. Didn't know that this currently NOT the case.
-
-
Hi,
When I use code like this
g.setFontVector(20); g.drawString("Temp:"+t + "\nBattery:" + b + "%\n" + c + "\nUptime: " + u + " hr",0,60);
the "\n" creates a newline, works fine, nice feature.
But there is no spacing between the lines so the result is not very readable. In all cases the characters of the lines "touch" and in some cases characters of lines even "overlap" ( see picture in annex ).
Searched the documentation for a line-space setting, but did not find it.
Is there a line-space setting, and if not, is it a suggestion to add one?
-
No, that's not it.
The lines are in the example on your site and I just left them in.
https://www.espruino.com/Puck.js+Controlling+Other+PucksTo be sure I removed them but it makes no difference. Output with the setInterval:
RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX Quit Quit received, disconnecting now Getting data now Done! RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX Quit Quit received, disconnecting now Getting data now Uncaught Error: Unhandled promise rejection: Disconnected >
I think I'm getting close to a "perfect" solution and in hindsite it will be a simple obvious mistake on my part. I catching up my reading om "promises" so at least I'm learning here, that's what it's all about, right!?
Appreciate your tireless help!
-
Yes, an automatic retry has crossed my mind, but I wanted to understand why these thing "happed" first.
Based on https://www.espruino.com/Puck.js+Controlling+Other+Pucks I made a version with busy/connected flags.
// Are we busy? var busy = false; // The device, if we're connected var connected = false; var d; // The 'tx' characteristic, if connected var txCharacteristic = false; var rxCharacteristic = false; function getData(){ // Function to call 'toggle' on the other Puck if (!busy) { print("Getting data now"); busy = true; if (!connected) { NRF.connect("30:ae:a4:5d:b3:f2 public").then(function(device) { // print(device); d=device; return d.getPrimaryService("6e400001-b5a3-f393-e0a9-e50e24dcca9e"); }).then(function(s) { return s.getCharacteristic("6e400003-b5a3-f393-e0a9-e50e24dcca9e"); }).then(function(c) { // print(c); rxCharacteristic = c; busy = false; c.on('characteristicvaluechanged', function(event) { buf =E.toString(event.target.value.buffer); print("RX " + buf); if(buf.includes("Quit")){ print("Quit received, disconnecting now"); d.disconnect(); connected=false; busy = false; } }); return c.startNotifications(); }); } } } getData();
This works brilliant.
- Every time I upload the sketch connection is made to the UART server without any errors or complaints.
- And the disconnect is fired at receiving of "Quit", and recognised by the UART server.
Output of 2 subsequent uploads, no errors.
____ _ | __|___ ___ ___ _ _|_|___ ___ | __|_ -| . | _| | | | | . | |____|___| _|_| |___|_|_|_|___| |_| espruino.com 2v06 (c) 2019 G.Williams >Getting data now RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX Quit Quit received, disconnecting now > ____ _ | __|___ ___ ___ _ _|_|___ ___ | __|_ -| . | _| | | | | . | |____|___| _|_| |___|_|_|_|___| |_| espruino.com 2v06 (c) 2019 G.Williams >Getting data now RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX Quit Quit received, disconnecting now Disconnected from Web Bluetooth, Bangle.js 0195 >
But when I execute the getData function with a setInterval, the "uncaught errors .... promise....." reappear.
____ _ | __|___ ___ ___ _ _|_|___ ___ | __|_ -| . | _| | | | | . | |____|___| _|_| |___|_|_|_|___| |_| espruino.com 2v06 (c) 2019 G.Williams > Getting data now Getting data now Uncaught Error: Unhandled promise rejection: BLE error 0x12 RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX D07-06-2020 12.23 RX SAAPL,311.01,+11.5% RX SASML,299.45,-4.5% RX Hottentottententente RX Quit Quit received, disconnecting now Getting data now Uncaught Error: Unhandled promise rejection: Disconnected >
I assume that in the subsequent uploads the Espruino environment is fully reset, and the errors do not occur, but using setInterval the environment is not "cleaned" in a similar way.
- Every time I upload the sketch connection is made to the UART server without any errors or complaints.
-
-
I added a setInterval to process the code ever 10 secs, the behaviour is now like this:
- sometimes it is processed "OK" with the overshoot and the "uncaught error BLE task .... progress"
- sometimes it result just in an "uncaught error ........ promise rejection", no data received
Maybe you know the source of the "uncaught error ........ promise rejection"?
Code and selection of output for reference below.
setInterval(function() { var gatt; print("Getting data from server"); NRF.connect("30:ae:a4:5d:b3:f2 public").then(function(g) { gatt = g; return gatt.getPrimaryService("6e400001-b5a3-f393-e0a9-e50e24dcca9e"); }).then(function(service) { return service.getCharacteristic("6e400003-b5a3-f393-e0a9-e50e24dcca9e"); }).then(function(characteristic) { characteristic.on('characteristicvaluechanged', function(event) { buf =E.toString(event.target.value.buffer); console.log("RX: "+buf); if(buf.includes("Quit")){ print("Quit received, disconnecting now"); gatt.disconnect(); } }); return characteristic.startNotifications(); }).then(function() { console.log("Done!"); }); }, 10000);
Output like this:
Getting data from server Uncaught Error: Unhandled promise rejection: Disconnected Getting data from server Uncaught Error: Unhandled promise rejection: Disconnected Getting data from server Uncaught Error: Unhandled promise rejection: Disconnected Getting data from server RX: D07-06-2020 12.23 RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% RX: Hottentottententente RX: Quit Quit received, disconnecting now RX: D07-06-2020 12.23 Uncaught Error: BLE task 4 is already in progress at line 1 col 149 ...ting now");gatt.disconnect();} ^ in function called from system RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% RX: Hottentottententente Uncaught Error: Unhandled promise rejection: Disconnected Getting data from server Uncaught Error: Unhandled promise rejection: Disconnected Getting data from server RX: D07-06-2020 12.23 RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% RX: Hottentottententente RX: Quit Quit received, disconnecting now RX: D07-06-2020 12.23 Uncaught Error: BLE task 4 is already in progress at line 1 col 149 ...ting now");gatt.disconnect();} ^ in function called from system RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% RX: Hottentottententente Uncaught Error: Unhandled promise rejection: Disconnected Getting data from server Uncaught Error: Unhandled promise rejection: Disconnected Getting data from server Uncaught Error: Unhandled promise rejection: Disconnected Getting data from server RX: D07-06-2020 12.23 RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% RX: Hottentottententente RX: Quit Quit received, disconnecting now
- sometimes it is processed "OK" with the overshoot and the "uncaught error BLE task .... progress"
-
Ok so BLE task 3 is already in progress should be interpreted as disconnect, right ;-)
And the guess that the 1st disconnect is fired, but takes some time to process, so that the 2nd disconnect result in "BLE task 3 ......progress" seems plausible.
But I'm not out of the woods yet.
When I start the code I get this, as before, disconnects after some "overshoot".
The UART server recognises the disconnect after the last outputline, stops sending notification, start advertising again for a new connection._ | __|___ ___ ___ _ _|_|___ ___ | __|_ -| . | _| | | | | . | |____|___| _|_| |___|_|_|_|___| |_| espruino.com 2v06 (c) 2019 G.Williams > Done! RX: SASML,299.45,-4.5% RX: Hottentottententente RX: Quit Quit received, disconnecting now RX: D07-06-2020 12.23 RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% >
When I re-upload the code for a 2nd time, the UART server recognises the reconnect and starts sending data, but the BangJS does not process the notifications and displays no output, and after 10 secs or so I get an "Uncaught error". My server never gets a disconnect, so keeps sending data, forever.....
____ _ | __|___ ___ ___ _ _|_|___ ___ | __|_ -| . | _| | | | | . | |____|___| _|_| |___|_|_|_|___| |_| espruino.com 2v06 (c) 2019 G.Williams > Uncaught Error: Unhandled promise rejection: Disconnected >
My intention was to process the code with a setInterval every minute or so, but with the current behaviour that seems problematic.
Hmmmmm, problems, problems.
Thanks for your help though, if you have any other suggestions, yes please.
-
Added a “print” when “Quit" is received
It seems like the "disconnect" is not executed, output is:
> Done! RX: SASML,299.45,-4.5% RX: Hottentottententente RX: Quit Quit received, disconnecting now RX: D07-06-2020 12.23 RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% RX: Hottentottententente RX: Quit Quit received, disconnecting now RX: D07-06-2020 12.23 Uncaught Error: BLE task 3 is already in progress at line 1 col 149 ...ting now");gatt.disconnect();} ^ in function called from system RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% >
-
Thanks for your response Gordon.
Code changed to:
var gatt; NRF.connect("30:ae:a4:5d:b3:f2 public").then(function(g) { gatt = g; return gatt.getPrimaryService("6e400001-b5a3-f393-e0a9-e50e24dcca9e"); }).then(function(service) { return service.getCharacteristic("6e400003-b5a3-f393-e0a9-e50e24dcca9e"); }).then(function(characteristic) { characteristic.on('characteristicvaluechanged', function(event) { buf =E.toString(event.target.value.buffer); console.log("RX: "+buf); if(buf.includes("Quit")){ gatt.disconnect(); } }); return characteristic.startNotifications(); }).then(function() { console.log("Done!"); });
The response after uploading the code varies with changing the code, here 3 examples:
____ _ | __|___ ___ ___ _ _|_|___ ___ | __|_ -| . | _| | | | | . | |____|___| _|_| |___|_|_|_|___| |_| espruino.com 2v06 (c) 2019 G.Williams > Done! RX: D07-06-2020 12.23 RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% RX: Hottentottententente RX: Quit RX: D07-06-2020 12.23 RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% RX: Hottentottententente RX: Quit Uncaught Error: BLE task 3 is already in progress at line 1 col 107 ...s("Quit")){gatt.disconnect();} ^ in function called from system RX: D07-06-2020 12.23 RX: SAAPL,311.01,+11.5% >
____ _ | __|___ ___ ___ _ _|_|___ ___ | __|_ -| . | _| | | | | . | |____|___| _|_| |___|_|_|_|___| |_| espruino.com 2v06 (c) 2019 G.Williams > Done! RX: D07-06-2020 12.23 RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% RX: Hottentottententente RX: Quit RX: D07-06-2020 12.23 RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% RX: Hottentottententente RX: Quit Uncaught Error: Not connected at line 1 col 107 ...s("Quit")){gatt.disconnect();} ^ in function called from system >
____ _ | __|___ ___ ___ _ _|_|___ ___ | __|_ -| . | _| | | | | . | |____|___| _|_| |___|_|_|_|___| |_| espruino.com 2v06 (c) 2019 G.Williams > Uncaught Error: Unhandled promise rejection: Disconnected >
Not sure what is happening in the background.
-
Hi,
Espruino beginner here, in need for some help.
I am running code from here:
https://www.espruino.com/BLE+UARTvar gatt; NRF.connect("30:ae:a4:5d:b3:f2 public").then(function(g) { gatt = g; return gatt.getPrimaryService("6e400001-b5a3-f393-e0a9-e50e24dcca9e"); }).then(function(service) { return service.getCharacteristic("6e400003-b5a3-f393-e0a9-e50e24dcca9e"); }).then(function(characteristic) { characteristic.on('characteristicvaluechanged', function(event) { console.log("RX: "+E.toString(event.target.value.buffer)); }); return characteristic.startNotifications(); }).then(function() { console.log("Done!"); });
The code:
- connects OK to my BLE UART server (An ESP32 with Arduino BLE server code that repeats sending BLE-UART messages)
displays the data sent by the BLE server in the Espruino IDE.

Data is like:RX: D07-06-2020 12.23 RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% RX: Hottentottententente RX: Quit RX: D07-06-2020 12.23 RX: SAAPL,311.01,+11.5% RX: SASML,299.45,-4.5% RX: Hottentottententente RX: Quit RX: D07-06-2020 12.23
But I would like to disconnect from the server when data “Quit” is received.
Something like:
if(buffer.includes("Quit"){ gatt.disconnect(); }
Have read many things, search the forum, and tried many examples, to no avail.
Who can help along?
- connects OK to my BLE UART server (An ESP32 with Arduino BLE server code that repeats sending BLE-UART messages)
OK, but in your picture at least there is 1 pixel between each successive line (between the T, B and C character).
I'm running 2V06 on Bangle, and the 1 pixel spacing between the T,B, and C char is not there.