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. 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. default:
  99. break;
  100. }
  101. return true;
  102. }
  103. if(event->type == InputTypeLong) {
  104. switch(event->key) {
  105. case InputKeyOk:
  106. handle_misc_cmd(hw, CountDownTimerReset);
  107. break;
  108. case InputKeyBack:
  109. return false;
  110. break;
  111. default:
  112. break;
  113. }
  114. return true;
  115. }
  116. return false;
  117. }
  118. static void timer_cb(void* ctx) {
  119. furi_assert(ctx);
  120. CountDownTimView* cdv = (CountDownTimView*)ctx;
  121. int32_t count;
  122. bool timeup = false;
  123. // decrement counter
  124. with_view_model(
  125. cdv->view,
  126. CountDownModel * model,
  127. {
  128. count = model->count;
  129. count--;
  130. // check timeup
  131. if(count <= 0) {
  132. count = 0;
  133. timeup = true;
  134. }
  135. model->count = count;
  136. },
  137. true);
  138. if(timeup) {
  139. handle_misc_cmd(cdv, CountDownTimerTimeUp);
  140. }
  141. }
  142. static void handle_time_setting_updown(CountDownTimView* cdv, CountDownViewCmd cmd) {
  143. int32_t count;
  144. with_view_model(
  145. cdv->view,
  146. CountDownModel * model,
  147. {
  148. count = model->count;
  149. switch(cmd) {
  150. case CountDownTimerMinuteUp:
  151. count += 60;
  152. break;
  153. case CountDownTimerMinuteDown:
  154. count -= 60;
  155. break;
  156. case CountDownTimerHourDown:
  157. count -= 3600;
  158. break;
  159. case CountDownTimerHourUp:
  160. count += 3600;
  161. break;
  162. case CountDownTimerSecUp:
  163. count++;
  164. break;
  165. case CountDownTimerSecDown:
  166. count--;
  167. break;
  168. default:
  169. break;
  170. }
  171. if(count < 0) {
  172. count = 0;
  173. }
  174. // update count state
  175. model->count = count;
  176. // save the count time setting
  177. model->saved_count_setting = count;
  178. },
  179. true);
  180. }
  181. static void handle_misc_cmd(CountDownTimView* hw, CountDownViewCmd cmd) {
  182. switch(cmd) {
  183. case CountDownTimerTimeUp:
  184. notification_timeup();
  185. break;
  186. case CountDownTimerReset:
  187. furi_timer_stop(hw->timer);
  188. countdown_timer_view_state_reset(hw);
  189. notification_off();
  190. break;
  191. case CountDownTimerToggleCounting:
  192. countdown_timer_state_toggle(hw);
  193. break;
  194. default:
  195. break;
  196. }
  197. return;
  198. }
  199. static void handle_time_setting_select(InputKey key, CountDownTimView* cdv) {
  200. bool counting = cdv->counting;
  201. CountDownViewCmd setting_cmd = CountDownTimerSecUp;
  202. CountDownViewSelect selection;
  203. if(counting) {
  204. return;
  205. }
  206. // load current selection from model context
  207. with_view_model(
  208. cdv->view, CountDownModel * model, { selection = model->select; }, false);
  209. // select
  210. switch(key) {
  211. case InputKeyUp:
  212. switch(selection) {
  213. case CountDownTimerSelectSec:
  214. setting_cmd = CountDownTimerSecUp;
  215. break;
  216. case CountDownTimerSelectMinute:
  217. setting_cmd = CountDownTimerMinuteUp;
  218. break;
  219. case CountDownTimerSelectHour:
  220. setting_cmd = CountDownTimerHourUp;
  221. break;
  222. }
  223. handle_time_setting_updown(cdv, setting_cmd);
  224. break;
  225. case InputKeyDown:
  226. switch(selection) {
  227. case CountDownTimerSelectSec:
  228. setting_cmd = CountDownTimerSecDown;
  229. break;
  230. case CountDownTimerSelectMinute:
  231. setting_cmd = CountDownTimerMinuteDown;
  232. break;
  233. case CountDownTimerSelectHour:
  234. setting_cmd = CountDownTimerHourDown;
  235. break;
  236. }
  237. handle_time_setting_updown(cdv, setting_cmd);
  238. break;
  239. case InputKeyRight:
  240. selection--;
  241. selection = selection % 3;
  242. break;
  243. case InputKeyLeft:
  244. selection++;
  245. selection = selection % 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. }