countdown_view.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #include "countdown_view.h"
  2. #include "../utils/utils.h"
  3. // internal
  4. static void handle_misc_cmd(CountDownTimView* hw, CountDownViewCmd cmd);
  5. static void handle_time_setting_updown(CountDownTimView* cdv, CountDownViewCmd cmd);
  6. static void handle_time_setting_select(InputKey key, CountDownTimView* cdv);
  7. static void draw_selection(Canvas* canvas, CountDownViewSelect selection);
  8. static void countdown_timer_start_counting(CountDownTimView* cdv);
  9. static void countdown_timer_pause_counting(CountDownTimView* cdv);
  10. // callbacks
  11. static void countdown_timer_view_on_enter(void* ctx);
  12. static void countdown_timer_view_on_draw(Canvas* canvas, void* ctx);
  13. static bool countdown_timer_view_on_input(InputEvent* event, void* ctx);
  14. static void timer_cb(void* ctx);
  15. CountDownTimView* countdown_timer_view_new() {
  16. CountDownTimView* cdv = (CountDownTimView*)(malloc(sizeof(CountDownTimView)));
  17. cdv->view = view_alloc();
  18. cdv->timer = furi_timer_alloc(timer_cb, FuriTimerTypePeriodic, cdv);
  19. cdv->counting = false;
  20. view_set_context(cdv->view, cdv);
  21. view_allocate_model(cdv->view, ViewModelTypeLocking, sizeof(CountDownModel));
  22. view_set_draw_callback(cdv->view, countdown_timer_view_on_draw);
  23. view_set_input_callback(cdv->view, countdown_timer_view_on_input);
  24. view_set_enter_callback(cdv->view, countdown_timer_view_on_enter);
  25. return cdv;
  26. }
  27. void countdown_timer_view_delete(CountDownTimView* cdv) {
  28. furi_assert(cdv);
  29. view_free(cdv->view);
  30. furi_timer_stop(cdv->timer);
  31. furi_timer_free(cdv->timer);
  32. free(cdv);
  33. }
  34. View* countdown_timer_view_get_view(CountDownTimView* cdv) {
  35. return cdv->view;
  36. }
  37. void countdown_timer_view_state_reset(CountDownTimView* cdv) {
  38. cdv->counting = false;
  39. with_view_model(
  40. cdv->view, CountDownModel * model, { model->count = model->saved_count_setting; }, true)
  41. }
  42. void countdown_timer_state_toggle(CountDownTimView* cdv) {
  43. bool on = cdv->counting;
  44. if(!on) {
  45. countdown_timer_start_counting(cdv);
  46. } else {
  47. countdown_timer_pause_counting(cdv);
  48. }
  49. cdv->counting = !on;
  50. }
  51. // on enter callback, CountDownTimView as ctx
  52. static void countdown_timer_view_on_enter(void* ctx) {
  53. furi_assert(ctx);
  54. CountDownTimView* cdv = (CountDownTimView*)ctx;
  55. // set current count to a initial value
  56. with_view_model(
  57. cdv->view,
  58. CountDownModel * model,
  59. {
  60. model->count = INIT_COUNT;
  61. model->saved_count_setting = INIT_COUNT;
  62. },
  63. true);
  64. }
  65. // view draw callback, CountDownModel as ctx
  66. static void countdown_timer_view_on_draw(Canvas* canvas, void* ctx) {
  67. furi_assert(ctx);
  68. CountDownModel* model = (CountDownModel*)ctx;
  69. char buffer[64];
  70. int32_t count = model->count;
  71. int32_t expected_count = model->saved_count_setting;
  72. furi_check(expected_count > 0, "expected_count < 0");
  73. CountDownViewSelect select = model->select;
  74. // elements_frame(canvas, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  75. canvas_set_font(canvas, FontBigNumbers);
  76. draw_selection(canvas, select);
  77. parse_sec_to_time_str(buffer, sizeof(buffer), count);
  78. canvas_draw_str_aligned(
  79. canvas, SCREEN_CENTER_X, SCREEN_CENTER_Y, AlignCenter, AlignCenter, buffer);
  80. elements_progress_bar(canvas, 0, 0, SCREEN_WIDTH, (1.0 * count / expected_count));
  81. }
  82. // keys input event callback, CountDownTimView as ctx
  83. static bool countdown_timer_view_on_input(InputEvent* event, void* ctx) {
  84. furi_assert(ctx);
  85. CountDownTimView* hw = (CountDownTimView*)ctx;
  86. if(event->type == InputTypeShort || event->type == InputTypeRepeat) {
  87. switch(event->key) {
  88. case InputKeyUp:
  89. case InputKeyDown:
  90. case InputKeyRight:
  91. case InputKeyLeft:
  92. handle_time_setting_select(event->key, hw);
  93. break;
  94. case InputKeyOk:
  95. if(event->type == InputTypeShort) {
  96. handle_misc_cmd(hw, CountDownTimerToggleCounting);
  97. }
  98. break;
  99. case InputKeyBack:
  100. return false;
  101. break;
  102. default:
  103. break;
  104. }
  105. return true;
  106. }
  107. if(event->type == InputTypeLong) {
  108. switch(event->key) {
  109. case InputKeyOk:
  110. handle_misc_cmd(hw, CountDownTimerReset);
  111. break;
  112. case InputKeyBack:
  113. return false;
  114. break;
  115. default:
  116. break;
  117. }
  118. return true;
  119. }
  120. return false;
  121. }
  122. static void timer_cb(void* ctx) {
  123. furi_assert(ctx);
  124. CountDownTimView* cdv = (CountDownTimView*)ctx;
  125. int32_t count;
  126. bool timeup = false;
  127. // decrement counter
  128. with_view_model(
  129. cdv->view,
  130. CountDownModel * model,
  131. {
  132. count = model->count;
  133. count--;
  134. // check timeup
  135. if(count <= 0) {
  136. count = 0;
  137. timeup = true;
  138. }
  139. model->count = count;
  140. },
  141. true);
  142. if(timeup) {
  143. handle_misc_cmd(cdv, CountDownTimerTimeUp);
  144. }
  145. }
  146. static void handle_time_setting_updown(CountDownTimView* cdv, CountDownViewCmd cmd) {
  147. int32_t count;
  148. with_view_model(
  149. cdv->view,
  150. CountDownModel * model,
  151. {
  152. count = model->count;
  153. switch(cmd) {
  154. case CountDownTimerMinuteUp:
  155. count += 60;
  156. break;
  157. case CountDownTimerMinuteDown:
  158. count -= 60;
  159. break;
  160. case CountDownTimerHourDown:
  161. count -= 3600;
  162. break;
  163. case CountDownTimerHourUp:
  164. count += 3600;
  165. break;
  166. case CountDownTimerSecUp:
  167. count++;
  168. break;
  169. case CountDownTimerSecDown:
  170. count--;
  171. break;
  172. default:
  173. break;
  174. }
  175. count = MAX(count, 1);
  176. // update count state
  177. model->count = count;
  178. // save the count time setting
  179. model->saved_count_setting = count;
  180. },
  181. true);
  182. }
  183. static void handle_misc_cmd(CountDownTimView* hw, CountDownViewCmd cmd) {
  184. switch(cmd) {
  185. case CountDownTimerTimeUp:
  186. notification_timeup();
  187. break;
  188. case CountDownTimerReset:
  189. furi_timer_stop(hw->timer);
  190. countdown_timer_view_state_reset(hw);
  191. notification_off();
  192. break;
  193. case CountDownTimerToggleCounting:
  194. countdown_timer_state_toggle(hw);
  195. break;
  196. default:
  197. break;
  198. }
  199. return;
  200. }
  201. static void handle_time_setting_select(InputKey key, CountDownTimView* cdv) {
  202. bool counting = cdv->counting;
  203. CountDownViewCmd setting_cmd = CountDownTimerSecUp;
  204. CountDownViewSelect selection;
  205. if(counting) {
  206. return;
  207. }
  208. // load current selection from model context
  209. with_view_model(
  210. cdv->view, CountDownModel * model, { selection = model->select; }, false);
  211. // select
  212. switch(key) {
  213. case InputKeyUp:
  214. switch(selection) {
  215. case CountDownTimerSelectSec:
  216. setting_cmd = CountDownTimerSecUp;
  217. break;
  218. case CountDownTimerSelectMinute:
  219. setting_cmd = CountDownTimerMinuteUp;
  220. break;
  221. case CountDownTimerSelectHour:
  222. setting_cmd = CountDownTimerHourUp;
  223. break;
  224. }
  225. handle_time_setting_updown(cdv, setting_cmd);
  226. break;
  227. case InputKeyDown:
  228. switch(selection) {
  229. case CountDownTimerSelectSec:
  230. setting_cmd = CountDownTimerSecDown;
  231. break;
  232. case CountDownTimerSelectMinute:
  233. setting_cmd = CountDownTimerMinuteDown;
  234. break;
  235. case CountDownTimerSelectHour:
  236. setting_cmd = CountDownTimerHourDown;
  237. break;
  238. }
  239. handle_time_setting_updown(cdv, setting_cmd);
  240. break;
  241. case InputKeyRight:
  242. selection = (3 + selection - 1) % 3;
  243. break;
  244. case InputKeyLeft:
  245. selection = (3 + selection + 1) % 3;
  246. break;
  247. default:
  248. break;
  249. }
  250. // save selection to model context
  251. with_view_model(
  252. cdv->view, CountDownModel * model, { model->select = selection; }, false);
  253. }
  254. static void draw_selection(Canvas* canvas, CountDownViewSelect selection) {
  255. switch(selection) {
  256. case CountDownTimerSelectSec:
  257. elements_slightly_rounded_box(canvas, SCREEN_CENTER_X + 25, SCREEN_CENTER_Y + 11, 24, 2);
  258. break;
  259. case CountDownTimerSelectMinute:
  260. elements_slightly_rounded_box(canvas, SCREEN_CENTER_X - 10, SCREEN_CENTER_Y + 11, 21, 2);
  261. break;
  262. case CountDownTimerSelectHour:
  263. elements_slightly_rounded_box(canvas, SCREEN_CENTER_X - 47, SCREEN_CENTER_Y + 11, 24, 2);
  264. break;
  265. }
  266. }
  267. static void countdown_timer_start_counting(CountDownTimView* cdv) {
  268. furi_timer_start(cdv->timer, furi_kernel_get_tick_frequency() * 1); // 1s
  269. }
  270. static void countdown_timer_pause_counting(CountDownTimView* cdv) {
  271. furi_timer_stop(cdv->timer);
  272. notification_off();
  273. }