Parcourir la source

Periodic timer: simple than never

SG il y a 1 an
Parent
commit
af5faccd3d
3 fichiers modifiés avec 8 ajouts et 14 suppressions
  1. 1 5
      clock_timer.c
  2. 1 5
      clock_timer.h
  3. 6 4
      game_engine.c

+ 1 - 5
clock_timer.c

@@ -27,7 +27,7 @@ static void clock_timer_isr(void* context) {
     LL_TIM_ClearFlag_UPDATE(FURI_HAL_CLOCK_TIMER);
 }
 
-void clock_timer_init(ClockTimerCallback callback, void* context, float period) {
+void clock_timer_start(ClockTimerCallback callback, void* context, float period) {
     clock_timer.callback = callback;
     clock_timer.context = context;
 
@@ -39,9 +39,7 @@ void clock_timer_init(ClockTimerCallback callback, void* context, float period)
     LL_TIM_Init(FURI_HAL_CLOCK_TIMER, &TIM_InitStruct);
 
     furi_hal_interrupt_set_isr(FURI_HAL_CLOCK_TIMER_IRQ, clock_timer_isr, clock_timer.context);
-}
 
-void clock_timer_start(void) {
     LL_TIM_EnableIT_UPDATE(FURI_HAL_CLOCK_TIMER);
     LL_TIM_EnableCounter(FURI_HAL_CLOCK_TIMER);
 }
@@ -49,9 +47,7 @@ void clock_timer_start(void) {
 void clock_timer_stop(void) {
     LL_TIM_DisableIT_UPDATE(FURI_HAL_CLOCK_TIMER);
     LL_TIM_DisableCounter(FURI_HAL_CLOCK_TIMER);
-}
 
-void clock_timer_deinit(void) {
     furi_hal_bus_disable(FURI_HAL_CLOCK_TIMER_BUS);
     furi_hal_interrupt_set_isr(FURI_HAL_CLOCK_TIMER_IRQ, NULL, NULL);
 }

+ 1 - 5
clock_timer.h

@@ -6,14 +6,10 @@ extern "C" {
 
 typedef void (*ClockTimerCallback)(void* context);
 
-void clock_timer_init(ClockTimerCallback callback, void* context, float period);
-
-void clock_timer_start(void);
+void clock_timer_start(ClockTimerCallback callback, void* context, float period);
 
 void clock_timer_stop(void);
 
-void clock_timer_deinit(void);
-
 #ifdef __cplusplus
 }
 #endif

+ 6 - 4
game_engine.c

@@ -94,8 +94,7 @@ void game_engine_run(GameEngine* engine) {
         furi_pubsub_subscribe(engine->input_pubsub, input_events_callback, &input_state);
 
     // start "game update" timer
-    clock_timer_init(clock_timer_callback, engine, engine->settings.fps);
-    clock_timer_start();
+    clock_timer_start(clock_timer_callback, engine, engine->settings.fps);
 
     // init fps counter
     uint32_t time_start = DWT->CYCCNT;
@@ -115,7 +114,6 @@ void game_engine_run(GameEngine* engine) {
             input_prev_state = input_current_state;
 
             canvas_reset(canvas);
-
             uint32_t time_end = DWT->CYCCNT;
             uint32_t time_delta = time_end - time_start;
             time_start = time_end;
@@ -128,6 +126,10 @@ void game_engine_run(GameEngine* engine) {
 
             if(input.pressed != 0) {
                 FURI_LOG_I("input", "pressed: %lu", input.pressed);
+
+                if(input.pressed & (1 << InputKeyBack)) {
+                    game_engine_stop(engine);
+                }
             }
 
             if(input.released != 0) {
@@ -141,10 +143,10 @@ void game_engine_run(GameEngine* engine) {
     }
 
     clock_timer_stop();
-    clock_timer_deinit();
 
     gui_direct_draw_release(engine->gui);
     furi_pubsub_unsubscribe(engine->input_pubsub, input_subscription);
+
     engine->running = false;
 }