...
// Read range function
VL6180X.prototype.readRange = function() {
while (!(this.read8(C.VL6180X_REG_RESULT_RANGE_STATUS) & 0x01)); // wait for device to be ready for range measurement
this.write8(C.VL6180X_REG_SYSRANGE_START, 0x01); // Start a range measurement
while (!(this.read8(C.VL6180X_REG_RESULT_INTERRUPT_STATUS_GPIO) & 0x04)); // Poll until bit 2 is set
var range = this.read8(C.VL6180X_REG_RESULT_RANGE_VAL); // read range in mm
this.write8(C.VL6180X_REG_SYSTEM_INTERRUPT_CLEAR, 0x07); // clear interrupt
return range;
};
...
This - while / continuously polling in JS loop until condition met - is not flying well with Espruino... it is actually inhibiting Espruino to function properly. Espruino is event driven; think of JS code as interrupt service routines: they get invoked by a hardware event - pin state change or timer event - and have to finish - come to an end (or put into (another) timeout the continuation/completion) within reasonable time in order to release the (cpu) resource to be able to respond timely to the next event. (You can do 'logical while' / polling, but it has to be with a timeout.)
You do not show any wiring info (nor what Espruino board you are using)... So I assume you do this - at this point in time - all in software... To get it done done all in software, use retries on timeouts, set overall timeout or maximum retries and make it with callback(s) or Promise.
Since the sensor has an interrupt pin (GPIO1), you can simplify your code significantly and lighten the cpu work by connecting that pin to an Espruino pin and watch it by the module as necessary.
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Took a quick look at the module, especially at lines beginning at https://github.com/SalvoCas/EspruinoDocs/blob/master/devices/VL6180X.js#L147
This - while / continuously polling in JS loop until condition met - is not flying well with Espruino... it is actually inhibiting Espruino to function properly. Espruino is event driven; think of JS code as interrupt service routines: they get invoked by a hardware event - pin state change or timer event - and have to finish - come to an end (or put into (another) timeout the continuation/completion) within reasonable time in order to release the (cpu) resource to be able to respond timely to the next event. (You can do 'logical while' / polling, but it has to be with a timeout.)
You do not show any wiring info (nor what Espruino board you are using)... So I assume you do this - at this point in time - all in software... To get it done done all in software, use retries on timeouts, set overall timeout or maximum retries and make it with callback(s) or Promise.
Since the sensor has an interrupt pin (GPIO1), you can simplify your code significantly and lighten the cpu work by connecting that pin to an Espruino pin and watch it by the module as necessary.