countdown_view.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. CountDownViewSelect select = model->select;
  73. // elements_frame(canvas, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
  74. canvas_set_font(canvas, FontBigNumbers);
  75. draw_selection(canvas, select);
  76. parse_sec_to_time_str(buffer, sizeof(buffer), count);
  77. canvas_draw_str_aligned(
  78. canvas, SCREEN_CENTER_X, SCREEN_CENTER_Y, AlignCenter, AlignCenter, buffer);
  79. elements_progress_bar(canvas, 0, 0, SCREEN_WIDTH, (1.0 * count / expected_count));
  80. }
  81. // keys input event callback, CountDownTimView as ctx
  82. static bool countdown_timer_view_on_input(InputEvent* event, void* ctx) {
  83. furi_assert(ctx);
  84. CountDownTimView* hw = (CountDownTimView*)ctx;
  85. if(event->type == InputTypeShort || event->type == InputTypeRepeat) {
  86. switch(event->key) {
  87. case InputKeyUp:
  88. case InputKeyDown:
  89. case InputKeyRight:
  90. case InputKeyLeft:
  91. handle_time_setting_select(event->key, hw);
  92. break;
  93. case InputKeyOk:
  94. if(event->type == InputTypeShort) {
  95. handle_misc_cmd(hw, CountDownTimerToggleCounting);
  96. }
  97. break;
  98. case InputKeyBack:
  99. return false;
  100. break;
  101. default:
  102. break;
  103. }
  104. return true;
  105. }
  106. if(event->type == InputTypeLong) {
  107. switch(event->key) {
  108. case InputKeyOk:
  109. handle_misc_cmd(hw, CountDownTimerReset);
  110. break;
  111. case InputKeyBack:
  112. return false;
  113. break;
  114. default:
  115. break;
  116. }
  117. return true;
  118. }
  119. return false;
  120. }
  121. static void timer_cb(void* ctx) {
  122. furi_assert(ctx);
  123. CountDownTimView* cdv = (CountDownTimView*)ctx;
  124. int32_t count;
  125. bool timeup = false;
  126. // decrement counter
  127. with_view_model(
  128. cdv->view,
  129. CountDownModel * model,
  130. {
  131. count = model->count;
  132. count--;
  133. // check timeup
  134. if(count <= 0) {
  135. count = 0;
  136. timeup = true;
  137. }
  138. model->count = count;
  139. },
  140. true);
  141. if(timeup) {
  142. handle_misc_cmd(cdv, CountDownTimerTimeUp);
  143. }
  144. }
  145. static void handle_time_setting_updown(CountDownTimView* cdv, CountDownViewCmd cmd) {
  146. int32_t count;
  147. with_view_model(
  148. cdv->view,
  149. CountDownModel * model,
  150. {
  151. count = model->count;
  152. switch(cmd) {
  153. case CountDownTimerMinuteUp:
  154. count += 60;
  155. break;
  156. case CountDownTimerMinuteDown:
  157. count -= 60;
  158. break;
  159. case CountDownTimerHourDown:
  160. count -= 3600;
  161. break;
  162. case CountDownTimerHourUp:
  163. count += 3600;
  164. break;
  165. case CountDownTimerSecUp:
  166. count++;
  167. break;
  168. case CountDownTimerSecDown:
  169. count--;
  170. break;
  171. default:
  172. break;
  173. }
  174. if(count < 0) {
  175. count = 0;
  176. }
  177. // update count state
  178. model->count = count;
  179. // save the count time setting
  180. model->saved_count_setting = count;
  181. },
  182. true);
  183. }
  184. static void handle_misc_cmd(CountDownTimView* hw, CountDownViewCmd cmd) {
  185. switch(cmd) {
  186. case CountDownTimerTimeUp:
  187. notification_timeup();
  188. break;
  189. case CountDownTimerReset:
  190. furi_timer_stop(hw->timer);
  191. countdown_timer_view_state_reset(hw);
  192. notification_off();
  193. break;
  194. case CountDownTimerToggleCounting:
  195. countdown_timer_state_toggle(hw);
  196. break;
  197. default:
  198. break;
  199. }
  200. return;
  201. }
  202. static void handle_time_setting_select(InputKey key, CountDownTimView* cdv) {
  203. bool counting = cdv->counting;
  204. CountDownViewCmd setting_cmd = CountDownTimerSecUp;
  205. CountDownViewSelect selection;
  206. if(counting) {
  207. return;
  208. }
  209. // load current selection from model context
  210. with_view_model(
  211. cdv->view, CountDownModel * model, { selection = model->select; }, false);
  212. // select
  213. switch(key) {
  214. case InputKeyUp:
  215. switch(selection) {
  216. case CountDownTimerSelectSec:
  217. setting_cmd = CountDownTimerSecUp;
  218. break;
  219. case CountDownTimerSelectMinute:
  220. setting_cmd = CountDownTimerMinuteUp;
  221. break;
  222. case CountDownTimerSelectHour:
  223. setting_cmd = CountDownTimerHourUp;
  224. break;
  225. }
  226. handle_time_setting_updown(cdv, setting_cmd);
  227. break;
  228. case InputKeyDown:
  229. switch(selection) {
  230. case CountDownTimerSelectSec:
  231. setting_cmd = CountDownTimerSecDown;
  232. break;
  233. case CountDownTimerSelectMinute:
  234. setting_cmd = CountDownTimerMinuteDown;
  235. break;
  236. case CountDownTimerSelectHour:
  237. setting_cmd = CountDownTimerHourDown;
  238. break;
  239. }
  240. handle_time_setting_updown(cdv, setting_cmd);
  241. break;
  242. case InputKeyRight:
  243. selection--;
  244. selection = selection % 3;
  245. break;
  246. case InputKeyLeft:
  247. selection++;
  248. selection = selection % 3;
  249. break;
  250. default:
  251. break;
  252. }
  253. // save selection to model context
  254. with_view_model(
  255. cdv->view, CountDownModel * model, { model->select = selection; }, false);
  256. }
  257. static void draw_selection(Canvas* canvas, CountDownViewSelect selection) {
  258. switch(selection) {
  259. case CountDownTimerSelectSec:
  260. elements_slightly_rounded_box(canvas, SCREEN_CENTER_X + 25, SCREEN_CENTER_Y + 11, 24, 2);
  261. break;
  262. case CountDownTimerSelectMinute:
  263. elements_slightly_rounded_box(canvas, SCREEN_CENTER_X - 10, SCREEN_CENTER_Y + 11, 21, 2);
  264. break;
  265. case CountDownTimerSelectHour:
  266. elements_slightly_rounded_box(canvas, SCREEN_CENTER_X - 47, SCREEN_CENTER_Y + 11, 24, 2);
  267. break;
  268. }
  269. }
  270. static void countdown_timer_start_counting(CountDownTimView* cdv) {
  271. furi_timer_start(cdv->timer, furi_kernel_get_tick_frequency() * 1); // 1s
  272. }
  273. static void countdown_timer_pause_counting(CountDownTimView* cdv) {
  274. furi_timer_stop(cdv->timer);
  275. notification_off();
  276. }