From 52a90288fd2b744b68d584db316fcf3fede84262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Milants?= Date: Fri, 2 Apr 2021 16:56:14 +0200 Subject: [PATCH] Handle return code from BMA driver, and set a flag is the initialization fails. This allows to boot InfiniTime even if the device cannot initialize. --- src/components/motion/MotionController.cpp | 3 +++ src/components/motion/MotionController.h | 7 +++++-- src/displayapp/screens/WatchFaceDigital.cpp | 8 +++++-- src/displayapp/screens/WatchFaceDigital.h | 1 + src/drivers/Bma421.cpp | 23 +++++++++++++-------- src/drivers/Bma421.h | 5 +++-- src/systemtask/SystemTask.cpp | 1 + 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index bad9d45f..e9ee314b 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -31,3 +31,6 @@ bool MotionController::ShouldWakeUp(bool isSleeping) { } return false; } +void MotionController::IsSensorOk(bool isOk) { + isSensorOk = isOk; +} diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index dbbbf910..5881997f 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -14,13 +14,16 @@ namespace Pinetime { uint32_t NbSteps() const { return nbSteps; } bool ShouldWakeUp(bool isSleeping); - private: + void IsSensorOk(bool isOk); + bool IsSensorOk() const { return isSensorOk; } + + private: uint32_t nbSteps; int16_t x; int16_t y; int16_t z; int16_t lastYForWakeUp = 0; - + bool isSensorOk = false; }; } } \ No newline at end of file diff --git a/src/displayapp/screens/WatchFaceDigital.cpp b/src/displayapp/screens/WatchFaceDigital.cpp index 2204e177..2dd89bb4 100644 --- a/src/displayapp/screens/WatchFaceDigital.cpp +++ b/src/displayapp/screens/WatchFaceDigital.cpp @@ -240,9 +240,13 @@ bool WatchFaceDigital::Refresh() { } stepCount = motionController.NbSteps(); - if(stepCount.IsUpdated()) { + motionSensorOk = motionController.IsSensorOk(); + if(stepCount.IsUpdated() || motionSensorOk.IsUpdated()) { char stepBuffer[5]; - sprintf(stepBuffer, "%lu", stepCount.Get()); + if(motionSensorOk.Get()) + sprintf(stepBuffer, "%lu", stepCount.Get()); + else + sprintf(stepBuffer, "---", stepCount.Get()); lv_label_set_text(stepValue, stepBuffer); lv_obj_align(stepValue, lv_scr_act(), LV_ALIGN_IN_BOTTOM_RIGHT, -5, -2); lv_obj_align(stepIcon, stepValue, LV_ALIGN_OUT_LEFT_MID, -5, 0); diff --git a/src/displayapp/screens/WatchFaceDigital.h b/src/displayapp/screens/WatchFaceDigital.h index e6514f1e..0f6a2f0c 100644 --- a/src/displayapp/screens/WatchFaceDigital.h +++ b/src/displayapp/screens/WatchFaceDigital.h @@ -50,6 +50,7 @@ namespace Pinetime { DirtyValue batteryPercentRemaining {}; DirtyValue bleState {}; DirtyValue> currentDateTime{}; + DirtyValue motionSensorOk {}; DirtyValue stepCount {}; DirtyValue heartbeat {}; DirtyValue heartbeatRunning {}; diff --git a/src/drivers/Bma421.cpp b/src/drivers/Bma421.cpp index 9e98f1ca..4e33ef8b 100644 --- a/src/drivers/Bma421.cpp +++ b/src/drivers/Bma421.cpp @@ -36,27 +36,27 @@ Bma421::Bma421(TwiMaster& twiMaster, uint8_t twiAddress) : twiMaster{twiMaster}, void Bma421::Init() { auto ret = bma4_soft_reset(&bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; nrf_delay_ms(1); ret = bma423_init(&bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; ret = bma423_write_config_file(&bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; ret = bma4_set_interrupt_mode(BMA4_LATCH_MODE, &bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; ret = bma423_feature_enable(BMA423_STEP_CNTR, 1, &bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; ret = bma423_step_detector_enable(0, &bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; ret = bma4_set_accel_enable(1, &bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; struct bma4_accel_config accel_conf; accel_conf.odr = BMA4_OUTPUT_DATA_RATE_100HZ; @@ -64,7 +64,9 @@ void Bma421::Init() { accel_conf.bandwidth = BMA4_ACCEL_NORMAL_AVG4; accel_conf.perf_mode = BMA4_CIC_AVG_MODE; ret = bma4_set_accel_config(&accel_conf, &bma); - ASSERT(ret == BMA4_OK); + if(ret != BMA4_OK) return; + + isOk = true; } void Bma421::Reset() { @@ -81,6 +83,7 @@ void Bma421::Write(uint8_t registerAddress, const uint8_t *data, size_t size) { } Bma421::Values Bma421::Process() { + if(not isOk) return {}; struct bma4_accel data; bma4_read_accel_xyz(&data, &bma); @@ -99,4 +102,6 @@ Bma421::Values Bma421::Process() { // X and Y axis are swapped because of the way the sensor is mounted in the PineTime return {steps, data.y, data.x, data.z}; } - +bool Bma421::IsOk() const { + return isOk; +} diff --git a/src/drivers/Bma421.h b/src/drivers/Bma421.h index fbb190ae..e2e24f0d 100644 --- a/src/drivers/Bma421.h +++ b/src/drivers/Bma421.h @@ -25,12 +25,13 @@ namespace Pinetime { void Read(uint8_t registerAddress, uint8_t *buffer, size_t size); void Write(uint8_t registerAddress, const uint8_t *data, size_t size); + bool IsOk() const; + private: TwiMaster& twiMaster; uint8_t deviceAddress = 0x18; - - struct bma4_dev bma; + bool isOk = false; }; } } \ No newline at end of file diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 74c2c46e..d2f38adb 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -243,6 +243,7 @@ void SystemTask::UpdateMotion() { if(isSleeping) twiMaster.Sleep(); + motionController.IsSensorOk(motionSensor.IsOk()); motionController.Update(motionValues.x, motionValues.y, motionValues.z,