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.
This commit is contained in:
NeroBurner 2022-06-05 22:03:53 +02:00 committed by GitHub
parent f64e1aab80
commit ce22ba29c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 49 additions and 108 deletions

View File

@ -92,8 +92,6 @@ target_sources(infinisim PUBLIC
sim/components/heartrate/HeartRateController.cpp sim/components/heartrate/HeartRateController.cpp
sim/components/motion/MotionController.h sim/components/motion/MotionController.h
sim/components/motion/MotionController.cpp sim/components/motion/MotionController.cpp
sim/components/motor/MotorController.h
sim/components/motor/MotorController.cpp
sim/drivers/Watchdog.h sim/drivers/Watchdog.h
sim/drivers/Watchdog.cpp sim/drivers/Watchdog.cpp
sim/drivers/Bma421.h sim/drivers/Bma421.h
@ -210,6 +208,8 @@ target_sources(infinisim PUBLIC
${InfiniTime_DIR}/src/components/ble/NotificationManager.cpp ${InfiniTime_DIR}/src/components/ble/NotificationManager.cpp
${InfiniTime_DIR}/src/components/fs/FS.h ${InfiniTime_DIR}/src/components/fs/FS.h
${InfiniTime_DIR}/src/components/fs/FS.cpp ${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.h
${InfiniTime_DIR}/src/components/timer/TimerController.cpp ${InfiniTime_DIR}/src/components/timer/TimerController.cpp
${InfiniTime_DIR}/src/drivers/PinMap.h ${InfiniTime_DIR}/src/drivers/PinMap.h

View File

@ -51,6 +51,7 @@
#include "displayapp/LittleVgl.h" #include "displayapp/LittleVgl.h"
#include <nrfx_gpiote.h> #include <nrfx_gpiote.h>
#include <hal/nrf_gpio.h>
#include <iostream> #include <iostream>
#include <typeinfo> #include <typeinfo>
@ -402,19 +403,11 @@ public:
} }
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer); 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 { // motorController.motor_is_running
constexpr const int center_x = 45; constexpr const int center_x = 45;
constexpr const int center_y = 15; 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); draw_circle_red(center_x, center_y, 15);
} else { } else {
draw_circle_grey(center_x, center_y, 15); draw_circle_grey(center_x, center_y, 15);

View File

@ -1,58 +0,0 @@
#include "components/motor/MotorController.h"
#include <SDL2/SDL.h>
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<MotorController*>(p_context);
motorController->RunForDuration(50);
}
Uint32 StopMotor_callback(Uint32 interval, void *param)
{
auto* motorController = static_cast<MotorController*>(param);
motorController->motor_is_running = false;
return 0; // cancel timer
}
Uint32 Ring_callback(Uint32 interval, void *param)
{
auto* motorController = static_cast<MotorController*>(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<MotorController*>(p_context);
motorController->motor_is_running = false;
}

View File

@ -1,25 +0,0 @@
#pragma once
#include <cstdint>
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);
};
}
}

View File

@ -57,8 +57,12 @@ ret_code_t app_timer_init(void) {
ret_code_t app_timer_create(app_timer_t *p_timer_id, ret_code_t app_timer_create(app_timer_t *p_timer_id,
app_timer_mode_t mode, app_timer_mode_t mode,
app_timer_timeout_handler_t timeout_handler) { app_timer_timeout_handler_t timeout_handler) {
if (mode != APP_TIMER_MODE_SINGLE_SHOT) { if (mode == APP_TIMER_MODE_SINGLE_SHOT) {
throw std::runtime_error("only mode 'APP_TIMER_MODE_SINGLE_SHOT' implemented"); 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; p_timer_id->handler = timeout_handler;
return 0; return 0;
@ -67,10 +71,15 @@ Uint32 timeout_callback_wrapper(Uint32 interval, void *param)
{ {
auto* timer_id = static_cast<app_timer_t*>(param); auto* timer_id = static_cast<app_timer_t*>(param);
timer_id->handler(timer_id->p_context); 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) { 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.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)); timer_id.sdl_timer_id = SDL_AddTimer(timeout_ticks, timeout_callback_wrapper, (void*)(&timer_id));
return 0; return 0;
} }

View File

@ -88,6 +88,13 @@ typedef uint32_t ret_code_t;
/**@brief Application time-out handler type. */ /**@brief Application time-out handler type. */
typedef void (*app_timer_timeout_handler_t)(void * p_context); 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 struct app_timer_t
{ {
//nrf_sortlist_item_t list_item; /**< Token used by sortlist. */ //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). */ uint32_t repeat_period; /**< Repeat period (0 if single shot mode). */
app_timer_timeout_handler_t handler; /**< User handler. */ app_timer_timeout_handler_t handler; /**< User handler. */
void * p_context; /**< User context. */ void * p_context; /**< User context. */
bool repeating;
//NRF_LOG_INSTANCE_PTR_DECLARE(p_log) /**< Pointer to instance of the logger object (Conditionally compiled). */ //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. */ //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. /**@brief Function for initializing the timer module.
* *
* @retval NRF_SUCCESS If the module was initialized successfully. * @retval NRF_SUCCESS If the module was initialized successfully.

View File

@ -6,8 +6,20 @@
#include <stdexcept> #include <stdexcept>
#include <string> // std::to_string #include <string> // std::to_string
void nrf_gpio_cfg_default(uint32_t pin_number) {} bool motor_running = false;
void nrf_gpio_pin_set(uint32_t pin_number) {}
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) uint32_t nrf_gpio_pin_read(uint32_t pin_number)
{ {
if (pin_number == Pinetime::PinMap::Button) { 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; bool right_click = (buttons & SDL_BUTTON_RMASK) != 0;
return right_click; 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)); throw std::runtime_error("nrf_gpio_pin_read: unhandled pin_number: " + std::to_string(pin_number));
return 0; return 0;
} }
void nrf_gpio_cfg_output(uint32_t pin_number) {} 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, void nrf_gpio_range_cfg_input(uint32_t pin_range_start,
uint32_t pin_range_end, uint32_t pin_range_end,
nrf_gpio_pin_pull_t pull_config) {} nrf_gpio_pin_pull_t pull_config) {}