dtmf_dolphin_dialer.c 11 KB

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