flipp_pomodoro.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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[state->stage];
  18. state->started_at_timestamp = time_now();
  19. };
  20. char *flipp_pomodoro__next_stage_label(FlippPomodoroState *state)
  21. {
  22. furi_assert(state);
  23. return next_stage_label[state->stage];
  24. };
  25. void flipp_pomodoro__destroy(FlippPomodoroState *state)
  26. {
  27. furi_assert(state);
  28. free(state);
  29. };
  30. uint32_t flipp_pomodoro__current_stage_total_duration(FlippPomodoroState *state)
  31. {
  32. const int32_t stage_duration_seconds_map[] = {
  33. [Work] = 25 * TIME_SECONDS_IN_MINUTE,
  34. [Rest] = 5 * TIME_SECONDS_IN_MINUTE,
  35. };
  36. return stage_duration_seconds_map[state->stage];
  37. };
  38. uint32_t flipp_pomodoro__stage_expires_timestamp(FlippPomodoroState *state)
  39. {
  40. return state->started_at_timestamp + flipp_pomodoro__current_stage_total_duration(state);
  41. };
  42. TimeDifference flipp_pomodoro__stage_remaining_duration(FlippPomodoroState *state)
  43. {
  44. const uint32_t stage_ends_at = flipp_pomodoro__stage_expires_timestamp(state);
  45. return time_difference_seconds(time_now(), stage_ends_at);
  46. };
  47. bool flipp_pomodoro__is_stage_expired(FlippPomodoroState *state)
  48. {
  49. const uint32_t expired_by = flipp_pomodoro__stage_expires_timestamp(state);
  50. const uint8_t seamless_change_span_seconds = 1;
  51. return (time_now() - seamless_change_span_seconds) >= expired_by;
  52. };
  53. FlippPomodoroState *flipp_pomodoro__new()
  54. {
  55. FlippPomodoroState *state = malloc(sizeof(FlippPomodoroState));
  56. const uint32_t now = time_now();
  57. state->started_at_timestamp = now;
  58. state->stage = default_stage;
  59. return state;
  60. };