flipper_chronometer.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_pwm.h>
  10. #include <furi_hal_power.h>
  11. #include <locale/locale.h>
  12. #include <lib/toolbox/md5.h>
  13. #define SCREEN_SIZE_X 128
  14. #define SCREEN_SIZE_Y 64
  15. typedef enum {
  16. EventTypeInput,
  17. ClockEventTypeTick,
  18. EventGPIO,
  19. } EventType;
  20. typedef struct {
  21. EventType type;
  22. InputEvent input;
  23. } EventApp;
  24. #define lineArraySize 128
  25. typedef struct {
  26. FuriMutex* mutex;
  27. uint32_t timer;
  28. uint8_t minute;
  29. } mutexStruct;
  30. static void draw_callback(Canvas* canvas, void* ctx)
  31. {
  32. mutexStruct* mutexVal = ctx;
  33. mutexStruct mutexDraw;
  34. furi_mutex_acquire(mutexVal->mutex, FuriWaitForever);
  35. memcpy(&mutexDraw, mutexVal, sizeof(mutexStruct));
  36. furi_mutex_release(mutexVal->mutex);
  37. char buffer[16];
  38. snprintf(buffer, sizeof(buffer), "%02u:%02lu:%03lu", mutexDraw.minute, mutexDraw.timer / 64000000, (mutexDraw.timer % 64000000) / 64000);
  39. canvas_set_font(canvas, FontBigNumbers);
  40. canvas_draw_str_aligned(canvas, SCREEN_SIZE_X/2, SCREEN_SIZE_Y/2 + 5, AlignCenter, 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. uint32_t previousTimer = 0;
  71. mutexVal.mutex= furi_mutex_alloc(FuriMutexTypeNormal);
  72. if(!mutexVal.mutex) {
  73. furi_message_queue_free(event_queue);
  74. return 255;
  75. }
  76. ViewPort* view_port = view_port_alloc();
  77. view_port_draw_callback_set(view_port, draw_callback, &mutexVal.mutex);
  78. view_port_input_callback_set(view_port, input_callback, event_queue);
  79. Gui* gui = furi_record_open(RECORD_GUI);
  80. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  81. FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
  82. uint8_t enableChrono = 0;
  83. while(1)
  84. {
  85. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  86. uint8_t screenRefresh = 0;
  87. if (event_status == FuriStatusOk)
  88. {
  89. if(event.type == EventTypeInput)
  90. {
  91. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong)
  92. {
  93. break;
  94. }
  95. else if (event.input.key == InputKeyOk && event.input.type == InputTypeShort)
  96. {
  97. if (enableChrono == 1)
  98. {
  99. LL_TIM_DisableCounter(TIM2);
  100. furi_timer_stop(timer);
  101. enableChrono = 0;
  102. }
  103. else
  104. {
  105. LL_TIM_EnableCounter(TIM2);
  106. furi_timer_start(timer, 43); // better to use prime number as timer for millisecond refresh effect
  107. enableChrono = 1;
  108. }
  109. screenRefresh = 1;
  110. }
  111. else if (enableChrono == 0 && event.input.key == InputKeyOk && event.input.type == InputTypeLong)
  112. {
  113. LL_TIM_SetCounter(TIM2, 0);
  114. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  115. mutexVal.minute = 0;
  116. mutexVal.timer = 0;
  117. furi_mutex_release(mutexVal.mutex);
  118. screenRefresh = 1;
  119. }
  120. }
  121. else if (event.type == ClockEventTypeTick)
  122. {
  123. screenRefresh = 1;
  124. }
  125. }
  126. if (screenRefresh == 1)
  127. {
  128. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  129. previousTimer = mutexVal.timer;
  130. mutexVal.timer = TIM2->CNT;
  131. if (mutexVal.timer < previousTimer)
  132. {
  133. if (mutexVal.minute < 59) mutexVal.minute++;
  134. else
  135. {
  136. LL_TIM_DisableCounter(TIM2);
  137. mutexVal.timer = 3839999999;
  138. furi_timer_stop(timer);
  139. enableChrono = 0;
  140. }
  141. }
  142. furi_mutex_release(mutexVal.mutex);
  143. view_port_update(view_port);
  144. }
  145. }
  146. LL_TIM_DisableCounter(TIM2);
  147. furi_hal_bus_disable(FuriHalBusTIM2);
  148. furi_message_queue_free(event_queue);
  149. furi_mutex_free(mutexVal.mutex);
  150. gui_remove_view_port(gui, view_port);
  151. view_port_free(view_port);
  152. furi_timer_free(timer);
  153. furi_record_close(RECORD_GUI);
  154. return 0;
  155. }