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.

This commit is contained in:
Jean-François Milants 2021-04-02 16:56:14 +02:00
parent c7cc47ae30
commit 52a90288fd
7 changed files with 33 additions and 15 deletions

View File

@ -31,3 +31,6 @@ bool MotionController::ShouldWakeUp(bool isSleeping) {
}
return false;
}
void MotionController::IsSensorOk(bool isOk) {
isSensorOk = isOk;
}

View File

@ -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;
};
}
}

View File

@ -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);

View File

@ -50,6 +50,7 @@ namespace Pinetime {
DirtyValue<int> batteryPercentRemaining {};
DirtyValue<bool> bleState {};
DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime{};
DirtyValue<bool> motionSensorOk {};
DirtyValue<uint32_t> stepCount {};
DirtyValue<uint8_t> heartbeat {};
DirtyValue<bool> heartbeatRunning {};

View File

@ -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;
}

View File

@ -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;
};
}
}

View File

@ -243,6 +243,7 @@ void SystemTask::UpdateMotion() {
if(isSleeping)
twiMaster.Sleep();
motionController.IsSensorOk(motionSensor.IsOk());
motionController.Update(motionValues.x,
motionValues.y,
motionValues.z,