dtmf_dolphin_dialer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include "dtmf_dolphin_dialer.h"
  2. #include <gui/elements.h>
  3. typedef struct DTMFDolphinDialer {
  4. View* view;
  5. DTMFDolphinDialerOkCallback callback;
  6. void* context;
  7. } DTMFDolphinDialer;
  8. typedef struct {
  9. DTMFDolphinToneSection section;
  10. uint8_t row;
  11. uint8_t col;
  12. float freq1;
  13. float freq2;
  14. bool playing;
  15. } DTMFDolphinDialerModel;
  16. static bool dtmf_dolphin_dialer_process_up(DTMFDolphinDialer* dtmf_dolphin_dialer);
  17. static bool dtmf_dolphin_dialer_process_down(DTMFDolphinDialer* dtmf_dolphin_dialer);
  18. static bool dtmf_dolphin_dialer_process_left(DTMFDolphinDialer* dtmf_dolphin_dialer);
  19. static bool dtmf_dolphin_dialer_process_right(DTMFDolphinDialer* dtmf_dolphin_dialer);
  20. static bool dtmf_dolphin_dialer_process_ok(DTMFDolphinDialer* dtmf_dolphin_dialer, InputEvent* event);
  21. void draw_button(Canvas* canvas, uint8_t row, uint8_t col, bool invert) {
  22. uint8_t left = DTMF_DOLPHIN_NUMPAD_X + \
  23. // ((col + 1) * DTMF_DOLPHIN_BUTTON_PADDING) +
  24. (col * DTMF_DOLPHIN_BUTTON_WIDTH);
  25. // (col * DTMF_DOLPHIN_BUTTON_PADDING);
  26. uint8_t top = DTMF_DOLPHIN_NUMPAD_Y + \
  27. // ((row + 1) * DTMF_DOLPHIN_BUTTON_PADDING) +
  28. (row * DTMF_DOLPHIN_BUTTON_HEIGHT);
  29. // (row * DTMF_DOLPHIN_BUTTON_PADDING);
  30. uint8_t span = dtmf_dolphin_get_tone_span(row, col);
  31. if (span == 0) {
  32. return;
  33. }
  34. canvas_set_color(canvas, ColorBlack);
  35. if (invert)
  36. canvas_draw_rbox(canvas, left, top,
  37. (DTMF_DOLPHIN_BUTTON_WIDTH * span) - (DTMF_DOLPHIN_BUTTON_PADDING * 2),
  38. DTMF_DOLPHIN_BUTTON_HEIGHT - (DTMF_DOLPHIN_BUTTON_PADDING * 2),
  39. 2);
  40. else
  41. canvas_draw_rframe(canvas, left, top,
  42. (DTMF_DOLPHIN_BUTTON_WIDTH * span) - (DTMF_DOLPHIN_BUTTON_PADDING * 2),
  43. DTMF_DOLPHIN_BUTTON_HEIGHT- (DTMF_DOLPHIN_BUTTON_PADDING * 2),
  44. 2);
  45. if (invert)
  46. canvas_invert_color(canvas);
  47. canvas_set_font(canvas, FontSecondary);
  48. // canvas_set_color(canvas, invert ? ColorWhite : ColorBlack);
  49. canvas_draw_str_aligned(canvas,
  50. left - 1 + (int) ((DTMF_DOLPHIN_BUTTON_WIDTH * span) / 2),
  51. top + (int) (DTMF_DOLPHIN_BUTTON_HEIGHT / 2),
  52. AlignCenter,
  53. AlignCenter,
  54. dtmf_dolphin_data_get_tone_name(row, col));
  55. if (invert)
  56. canvas_invert_color(canvas);
  57. }
  58. void draw_dialer(Canvas* canvas, void* _model) {
  59. DTMFDolphinDialerModel* model = _model;
  60. uint8_t max_rows;
  61. uint8_t max_cols;
  62. uint8_t max_span;
  63. dtmf_dolphin_tone_get_max_pos(&max_rows, &max_cols, &max_span);
  64. canvas_set_font(canvas, FontSecondary);
  65. for (int r = 0; r < max_rows; r++) {
  66. for (int c = 0; c < max_cols; c++) {
  67. if (model->row == r && model->col == c)
  68. draw_button(canvas, r, c, true);
  69. else
  70. draw_button(canvas, r, c, false);
  71. }
  72. }
  73. }
  74. void update_frequencies(DTMFDolphinDialerModel *model) {
  75. dtmf_dolphin_data_get_tone_frequencies(&model->freq1, &model->freq2, model->row, model->col);
  76. }
  77. static void dtmf_dolphin_dialer_draw_callback(Canvas* canvas, void* _model) {
  78. DTMFDolphinDialerModel* model = _model;
  79. if (model->playing) {
  80. // Leverage the prioritized draw callback to handle
  81. // the DMA so that it doesn't skip.
  82. dtmf_dolphin_audio_handle_tick();
  83. // Don't do any drawing if audio is playing.
  84. canvas_set_font(canvas, FontPrimary);
  85. elements_multiline_text_aligned(
  86. canvas,
  87. canvas_width(canvas) / 2,
  88. canvas_height(canvas) / 2,
  89. AlignCenter,
  90. AlignCenter,
  91. "Playing Tones");
  92. return;
  93. }
  94. update_frequencies(model);
  95. uint8_t max_rows = 0;
  96. uint8_t max_cols = 0;
  97. uint8_t max_span = 0;
  98. dtmf_dolphin_tone_get_max_pos(&max_rows, &max_cols, &max_span);
  99. canvas_set_font(canvas, FontPrimary);
  100. elements_multiline_text(canvas, 2, 10, dtmf_dolphin_data_get_current_section_name());
  101. canvas_draw_line(canvas,
  102. (max_span * DTMF_DOLPHIN_BUTTON_WIDTH) + 1, 0,
  103. (max_span * DTMF_DOLPHIN_BUTTON_WIDTH) + 1, canvas_height(canvas));
  104. elements_multiline_text(canvas, (max_span * DTMF_DOLPHIN_BUTTON_WIDTH) + 4, 10, "Detail");
  105. canvas_draw_line(canvas, 0, DTMF_DOLPHIN_NUMPAD_Y - 3, canvas_width(canvas), DTMF_DOLPHIN_NUMPAD_Y - 3);
  106. // elements_multiline_text_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Dialer Mode");
  107. draw_dialer(canvas, model);
  108. string_t output;
  109. string_init(output);
  110. if (model->freq1 && model->freq2) {
  111. string_cat_printf(
  112. output,
  113. "Dual Tone\nF1: %u Hz\nF2: %u Hz\n",
  114. (unsigned int) model->freq1,
  115. (unsigned int) model->freq2);
  116. } else if (model->freq1) {
  117. string_cat_printf(
  118. output,
  119. "Single Tone\nF: %u Hz\n",
  120. (unsigned int) model->freq1);
  121. }
  122. canvas_set_font(canvas, FontSecondary);
  123. canvas_set_color(canvas, ColorBlack);
  124. elements_multiline_text(canvas, (max_span * DTMF_DOLPHIN_BUTTON_WIDTH) + 4, 21, string_get_cstr(output));
  125. string_clear(output);
  126. }
  127. static bool dtmf_dolphin_dialer_input_callback(InputEvent* event, void* context) {
  128. furi_assert(context);
  129. DTMFDolphinDialer* dtmf_dolphin_dialer = context;
  130. bool consumed = false;
  131. if(event->type == InputTypeShort) {
  132. if(event->key == InputKeyRight) {
  133. consumed = dtmf_dolphin_dialer_process_right(dtmf_dolphin_dialer);
  134. } else if(event->key == InputKeyLeft) {
  135. consumed = dtmf_dolphin_dialer_process_left(dtmf_dolphin_dialer);
  136. } else if(event->key == InputKeyUp) {
  137. consumed = dtmf_dolphin_dialer_process_up(dtmf_dolphin_dialer);
  138. } else if(event->key == InputKeyDown) {
  139. consumed = dtmf_dolphin_dialer_process_down(dtmf_dolphin_dialer);
  140. }
  141. } else if(event->key == InputKeyOk) {
  142. consumed = dtmf_dolphin_dialer_process_ok(dtmf_dolphin_dialer, event);
  143. }
  144. return consumed;
  145. }
  146. static bool dtmf_dolphin_dialer_process_up(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  147. with_view_model(
  148. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  149. uint8_t span = 0;
  150. uint8_t cursor = model->row;
  151. while (span == 0 && cursor > 0) {
  152. cursor--;
  153. span = dtmf_dolphin_get_tone_span(cursor, model->col);
  154. }
  155. if (span != 0) {
  156. model->row = cursor;
  157. }
  158. return true;
  159. });
  160. return true;
  161. }
  162. static bool dtmf_dolphin_dialer_process_down(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  163. uint8_t max_rows = 0;
  164. uint8_t max_cols = 0;
  165. uint8_t max_span = 0;
  166. dtmf_dolphin_tone_get_max_pos(&max_rows, &max_cols, &max_span);
  167. with_view_model(
  168. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  169. uint8_t span = 0;
  170. uint8_t cursor = model->row;
  171. while(span == 0 && cursor < max_rows - 1) {
  172. cursor++;
  173. span = dtmf_dolphin_get_tone_span(cursor, model->col);
  174. }
  175. if (span != 0) {
  176. model->row = cursor;
  177. }
  178. return true;
  179. });
  180. return true;
  181. }
  182. static bool dtmf_dolphin_dialer_process_left(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  183. with_view_model(
  184. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  185. uint8_t span = 0;
  186. uint8_t cursor = model->col;
  187. while (span == 0 && cursor > 0) {
  188. cursor--;
  189. span = dtmf_dolphin_get_tone_span(model->row, cursor);
  190. }
  191. if (span != 0) {
  192. model->col = cursor;
  193. }
  194. return true;
  195. });
  196. return true;
  197. }
  198. static bool dtmf_dolphin_dialer_process_right(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  199. uint8_t max_rows = 0;
  200. uint8_t max_cols = 0;
  201. uint8_t max_span = 0;
  202. dtmf_dolphin_tone_get_max_pos(&max_rows, &max_cols, &max_span);
  203. with_view_model(
  204. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  205. uint8_t span = 0;
  206. uint8_t cursor = model->col;
  207. while(span == 0 && cursor < max_cols - 1) {
  208. cursor++;
  209. span = dtmf_dolphin_get_tone_span(model->row, cursor);
  210. }
  211. if (span != 0) {
  212. model->col = cursor;
  213. }
  214. return true;
  215. });
  216. return true;
  217. }
  218. static bool dtmf_dolphin_dialer_process_ok(DTMFDolphinDialer* dtmf_dolphin_dialer, InputEvent* event) {
  219. bool consumed = false;
  220. with_view_model(
  221. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  222. if (event->type == InputTypePress) {
  223. model->playing = dtmf_dolphin_audio_play_tones(model->freq1, model->freq2);
  224. } else if (event->type == InputTypeRelease) {
  225. model->playing = !dtmf_dolphin_audio_stop_tones();
  226. }
  227. return true;
  228. });
  229. return consumed;
  230. }
  231. static void dtmf_dolphin_dialer_enter_callback(void* context) {
  232. furi_assert(context);
  233. DTMFDolphinDialer* dtmf_dolphin_dialer = context;
  234. with_view_model(
  235. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  236. model->col = 0;
  237. model->row = 0;
  238. model->section = 0;
  239. model->freq1 = 0.0;
  240. model->freq2 = 0.0;
  241. model->playing = false;
  242. return true;
  243. }
  244. );
  245. }
  246. DTMFDolphinDialer* dtmf_dolphin_dialer_alloc() {
  247. DTMFDolphinDialer* dtmf_dolphin_dialer = malloc(sizeof(DTMFDolphinDialer));
  248. dtmf_dolphin_dialer->view = view_alloc();
  249. view_allocate_model(dtmf_dolphin_dialer->view, ViewModelTypeLocking, sizeof(DTMFDolphinDialerModel));
  250. with_view_model(
  251. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  252. model->col = 0;
  253. model->row = 0;
  254. model->section = 0;
  255. model->freq1 = 0.0;
  256. model->freq2 = 0.0;
  257. model->playing = false;
  258. return true;
  259. }
  260. );
  261. view_set_context(dtmf_dolphin_dialer->view, dtmf_dolphin_dialer);
  262. view_set_draw_callback(dtmf_dolphin_dialer->view, dtmf_dolphin_dialer_draw_callback);
  263. view_set_input_callback(dtmf_dolphin_dialer->view, dtmf_dolphin_dialer_input_callback);
  264. view_set_enter_callback(dtmf_dolphin_dialer->view, dtmf_dolphin_dialer_enter_callback);
  265. return dtmf_dolphin_dialer;
  266. }
  267. void dtmf_dolphin_dialer_free(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  268. furi_assert(dtmf_dolphin_dialer);
  269. view_free(dtmf_dolphin_dialer->view);
  270. free(dtmf_dolphin_dialer);
  271. }
  272. View* dtmf_dolphin_dialer_get_view(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  273. furi_assert(dtmf_dolphin_dialer);
  274. return dtmf_dolphin_dialer->view;
  275. }