nfc_playlist.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include <furi.h>
  2. #include <storage/storage.h>
  3. #include <toolbox/stream/stream.h>
  4. #include <toolbox/stream/file_stream.h>
  5. #include <nfc_playlist_worker.h>
  6. #include <gui/gui.h>
  7. #include <gui/view_dispatcher.h>
  8. #include <gui/scene_manager.h>
  9. #include <gui/modules/menu.h>
  10. #include <gui/modules/submenu.h>
  11. #include <gui/modules/popup.h>
  12. // Define log tag
  13. #define TAG "NfcPlaylist"
  14. /** ids for all scenes used by the app */
  15. typedef enum {
  16. NfcPlaylistScene_MainMenu,
  17. NfcPlaylistScene_FirstPopup,
  18. NfcPlaylistScene_count
  19. } NfcPlaylistScene;
  20. /** ids for the 2 types of view used by the app */
  21. typedef enum { NfcPlaylistView_Menu, NfcPlaylistView_Popup } NfcPlaylistView;
  22. /** the app context struct */
  23. typedef struct {
  24. SceneManager* scene_manager;
  25. ViewDispatcher* view_dispatcher;
  26. Submenu* menu;
  27. Popup* popup;
  28. int emulate_timeout;
  29. } NfcPlaylist;
  30. /** all custom events */
  31. typedef enum { NfcPlaylistEvent_ShowPopupOne } NfcPlaylistEvent;
  32. /* main menu scene */
  33. /** indices for menu items */
  34. typedef enum { NfcPlaylistMenuSelection_One } NfcPlaylistMenuSelection;
  35. /** main menu callback - sends a custom event to the scene manager based on the menu selection */
  36. void nfc_playlist_menu_callback_main_menu(void* context, uint32_t index) {
  37. FURI_LOG_T(TAG, "nfc_playlist_menu_callback_main_menu");
  38. NfcPlaylist* app = context;
  39. switch(index) {
  40. case NfcPlaylistMenuSelection_One:
  41. scene_manager_handle_custom_event(app->scene_manager, NfcPlaylistEvent_ShowPopupOne);
  42. break;
  43. }
  44. }
  45. /** resets the menu, gives it content, callbacks and selection enums */
  46. void nfc_playlist_scene_on_enter_main_menu(void* context) {
  47. FURI_LOG_T(TAG, "nfc_playlist_scene_on_enter_main_menu");
  48. NfcPlaylist* app = context;
  49. submenu_reset(app->menu);
  50. submenu_set_header(app->menu, "NFC Playlist");
  51. submenu_add_item(
  52. app->menu,
  53. "Start",
  54. NfcPlaylistMenuSelection_One,
  55. nfc_playlist_menu_callback_main_menu,
  56. app);
  57. view_dispatcher_switch_to_view(app->view_dispatcher, NfcPlaylistView_Menu);
  58. }
  59. /** main menu event handler - switches scene based on the event */
  60. bool nfc_playlist_scene_on_event_main_menu(void* context, SceneManagerEvent event) {
  61. FURI_LOG_T(TAG, "nfc_playlist_scene_on_event_main_menu");
  62. NfcPlaylist* app = context;
  63. bool consumed = false;
  64. switch(event.type) {
  65. case SceneManagerEventTypeCustom:
  66. switch(event.event) {
  67. case NfcPlaylistEvent_ShowPopupOne:
  68. scene_manager_next_scene(app->scene_manager, NfcPlaylistScene_FirstPopup);
  69. consumed = true;
  70. break;
  71. }
  72. break;
  73. default: // eg. SceneManagerEventTypeBack, SceneManagerEventTypeTick
  74. consumed = false;
  75. break;
  76. }
  77. return consumed;
  78. }
  79. void nfc_playlist_scene_on_exit_main_menu(void* context) {
  80. FURI_LOG_T(TAG, "nfc_playlist_scene_on_exit_main_menu");
  81. NfcPlaylist* app = context;
  82. submenu_reset(app->menu);
  83. }
  84. /* popup 1 scene */
  85. void nfc_playlist_scene_on_enter_popup_emulating(void* context) {
  86. FURI_LOG_T(TAG, "nfc_playlist_scene_on_enter_popup_emulating");
  87. NfcPlaylist* app = context;
  88. // open/alloc resources
  89. Storage* storage = furi_record_open(RECORD_STORAGE);
  90. Stream* stream = file_stream_alloc(storage);
  91. FuriString* line = furi_string_alloc();
  92. NfcPlaylistWorker* nfc_worker = nfc_playlist_worker_alloc();
  93. // Read file
  94. if(file_stream_open(stream, APP_DATA_PATH("playlist.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
  95. popup_reset(app->popup);
  96. // read the file line by line and print the text
  97. while(stream_read_line(stream, line)) {
  98. char* str = (char*)furi_string_get_cstr(line);
  99. popup_set_context(app->popup, app);
  100. popup_set_header(app->popup, "Emulating", 64, 10, AlignCenter, AlignTop);
  101. popup_set_text(app->popup, str, 64, 30, AlignCenter, AlignTop);
  102. view_dispatcher_switch_to_view(app->view_dispatcher, NfcPlaylistView_Popup);
  103. nfc_playlist_worker_set_nfc_data(nfc_worker, str);
  104. nfc_playlist_worker_start(nfc_worker);
  105. furi_delay_ms(app->emulate_timeout);
  106. nfc_playlist_worker_stop(nfc_worker);
  107. furi_string_reset(line);
  108. }
  109. } else {
  110. FURI_LOG_E(TAG, "Failed to open file");
  111. }
  112. // Free/close resources
  113. furi_string_free(line);
  114. file_stream_close(stream);
  115. stream_free(stream);
  116. nfc_playlist_worker_free(nfc_worker);
  117. // Close storage
  118. furi_record_close(RECORD_STORAGE);
  119. popup_reset(app->popup);
  120. scene_manager_previous_scene(app->scene_manager);
  121. }
  122. bool nfc_playlist_scene_on_event_popup_emulating(void* context, SceneManagerEvent event) {
  123. FURI_LOG_T(TAG, "nfc_playlist_scene_on_event_popup_emulating");
  124. UNUSED(context);
  125. UNUSED(event);
  126. return false;
  127. }
  128. void nfc_playlist_scene_on_exit_popup_emulating(void* context) {
  129. FURI_LOG_T(TAG, "nfc_playlist_scene_on_exit_popup_emulating");
  130. NfcPlaylist* app = context;
  131. popup_reset(app->popup);
  132. }
  133. /** collection of all scene on_enter handlers - in the same order as their enum */
  134. void (*const nfc_playlist_scene_on_enter_handlers[])(void*) = {
  135. nfc_playlist_scene_on_enter_main_menu,
  136. nfc_playlist_scene_on_enter_popup_emulating};
  137. /** collection of all scene on event handlers - in the same order as their enum */
  138. bool (*const nfc_playlist_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
  139. nfc_playlist_scene_on_event_main_menu,
  140. nfc_playlist_scene_on_event_popup_emulating};
  141. /** collection of all scene on exit handlers - in the same order as their enum */
  142. void (*const nfc_playlist_scene_on_exit_handlers[])(void*) = {
  143. nfc_playlist_scene_on_exit_main_menu,
  144. nfc_playlist_scene_on_exit_popup_emulating};
  145. /** collection of all on_enter, on_event, on_exit handlers */
  146. const SceneManagerHandlers nfc_playlist_scene_event_handlers = {
  147. .on_enter_handlers = nfc_playlist_scene_on_enter_handlers,
  148. .on_event_handlers = nfc_playlist_scene_on_event_handlers,
  149. .on_exit_handlers = nfc_playlist_scene_on_exit_handlers,
  150. .scene_num = NfcPlaylistScene_count};
  151. /** custom event handler - passes the event to the scene manager */
  152. bool nfc_playlist_scene_manager_custom_event_callback(void* context, uint32_t custom_event) {
  153. FURI_LOG_T(TAG, "nfc_playlist_scene_manager_custom_event_callback");
  154. furi_assert(context);
  155. NfcPlaylist* app = context;
  156. return scene_manager_handle_custom_event(app->scene_manager, custom_event);
  157. }
  158. /** navigation event handler - passes the event to the scene manager */
  159. bool nfc_playlist_scene_manager_navigation_event_callback(void* context) {
  160. FURI_LOG_T(TAG, "nfc_playlist_scene_manager_navigation_event_callback");
  161. furi_assert(context);
  162. NfcPlaylist* app = context;
  163. return scene_manager_handle_back_event(app->scene_manager);
  164. }
  165. /** initialise the scene manager with all handlers */
  166. void nfc_playlist_scene_manager_init(NfcPlaylist* app) {
  167. FURI_LOG_T(TAG, "nfc_playlist_scene_manager_init");
  168. app->scene_manager = scene_manager_alloc(&nfc_playlist_scene_event_handlers, app);
  169. }
  170. /** initialise the views, and initialise the view dispatcher with all views */
  171. void nfc_playlist_view_dispatcher_init(NfcPlaylist* app) {
  172. FURI_LOG_T(TAG, "nfc_playlist_view_dispatcher_init");
  173. app->view_dispatcher = view_dispatcher_alloc();
  174. view_dispatcher_enable_queue(app->view_dispatcher);
  175. // allocate each view
  176. FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init allocating views");
  177. app->menu = submenu_alloc();
  178. app->popup = popup_alloc();
  179. // assign callback that pass events from views to the scene manager
  180. FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init setting callbacks");
  181. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  182. view_dispatcher_set_custom_event_callback(
  183. app->view_dispatcher, nfc_playlist_scene_manager_custom_event_callback);
  184. view_dispatcher_set_navigation_event_callback(
  185. app->view_dispatcher, nfc_playlist_scene_manager_navigation_event_callback);
  186. // add views to the dispatcher, indexed by their enum value
  187. FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init adding view menu");
  188. view_dispatcher_add_view(app->view_dispatcher, NfcPlaylistView_Menu, submenu_get_view(app->menu));
  189. FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init adding view popup");
  190. view_dispatcher_add_view(app->view_dispatcher, NfcPlaylistView_Popup, popup_get_view(app->popup));
  191. }
  192. /** initialise app data, scene manager, and view dispatcher */
  193. NfcPlaylist* nfc_playlist_init() {
  194. FURI_LOG_T(TAG, "nfc_playlist_init");
  195. NfcPlaylist* app = malloc(sizeof(NfcPlaylist));
  196. nfc_playlist_scene_manager_init(app);
  197. nfc_playlist_view_dispatcher_init(app);
  198. return app;
  199. }
  200. /** free all app data, scene manager, and view dispatcher */
  201. void nfc_playlist_free(NfcPlaylist* app) {
  202. FURI_LOG_T(TAG, "nfc_playlist_free");
  203. scene_manager_free(app->scene_manager);
  204. view_dispatcher_remove_view(app->view_dispatcher, NfcPlaylistView_Menu);
  205. view_dispatcher_remove_view(app->view_dispatcher, NfcPlaylistView_Popup);
  206. view_dispatcher_free(app->view_dispatcher);
  207. submenu_free(app->menu);
  208. popup_free(app->popup);
  209. free(app);
  210. }
  211. /** go to trace log level in the dev environment */
  212. void nfc_playlist_set_log_level() {
  213. #ifdef FURI_DEBUG
  214. furi_log_set_level(FuriLogLevelTrace);
  215. #else
  216. furi_log_set_level(FuriLogLevelInfo);
  217. #endif
  218. }
  219. // Application entry point
  220. int32_t nfc_playlist_main(void* p) {
  221. // Mark argument as unused
  222. UNUSED(p);
  223. nfc_playlist_set_log_level();
  224. // create the app context struct, scene manager, and view dispatcher
  225. FURI_LOG_I(TAG, "Test app starting...");
  226. NfcPlaylist* app = nfc_playlist_init();
  227. // set the scene and launch the main loop
  228. Gui* gui = furi_record_open(RECORD_GUI);
  229. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  230. scene_manager_next_scene(app->scene_manager, NfcPlaylistScene_MainMenu);
  231. FURI_LOG_D(TAG, "Starting dispatcher...");
  232. view_dispatcher_run(app->view_dispatcher);
  233. app->emulate_timeout = 2000;
  234. // free all memory
  235. FURI_LOG_I(TAG, "Test app finishing...");
  236. furi_record_close(RECORD_GUI);
  237. nfc_playlist_free(app);
  238. return 0;
  239. }