metroflip.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "metroflip_i.h"
  2. static bool metroflip_custom_event_callback(void* context, uint32_t event) {
  3. furi_assert(context);
  4. Metroflip* app = context;
  5. return scene_manager_handle_custom_event(app->scene_manager, event);
  6. }
  7. static bool metroflip_back_event_callback(void* context) {
  8. furi_assert(context);
  9. Metroflip* app = context;
  10. return scene_manager_handle_back_event(app->scene_manager);
  11. }
  12. Metroflip* metroflip_alloc() {
  13. Metroflip* app = malloc(sizeof(Metroflip));
  14. app->gui = furi_record_open(RECORD_GUI);
  15. //nfc device
  16. app->nfc = nfc_alloc();
  17. app->nfc_device = nfc_device_alloc();
  18. // notifs
  19. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  20. // View Dispatcher and Scene Manager
  21. app->view_dispatcher = view_dispatcher_alloc();
  22. app->scene_manager = scene_manager_alloc(&metroflip_scene_handlers, app);
  23. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  24. view_dispatcher_set_custom_event_callback(
  25. app->view_dispatcher, metroflip_custom_event_callback);
  26. view_dispatcher_set_navigation_event_callback(
  27. app->view_dispatcher, metroflip_back_event_callback);
  28. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  29. // Custom Widget
  30. app->widget = widget_alloc();
  31. view_dispatcher_add_view(
  32. app->view_dispatcher, MetroflipViewWidget, widget_get_view(app->widget));
  33. // Gui Modules
  34. app->submenu = submenu_alloc();
  35. view_dispatcher_add_view(
  36. app->view_dispatcher, MetroflipViewSubmenu, submenu_get_view(app->submenu));
  37. app->text_input = text_input_alloc();
  38. view_dispatcher_add_view(
  39. app->view_dispatcher, MetroflipViewTextInput, text_input_get_view(app->text_input));
  40. app->byte_input = byte_input_alloc();
  41. view_dispatcher_add_view(
  42. app->view_dispatcher, MetroflipViewByteInput, byte_input_get_view(app->byte_input));
  43. app->popup = popup_alloc();
  44. view_dispatcher_add_view(app->view_dispatcher, MetroflipViewPopup, popup_get_view(app->popup));
  45. app->nfc_device = nfc_device_alloc();
  46. // TextBox
  47. app->text_box = text_box_alloc();
  48. view_dispatcher_add_view(
  49. app->view_dispatcher, MetroflipViewTextBox, text_box_get_view(app->text_box));
  50. app->text_box_store = furi_string_alloc();
  51. return app;
  52. }
  53. void metroflip_free(Metroflip* app) {
  54. furi_assert(app);
  55. //nfc device
  56. nfc_free(app->nfc);
  57. nfc_device_free(app->nfc_device);
  58. //notifs
  59. furi_record_close(RECORD_NOTIFICATION);
  60. app->notifications = NULL;
  61. // Gui modules
  62. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewSubmenu);
  63. submenu_free(app->submenu);
  64. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewTextInput);
  65. text_input_free(app->text_input);
  66. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewByteInput);
  67. byte_input_free(app->byte_input);
  68. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewPopup);
  69. popup_free(app->popup);
  70. // Custom Widget
  71. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewWidget);
  72. widget_free(app->widget);
  73. // TextBox
  74. view_dispatcher_remove_view(app->view_dispatcher, MetroflipViewTextBox);
  75. text_box_free(app->text_box);
  76. furi_string_free(app->text_box_store);
  77. // View Dispatcher and Scene Manager
  78. view_dispatcher_free(app->view_dispatcher);
  79. scene_manager_free(app->scene_manager);
  80. // Records
  81. furi_record_close(RECORD_GUI);
  82. free(app);
  83. }
  84. static const NotificationSequence metroflip_app_sequence_blink_start_blue = {
  85. &message_blink_start_10,
  86. &message_blink_set_color_blue,
  87. &message_do_not_reset,
  88. NULL,
  89. };
  90. static const NotificationSequence metroflip_app_sequence_blink_stop = {
  91. &message_blink_stop,
  92. NULL,
  93. };
  94. void metroflip_app_blink_start(Metroflip* metroflip) {
  95. notification_message(metroflip->notifications, &metroflip_app_sequence_blink_start_blue);
  96. }
  97. void metroflip_app_blink_stop(Metroflip* metroflip) {
  98. notification_message(metroflip->notifications, &metroflip_app_sequence_blink_stop);
  99. }
  100. extern int32_t metroflip(void* p) {
  101. UNUSED(p);
  102. Metroflip* app = metroflip_alloc();
  103. scene_manager_set_scene_state(app->scene_manager, MetroflipSceneStart, MetroflipSceneRavKav);
  104. scene_manager_next_scene(app->scene_manager, MetroflipSceneStart);
  105. view_dispatcher_run(app->view_dispatcher);
  106. metroflip_free(app);
  107. return 0;
  108. }