flipp_pomodoro_app.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "flipp_pomodoro_app_i.h"
  2. #include <furi.h>
  3. #include <notification/notification_messages.h>
  4. #include <gui/gui.h>
  5. #include <dolphin/dolphin.h>
  6. #include <gui/elements.h>
  7. #include <input/input.h>
  8. /* Magic happens here -- this file is generated by fbt.
  9. * Just set fap_icon_assets in application.fam and #include {APPID}_icons.h */
  10. #include "flipp_pomodoro_icons.h"
  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. const PomodoroStage default_stage = Work;
  76. typedef struct {
  77. PomodoroStage stage;
  78. uint32_t started_at_timestamp;
  79. } FlippPomodoroState;
  80. static void flipp_pomodoro__toggle_stage(FlippPomodoroState* state) {
  81. furi_assert(state);
  82. state->stage = stage_rotaion_map[state->stage];
  83. state->started_at_timestamp = time_now();
  84. }
  85. static char* flipp_pomodoro__next_stage_label(FlippPomodoroState* state) {
  86. furi_assert(state);
  87. return next_stage_label[state->stage];
  88. };
  89. static void flipp_pomodoro__destroy(FlippPomodoroState* state) {
  90. furi_assert(state);
  91. free(state);
  92. }
  93. static uint32_t flipp_pomodoro__current_stage_total_duration(FlippPomodoroState* state) {
  94. const int32_t stage_duration_seconds_map[] = {
  95. [Work] = 25 * TIME_SECONDS_IN_MINUTE,
  96. [Rest] = 5 * TIME_SECONDS_IN_MINUTE,
  97. };
  98. return stage_duration_seconds_map[state->stage];
  99. }
  100. static uint32_t flipp_pomodoro__stage_expires_timestamp(FlippPomodoroState* state) {
  101. return state->started_at_timestamp + flipp_pomodoro__current_stage_total_duration(state);
  102. }
  103. static TimeDifference flipp_pomodoro__stage_remaining_duration(FlippPomodoroState* state) {
  104. const uint32_t stage_ends_at = flipp_pomodoro__stage_expires_timestamp(state);
  105. return time_difference_seconds(time_now(), stage_ends_at);
  106. }
  107. static bool flipp_pomodoro__is_stage_expired(FlippPomodoroState* state) {
  108. const uint32_t expired_by = flipp_pomodoro__stage_expires_timestamp(state);
  109. const uint8_t seamless_change_span_seconds = 1;
  110. return (time_now() - seamless_change_span_seconds) >= expired_by;
  111. }
  112. static FlippPomodoroState flipp_pomodoro__new() {
  113. const uint32_t now = time_now();
  114. const FlippPomodoroState new_state = {.stage=default_stage, .started_at_timestamp=now};
  115. return new_state;
  116. }
  117. typedef struct {
  118. FlippPomodoroState* state;
  119. } DrawContext;
  120. // Screen is 128x64 px
  121. static void app_draw_callback(Canvas* canvas, void* ctx) {
  122. // WARNING: place no side-effects into rener cycle!!
  123. DrawContext* draw_context = ctx;
  124. const TimeDifference remaining_stage_time = flipp_pomodoro__stage_remaining_duration(draw_context->state);
  125. // Format remaining stage time;
  126. FuriString* timer_string = furi_string_alloc();
  127. furi_string_printf(timer_string, "%02u:%02u", remaining_stage_time.minutes, remaining_stage_time.seconds);
  128. const char* remaining_stage_time_string = furi_string_get_cstr(timer_string);
  129. // Render interface
  130. canvas_clear(canvas);
  131. canvas_draw_icon(canvas, 0, 0, stage_background_image[draw_context->state->stage]);
  132. // Countdown section
  133. const uint8_t countdown_box_height = canvas_height(canvas)*0.4;
  134. const uint8_t countdown_box_width = canvas_width(canvas)*0.5;
  135. const uint8_t countdown_box_x = canvas_width(canvas) - countdown_box_width - 2;
  136. const uint8_t countdown_box_y = 0;
  137. elements_bold_rounded_frame(canvas,
  138. countdown_box_x,
  139. countdown_box_y,
  140. countdown_box_width,
  141. countdown_box_height
  142. );
  143. canvas_set_font(canvas, FontBigNumbers);
  144. canvas_draw_str_aligned(
  145. canvas,
  146. countdown_box_x + (countdown_box_width/2),
  147. countdown_box_y + (countdown_box_height/2),
  148. AlignCenter,
  149. AlignCenter,
  150. remaining_stage_time_string
  151. );
  152. // Draw layout
  153. canvas_set_font(canvas, FontSecondary);
  154. elements_button_right(canvas, flipp_pomodoro__next_stage_label(draw_context->state));
  155. // Cleanup
  156. furi_string_free(timer_string);
  157. }
  158. static void clock_tick_callback(void* ctx) {
  159. furi_assert(ctx);
  160. FuriMessageQueue* queue = ctx;
  161. Action action = {.type = TimerTickType};
  162. furi_message_queue_put(queue, &action, 0);
  163. }
  164. static void app_input_callback(InputEvent* input_event, void* ctx) {
  165. furi_assert(ctx);
  166. Action action = {.type=InputEventType, .payload=input_event};
  167. FuriMessageQueue* event_queue = ctx;
  168. furi_message_queue_put(event_queue, &action, FuriWaitForever);
  169. };
  170. static bool is_input_event(Action action) {
  171. return action.type == InputEventType;
  172. }
  173. static bool is_press_or_repeat(InputEvent* input_event) {
  174. return (input_event->type == InputTypePress) || (input_event->type == InputTypeRepeat);
  175. }
  176. static bool is_button_back_pressed(Action action) {
  177. return is_input_event(action)
  178. && is_press_or_repeat(action.payload)
  179. && ((InputEvent*)action.payload)->key == InputKeyBack;
  180. }
  181. static bool is_button_right_pressed(Action action) {
  182. return is_input_event(action)
  183. && is_press_or_repeat(action.payload)
  184. && ((InputEvent*)action.payload)->key == InputKeyRight;
  185. }
  186. static bool is_timer_tick(Action action) {
  187. return action.type == TimerTickType;
  188. };
  189. int32_t flipp_pomodoro_main(void* p) {
  190. UNUSED(p);
  191. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(Action));
  192. FlippPomodoroState state = flipp_pomodoro__new();
  193. // Configure view port
  194. ViewPort* view_port = view_port_alloc();
  195. DrawContext draw_context = {.state=&state};
  196. view_port_draw_callback_set(view_port, app_draw_callback, &draw_context);
  197. view_port_input_callback_set(view_port, app_input_callback, event_queue);
  198. // Initiate timer
  199. FuriTimer* timer = furi_timer_alloc(clock_tick_callback, FuriTimerTypePeriodic, event_queue);
  200. furi_timer_start(timer, 500);
  201. NotificationApp* notification_app = furi_record_open(RECORD_NOTIFICATION);
  202. // Register view port in GUI
  203. Gui* gui = furi_record_open(RECORD_GUI);
  204. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  205. bool running = true;
  206. const int queue_reading_window_tics = 200;
  207. while(running) {
  208. Action action;
  209. if(furi_message_queue_get(event_queue, &action, queue_reading_window_tics) != FuriStatusOk) {
  210. // Queue read is failed
  211. continue;
  212. };
  213. if(!action.type) {
  214. // No item in queue
  215. continue;
  216. }
  217. if(is_button_back_pressed(action)) {
  218. running = false;
  219. continue;
  220. };
  221. if(is_timer_tick(action)) {
  222. if(flipp_pomodoro__is_stage_expired(&state)) {
  223. if(state.stage == Work) {
  224. // REGISTER a deed on work stage complete to get an acheivement
  225. DOLPHIN_DEED(DolphinDeedPluginGameWin);
  226. };
  227. flipp_pomodoro__toggle_stage(&state);
  228. notification_message(notification_app, stage_start_notification_sequence_map[state.stage]);
  229. };
  230. }
  231. if(is_button_right_pressed(action)) {
  232. flipp_pomodoro__toggle_stage(&state);
  233. };
  234. view_port_update(view_port); // Only re-draw on event
  235. }
  236. view_port_enabled_set(view_port, false);
  237. gui_remove_view_port(gui, view_port);
  238. view_port_free(view_port);
  239. furi_message_queue_free(event_queue);
  240. furi_record_close(RECORD_GUI);
  241. furi_timer_free(timer);
  242. flipp_pomodoro__destroy(&state);
  243. furi_record_close(RECORD_NOTIFICATION);
  244. return 0;
  245. }