dtmf_dolphin_dialer.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. DTMFDolphinAudio player;
  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. canvas_set_color(canvas, ColorBlack);
  32. if (invert)
  33. canvas_draw_rbox(canvas, left, top,
  34. (DTMF_DOLPHIN_BUTTON_WIDTH * span) - (DTMF_DOLPHIN_BUTTON_PADDING * 2),
  35. DTMF_DOLPHIN_BUTTON_HEIGHT - (DTMF_DOLPHIN_BUTTON_PADDING * 2),
  36. 2);
  37. else
  38. canvas_draw_rframe(canvas, left, top,
  39. (DTMF_DOLPHIN_BUTTON_WIDTH * span) - (DTMF_DOLPHIN_BUTTON_PADDING * 2),
  40. DTMF_DOLPHIN_BUTTON_HEIGHT- (DTMF_DOLPHIN_BUTTON_PADDING * 2),
  41. 2);
  42. if (invert)
  43. canvas_invert_color(canvas);
  44. canvas_set_font(canvas, FontSecondary);
  45. // canvas_set_color(canvas, invert ? ColorWhite : ColorBlack);
  46. canvas_draw_str_aligned(canvas,
  47. left - 1 + (int) ((DTMF_DOLPHIN_BUTTON_WIDTH * span) / 2),
  48. top + (int) (DTMF_DOLPHIN_BUTTON_HEIGHT / 2),
  49. AlignCenter,
  50. AlignCenter,
  51. dtmf_dolphin_data_get_tone_name(row, col));
  52. if (invert)
  53. canvas_invert_color(canvas);
  54. }
  55. void draw_dialer(Canvas* canvas, void* _model) {
  56. DTMFDolphinDialerModel* model = _model;
  57. uint8_t max_rows;
  58. uint8_t max_cols;
  59. uint8_t max_span;
  60. dtmf_dolphin_tone_get_max_pos(&max_rows, &max_cols, &max_span);
  61. canvas_set_font(canvas, FontSecondary);
  62. for (int r = 0; r < max_rows; r++) {
  63. for (int c = 0; c < max_cols; c++) {
  64. if (model->row == r && model->col == c)
  65. draw_button(canvas, r, c, true);
  66. else
  67. draw_button(canvas, r, c, false);
  68. }
  69. }
  70. }
  71. void update_frequencies(DTMFDolphinDialerModel *model) {
  72. dtmf_dolphin_data_get_tone_frequencies(&model->freq1, &model->freq2, model->row, model->col);
  73. }
  74. static void dtmf_dolphin_dialer_draw_callback(Canvas* canvas, void* _model) {
  75. DTMFDolphinDialerModel* model = _model;
  76. update_frequencies(model);
  77. uint8_t max_rows;
  78. uint8_t max_cols;
  79. uint8_t max_span;
  80. dtmf_dolphin_tone_get_max_pos(&max_rows, &max_cols, &max_span);
  81. canvas_set_font(canvas, FontPrimary);
  82. elements_multiline_text(canvas, 2, 10, dtmf_dolphin_data_get_current_section_name());
  83. canvas_draw_line(canvas,
  84. (max_span * DTMF_DOLPHIN_BUTTON_WIDTH) + 1, 0,
  85. (max_span * DTMF_DOLPHIN_BUTTON_WIDTH) + 1, canvas_height(canvas));
  86. elements_multiline_text(canvas, (max_span * DTMF_DOLPHIN_BUTTON_WIDTH) + 4, 10, "Detail");
  87. canvas_draw_line(canvas, 0, DTMF_DOLPHIN_NUMPAD_Y - 3, canvas_width(canvas), DTMF_DOLPHIN_NUMPAD_Y - 3);
  88. // elements_multiline_text_aligned(canvas, 64, 2, AlignCenter, AlignTop, "Dialer Mode");
  89. draw_dialer(canvas, model);
  90. string_t output;
  91. string_init(output);
  92. string_cat_printf(
  93. output,
  94. "F1: %u Hz\nF2: %u Hz",
  95. model->freq1 ? (unsigned int) model->freq1 : 0,
  96. model->freq2 ? (unsigned int) model->freq2 : 0);
  97. canvas_set_font(canvas, FontSecondary);
  98. canvas_set_color(canvas, ColorBlack);
  99. elements_multiline_text(canvas, (max_span * DTMF_DOLPHIN_BUTTON_WIDTH) + 4, 21, string_get_cstr(output));
  100. string_clear(output);
  101. }
  102. static bool dtmf_dolphin_dialer_input_callback(InputEvent* event, void* context) {
  103. furi_assert(context);
  104. DTMFDolphinDialer* dtmf_dolphin_dialer = context;
  105. bool consumed = false;
  106. if(event->type == InputTypeShort) {
  107. if(event->key == InputKeyRight) {
  108. consumed = dtmf_dolphin_dialer_process_right(dtmf_dolphin_dialer);
  109. } else if(event->key == InputKeyLeft) {
  110. consumed = dtmf_dolphin_dialer_process_left(dtmf_dolphin_dialer);
  111. } else if(event->key == InputKeyUp) {
  112. consumed = dtmf_dolphin_dialer_process_up(dtmf_dolphin_dialer);
  113. } else if(event->key == InputKeyDown) {
  114. consumed = dtmf_dolphin_dialer_process_down(dtmf_dolphin_dialer);
  115. }
  116. } else if(event->key == InputKeyOk) {
  117. consumed = dtmf_dolphin_dialer_process_ok(dtmf_dolphin_dialer, event);
  118. }
  119. return consumed;
  120. }
  121. static bool dtmf_dolphin_dialer_process_up(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  122. with_view_model(
  123. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  124. if(model->row > 0) {
  125. model->row--;
  126. }
  127. return true;
  128. });
  129. return true;
  130. }
  131. static bool dtmf_dolphin_dialer_process_down(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  132. uint8_t max_rows = 0;
  133. uint8_t max_cols = 0;
  134. uint8_t max_span = 0;
  135. dtmf_dolphin_tone_get_max_pos(&max_rows, &max_cols, &max_span);
  136. with_view_model(
  137. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  138. if(model->row < max_rows - 1) {
  139. model->row++;
  140. }
  141. return true;
  142. });
  143. return true;
  144. }
  145. static bool dtmf_dolphin_dialer_process_left(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  146. with_view_model(
  147. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  148. if(model->col > 0) {
  149. model->col--;
  150. }
  151. return true;
  152. });
  153. return true;
  154. }
  155. static bool dtmf_dolphin_dialer_process_right(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  156. uint8_t max_rows = 0;
  157. uint8_t max_cols = 0;
  158. uint8_t max_span = 0;
  159. dtmf_dolphin_tone_get_max_pos(&max_rows, &max_cols, &max_span);
  160. with_view_model(
  161. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  162. if(model->col < max_cols - 1) {
  163. model->col++;
  164. }
  165. return true;
  166. });
  167. return true;
  168. }
  169. static bool dtmf_dolphin_dialer_process_ok(DTMFDolphinDialer* dtmf_dolphin_dialer, InputEvent* event) {
  170. bool consumed = false;
  171. with_view_model(
  172. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  173. if (event->type == InputTypePress) {
  174. dtmf_dolphin_audio_play_tones(model->freq1, model->freq2);
  175. } else if (event->type == InputTypeRelease) {
  176. dtmf_dolphin_audio_stop_tones();
  177. }
  178. return true;
  179. });
  180. return consumed;
  181. }
  182. DTMFDolphinDialer* dtmf_dolphin_dialer_alloc() {
  183. DTMFDolphinDialer* dtmf_dolphin_dialer = malloc(sizeof(DTMFDolphinDialer));
  184. dtmf_dolphin_dialer->view = view_alloc();
  185. view_allocate_model(dtmf_dolphin_dialer->view, ViewModelTypeLocking, sizeof(DTMFDolphinDialerModel));
  186. with_view_model(
  187. dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  188. model->col = 0;
  189. model->row = 0;
  190. model->section = 0;
  191. model->freq1 = 0.0;
  192. model->freq2 = 0.0;
  193. return true;
  194. }
  195. );
  196. view_set_context(dtmf_dolphin_dialer->view, dtmf_dolphin_dialer);
  197. view_set_draw_callback(dtmf_dolphin_dialer->view, dtmf_dolphin_dialer_draw_callback);
  198. view_set_input_callback(dtmf_dolphin_dialer->view, dtmf_dolphin_dialer_input_callback);
  199. return dtmf_dolphin_dialer;
  200. }
  201. void dtmf_dolphin_dialer_free(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  202. furi_assert(dtmf_dolphin_dialer);
  203. view_free(dtmf_dolphin_dialer->view);
  204. free(dtmf_dolphin_dialer);
  205. }
  206. View* dtmf_dolphin_dialer_get_view(DTMFDolphinDialer* dtmf_dolphin_dialer) {
  207. furi_assert(dtmf_dolphin_dialer);
  208. return dtmf_dolphin_dialer->view;
  209. }
  210. // void dtmf_dolphin_dialer_set_ok_callback(DTMFDolphinDialer* dtmf_dolphin_dialer, DTMFDolphinDialerOkCallback callback, void* context) {
  211. // furi_assert(dtmf_dolphin_dialer);
  212. // furi_assert(callback);
  213. // with_view_model(
  214. // dtmf_dolphin_dialer->view, (DTMFDolphinDialerModel * model) {
  215. // UNUSED(model);
  216. // dtmf_dolphin_dialer->callback = callback;
  217. // dtmf_dolphin_dialer->context = context;
  218. // return false;
  219. // });
  220. // }