优化程序架构
This commit is contained in:
+33
-28
@@ -90,10 +90,15 @@ static const AppModeStep_t *const s_tables[7] = {
|
||||
0, s_mode1, s_mode2, s_mode3, s_mode4, s_mode5, s_mode6
|
||||
};
|
||||
|
||||
static uint8_t s_initialized;
|
||||
static uint8_t s_active;
|
||||
static uint8_t s_step;
|
||||
static u32 s_step_t0;
|
||||
/** 模块状态,替代原来散落的 4 个 static */
|
||||
typedef struct {
|
||||
uint8_t initialized;
|
||||
uint8_t active; /**< 0=停转,1~6=当前玩法 */
|
||||
uint8_t step; /**< 当前节拍在表里的下标 */
|
||||
u32 step_t0; /**< 当前节拍起始时间戳,用于判断是否该跳下一步 */
|
||||
} AppModeCtx_t;
|
||||
|
||||
static AppModeCtx_t s_mode;
|
||||
|
||||
extern u32 timer_get_ms(void);
|
||||
|
||||
@@ -103,20 +108,20 @@ extern u32 timer_get_ms(void);
|
||||
*/
|
||||
static void app_mode_apply_step(void)
|
||||
{
|
||||
const AppModeStep_t *tbl = s_tables[s_active];
|
||||
const AppModeStep_t *tbl = s_tables[s_mode.active];
|
||||
|
||||
if (!tbl) {
|
||||
bsp_motor_set(BSP_MOTOR_STOP);
|
||||
return;
|
||||
}
|
||||
if (tbl[s_step].ms == 0) {
|
||||
s_step = 0;
|
||||
if (tbl[s_mode.step].ms == 0) {
|
||||
s_mode.step = 0;
|
||||
}
|
||||
s_step_t0 = timer_get_ms();
|
||||
if (tbl[s_step].dir == BSP_MOTOR_STOP) {
|
||||
s_mode.step_t0 = timer_get_ms();
|
||||
if (tbl[s_mode.step].dir == BSP_MOTOR_STOP) {
|
||||
bsp_motor_set(BSP_MOTOR_STOP);
|
||||
} else {
|
||||
bsp_motor_set_pwm(tbl[s_step].dir, tbl[s_step].duty);
|
||||
bsp_motor_set_pwm(tbl[s_mode.step].dir, tbl[s_mode.step].duty);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,10 +131,10 @@ static void app_mode_apply_step(void)
|
||||
*/
|
||||
AppMode_Ret_t app_mode_init(void)
|
||||
{
|
||||
s_active = 0;
|
||||
s_step = 0;
|
||||
s_step_t0 = timer_get_ms();
|
||||
s_initialized = 1;
|
||||
s_mode.active = 0;
|
||||
s_mode.step = 0;
|
||||
s_mode.step_t0 = timer_get_ms();
|
||||
s_mode.initialized = 1;
|
||||
bsp_motor_set(BSP_MOTOR_STOP);
|
||||
return APP_MODE_OK;
|
||||
}
|
||||
@@ -140,7 +145,7 @@ AppMode_Ret_t app_mode_init(void)
|
||||
*/
|
||||
AppMode_Ret_t app_mode_get_status(void)
|
||||
{
|
||||
return s_initialized ? APP_MODE_OK : APP_MODE_ERR_NOT_INIT;
|
||||
return s_mode.initialized ? APP_MODE_OK : APP_MODE_ERR_NOT_INIT;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -149,7 +154,7 @@ AppMode_Ret_t app_mode_get_status(void)
|
||||
*/
|
||||
uint8_t app_mode_get_active(void)
|
||||
{
|
||||
return s_active;
|
||||
return s_mode.active;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -159,15 +164,15 @@ uint8_t app_mode_get_active(void)
|
||||
*/
|
||||
AppMode_Ret_t app_mode_start(uint8_t mode)
|
||||
{
|
||||
if (!s_initialized) {
|
||||
if (!s_mode.initialized) {
|
||||
return APP_MODE_ERR_NOT_INIT;
|
||||
}
|
||||
if (mode < 1 || mode > 6) {
|
||||
app_mode_stop();
|
||||
return APP_MODE_ERR_PARAM;
|
||||
}
|
||||
s_active = mode;
|
||||
s_step = 0;
|
||||
s_mode.active = mode;
|
||||
s_mode.step = 0;
|
||||
app_mode_apply_step();
|
||||
log_info("auto mode %d start\n", mode);
|
||||
return APP_MODE_OK;
|
||||
@@ -179,12 +184,12 @@ AppMode_Ret_t app_mode_start(uint8_t mode)
|
||||
*/
|
||||
void app_mode_stop(void)
|
||||
{
|
||||
if (s_active) {
|
||||
log_info("auto mode %d stop\n", s_active);
|
||||
if (s_mode.active) {
|
||||
log_info("auto mode %d stop\n", s_mode.active);
|
||||
}
|
||||
s_active = 0;
|
||||
s_step = 0;
|
||||
s_step_t0 = timer_get_ms();
|
||||
s_mode.active = 0;
|
||||
s_mode.step = 0;
|
||||
s_mode.step_t0 = timer_get_ms();
|
||||
bsp_motor_set(BSP_MOTOR_STOP);
|
||||
}
|
||||
|
||||
@@ -197,17 +202,17 @@ void app_mode_run(void)
|
||||
const AppModeStep_t *tbl;
|
||||
|
||||
bsp_motor_brake_tick();
|
||||
if (!s_initialized || s_active == 0) {
|
||||
if (!s_mode.initialized || s_mode.active == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
tbl = s_tables[s_active];
|
||||
tbl = s_tables[s_mode.active];
|
||||
if (!tbl) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((timer_get_ms() - s_step_t0) >= tbl[s_step].ms) {
|
||||
s_step++;
|
||||
if ((timer_get_ms() - s_mode.step_t0) >= tbl[s_mode.step].ms) {
|
||||
s_mode.step++;
|
||||
app_mode_apply_step();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user