tone_gen.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <gui/canvas.h>
  2. #include <gui/modules/menu.h>
  3. #include <gui/modules/submenu.h>
  4. #include <gui/modules/variable_item_list.h>
  5. /* generated by fbt from .png files in images folder */
  6. #include <tone_gen_icons.h>
  7. #include "app_context.h"
  8. #include "tone_gen.h"
  9. #include "scenes/starting_scene.h"
  10. #include "scenes/playback_scene.h"
  11. #include "scenes/settings_scene.h"
  12. /** collection of all scene on_enter handlers - in the same order as their enum */
  13. void (*const scene_on_enter_handlers[])(void*) = {
  14. scene_on_enter_starting_scene,
  15. scene_on_enter_playback_scene,
  16. scene_on_enter_settings_scene,
  17. };
  18. /** collection of all scene on event handlers - in the same order as their enum */
  19. bool (*const scene_on_event_handlers[])(void*, SceneManagerEvent) = {
  20. scene_on_event_starting_scene,
  21. scene_on_event_playback_scene,
  22. scene_on_event_settings_scene,
  23. };
  24. /** collection of all scene on exit handlers - in the same order as their enum */
  25. void (*const scene_on_exit_handlers[])(void*) = {
  26. scene_on_exit_starting_scene,
  27. scene_on_exit_playback_scene,
  28. scene_on_exit_settings_scene,
  29. };
  30. const SceneManagerHandlers scene_event_handlers = {
  31. .on_enter_handlers = scene_on_enter_handlers,
  32. .on_event_handlers = scene_on_event_handlers,
  33. .on_exit_handlers = scene_on_exit_handlers,
  34. .scene_num = ToneGenAppScene_count};
  35. int setupViews(struct AppContext_t** appContext) {
  36. // Create views
  37. FURI_LOG_I(TAG, "Creating views");
  38. struct View_t* sharedMenuView = malloc(sizeof(struct View_t));
  39. sharedMenuView->viewData = menu_alloc();
  40. sharedMenuView->viewId = ToneGenAppView_SharedMenu;
  41. sharedMenuView->type = MENU;
  42. struct View_t* submenuView = malloc(sizeof(struct View_t));
  43. submenuView->viewData = submenu_alloc();
  44. submenuView->viewId = ToneGenAppView_Submenu;
  45. submenuView->type = SUBMENU;
  46. struct View_t* playbackView = malloc(sizeof(struct View_t));
  47. playbackView->viewData = view_alloc();
  48. playbackView->viewId = ToneGenAppView_PlaybackView;
  49. playbackView->type = VIEW;
  50. FURI_LOG_I(TAG, "creating var-item-list view");
  51. struct View_t* variableItemListView = malloc(sizeof(struct View_t));
  52. FURI_LOG_I(TAG, "allocating view data");
  53. variableItemListView->viewData = variable_item_list_alloc();
  54. FURI_LOG_I(TAG, "setting view id");
  55. variableItemListView->viewId = ToneGenAppView_VariableItemList;
  56. FURI_LOG_I(TAG, "setting view type");
  57. variableItemListView->type = VARIABLE_ITEM_LIST;
  58. FURI_LOG_I(TAG, "moving on");
  59. // Add views to the app context to be managed there
  60. FURI_LOG_I(TAG, "Adding views to app context");
  61. AppContextStatus result = addViewToAppContext(appContext, sharedMenuView);
  62. if(result != APP_CONTEXT_OK) {
  63. FURI_LOG_E(TAG, "There was a problem adding the view %d!", sharedMenuView->viewId);
  64. return -1;
  65. }
  66. result = addViewToAppContext(appContext, submenuView);
  67. if(result != APP_CONTEXT_OK) {
  68. FURI_LOG_E(TAG, "There was a problem adding the view %d!", submenuView->viewId);
  69. return -1;
  70. }
  71. result = addViewToAppContext(appContext, playbackView);
  72. if(result != APP_CONTEXT_OK) {
  73. FURI_LOG_E(TAG, "There was a problem adding the view %d!", playbackView->viewId);
  74. return -1;
  75. }
  76. FURI_LOG_I(TAG, "Adding variable item list view");
  77. result = addViewToAppContext(appContext, variableItemListView);
  78. if(result != APP_CONTEXT_OK) {
  79. FURI_LOG_E(TAG, "There was a problem adding the view %d!", variableItemListView->viewId);
  80. return -1;
  81. }
  82. // On the playback view, ensure we only allocate for the model once
  83. FURI_LOG_I(TAG, "allocating view model for playback");
  84. view_allocate_model(playbackView->viewData, ViewModelTypeLockFree, sizeof(struct ToneData_t));
  85. return 0;
  86. }
  87. int32_t tone_gen_app(void* p) {
  88. UNUSED(p);
  89. FURI_LOG_I(TAG, "Tone gen app starting...");
  90. struct AppContext_t* appContext;
  91. AppContextStatus result =
  92. initializeAppContext(&appContext, ToneGenAppView_count, &scene_event_handlers);
  93. if(result == APP_CONTEXT_OK) {
  94. appContext->additionalData = malloc(sizeof(struct ToneData_t));
  95. ((struct ToneData_t*)appContext->additionalData)->animationOffset = 0;
  96. ((struct ToneData_t*)appContext->additionalData)->frequency = 440;
  97. ((struct ToneData_t*)appContext->additionalData)->waveType = SINE;
  98. ((struct ToneData_t*)appContext->additionalData)->volume = 1.0f;
  99. result = setupViews(&appContext);
  100. if(result == 0) {
  101. // set the scene and launch the main loop
  102. FURI_LOG_D(TAG, "Setting the scene");
  103. Gui* gui = furi_record_open(RECORD_GUI);
  104. view_dispatcher_attach_to_gui(
  105. appContext->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  106. scene_manager_next_scene(appContext->scene_manager, ToneGenAppScene_Starting);
  107. FURI_LOG_D(TAG, "Starting the view dispatcher");
  108. view_dispatcher_run(appContext->view_dispatcher);
  109. }
  110. // free all memory
  111. FURI_LOG_D(TAG, "Ending the app");
  112. furi_record_close(RECORD_GUI);
  113. freeAppContext(&appContext);
  114. return 0;
  115. }
  116. return -1;
  117. }