countdown_view.c 8.8 KB

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