Skip to content

Commit

Permalink
introduce timestamp since barometer on
Browse files Browse the repository at this point in the history
  • Loading branch information
d3nd3 committed Jan 13, 2024
1 parent 0d2ca99 commit 076dba0
Showing 1 changed file with 44 additions and 23 deletions.
67 changes: 44 additions & 23 deletions libs/banglejs/jswrap_bangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,9 @@ int barometerDP[9]; // pressure calibration
#endif

/// Promise when pressure is requested
JsVar *promisePressure;
bool getPressureReady;
JsVar *promisePressure; // return promise of getPressure()
bool getPressureReady; // getPressure() setInterval triggered
JsSysTime pressureOnSince; // how long barometer has been on
double barometerPressure;
double barometerTemperature;
double barometerAltitude;
Expand Down Expand Up @@ -3174,6 +3175,7 @@ bool jswrap_banglejs_setBarometerPower(bool isOn, JsVar *appId) {
while (tries-- > 0) {
if (isOn) {
if (!wasOn) {
pressureOnSince = jshGetSystemTime();
#ifdef PRESSURE_DEVICE_SPL06_007_EN
if (PRESSURE_DEVICE_SPL06_007_EN) {
unsigned char buf[SPL06_COEF_NUM];
Expand Down Expand Up @@ -4245,14 +4247,16 @@ bool jswrap_banglejs_idle() {
jsiQueueObjectCallbacks(bangle, JS_EVENT_PREFIX"pressure", &o, 1);
if (getPressureReady) {
getPressureReady = false;
// disable sensor now we have a result
JsVar *id = jsvNewFromString("getPressure");
jswrap_banglejs_setBarometerPower(0, id);
jsvUnLock(id);
// resolve the promise
jspromise_resolve(promisePressure, o);
jsvUnLock(promisePressure);
promisePressure = 0;
if (promisePressure) {
// disable sensor now we have a result
JsVar *id = jsvNewFromString("getPressure");
jswrap_banglejs_setBarometerPower(0, id);
jsvUnLock(id);
// resolve the promise
jspromise_resolve(promisePressure, o);
jsvUnLock(promisePressure);
promisePressure = 0;
}
}
jsvUnLock(o);
}
Expand Down Expand Up @@ -5058,7 +5062,34 @@ JsVar *jswrap_banglejs_getPressure() {
}
promisePressure = jspromise_create();
if (!promisePressure) return 0;

int powerOnTimeout = 500;
#ifdef PRESSURE_DEVICE_BMP280_EN
if (PRESSURE_DEVICE_BMP280_EN)
powerOnTimeout = 750; // some devices seem to need this long to boot reliably
#endif
#ifdef PRESSURE_DEVICE_SPL06_007_EN
if (PRESSURE_DEVICE_SPL06_007_EN)
powerOnTimeout = 400; // on SPL06 we may actually be leaving it *too long* before requesting data, and it starts to do another reading
#endif
// If barometer is already on, just resolve promise with the current result
if (bangleFlags & JSBF_BAROMETER_ON) {
JsSysTime delta = jshGetSystemTime() - pressureOnSince;
if ( delta > 0 && delta < powerOnTimeout) {
// barometer hasn't been on long enough
jsvUnLock(jsiSetTimeout(jswrap_banglejs_getPressure_callback, delta));
return jsvLockAgain(promisePressure);
} else {
// we do not turn barometer on in this case
JsVar *o = jswrap_banglejs_getBarometerObject();
if (o) jspromise_resolve(promisePressure, o);
jsvUnLock(o);
JsVar *r = promisePressure;
promisePressure = 0;
return r;
}

}
// Turning barometer on, will turn off in peripheralPollHandler
JsVar *id = jsvNewFromString("getPressure");
jswrap_banglejs_setBarometerPower(1, id);
jsvUnLock(id);
Expand All @@ -5073,7 +5104,6 @@ JsVar *jswrap_banglejs_getPressure() {
promisePressure = 0;
return r;
}

bool hadError = jspHasError();
if (hadError) {
JsVar *msg = jsvNewFromString("I2C barometer error");
Expand All @@ -5084,17 +5114,8 @@ JsVar *jswrap_banglejs_getPressure() {
promisePressure = 0;
return r;
}

int powerOnTimeout = 500;
#ifdef PRESSURE_DEVICE_BMP280_EN
if (PRESSURE_DEVICE_BMP280_EN)
powerOnTimeout = 750; // some devices seem to need this long to boot reliably
#endif
#ifdef PRESSURE_DEVICE_SPL06_007_EN
if (PRESSURE_DEVICE_SPL06_007_EN)
powerOnTimeout = 400; // on SPL06 we may actually be leaving it *too long* before requesting data, and it starts to do another reading
#endif
jsvUnLock(jsiSetTimeout(jswrap_banglejs_getPressure_callback, powerOnTimeout));
//sets getPressureReady=true
jswrap_banglejs_getPressure_callback();
return jsvLockAgain(promisePressure);
#endif
}
Expand Down

0 comments on commit 076dba0

Please sign in to comment.