Skip to content

Commit ce22ba2

Browse files
authored
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.
1 parent f64e1aa commit ce22ba2

7 files changed

Lines changed: 49 additions & 108 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ target_sources(infinisim PUBLIC
9292
sim/components/heartrate/HeartRateController.cpp
9393
sim/components/motion/MotionController.h
9494
sim/components/motion/MotionController.cpp
95-
sim/components/motor/MotorController.h
96-
sim/components/motor/MotorController.cpp
9795
sim/drivers/Watchdog.h
9896
sim/drivers/Watchdog.cpp
9997
sim/drivers/Bma421.h
@@ -210,6 +208,8 @@ target_sources(infinisim PUBLIC
210208
${InfiniTime_DIR}/src/components/ble/NotificationManager.cpp
211209
${InfiniTime_DIR}/src/components/fs/FS.h
212210
${InfiniTime_DIR}/src/components/fs/FS.cpp
211+
${InfiniTime_DIR}/src/components/motor/MotorController.h
212+
${InfiniTime_DIR}/src/components/motor/MotorController.cpp
213213
${InfiniTime_DIR}/src/components/timer/TimerController.h
214214
${InfiniTime_DIR}/src/components/timer/TimerController.cpp
215215
${InfiniTime_DIR}/src/drivers/PinMap.h

main.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "displayapp/LittleVgl.h"
5252

5353
#include <nrfx_gpiote.h>
54+
#include <hal/nrf_gpio.h>
5455

5556
#include <iostream>
5657
#include <typeinfo>
@@ -402,19 +403,11 @@ class Framework {
402403
}
403404
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
404405
SDL_RenderClear(renderer);
405-
{ // motorController.is_ringing
406-
constexpr const int center_x = 15;
407-
constexpr const int center_y = 15;
408-
if (motorController.is_ringing) {
409-
draw_circle_red(center_x, center_y, 15);
410-
} else {
411-
draw_circle_grey(center_x, center_y, 15);
412-
}
413-
}
414406
{ // motorController.motor_is_running
415407
constexpr const int center_x = 45;
416408
constexpr const int center_y = 15;
417-
if (motorController.motor_is_running) {
409+
bool motor_is_running = nrf_gpio_pin_read(Pinetime::PinMap::Motor);
410+
if (motor_is_running) {
418411
draw_circle_red(center_x, center_y, 15);
419412
} else {
420413
draw_circle_grey(center_x, center_y, 15);

sim/components/motor/MotorController.cpp

Lines changed: 0 additions & 58 deletions
This file was deleted.

sim/components/motor/MotorController.h

Lines changed: 0 additions & 25 deletions
This file was deleted.

sim/libraries/timer/app_timer.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ ret_code_t app_timer_init(void) {
5757
ret_code_t app_timer_create(app_timer_t *p_timer_id,
5858
app_timer_mode_t mode,
5959
app_timer_timeout_handler_t timeout_handler) {
60-
if (mode != APP_TIMER_MODE_SINGLE_SHOT) {
61-
throw std::runtime_error("only mode 'APP_TIMER_MODE_SINGLE_SHOT' implemented");
60+
if (mode == APP_TIMER_MODE_SINGLE_SHOT) {
61+
p_timer_id->repeating = false;
62+
} else if (mode == APP_TIMER_MODE_REPEATED) {
63+
p_timer_id->repeating = true;
64+
} else {
65+
throw std::runtime_error("only mode 'APP_TIMER_MODE_SINGLE_SHOT' or 'APP_TIMER_MODE_REPEATED' implemented");
6266
}
6367
p_timer_id->handler = timeout_handler;
6468
return 0;
@@ -67,10 +71,15 @@ Uint32 timeout_callback_wrapper(Uint32 interval, void *param)
6771
{
6872
auto* timer_id = static_cast<app_timer_t*>(param);
6973
timer_id->handler(timer_id->p_context);
70-
return 0; // cancel timer
74+
if (timer_id->repeating) {
75+
return timer_id->repeat_period;
76+
} else {
77+
return 0; // cancel timer
78+
}
7179
}
7280
ret_code_t app_timer_start(app_timer_t &timer_id, uint32_t timeout_ticks, void * p_context) {
7381
timer_id.p_context = p_context;
82+
timer_id.repeat_period = timeout_ticks;
7483
timer_id.sdl_timer_id = SDL_AddTimer(timeout_ticks, timeout_callback_wrapper, (void*)(&timer_id));
7584
return 0;
7685
}

sim/libraries/timer/app_timer.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ typedef uint32_t ret_code_t;
8888
/**@brief Application time-out handler type. */
8989
typedef void (*app_timer_timeout_handler_t)(void * p_context);
9090

91+
/**@brief Timer modes. */
92+
typedef enum
93+
{
94+
APP_TIMER_MODE_SINGLE_SHOT, /**< The timer will expire only once. */
95+
APP_TIMER_MODE_REPEATED /**< The timer will restart each time it expires. */
96+
} app_timer_mode_t;
97+
9198
struct app_timer_t
9299
{
93100
//nrf_sortlist_item_t list_item; /**< Token used by sortlist. */
@@ -96,6 +103,7 @@ struct app_timer_t
96103
uint32_t repeat_period; /**< Repeat period (0 if single shot mode). */
97104
app_timer_timeout_handler_t handler; /**< User handler. */
98105
void * p_context; /**< User context. */
106+
bool repeating;
99107
//NRF_LOG_INSTANCE_PTR_DECLARE(p_log) /**< Pointer to instance of the logger object (Conditionally compiled). */
100108
//volatile bool active; /**< Flag indicating that timer is active. */
101109
};
@@ -127,13 +135,6 @@ uint32_t constexpr APP_TIMER_TICKS(uint32_t ms) {
127135
);
128136
}
129137

130-
/**@brief Timer modes. */
131-
typedef enum
132-
{
133-
APP_TIMER_MODE_SINGLE_SHOT, /**< The timer will expire only once. */
134-
APP_TIMER_MODE_REPEATED /**< The timer will restart each time it expires. */
135-
} app_timer_mode_t;
136-
137138
/**@brief Function for initializing the timer module.
138139
*
139140
* @retval NRF_SUCCESS If the module was initialized successfully.

sim/nrfx/hal/nrf_gpio.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,20 @@
66
#include <stdexcept>
77
#include <string> // std::to_string
88

9-
void nrf_gpio_cfg_default(uint32_t pin_number) {}
10-
void nrf_gpio_pin_set(uint32_t pin_number) {}
9+
bool motor_running = false;
10+
11+
void nrf_gpio_cfg_default(uint32_t pin_number) {
12+
if (pin_number == Pinetime::PinMap::Motor)
13+
{
14+
motor_running = true;
15+
}
16+
}
17+
void nrf_gpio_pin_set(uint32_t pin_number) {
18+
if (pin_number == Pinetime::PinMap::Motor)
19+
{
20+
motor_running = false;
21+
}
22+
}
1123
uint32_t nrf_gpio_pin_read(uint32_t pin_number)
1224
{
1325
if (pin_number == Pinetime::PinMap::Button) {
@@ -16,12 +28,21 @@ uint32_t nrf_gpio_pin_read(uint32_t pin_number)
1628
bool right_click = (buttons & SDL_BUTTON_RMASK) != 0;
1729
return right_click;
1830
}
31+
else if (pin_number == Pinetime::PinMap::Motor)
32+
{
33+
return motor_running;
34+
}
1935
throw std::runtime_error("nrf_gpio_pin_read: unhandled pin_number: " + std::to_string(pin_number));
2036
return 0;
2137
}
2238

2339
void nrf_gpio_cfg_output(uint32_t pin_number) {}
24-
void nrf_gpio_pin_clear(uint32_t pin_number) {}
40+
void nrf_gpio_pin_clear(uint32_t pin_number) {
41+
if (pin_number == Pinetime::PinMap::Motor)
42+
{
43+
motor_running = true;
44+
}
45+
}
2546
void nrf_gpio_range_cfg_input(uint32_t pin_range_start,
2647
uint32_t pin_range_end,
2748
nrf_gpio_pin_pull_t pull_config) {}

0 commit comments

Comments
 (0)