flipp_pomodoro.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <notification/notification_messages.h>
  4. #include <gui/gui.h>
  5. #include <gui/elements.h>
  6. #include <input/input.h>
  7. /* Magic happens here -- this file is generated by fbt.
  8. * Just set fap_icon_assets in application.fam and #include {APPID}_icons.h */
  9. #include "flipp_pomodoro_icons.h"
  10. const int SECONDS_IN_MINUTE = 60;
  11. /// @brief Actions to be processed in a queue
  12. typedef enum {
  13. TimerTickType = 42,
  14. InputEventType,
  15. } ActionType;
  16. /// @brief Single action contains type and payload
  17. typedef struct {
  18. ActionType type;
  19. void* payload;
  20. } Action;
  21. typedef enum {
  22. Work,
  23. Rest,
  24. } PomodoroStage;
  25. static const NotificationSequence work_start_notification = {
  26. &message_display_backlight_on,
  27. &message_vibro_on,
  28. &message_note_b5,
  29. &message_delay_250,
  30. &message_note_d5,
  31. &message_delay_250,
  32. &message_sound_off,
  33. &message_vibro_off,
  34. &message_green_255,
  35. &message_delay_1000,
  36. &message_green_0,
  37. &message_delay_250,
  38. &message_green_255,
  39. &message_delay_1000,
  40. NULL,
  41. };
  42. static const NotificationSequence rest_start_notification = {
  43. &message_display_backlight_on,
  44. &message_vibro_on,
  45. &message_note_d5,
  46. &message_delay_250,
  47. &message_note_b5,
  48. &message_delay_250,
  49. &message_sound_off,
  50. &message_vibro_off,
  51. &message_red_255,
  52. &message_delay_1000,
  53. &message_red_0,
  54. &message_delay_250,
  55. &message_red_255,
  56. &message_delay_1000,
  57. NULL,
  58. };
  59. static const NotificationSequence* stage_start_notification_sequence_map[] = {
  60. [Work] = &work_start_notification,
  61. [Rest] = &rest_start_notification,
  62. };
  63. static char* next_stage_label[] = {
  64. [Work] = "Get Rest",
  65. [Rest] = "Start Work",
  66. };
  67. static const Icon* stage_background_image[] = {
  68. [Work] = &I_flipp_pomodoro_work_64,
  69. [Rest] = &I_flipp_pomodoro_rest_64,
  70. };
  71. static const PomodoroStage stage_rotaion_map[] = {
  72. [Work] = Rest,
  73. [Rest] = Work,
  74. };
  75. static const int32_t stage_duration_seconds_map[] = {
  76. [Work] = 25 * SECONDS_IN_MINUTE,
  77. [Rest] = 5 * SECONDS_IN_MINUTE,
  78. };
  79. const PomodoroStage default_stage = Work;
  80. /// @brief Container for a time period
  81. typedef struct {
  82. uint8_t seconds;
  83. uint8_t minutes;
  84. uint32_t total_seconds;
  85. } TimeDifference;
  86. typedef struct {
  87. PomodoroStage stage;
  88. uint32_t started_at_timestamp;
  89. } FlippPomodoroState;
  90. /// @brief Calculates difference between two provided timestamps
  91. /// @param begin - start timestamp of the period
  92. /// @param end - end timestamp of the period to measure
  93. /// @return TimeDifference struct
  94. static TimeDifference get_timestamp_difference_seconds(uint32_t begin, uint32_t end) {
  95. const uint32_t duration_seconds = end - begin;
  96. uint32_t minutes = (duration_seconds / SECONDS_IN_MINUTE) % SECONDS_IN_MINUTE;
  97. uint32_t seconds = duration_seconds % SECONDS_IN_MINUTE;
  98. return (TimeDifference){.total_seconds=duration_seconds, .minutes=minutes, .seconds=seconds};
  99. }
  100. static void flipp_pomodoro__toggle_stage(FlippPomodoroState* state) {
  101. furi_assert(state);
  102. state->stage = stage_rotaion_map[state->stage];
  103. state->started_at_timestamp = furi_hal_rtc_get_timestamp();
  104. }
  105. static char* flipp_pomodoro__next_stage_label(FlippPomodoroState* state) {
  106. furi_assert(state);
  107. return next_stage_label[state->stage];
  108. };
  109. static void flipp_pomodoro__destroy(FlippPomodoroState* state) {
  110. furi_assert(state);
  111. free(state);
  112. }
  113. static uint32_t flipp_pomodoro__stage_expires_timestamp(FlippPomodoroState* state) {
  114. return state->started_at_timestamp + stage_duration_seconds_map[state->stage];
  115. }
  116. static TimeDifference flipp_pomodoro__stage_remaining_duration(FlippPomodoroState* state) {
  117. const uint32_t now = furi_hal_rtc_get_timestamp();
  118. const uint32_t stage_ends_at = flipp_pomodoro__stage_expires_timestamp(state);
  119. return get_timestamp_difference_seconds(now, stage_ends_at);
  120. }
  121. static bool flipp_pomodoro__is_stage_expired(FlippPomodoroState* state) {
  122. const uint32_t now = furi_hal_rtc_get_timestamp();
  123. const uint32_t expired_by = flipp_pomodoro__stage_expires_timestamp(state);
  124. const uint8_t seamless_change_span_seconds = 1;
  125. return (now - seamless_change_span_seconds) >= expired_by;
  126. }
  127. static FlippPomodoroState flipp_pomodoro__new() {
  128. const uint32_t now = furi_hal_rtc_get_timestamp();
  129. const FlippPomodoroState new_state = {.stage=default_stage, .started_at_timestamp=now};
  130. return new_state;
  131. }
  132. typedef struct {
  133. FlippPomodoroState* state;
  134. } DrawContext;
  135. // Screen is 128x64 px
  136. static void app_draw_callback(Canvas* canvas, void* ctx) {
  137. // WARNING: place no side-effects into rener cycle!!
  138. DrawContext* draw_context = ctx;
  139. const TimeDifference remaining_stage_time = flipp_pomodoro__stage_remaining_duration(draw_context->state);
  140. // Format remaining stage time;
  141. FuriString* timer_string = furi_string_alloc();
  142. furi_string_printf(timer_string, "%02u:%02u", remaining_stage_time.minutes, remaining_stage_time.seconds);
  143. const char* remaining_stage_time_string = furi_string_get_cstr(timer_string);
  144. // Render interface
  145. canvas_clear(canvas);
  146. canvas_draw_icon(canvas, 0, 0, stage_background_image[draw_context->state->stage]);
  147. // Countdown section
  148. const uint8_t countdown_box_height = canvas_height(canvas)*0.4;
  149. const uint8_t countdown_box_width = canvas_width(canvas)*0.5;
  150. const uint8_t countdown_box_x = canvas_width(canvas) - countdown_box_width - 2;
  151. const uint8_t countdown_box_y = 0;
  152. elements_bold_rounded_frame(canvas,
  153. countdown_box_x,
  154. countdown_box_y,
  155. countdown_box_width,
  156. countdown_box_height
  157. );
  158. canvas_set_font(canvas, FontBigNumbers);
  159. canvas_draw_str_aligned(
  160. canvas,
  161. countdown_box_x + (countdown_box_width/2),
  162. countdown_box_y + (countdown_box_height/2),
  163. AlignCenter,
  164. AlignCenter,
  165. remaining_stage_time_string
  166. );
  167. // Draw layout
  168. canvas_set_font(canvas, FontSecondary);
  169. elements_button_right(canvas, flipp_pomodoro__next_stage_label(draw_context->state));
  170. // Cleanup
  171. furi_string_free(timer_string);
  172. }
  173. static void clock_tick_callback(void* ctx) {
  174. furi_assert(ctx);
  175. FuriMessageQueue* queue = ctx;
  176. Action action = {.type = TimerTickType};
  177. furi_message_queue_put(queue, &action, 0);
  178. }
  179. static void app_input_callback(InputEvent* input_event, void* ctx) {
  180. furi_assert(ctx);
  181. Action action = {.type=InputEventType, .payload=input_event};
  182. FuriMessageQueue* event_queue = ctx;
  183. furi_message_queue_put(event_queue, &action, FuriWaitForever);
  184. };
  185. static bool input_events_reducer (FlippPomodoroState* state, InputEvent* input_event) {
  186. bool keep_running = true;
  187. if((input_event->type == InputTypePress) || (input_event->type == InputTypeRepeat)) {
  188. switch(input_event->key) {
  189. case InputKeyRight:
  190. flipp_pomodoro__toggle_stage(state);
  191. break;
  192. case InputKeyBack:
  193. keep_running = false;
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. return keep_running;
  200. }
  201. int32_t flipp_pomodoro_main(void* p) {
  202. UNUSED(p);
  203. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(Action));
  204. FlippPomodoroState state = flipp_pomodoro__new();
  205. // Configure view port
  206. ViewPort* view_port = view_port_alloc();
  207. DrawContext draw_context = {.state=&state};
  208. view_port_draw_callback_set(view_port, app_draw_callback, &draw_context);
  209. view_port_input_callback_set(view_port, app_input_callback, event_queue);
  210. // Initiate timer
  211. FuriTimer* timer = furi_timer_alloc(clock_tick_callback, FuriTimerTypePeriodic, event_queue);
  212. furi_timer_start(timer, 500);
  213. NotificationApp* notification_app = furi_record_open(RECORD_NOTIFICATION);
  214. // Register view port in GUI
  215. Gui* gui = furi_record_open(RECORD_GUI);
  216. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  217. bool running = true;
  218. while(running) {
  219. Action action;
  220. if(furi_message_queue_get(event_queue, &action, 200) != FuriStatusOk) {
  221. continue;
  222. };
  223. if(!action.type) {
  224. continue;
  225. }
  226. switch (action.type) {
  227. case InputEventType:
  228. running = input_events_reducer(&state, action.payload);
  229. break;
  230. case TimerTickType:
  231. if(flipp_pomodoro__is_stage_expired(&state)) {
  232. flipp_pomodoro__toggle_stage(&state);
  233. notification_message(notification_app, stage_start_notification_sequence_map[state.stage]);
  234. };
  235. break;
  236. default:
  237. break;
  238. }
  239. view_port_update(view_port); // Only re-draw on event
  240. }
  241. view_port_enabled_set(view_port, false);
  242. gui_remove_view_port(gui, view_port);
  243. view_port_free(view_port);
  244. furi_message_queue_free(event_queue);
  245. furi_record_close(RECORD_GUI);
  246. furi_timer_free(timer);
  247. flipp_pomodoro__destroy(&state);
  248. furi_record_close(RECORD_NOTIFICATION);
  249. return 0;
  250. }