dtmf_dolphin.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "dtmf_dolphin_i.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. static bool dtmf_dolphin_app_custom_event_callback(void* context, uint32_t event) {
  5. furi_assert(context);
  6. DTMFDolphinApp* app = context;
  7. return scene_manager_handle_custom_event(app->scene_manager, event);
  8. }
  9. static bool dtmf_dolphin_app_back_event_callback(void* context) {
  10. furi_assert(context);
  11. DTMFDolphinApp* app = context;
  12. return scene_manager_handle_back_event(app->scene_manager);
  13. }
  14. static void dtmf_dolphin_app_tick_event_callback(void* context) {
  15. furi_assert(context);
  16. DTMFDolphinApp* app = context;
  17. // Needed to handle queueing to ISR and prioritization of audio
  18. if (app->player.playing) {
  19. dtmf_dolphin_player_handle_tick();
  20. } else {
  21. scene_manager_handle_tick_event(app->scene_manager);
  22. }
  23. }
  24. static DTMFDolphinApp* app_alloc() {
  25. DTMFDolphinApp* app = malloc(sizeof(DTMFDolphinApp));
  26. app->player.half_samples = 4 * 1024;
  27. app->player.sample_count = 8 * 1024;
  28. app->player.sample_buffer = malloc(sizeof(uint16_t) * app->player.sample_count);
  29. app->player.buffer_buffer = malloc(sizeof(uint8_t) * app->player.sample_count);
  30. app->player.wf1_period = 0;
  31. app->player.wf2_period = 0;
  32. app->player.wf1_freq = 0;
  33. app->player.wf2_freq = 0;
  34. app->player.wf1_pos = 0;
  35. app->player.wf2_pos = 0;
  36. app->player.queue = furi_message_queue_alloc(10, sizeof(DTMFDolphinEvent));
  37. app->player.volume = 2.0f;
  38. app->player.playing = false;
  39. app->gui = furi_record_open(RECORD_GUI);
  40. app->view_dispatcher = view_dispatcher_alloc();
  41. app->scene_manager = scene_manager_alloc(&dtmf_dolphin_scene_handlers, app);
  42. view_dispatcher_enable_queue(app->view_dispatcher);
  43. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  44. view_dispatcher_set_custom_event_callback(
  45. app->view_dispatcher, dtmf_dolphin_app_custom_event_callback);
  46. view_dispatcher_set_navigation_event_callback(
  47. app->view_dispatcher, dtmf_dolphin_app_back_event_callback);
  48. view_dispatcher_set_tick_event_callback(
  49. app->view_dispatcher, dtmf_dolphin_app_tick_event_callback, 100);
  50. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  51. app->main_menu_list = variable_item_list_alloc();
  52. view_dispatcher_add_view(
  53. app->view_dispatcher,
  54. DTMFDolphinViewMainMenu,
  55. variable_item_list_get_view(app->main_menu_list));
  56. app->dtmf_dolphin_dialer = dtmf_dolphin_dialer_alloc();
  57. view_dispatcher_add_view(
  58. app->view_dispatcher,
  59. DTMFDolphinViewDialer,
  60. dtmf_dolphin_dialer_get_view(app->dtmf_dolphin_dialer));
  61. app->dtmf_dolphin_bluebox = dtmf_dolphin_bluebox_alloc();
  62. view_dispatcher_add_view(
  63. app->view_dispatcher,
  64. DTMFDolphinViewBluebox,
  65. dtmf_dolphin_bluebox_get_view(app->dtmf_dolphin_bluebox));
  66. app->dtmf_dolphin_play = widget_alloc();
  67. view_dispatcher_add_view(
  68. app->view_dispatcher,
  69. DTMFDolphinViewPlay,
  70. widget_get_view(app->dtmf_dolphin_play));
  71. // app->dialer_button_panel = button_panel_alloc();
  72. // app->bluebox_button_panel = button_panel_alloc();
  73. // app->redbox_button_panel = button_panel_alloc();
  74. app->notification = furi_record_open(RECORD_NOTIFICATION);
  75. notification_message(app->notification, &sequence_display_backlight_enforce_on);
  76. scene_manager_next_scene(app->scene_manager, DTMFDolphinSceneStart);
  77. return app;
  78. }
  79. static void app_free(DTMFDolphinApp* app) {
  80. furi_assert(app);
  81. view_dispatcher_remove_view(app->view_dispatcher, DTMFDolphinViewMainMenu);
  82. view_dispatcher_remove_view(app->view_dispatcher, DTMFDolphinViewBluebox);
  83. view_dispatcher_remove_view(app->view_dispatcher, DTMFDolphinViewDialer);
  84. view_dispatcher_remove_view(app->view_dispatcher, DTMFDolphinViewPlay);
  85. variable_item_list_free(app->main_menu_list);
  86. dtmf_dolphin_bluebox_free(app->dtmf_dolphin_bluebox);
  87. dtmf_dolphin_dialer_free(app->dtmf_dolphin_dialer);
  88. widget_free(app->dtmf_dolphin_play);
  89. view_dispatcher_free(app->view_dispatcher);
  90. scene_manager_free(app->scene_manager);
  91. furi_message_queue_free(app->player.queue);
  92. free(app->player.sample_buffer);
  93. // button_panel_free(app->dialer_button_panel);
  94. // button_panel_free(app->bluebox_button_panel);
  95. // button_panel_free(app->redbox_button_panel);
  96. notification_message(app->notification, &sequence_display_backlight_enforce_auto);
  97. furi_record_close(RECORD_GUI);
  98. furi_record_close(RECORD_NOTIFICATION);
  99. free(app);
  100. }
  101. int32_t dtmf_dolphin_app(void *p) {
  102. UNUSED(p);
  103. DTMFDolphinApp* app = app_alloc();
  104. dtmf_dolphin_player_init(&(app->player));
  105. view_dispatcher_run(app->view_dispatcher);
  106. app_free(app);
  107. return 0;
  108. }