flipper_chronometer.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // CC0 1.0 Universal (CC0 1.0)
  2. // Public Domain Dedication
  3. // https://github.com/nmrr
  4. #include <stdio.h>
  5. #include <furi.h>
  6. #include <gui/gui.h>
  7. #include <input/input.h>
  8. #include <notification/notification_messages.h>
  9. #include <furi_hal_power.h>
  10. #include <locale/locale.h>
  11. #define SCREEN_SIZE_X 128
  12. #define SCREEN_SIZE_Y 64
  13. typedef enum {
  14. EventTypeInput,
  15. ClockEventTypeTick,
  16. } EventType;
  17. typedef struct {
  18. EventType type;
  19. InputEvent input;
  20. } EventApp;
  21. typedef struct {
  22. FuriMutex* mutex;
  23. uint32_t timer;
  24. uint8_t minute;
  25. uint8_t hour;
  26. } mutexStruct;
  27. static void draw_callback(Canvas* canvas, void* ctx)
  28. {
  29. mutexStruct* mutexVal = ctx;
  30. mutexStruct mutexDraw;
  31. furi_mutex_acquire(mutexVal->mutex, FuriWaitForever);
  32. memcpy(&mutexDraw, mutexVal, sizeof(mutexStruct));
  33. furi_mutex_release(mutexVal->mutex);
  34. char buffer[16];
  35. canvas_set_font(canvas, FontBigNumbers);
  36. snprintf(buffer, sizeof(buffer), "%02u:%02u:%02lu", mutexDraw.hour, mutexDraw.minute, mutexDraw.timer / 64000000);
  37. canvas_draw_str_aligned(canvas, 5, SCREEN_SIZE_Y/2 + 5, AlignLeft, AlignBottom, buffer);
  38. canvas_set_font(canvas, FontPrimary);
  39. snprintf(buffer, sizeof(buffer), "%03lu", (mutexDraw.timer % 64000000) / 64000);
  40. canvas_draw_str_aligned(canvas, SCREEN_SIZE_X - 5, SCREEN_SIZE_Y/2, AlignRight, AlignBottom, buffer);
  41. }
  42. static void input_callback(InputEvent* input_event, void* ctx)
  43. {
  44. furi_assert(ctx);
  45. FuriMessageQueue* event_queue = ctx;
  46. EventApp event = {.type = EventTypeInput, .input = *input_event};
  47. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  48. }
  49. static void clock_tick(void* ctx) {
  50. furi_assert(ctx);
  51. FuriMessageQueue* queue = ctx;
  52. EventApp event = {.type = ClockEventTypeTick};
  53. furi_message_queue_put(queue, &event, 0);
  54. }
  55. int32_t flipper_chronometer_app()
  56. {
  57. // 64 MHz
  58. furi_hal_bus_enable(FuriHalBusTIM2);
  59. LL_TIM_SetCounterMode(TIM2, LL_TIM_COUNTERMODE_UP);
  60. LL_TIM_SetClockDivision(TIM2, LL_TIM_CLOCKDIVISION_DIV1);
  61. LL_TIM_SetPrescaler(TIM2, 0);
  62. // return to 0 after 1 min
  63. LL_TIM_SetAutoReload(TIM2, 3839999999);
  64. LL_TIM_SetCounter(TIM2, 0);
  65. EventApp event;
  66. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp));
  67. mutexStruct mutexVal;
  68. mutexVal.minute = 0;
  69. mutexVal.timer = 0;
  70. mutexVal.hour = 0;
  71. uint32_t previousTimer = 0;
  72. mutexVal.mutex= furi_mutex_alloc(FuriMutexTypeNormal);
  73. if(!mutexVal.mutex) {
  74. furi_message_queue_free(event_queue);
  75. return 255;
  76. }
  77. ViewPort* view_port = view_port_alloc();
  78. view_port_draw_callback_set(view_port, draw_callback, &mutexVal.mutex);
  79. view_port_input_callback_set(view_port, input_callback, event_queue);
  80. Gui* gui = furi_record_open(RECORD_GUI);
  81. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  82. FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
  83. uint8_t enableChrono = 0;
  84. while(1)
  85. {
  86. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  87. uint8_t screenRefresh = 0;
  88. if (event_status == FuriStatusOk)
  89. {
  90. if(event.type == EventTypeInput)
  91. {
  92. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong)
  93. {
  94. break;
  95. }
  96. else if (event.input.key == InputKeyOk && event.input.type == InputTypeShort)
  97. {
  98. if (enableChrono == 1)
  99. {
  100. LL_TIM_DisableCounter(TIM2);
  101. furi_timer_stop(timer);
  102. enableChrono = 0;
  103. }
  104. else
  105. {
  106. LL_TIM_EnableCounter(TIM2);
  107. furi_timer_start(timer, 43); // better to use prime number as timer for millisecond refresh effect
  108. enableChrono = 1;
  109. }
  110. screenRefresh = 1;
  111. }
  112. else if (enableChrono == 0 && event.input.key == InputKeyOk && event.input.type == InputTypeLong)
  113. {
  114. LL_TIM_SetCounter(TIM2, 0);
  115. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  116. mutexVal.minute = 0;
  117. mutexVal.hour = 0;
  118. mutexVal.timer = 0;
  119. furi_mutex_release(mutexVal.mutex);
  120. screenRefresh = 1;
  121. }
  122. }
  123. else if (event.type == ClockEventTypeTick)
  124. {
  125. screenRefresh = 1;
  126. }
  127. }
  128. if (screenRefresh == 1)
  129. {
  130. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  131. previousTimer = mutexVal.timer;
  132. mutexVal.timer = TIM2->CNT;
  133. if (mutexVal.timer < previousTimer)
  134. {
  135. if (mutexVal.minute < 59) mutexVal.minute++;
  136. else
  137. {
  138. if (mutexVal.hour < 99)
  139. {
  140. mutexVal.hour++;
  141. mutexVal.minute = 0;
  142. }
  143. else
  144. {
  145. LL_TIM_DisableCounter(TIM2);
  146. mutexVal.timer = 3839999999;
  147. furi_timer_stop(timer);
  148. enableChrono = 0;
  149. }
  150. }
  151. }
  152. furi_mutex_release(mutexVal.mutex);
  153. view_port_update(view_port);
  154. }
  155. }
  156. LL_TIM_DisableCounter(TIM2);
  157. furi_hal_bus_disable(FuriHalBusTIM2);
  158. furi_message_queue_free(event_queue);
  159. furi_mutex_free(mutexVal.mutex);
  160. gui_remove_view_port(gui, view_port);
  161. view_port_free(view_port);
  162. furi_timer_free(timer);
  163. furi_record_close(RECORD_GUI);
  164. return 0;
  165. }