dtmf_dolphin_dialer.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. FuriString *output = furi_string_alloc();
  109. if (model->freq1 && model->freq2) {
  110. furi_string_cat_printf(
  111. output,
  112. "Dual Tone\nF1: %u Hz\nF2: %u Hz\n",
  113. (unsigned int) model->freq1,
  114. (unsigned int) model->freq2);
  115. } else if (model->freq1) {
  116. furi_string_cat_printf(
  117. output,
  118. "Single Tone\nF: %u Hz\n",
  119. (unsigned int) model->freq1);
  120. }
  121. canvas_set_font(canvas, FontSecondary);
  122. canvas_set_color(canvas, ColorBlack);
  123. elements_multiline_text(canvas, (max_span * DTMF_DOLPHIN_BUTTON_WIDTH) + 4, 21, furi_string_get_cstr(output));
  124. furi_string_free(output);
  125. }
  126. static bool dtmf_dolphin_dialer_input_callback(InputEvent* event, void* context) {
  127. furi_assert(context);
  128. DTMFDolphinDialer* dtmf_dolphin_dialer = context;
  129. bool consumed = false;
  130. if(event->type == InputTypeShort) {
  131. if(event->key == InputKeyRight) {
  132. consumed = dtmf_dolphin_dialer_process_right(dtmf_dolphin_dialer);
  133. } else if(event->key == InputKeyLeft) {
  134. consumed = dtmf_dolphin_dialer_process_left(dtmf_dolphin_dialer);
  135. } else if(event->key == InputKeyUp) {
  136. consumed = dtmf_dolphin_dialer_process_up(dtmf_dolphin_dialer);
  137. } else if(event->key == InputKeyDown) {
  138. consumed = dtmf_dolphin_dialer_process_down(dtmf_dolphin_dialer);
  139. }
  140. } else if(event->key == InputKeyOk) {
  141. consumed = dtmf_dolphin_dialer_process_ok(dtmf_dolphin_dialer, event);
  142. }
  143. return consumed;
  144. }
  145. static bool dtmf_dolphin_dialer_process_up(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  146. with_view_model(
  147. dtmf_dolphin_dialer->view,
  148. DTMFDolphinDialerModel * model,
  149. {
  150. uint8_t span = 0;
  151. uint8_t cursor = model->row;
  152. while (span == 0 && cursor > 0) {
  153. cursor--;
  154. span = dtmf_dolphin_get_tone_span(cursor, model->col);
  155. }
  156. if (span != 0) {
  157. model->row = cursor;
  158. }
  159. },
  160. true);
  161. return true;
  162. }
  163. static bool dtmf_dolphin_dialer_process_down(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  164. uint8_t max_rows = 0;
  165. uint8_t max_cols = 0;
  166. uint8_t max_span = 0;
  167. dtmf_dolphin_tone_get_max_pos(&max_rows, &max_cols, &max_span);
  168. with_view_model(
  169. dtmf_dolphin_dialer->view,
  170. DTMFDolphinDialerModel * model,
  171. {
  172. uint8_t span = 0;
  173. uint8_t cursor = model->row;
  174. while(span == 0 && cursor < max_rows - 1) {
  175. cursor++;
  176. span = dtmf_dolphin_get_tone_span(cursor, model->col);
  177. }
  178. if (span != 0) {
  179. model->row = cursor;
  180. }
  181. },
  182. true);
  183. return true;
  184. }
  185. static bool dtmf_dolphin_dialer_process_left(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  186. with_view_model(
  187. dtmf_dolphin_dialer->view,
  188. DTMFDolphinDialerModel * model,
  189. {
  190. uint8_t span = 0;
  191. uint8_t cursor = model->col;
  192. while (span == 0 && cursor > 0) {
  193. cursor--;
  194. span = dtmf_dolphin_get_tone_span(model->row, cursor);
  195. }
  196. if (span != 0) {
  197. model->col = cursor;
  198. }
  199. },
  200. true);
  201. return true;
  202. }
  203. static bool dtmf_dolphin_dialer_process_right(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  204. uint8_t max_rows = 0;
  205. uint8_t max_cols = 0;
  206. uint8_t max_span = 0;
  207. dtmf_dolphin_tone_get_max_pos(&max_rows, &max_cols, &max_span);
  208. with_view_model(
  209. dtmf_dolphin_dialer->view,
  210. DTMFDolphinDialerModel * model,
  211. {
  212. uint8_t span = 0;
  213. uint8_t cursor = model->col;
  214. while(span == 0 && cursor < max_cols - 1) {
  215. cursor++;
  216. span = dtmf_dolphin_get_tone_span(model->row, cursor);
  217. }
  218. if (span != 0) {
  219. model->col = cursor;
  220. }
  221. },
  222. true);
  223. return true;
  224. }
  225. static bool dtmf_dolphin_dialer_process_ok(DTMFDolphinDialer* dtmf_dolphin_dialer, InputEvent* event) {
  226. bool consumed = false;
  227. with_view_model(
  228. dtmf_dolphin_dialer->view,
  229. DTMFDolphinDialerModel * model,
  230. {
  231. if (event->type == InputTypePress) {
  232. model->playing = dtmf_dolphin_audio_play_tones(model->freq1, model->freq2);
  233. } else if (event->type == InputTypeRelease) {
  234. model->playing = !dtmf_dolphin_audio_stop_tones();
  235. }
  236. },
  237. true);
  238. return consumed;
  239. }
  240. static void dtmf_dolphin_dialer_enter_callback(void* context) {
  241. furi_assert(context);
  242. DTMFDolphinDialer* dtmf_dolphin_dialer = context;
  243. with_view_model(
  244. dtmf_dolphin_dialer->view,
  245. DTMFDolphinDialerModel * model,
  246. {
  247. model->col = 0;
  248. model->row = 0;
  249. model->section = 0;
  250. model->freq1 = 0.0;
  251. model->freq2 = 0.0;
  252. model->playing = false;
  253. },
  254. true
  255. );
  256. }
  257. DTMFDolphinDialer* dtmf_dolphin_dialer_alloc() {
  258. DTMFDolphinDialer* dtmf_dolphin_dialer = malloc(sizeof(DTMFDolphinDialer));
  259. dtmf_dolphin_dialer->view = view_alloc();
  260. view_allocate_model(dtmf_dolphin_dialer->view, ViewModelTypeLocking, sizeof(DTMFDolphinDialerModel));
  261. with_view_model(
  262. dtmf_dolphin_dialer->view,
  263. DTMFDolphinDialerModel * model,
  264. {
  265. model->col = 0;
  266. model->row = 0;
  267. model->section = 0;
  268. model->freq1 = 0.0;
  269. model->freq2 = 0.0;
  270. model->playing = false;
  271. },
  272. true
  273. );
  274. view_set_context(dtmf_dolphin_dialer->view, dtmf_dolphin_dialer);
  275. view_set_draw_callback(dtmf_dolphin_dialer->view, dtmf_dolphin_dialer_draw_callback);
  276. view_set_input_callback(dtmf_dolphin_dialer->view, dtmf_dolphin_dialer_input_callback);
  277. view_set_enter_callback(dtmf_dolphin_dialer->view, dtmf_dolphin_dialer_enter_callback);
  278. return dtmf_dolphin_dialer;
  279. }
  280. void dtmf_dolphin_dialer_free(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  281. furi_assert(dtmf_dolphin_dialer);
  282. view_free(dtmf_dolphin_dialer->view);
  283. free(dtmf_dolphin_dialer);
  284. }
  285. View* dtmf_dolphin_dialer_get_view(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  286. furi_assert(dtmf_dolphin_dialer);
  287. return dtmf_dolphin_dialer->view;
  288. }