fbp.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "fbp.h"
  2. enum FBPSubmenuIndex {
  3. FBPSubmenuIndexInternal,
  4. FBPSubmenuIndexGPIOSimpleMotor,
  5. };
  6. uint32_t fbp_start_view(void* context) {
  7. UNUSED(context);
  8. return FBPAppViewSubmenu;
  9. }
  10. uint32_t fbp_exit(void* context) {
  11. UNUSED(context);
  12. return VIEW_NONE;
  13. }
  14. void fbp_submenu_callback(void* context, uint32_t index) {
  15. furi_assert(context);
  16. FBP* app = context;
  17. if(index == FBPSubmenuIndexInternal) {
  18. view_dispatcher_switch_to_view(app->view_dispatcher, FBPAppViewInternal);
  19. } else if(index == FBPSubmenuIndexGPIOSimpleMotor) {
  20. view_dispatcher_switch_to_view(app->view_dispatcher, FBPAppViewGPIOSimpleMotor);
  21. }
  22. }
  23. FBP* fbp_alloc() {
  24. FBP* app = malloc(sizeof(FBP));
  25. app->app_mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  26. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  27. app->bt_connected = false;
  28. app->bt = furi_record_open(RECORD_BT);
  29. app->gui = furi_record_open(RECORD_GUI);
  30. app->view_dispatcher = view_dispatcher_alloc();
  31. view_dispatcher_enable_queue(app->view_dispatcher);
  32. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  33. // set submenu
  34. app->submenu = submenu_alloc();
  35. view_set_previous_callback(submenu_get_view(app->submenu), fbp_exit);
  36. view_dispatcher_add_view(
  37. app->view_dispatcher, FBPAppViewSubmenu, submenu_get_view(app->submenu));
  38. // add Flipper Internal View
  39. app->flipper_vibrator = flipper_vibrator_alloc(app);
  40. submenu_add_item(
  41. app->submenu,
  42. "Flipper Internal Vibrator",
  43. FBPSubmenuIndexInternal,
  44. fbp_submenu_callback,
  45. app);
  46. view_set_previous_callback(flipper_vibrator_get_view(app->flipper_vibrator), fbp_start_view);
  47. view_dispatcher_add_view(
  48. app->view_dispatcher,
  49. FBPAppViewInternal,
  50. flipper_vibrator_get_view(app->flipper_vibrator));
  51. // add GPIO Simple Motor View
  52. app->gpio_simple_motor = gpio_simple_motor_alloc(app);
  53. submenu_add_item(
  54. app->submenu,
  55. "Flipper GPIO Simple Motor",
  56. FBPSubmenuIndexGPIOSimpleMotor,
  57. fbp_submenu_callback,
  58. app);
  59. view_set_previous_callback(gpio_simple_motor_get_view(app->gpio_simple_motor), fbp_start_view);
  60. view_dispatcher_add_view(
  61. app->view_dispatcher,
  62. FBPAppViewGPIOSimpleMotor,
  63. gpio_simple_motor_get_view(app->gpio_simple_motor));
  64. view_dispatcher_switch_to_view(app->view_dispatcher, FBPAppViewSubmenu);
  65. return app;
  66. }
  67. void fbs_free(FBP* app) {
  68. furi_assert(app);
  69. // Free views
  70. view_dispatcher_remove_view(app->view_dispatcher, FBPAppViewSubmenu);
  71. submenu_free(app->submenu);
  72. // free Flipper Internal Vibrator
  73. view_dispatcher_remove_view(app->view_dispatcher, FBPAppViewInternal);
  74. flipper_vibrator_free(app->flipper_vibrator);
  75. // free GPIO Simple Motor
  76. view_dispatcher_remove_view(app->view_dispatcher, FBPAppViewGPIOSimpleMotor);
  77. gpio_simple_motor_free(app->gpio_simple_motor);
  78. // Other deallocations
  79. view_dispatcher_free(app->view_dispatcher);
  80. furi_record_close(RECORD_GUI);
  81. app->gui = NULL;
  82. furi_mutex_free(app->app_mutex);
  83. furi_message_queue_free(app->event_queue);
  84. furi_record_close(RECORD_BT);
  85. app->bt = NULL;
  86. free(app);
  87. }
  88. int32_t fbp_app(void* p) {
  89. UNUSED(p);
  90. FBP* app = fbp_alloc();
  91. view_dispatcher_run(app->view_dispatcher);
  92. furi_hal_bt_serial_set_event_callback(0, NULL, NULL);
  93. fbs_free(app);
  94. return 0;
  95. }