nfc_playlist.c 11 KB

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