Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bangle.getPressure() redesign #2447

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 47 additions & 34 deletions libs/banglejs/jswrap_bangle.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,9 @@ int barometerDP[9]; // pressure calibration
#endif

/// Promise when pressure is requested
JsVar *promisePressure;
JsVar *promisePressure; // return promise of getPressure()
bool getPressureReady; // getPressure() setInterval triggered
JsSysTime pressureOnSince; // timestamp barometer last on
double barometerPressure;
double barometerTemperature;
double barometerAltitude;
Expand Down Expand Up @@ -3173,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 @@ -4122,6 +4125,7 @@ void jswrap_banglejs_kill() {
#ifdef PRESSURE_DEVICE
jsvUnLock(promisePressure);
promisePressure = 0;
getPressureReady = false;
#endif

jshPinWatch(BTN1_PININDEX, false, JSPW_NONE);
Expand Down Expand Up @@ -4241,7 +4245,20 @@ bool jswrap_banglejs_idle() {
if (bangleTasks & JSBT_PRESSURE_DATA) {
JsVar *o = jswrap_banglejs_getBarometerObject();
if (o) {
jsiQueueObjectCallbacks(bangle, JS_EVENT_PREFIX"pressure", &o, 1);
jsiQueueObjectCallbacks(bangle, JS_EVENT_PREFIX"pressure", &o, 1);
if (getPressureReady) {
getPressureReady = false;
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 @@ -5033,18 +5050,7 @@ JsVar *jswrap_banglejs_getBarometerObject() {
}

void jswrap_banglejs_getPressure_callback() {
JsVar *o = 0;
if (jswrap_banglejs_barometerPoll()) {
o = jswrap_banglejs_getBarometerObject();
}
// 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);
jsvUnLock2(promisePressure,o);
promisePressure = 0;
getPressureReady = true;
}
#endif // PRESSURE_DEVICE

Expand All @@ -5057,17 +5063,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) {
JsVar *o = jswrap_banglejs_getBarometerObject();
jspromise_resolve(promisePressure, o);
jsvUnLock(o);
JsVar *r = promisePressure;
promisePressure = 0;
return r;
JsSysTime delta = jshGetSystemTime() - pressureOnSince;
if ( delta > 0 && delta < jshGetTimeFromMilliseconds(powerOnTimeout)) {
// barometer hasn't been on long enough
jsvUnLock(jsiSetTimeout(jswrap_banglejs_getPressure_callback, jshGetMillisecondsFromTime(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 @@ -5082,7 +5105,6 @@ JsVar *jswrap_banglejs_getPressure() {
promisePressure = 0;
return r;
}

bool hadError = jspHasError();
if (hadError) {
JsVar *msg = jsvNewFromString("I2C barometer error");
Expand All @@ -5093,17 +5115,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
Loading