From 7d2ca5c63f01257fbd1b9d4e8fab79c54cea7112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Fran=C3=A7ois=20Milants?= Date: Wed, 28 Dec 2022 15:29:32 +0100 Subject: [PATCH] Refactor BMA421 as a generic class that implements the concept IsMotionSensor. --- src/components/motion/MotionController.cpp | 6 +- src/components/motion/MotionController.h | 5 +- src/drivers/Bma421.cpp | 12 ++-- src/drivers/Bma421.h | 77 +++++++++++---------- src/drivers/MotionSensor.h | 78 ++++++++++++++++++++++ src/main.cpp | 4 +- src/port/MotionSensor.h | 16 +++++ src/systemtask/SystemTask.cpp | 2 +- src/systemtask/SystemTask.h | 5 +- 9 files changed, 153 insertions(+), 52 deletions(-) create mode 100644 src/drivers/MotionSensor.h create mode 100644 src/port/MotionSensor.h diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 7dd32127..19f14f10 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -69,12 +69,12 @@ int32_t MotionController::currentShakeSpeed() { void MotionController::IsSensorOk(bool isOk) { isSensorOk = isOk; } -void MotionController::Init(Pinetime::Drivers::Bma421::DeviceTypes types) { +void MotionController::Init(Pinetime::Drivers::MotionSensors::DeviceTypes types) { switch (types) { - case Drivers::Bma421::DeviceTypes::BMA421: + case Drivers::MotionSensors::DeviceTypes::BMA421: this->deviceType = DeviceTypes::BMA421; break; - case Drivers::Bma421::DeviceTypes::BMA425: + case Drivers::MotionSensors::DeviceTypes::BMA425: this->deviceType = DeviceTypes::BMA425; break; default: diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index f80b11b9..717946c5 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -1,7 +1,8 @@ #pragma once #include -#include +#include +#include #include namespace Pinetime { @@ -48,7 +49,7 @@ namespace Pinetime { return deviceType; } - void Init(Pinetime::Drivers::Bma421::DeviceTypes types); + void Init(Pinetime::Drivers::MotionSensors::DeviceTypes types); void SetService(Pinetime::Controllers::MotionService* service); private: diff --git a/src/drivers/Bma421.cpp b/src/drivers/Bma421.cpp index 539cc8d1..ee3a88ca 100644 --- a/src/drivers/Bma421.cpp +++ b/src/drivers/Bma421.cpp @@ -4,7 +4,7 @@ #include "drivers/TwiMaster.h" #include -using namespace Pinetime::Drivers; +using namespace Pinetime::Drivers::MotionSensors; namespace { int8_t user_i2c_read(uint8_t reg_addr, uint8_t* reg_data, uint32_t length, void* intf_ptr) { @@ -44,13 +44,13 @@ void Bma421::Init() { switch (bma.chip_id) { case BMA423_CHIP_ID: - deviceType = DeviceTypes::BMA421; + deviceType = MotionSensors::DeviceTypes::BMA421; break; case BMA425_CHIP_ID: - deviceType = DeviceTypes::BMA425; + deviceType = MotionSensors::DeviceTypes::BMA425; break; default: - deviceType = DeviceTypes::Unknown; + deviceType = MotionSensors::DeviceTypes::Unknown; break; } @@ -99,7 +99,7 @@ void Bma421::Write(uint8_t registerAddress, const uint8_t* data, size_t size) { twiMaster.Write(deviceAddress, registerAddress, data, size); } -Bma421::Values Bma421::Process() { +Pinetime::Drivers::MotionSensors::Values Bma421::Process() { if (not isOk) return {}; struct bma4_accel data; @@ -133,6 +133,6 @@ void Bma421::SoftReset() { nrf_delay_ms(1); } } -Bma421::DeviceTypes Bma421::DeviceType() const { +Pinetime::Drivers::MotionSensors::DeviceTypes Bma421::DeviceType() const { return deviceType; } diff --git a/src/drivers/Bma421.h b/src/drivers/Bma421.h index 5f27da15..7660c3a2 100644 --- a/src/drivers/Bma421.h +++ b/src/drivers/Bma421.h @@ -1,47 +1,50 @@ #pragma once #include "drivers/TwiMaster.h" #include "port/TwiMaster.h" +#include "drivers/MotionSensor.h" #include namespace Pinetime { namespace Drivers { - class Bma421 { - public: - enum class DeviceTypes : uint8_t { Unknown, BMA421, BMA425 }; - struct Values { - uint32_t steps; - int16_t x; - int16_t y; - int16_t z; + namespace MotionSensors { + class Bma421 { + public: + enum class DeviceTypes : uint8_t { Unknown, BMA421, BMA425 }; + struct Values { + uint32_t steps; + int16_t x; + int16_t y; + int16_t z; + }; + Bma421(TwiMaster& twiMaster, uint8_t twiAddress); + Bma421(const Bma421&) = delete; + Bma421& operator=(const Bma421&) = delete; + Bma421(Bma421&&) = delete; + Bma421& operator=(Bma421&&) = delete; + + /// The chip freezes the TWI bus after the softreset operation. Softreset is separated from the + /// Init() method to allow the caller to uninit and then reinit the TWI device after the softreset. + void SoftReset(); + void Init(); + MotionSensors::Values Process(); + void ResetStepCounter(); + + 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; + MotionSensors::DeviceTypes DeviceType() const; + + private: + void Reset(); + + TwiMaster& twiMaster; + uint8_t deviceAddress = 0x18; + struct bma4_dev bma; + bool isOk = false; + bool isResetOk = false; + MotionSensors::DeviceTypes deviceType = MotionSensors::DeviceTypes::Unknown; }; - Bma421(TwiMaster& twiMaster, uint8_t twiAddress); - Bma421(const Bma421&) = delete; - Bma421& operator=(const Bma421&) = delete; - Bma421(Bma421&&) = delete; - Bma421& operator=(Bma421&&) = delete; - - /// The chip freezes the TWI bus after the softreset operation. Softreset is separated from the - /// Init() method to allow the caller to uninit and then reinit the TWI device after the softreset. - void SoftReset(); - void Init(); - Values Process(); - void ResetStepCounter(); - - 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; - DeviceTypes DeviceType() const; - - private: - void Reset(); - - TwiMaster& twiMaster; - uint8_t deviceAddress = 0x18; - struct bma4_dev bma; - bool isOk = false; - bool isResetOk = false; - DeviceTypes deviceType = DeviceTypes::Unknown; - }; + } } } \ No newline at end of file diff --git a/src/drivers/MotionSensor.h b/src/drivers/MotionSensor.h new file mode 100644 index 00000000..30f77d9a --- /dev/null +++ b/src/drivers/MotionSensor.h @@ -0,0 +1,78 @@ +#pragma once +#include +#include +#include + +namespace Pinetime { + namespace Drivers { + namespace MotionSensors { + enum class DeviceTypes : uint8_t { Unknown, BMA421, BMA425 }; + struct Values { + uint32_t steps; + int16_t x; + int16_t y; + int16_t z; + }; + } + + template + concept IsMotionSensor = requires(motionSensorImpl sensor, uint8_t registerAddress, uint8_t* data, uint8_t* constData, size_t size) { + { sensor.SoftReset() }; + { sensor.Init() }; + { sensor.Process() }; + { sensor.ResetStepCounter() }; + { sensor.IsOk() }; + { sensor.DeviceType() }; + { sensor.Read(registerAddress, data, size) }; + { sensor.Write(registerAddress, constData, size) }; + }; + + namespace Interface { + template + requires IsMotionSensor + class MotionSensor { + public: + explicit MotionSensor(T& impl) : impl {impl} {} + MotionSensor(const MotionSensor&) = delete; + MotionSensor& operator=(const MotionSensor&) = delete; + MotionSensor(MotionSensor&&) = delete; + MotionSensor& operator=(MotionSensor&&) = delete; + + void SoftReset() { + return impl.SoftReset(); + } + + void Init() { + impl.Init(); + } + + MotionSensors::Values Process() { + return impl.Process(); + } + + void ResetStepCounter() { + return impl.ResetStepCounter(); + } + + void Read(uint8_t registerAddress, uint8_t* buffer, size_t size) { + impl.Read(registerAddress, buffer, size); + } + + void Write(uint8_t registerAddress, const uint8_t* data, size_t size) { + impl.Write(registerAddress, data, size); + } + + bool IsOk() const { + return impl.IsOk(); + } + + MotionSensors::DeviceTypes DeviceType() const { + return impl.DeviceType(); + } + + private: + T& impl; + }; + } + } +} diff --git a/src/main.cpp b/src/main.cpp index d2b2866c..f02756fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,6 +59,7 @@ Pinetime::Logging::DummyLogger logger; #endif #include "port/TouchPanel.h" +#include "port/MotionSensor.h" static constexpr uint8_t touchPanelTwiAddress = 0x15; static constexpr uint8_t motionSensorTwiAddress = 0x18; @@ -101,7 +102,8 @@ Pinetime::Drivers::TouchPanel touchPanel {touchPanelImpl}; #endif Pinetime::Components::LittleVgl lvgl {lcd, touchPanel}; -Pinetime::Drivers::Bma421 motionSensor {twiMaster, motionSensorTwiAddress}; +Pinetime::Drivers::MotionSensors::Bma421 motionSensorImpl {twiMaster, motionSensorTwiAddress}; +Pinetime::Drivers::MotionSensor motionSensor {motionSensorImpl}; Pinetime::Drivers::Hrs3300 heartRateSensor {twiMaster, heartRateSensorTwiAddress}; TimerHandle_t debounceTimer; diff --git a/src/port/MotionSensor.h b/src/port/MotionSensor.h new file mode 100644 index 00000000..5a927274 --- /dev/null +++ b/src/port/MotionSensor.h @@ -0,0 +1,16 @@ +#pragma once +#include "drivers/MotionSensor.h" + +#ifdef TARGET_DEVICE_PINETIME + #include +#endif + +namespace Pinetime { + namespace Drivers { +#ifdef TARGET_DEVICE_PINETIME + using MotionSensor = Interface::MotionSensor; +#else + #error "No target device specified!" +#endif + } +} diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 79765105..966a86cc 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -55,7 +55,7 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, Pinetime::Controllers::MotorController& motorController, Pinetime::Drivers::Hrs3300& heartRateSensor, Pinetime::Controllers::MotionController& motionController, - Pinetime::Drivers::Bma421& motionSensor, + Pinetime::Drivers::MotionSensor& motionSensor, Controllers::Settings& settingsController, Pinetime::Controllers::HeartRateController& heartRateController, Pinetime::Applications::DisplayApp& displayApp, diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index 313435d1..543e1253 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -36,6 +36,7 @@ #include "drivers/Watchdog.h" #include "systemtask/Messages.h" #include "port/SpiMaster.h" +#include "port/MotionSensor.h" extern std::chrono::time_point NoInit_BackUpTime; namespace Pinetime { @@ -68,7 +69,7 @@ namespace Pinetime { Pinetime::Controllers::MotorController& motorController, Pinetime::Drivers::Hrs3300& heartRateSensor, Pinetime::Controllers::MotionController& motionController, - Pinetime::Drivers::Bma421& motionSensor, + Pinetime::Drivers::MotionSensor& motionSensor, Controllers::Settings& settingsController, Pinetime::Controllers::HeartRateController& heartRateController, Pinetime::Applications::DisplayApp& displayApp, @@ -113,7 +114,7 @@ namespace Pinetime { Pinetime::Controllers::NotificationManager& notificationManager; Pinetime::Controllers::MotorController& motorController; Pinetime::Drivers::Hrs3300& heartRateSensor; - Pinetime::Drivers::Bma421& motionSensor; + Pinetime::Drivers::MotionSensor& motionSensor; Pinetime::Controllers::Settings& settingsController; Pinetime::Controllers::HeartRateController& heartRateController; Pinetime::Controllers::MotionController& motionController;