flipp_pomodoro.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include "../helpers/time.h"
  4. #include "flipp_pomodoro.h"
  5. char *next_stage_label[] = {
  6. [Work] = "Get Rest",
  7. [Rest] = "Start Work",
  8. };
  9. const PomodoroStage stage_rotaion_map[] = {
  10. [Work] = Rest,
  11. [Rest] = Work,
  12. };
  13. const PomodoroStage default_stage = Work;
  14. void flipp_pomodoro__toggle_stage(FlippPomodoroState *state)
  15. {
  16. furi_assert(state);
  17. state->stage = stage_rotaion_map[flipp_pomodoro__get_stage(state)];
  18. state->started_at_timestamp = time_now();
  19. };
  20. PomodoroStage flipp_pomodoro__get_stage(FlippPomodoroState *state)
  21. {
  22. furi_assert(state);
  23. return state->stage;
  24. };
  25. char *flipp_pomodoro__next_stage_label(FlippPomodoroState *state)
  26. {
  27. furi_assert(state);
  28. return next_stage_label[flipp_pomodoro__get_stage(state)];
  29. };
  30. void flipp_pomodoro__destroy(FlippPomodoroState *state)
  31. {
  32. furi_assert(state);
  33. free(state);
  34. };
  35. uint32_t flipp_pomodoro__current_stage_total_duration(FlippPomodoroState *state)
  36. {
  37. const int32_t stage_duration_seconds_map[] = {
  38. [Work] = 25 * TIME_SECONDS_IN_MINUTE,
  39. [Rest] = 5 * TIME_SECONDS_IN_MINUTE,
  40. };
  41. return stage_duration_seconds_map[flipp_pomodoro__get_stage(state)];
  42. };
  43. uint32_t flipp_pomodoro__stage_expires_timestamp(FlippPomodoroState *state)
  44. {
  45. return state->started_at_timestamp + flipp_pomodoro__current_stage_total_duration(state);
  46. };
  47. TimeDifference flipp_pomodoro__stage_remaining_duration(FlippPomodoroState *state)
  48. {
  49. const uint32_t stage_ends_at = flipp_pomodoro__stage_expires_timestamp(state);
  50. return time_difference_seconds(time_now(), stage_ends_at);
  51. };
  52. bool flipp_pomodoro__is_stage_expired(FlippPomodoroState *state)
  53. {
  54. const uint32_t expired_by = flipp_pomodoro__stage_expires_timestamp(state);
  55. const uint8_t seamless_change_span_seconds = 1;
  56. return (time_now() - seamless_change_span_seconds) >= expired_by;
  57. };
  58. FlippPomodoroState *flipp_pomodoro__new()
  59. {
  60. FlippPomodoroState *state = malloc(sizeof(FlippPomodoroState));
  61. const uint32_t now = time_now();
  62. state->started_at_timestamp = now;
  63. state->stage = default_stage;
  64. return state;
  65. };