From ce22ba29c9a0cae58be41e685c7dcdfb555b89a0 Mon Sep 17 00:00:00 2001 From: NeroBurner Date: Sun, 5 Jun 2022 22:03:53 +0200 Subject: [PATCH] Remove MotorController, use nrf_gpio Motor pin, apptimer repeat (#34) Remove the custom MotorController class and use the upstream MotorController instead. To enable this move add custom code in nrf_gpio to handle the Motor pin instead of the custom motor_is_running member variable. Also implement the repeating app_timer type used in the upstream MotorController. --- CMakeLists.txt | 4 +- main.cpp | 13 ++---- sim/components/motor/MotorController.cpp | 58 ------------------------ sim/components/motor/MotorController.h | 25 ---------- sim/libraries/timer/app_timer.cpp | 15 ++++-- sim/libraries/timer/app_timer.h | 15 +++--- sim/nrfx/hal/nrf_gpio.cpp | 27 +++++++++-- 7 files changed, 49 insertions(+), 108 deletions(-) delete mode 100644 sim/components/motor/MotorController.cpp delete mode 100644 sim/components/motor/MotorController.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d8fa68c..0358c63 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,8 +92,6 @@ target_sources(infinisim PUBLIC sim/components/heartrate/HeartRateController.cpp sim/components/motion/MotionController.h sim/components/motion/MotionController.cpp - sim/components/motor/MotorController.h - sim/components/motor/MotorController.cpp sim/drivers/Watchdog.h sim/drivers/Watchdog.cpp sim/drivers/Bma421.h @@ -210,6 +208,8 @@ target_sources(infinisim PUBLIC ${InfiniTime_DIR}/src/components/ble/NotificationManager.cpp ${InfiniTime_DIR}/src/components/fs/FS.h ${InfiniTime_DIR}/src/components/fs/FS.cpp + ${InfiniTime_DIR}/src/components/motor/MotorController.h + ${InfiniTime_DIR}/src/components/motor/MotorController.cpp ${InfiniTime_DIR}/src/components/timer/TimerController.h ${InfiniTime_DIR}/src/components/timer/TimerController.cpp ${InfiniTime_DIR}/src/drivers/PinMap.h diff --git a/main.cpp b/main.cpp index c1b66cd..b39b0bf 100644 --- a/main.cpp +++ b/main.cpp @@ -51,6 +51,7 @@ #include "displayapp/LittleVgl.h" #include +#include #include #include @@ -402,19 +403,11 @@ public: } SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); SDL_RenderClear(renderer); - { // motorController.is_ringing - constexpr const int center_x = 15; - constexpr const int center_y = 15; - if (motorController.is_ringing) { - draw_circle_red(center_x, center_y, 15); - } else { - draw_circle_grey(center_x, center_y, 15); - } - } { // motorController.motor_is_running constexpr const int center_x = 45; constexpr const int center_y = 15; - if (motorController.motor_is_running) { + bool motor_is_running = nrf_gpio_pin_read(Pinetime::PinMap::Motor); + if (motor_is_running) { draw_circle_red(center_x, center_y, 15); } else { draw_circle_grey(center_x, center_y, 15); diff --git a/sim/components/motor/MotorController.cpp b/sim/components/motor/MotorController.cpp deleted file mode 100644 index 355a630..0000000 --- a/sim/components/motor/MotorController.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "components/motor/MotorController.h" - -#include - -using namespace Pinetime::Controllers; - -void MotorController::Init() { - //nrf_gpio_cfg_output(PinMap::Motor); - //nrf_gpio_pin_set(PinMap::Motor); - //app_timer_init(); - - //app_timer_create(&shortVibTimer, APP_TIMER_MODE_SINGLE_SHOT, StopMotor); - //app_timer_create(&longVibTimer, APP_TIMER_MODE_REPEATED, Ring); -} - -void MotorController::Ring(void* p_context) { - auto* motorController = static_cast(p_context); - motorController->RunForDuration(50); -} - -Uint32 StopMotor_callback(Uint32 interval, void *param) -{ - auto* motorController = static_cast(param); - motorController->motor_is_running = false; - return 0; // cancel timer -} -Uint32 Ring_callback(Uint32 interval, void *param) -{ - auto* motorController = static_cast(param); - motorController->RunForDuration(50); - if (motorController->is_ringing) { - return interval; - } - return 0; -} -void MotorController::RunForDuration(uint8_t motorDuration) { - this->motor_is_running = true; - SDL_AddTimer(motorDuration, StopMotor_callback, this); - //nrf_gpio_pin_clear(PinMap::Motor); - //app_timer_start(shortVibTimer, APP_TIMER_TICKS(motorDuration), nullptr); -} - -void MotorController::StartRinging() { - Ring(this); - is_ringing = true; - SDL_AddTimer(1000, Ring_callback, this); - //app_timer_start(longVibTimer, APP_TIMER_TICKS(1000), this); -} - -void MotorController::StopRinging() { - is_ringing = false; -} - -void MotorController::StopMotor(void* p_context) { - //nrf_gpio_pin_set(PinMap::Motor); - auto* motorController = static_cast(p_context); - motorController->motor_is_running = false; -} diff --git a/sim/components/motor/MotorController.h b/sim/components/motor/MotorController.h deleted file mode 100644 index b9b964c..0000000 --- a/sim/components/motor/MotorController.h +++ /dev/null @@ -1,25 +0,0 @@ -#pragma once - -#include - -namespace Pinetime { - namespace Controllers { - - class MotorController { - public: - MotorController() = default; - - void Init(); - void RunForDuration(uint8_t motorDuration); - void StartRinging(); - void StopRinging(); - - bool motor_is_running = false; - bool is_ringing = false; - - private: - static void Ring(void* p_context); - static void StopMotor(void* p_context); - }; - } -} diff --git a/sim/libraries/timer/app_timer.cpp b/sim/libraries/timer/app_timer.cpp index 69e3bdf..7b0915b 100644 --- a/sim/libraries/timer/app_timer.cpp +++ b/sim/libraries/timer/app_timer.cpp @@ -57,8 +57,12 @@ ret_code_t app_timer_init(void) { ret_code_t app_timer_create(app_timer_t *p_timer_id, app_timer_mode_t mode, app_timer_timeout_handler_t timeout_handler) { - if (mode != APP_TIMER_MODE_SINGLE_SHOT) { - throw std::runtime_error("only mode 'APP_TIMER_MODE_SINGLE_SHOT' implemented"); + if (mode == APP_TIMER_MODE_SINGLE_SHOT) { + p_timer_id->repeating = false; + } else if (mode == APP_TIMER_MODE_REPEATED) { + p_timer_id->repeating = true; + } else { + throw std::runtime_error("only mode 'APP_TIMER_MODE_SINGLE_SHOT' or 'APP_TIMER_MODE_REPEATED' implemented"); } p_timer_id->handler = timeout_handler; return 0; @@ -67,10 +71,15 @@ Uint32 timeout_callback_wrapper(Uint32 interval, void *param) { auto* timer_id = static_cast(param); timer_id->handler(timer_id->p_context); - return 0; // cancel timer + if (timer_id->repeating) { + return timer_id->repeat_period; + } else { + return 0; // cancel timer + } } ret_code_t app_timer_start(app_timer_t &timer_id, uint32_t timeout_ticks, void * p_context) { timer_id.p_context = p_context; + timer_id.repeat_period = timeout_ticks; timer_id.sdl_timer_id = SDL_AddTimer(timeout_ticks, timeout_callback_wrapper, (void*)(&timer_id)); return 0; } diff --git a/sim/libraries/timer/app_timer.h b/sim/libraries/timer/app_timer.h index 74c5d43..ebdbec8 100644 --- a/sim/libraries/timer/app_timer.h +++ b/sim/libraries/timer/app_timer.h @@ -88,6 +88,13 @@ typedef uint32_t ret_code_t; /**@brief Application time-out handler type. */ typedef void (*app_timer_timeout_handler_t)(void * p_context); +/**@brief Timer modes. */ +typedef enum +{ + APP_TIMER_MODE_SINGLE_SHOT, /**< The timer will expire only once. */ + APP_TIMER_MODE_REPEATED /**< The timer will restart each time it expires. */ +} app_timer_mode_t; + struct app_timer_t { //nrf_sortlist_item_t list_item; /**< Token used by sortlist. */ @@ -96,6 +103,7 @@ struct app_timer_t uint32_t repeat_period; /**< Repeat period (0 if single shot mode). */ app_timer_timeout_handler_t handler; /**< User handler. */ void * p_context; /**< User context. */ + bool repeating; //NRF_LOG_INSTANCE_PTR_DECLARE(p_log) /**< Pointer to instance of the logger object (Conditionally compiled). */ //volatile bool active; /**< Flag indicating that timer is active. */ }; @@ -127,13 +135,6 @@ uint32_t constexpr APP_TIMER_TICKS(uint32_t ms) { ); } -/**@brief Timer modes. */ -typedef enum -{ - APP_TIMER_MODE_SINGLE_SHOT, /**< The timer will expire only once. */ - APP_TIMER_MODE_REPEATED /**< The timer will restart each time it expires. */ -} app_timer_mode_t; - /**@brief Function for initializing the timer module. * * @retval NRF_SUCCESS If the module was initialized successfully. diff --git a/sim/nrfx/hal/nrf_gpio.cpp b/sim/nrfx/hal/nrf_gpio.cpp index 6e5a35c..9c62a59 100644 --- a/sim/nrfx/hal/nrf_gpio.cpp +++ b/sim/nrfx/hal/nrf_gpio.cpp @@ -6,8 +6,20 @@ #include #include // std::to_string -void nrf_gpio_cfg_default(uint32_t pin_number) {} -void nrf_gpio_pin_set(uint32_t pin_number) {} +bool motor_running = false; + +void nrf_gpio_cfg_default(uint32_t pin_number) { + if (pin_number == Pinetime::PinMap::Motor) + { + motor_running = true; + } +} +void nrf_gpio_pin_set(uint32_t pin_number) { + if (pin_number == Pinetime::PinMap::Motor) + { + motor_running = false; + } +} uint32_t nrf_gpio_pin_read(uint32_t pin_number) { if (pin_number == Pinetime::PinMap::Button) { @@ -16,12 +28,21 @@ uint32_t nrf_gpio_pin_read(uint32_t pin_number) bool right_click = (buttons & SDL_BUTTON_RMASK) != 0; return right_click; } + else if (pin_number == Pinetime::PinMap::Motor) + { + return motor_running; + } throw std::runtime_error("nrf_gpio_pin_read: unhandled pin_number: " + std::to_string(pin_number)); return 0; } void nrf_gpio_cfg_output(uint32_t pin_number) {} -void nrf_gpio_pin_clear(uint32_t pin_number) {} +void nrf_gpio_pin_clear(uint32_t pin_number) { + if (pin_number == Pinetime::PinMap::Motor) + { + motor_running = true; + } +} void nrf_gpio_range_cfg_input(uint32_t pin_range_start, uint32_t pin_range_end, nrf_gpio_pin_pull_t pull_config) {}