playlist.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. #include <furi.h>
  2. #include <gui/gui.h>
  3. #include <input/input.h>
  4. #include <dialogs/dialogs.h>
  5. #include <storage/storage.h>
  6. #include <lib/toolbox/path.h>
  7. #include <subghz_playlist_icons.h>
  8. #include <lib/subghz/protocols/protocol_items.h>
  9. #include <flipper_format/flipper_format_i.h>
  10. #include "helpers/radio_device_loader.h"
  11. #include "flipper_format_stream.h"
  12. #include "flipper_format_stream_i.h"
  13. #include <lib/subghz/transmitter.h>
  14. #include <lib/subghz/protocols/raw.h>
  15. #include "playlist_file.h"
  16. #include "canvas_helper.h"
  17. #define PLAYLIST_FOLDER "/ext/subplaylist"
  18. #define PLAYLIST_EXT ".txt"
  19. #define TAG "Playlist"
  20. #define STATE_NONE 0
  21. #define STATE_OVERVIEW 1
  22. #define STATE_SENDING 2
  23. #define WIDTH 128
  24. #define HEIGHT 64
  25. typedef struct {
  26. int current_count; // number of processed files
  27. int total_count; // number of items in the playlist
  28. int playlist_repetitions; // number of times to repeat the whole playlist
  29. int current_playlist_repetition; // current playlist repetition
  30. // last 3 files
  31. FuriString* prev_0_path; // current file
  32. FuriString* prev_1_path; // previous file
  33. FuriString* prev_2_path; // previous previous file
  34. FuriString* prev_3_path; // you get the idea
  35. int state; // current state
  36. ViewPort* view_port;
  37. } DisplayMeta;
  38. typedef struct {
  39. FuriThread* thread;
  40. Storage* storage;
  41. FlipperFormat* format;
  42. DisplayMeta* meta;
  43. FuriString* file_path; // path to the playlist file
  44. const SubGhzDevice* radio_device;
  45. bool ctl_request_exit; // can be set to true if the worker should exit
  46. bool ctl_pause; // can be set to true if the worker should pause
  47. bool ctl_request_skip; // can be set to true if the worker should skip the current file
  48. bool ctl_request_prev; // can be set to true if the worker should go to the previous file
  49. bool is_running; // indicates if the worker is running
  50. } PlaylistWorker;
  51. typedef struct {
  52. FuriMutex* mutex;
  53. FuriMessageQueue* input_queue;
  54. ViewPort* view_port;
  55. Gui* gui;
  56. DisplayMeta* meta;
  57. PlaylistWorker* worker;
  58. FuriString* file_path; // Path to the playlist file
  59. } Playlist;
  60. ////////////////////////////////////////////////////////////////////////////////
  61. void meta_set_state(DisplayMeta* meta, int state) {
  62. meta->state = state;
  63. view_port_update(meta->view_port);
  64. }
  65. static FuriHalSubGhzPreset str_to_preset(FuriString* preset) {
  66. if(furi_string_cmp_str(preset, "FuriHalSubGhzPresetOok270Async") == 0) {
  67. return FuriHalSubGhzPresetOok270Async;
  68. }
  69. if(furi_string_cmp_str(preset, "FuriHalSubGhzPresetOok650Async") == 0) {
  70. return FuriHalSubGhzPresetOok650Async;
  71. }
  72. if(furi_string_cmp_str(preset, "FuriHalSubGhzPreset2FSKDev238Async") == 0) {
  73. return FuriHalSubGhzPreset2FSKDev238Async;
  74. }
  75. if(furi_string_cmp_str(preset, "FuriHalSubGhzPreset2FSKDev476Async") == 0) {
  76. return FuriHalSubGhzPreset2FSKDev476Async;
  77. }
  78. if(furi_string_cmp_str(preset, "FuriHalSubGhzPresetMSK99_97KbAsync") == 0) {
  79. return FuriHalSubGhzPresetMSK99_97KbAsync;
  80. }
  81. if(furi_string_cmp_str(preset, "FuriHalSubGhzPresetMSK99_97KbAsync") == 0) {
  82. return FuriHalSubGhzPresetMSK99_97KbAsync;
  83. }
  84. return FuriHalSubGhzPresetCustom;
  85. }
  86. // -4: missing protocol
  87. // -3: missing preset
  88. // -2: transmit error
  89. // -1: error
  90. // 0: ok
  91. // 1: resend
  92. // 2: exited
  93. static int playlist_worker_process(
  94. PlaylistWorker* worker,
  95. FlipperFormat* fff_file,
  96. FlipperFormat* fff_data,
  97. const char* path,
  98. FuriString* preset,
  99. FuriString* protocol) {
  100. // actual sending of .sub file
  101. if(!flipper_format_file_open_existing(fff_file, path)) {
  102. FURI_LOG_E(TAG, " (TX) Failed to open %s", path);
  103. return -1;
  104. }
  105. // read frequency or default to 433.92MHz
  106. uint32_t frequency = 0;
  107. if(!flipper_format_read_uint32(fff_file, "Frequency", &frequency, 1)) {
  108. FURI_LOG_W(TAG, " (TX) Missing Frequency, defaulting to 433.92MHz");
  109. frequency = 433920000;
  110. }
  111. if(!subghz_devices_is_frequency_valid(worker->radio_device, frequency)) {
  112. FURI_LOG_E(
  113. TAG, " (TX) The SubGhz device used does not support the frequency %lu", frequency);
  114. return -2;
  115. }
  116. // check if preset is present
  117. if(!flipper_format_read_string(fff_file, "Preset", preset)) {
  118. FURI_LOG_E(TAG, " (TX) Missing Preset");
  119. return -3;
  120. }
  121. // check if protocol is present
  122. if(!flipper_format_read_string(fff_file, "Protocol", protocol)) {
  123. FURI_LOG_E(TAG, " (TX) Missing Protocol");
  124. return -4;
  125. }
  126. if(!furi_string_cmp_str(protocol, "RAW")) {
  127. subghz_protocol_raw_gen_fff_data(
  128. fff_data, path, subghz_devices_get_name(worker->radio_device));
  129. } else {
  130. stream_copy_full(
  131. flipper_format_get_raw_stream(fff_file), flipper_format_get_raw_stream(fff_data));
  132. }
  133. flipper_format_file_close(fff_file);
  134. flipper_format_free(fff_file);
  135. // (try to) send file
  136. SubGhzEnvironment* environment = subghz_environment_alloc();
  137. subghz_environment_set_protocol_registry(environment, (void*)&subghz_protocol_registry);
  138. SubGhzTransmitter* transmitter =
  139. subghz_transmitter_alloc_init(environment, furi_string_get_cstr(protocol));
  140. subghz_transmitter_deserialize(transmitter, fff_data);
  141. subghz_devices_load_preset(worker->radio_device, str_to_preset(preset), NULL);
  142. // there is no check for a custom preset
  143. frequency = subghz_devices_set_frequency(worker->radio_device, frequency);
  144. // Set device to TX and check frequency is alowed to TX
  145. if(!subghz_devices_set_tx(worker->radio_device)) {
  146. FURI_LOG_E(
  147. TAG,
  148. " (TX) The SubGhz device used does not support the frequency for transmitеing, %lu",
  149. frequency);
  150. return -5;
  151. }
  152. FURI_LOG_D(TAG, " (TX) Start sending ...");
  153. int status = 0;
  154. subghz_devices_start_async_tx(worker->radio_device, subghz_transmitter_yield, transmitter);
  155. while(!subghz_devices_is_async_complete_tx(worker->radio_device)) {
  156. if(worker->ctl_request_exit) {
  157. FURI_LOG_D(TAG, " (TX) Requested to exit. Cancelling sending...");
  158. status = 2;
  159. break;
  160. }
  161. if(worker->ctl_pause) {
  162. FURI_LOG_D(TAG, " (TX) Requested to pause. Cancelling and resending...");
  163. status = 1;
  164. break;
  165. }
  166. if(worker->ctl_request_skip) {
  167. worker->ctl_request_skip = false;
  168. FURI_LOG_D(TAG, " (TX) Requested to skip. Cancelling and resending...");
  169. status = 0;
  170. break;
  171. }
  172. if(worker->ctl_request_prev) {
  173. worker->ctl_request_prev = false;
  174. FURI_LOG_D(TAG, " (TX) Requested to prev. Cancelling and resending...");
  175. status = 3;
  176. break;
  177. }
  178. furi_delay_ms(50);
  179. }
  180. FURI_LOG_D(TAG, " (TX) Done sending.");
  181. subghz_devices_stop_async_tx(worker->radio_device);
  182. subghz_devices_idle(worker->radio_device);
  183. subghz_transmitter_free(transmitter);
  184. subghz_environment_free(environment);
  185. return status;
  186. }
  187. // true - the worker can continue
  188. // false - the worker should exit
  189. static bool playlist_worker_wait_pause(PlaylistWorker* worker) {
  190. // wait if paused
  191. while(worker->ctl_pause && !worker->ctl_request_exit) {
  192. furi_delay_ms(50);
  193. }
  194. // exit loop if requested to stop
  195. if(worker->ctl_request_exit) {
  196. FURI_LOG_D(TAG, "Requested to exit. Exiting loop...");
  197. return false;
  198. }
  199. return true;
  200. }
  201. void updatePlayListView(PlaylistWorker* worker, const char* str) {
  202. furi_string_reset(worker->meta->prev_3_path);
  203. furi_string_set(worker->meta->prev_3_path, furi_string_get_cstr(worker->meta->prev_2_path));
  204. furi_string_reset(worker->meta->prev_2_path);
  205. furi_string_set(worker->meta->prev_2_path, furi_string_get_cstr(worker->meta->prev_1_path));
  206. furi_string_reset(worker->meta->prev_1_path);
  207. furi_string_set(worker->meta->prev_1_path, furi_string_get_cstr(worker->meta->prev_0_path));
  208. furi_string_reset(worker->meta->prev_0_path);
  209. furi_string_set(worker->meta->prev_0_path, str);
  210. view_port_update(worker->meta->view_port);
  211. }
  212. static bool playlist_worker_play_playlist_once(
  213. PlaylistWorker* worker,
  214. Storage* storage,
  215. FlipperFormat* fff_head,
  216. FlipperFormat* fff_data,
  217. FuriString* data,
  218. FuriString* preset,
  219. FuriString* protocol) {
  220. //
  221. if(!flipper_format_rewind(fff_head)) {
  222. FURI_LOG_E(TAG, "Failed to rewind file");
  223. return false;
  224. }
  225. while(flipper_format_read_string(fff_head, "sub", data)) {
  226. if(!playlist_worker_wait_pause(worker)) {
  227. break;
  228. }
  229. // update state to sending
  230. meta_set_state(worker->meta, STATE_SENDING);
  231. ++worker->meta->current_count;
  232. const char* str = furi_string_get_cstr(data);
  233. // it's not fancy, but it works for now :)
  234. updatePlayListView(worker, str);
  235. for(int i = 0; i < 1; i++) {
  236. if(!playlist_worker_wait_pause(worker)) {
  237. break;
  238. }
  239. view_port_update(worker->meta->view_port);
  240. FURI_LOG_D(TAG, "(worker) Sending %s", str);
  241. FlipperFormat* fff_file = flipper_format_file_alloc(storage);
  242. int status =
  243. playlist_worker_process(worker, fff_file, fff_data, str, preset, protocol);
  244. // if there was an error, fff_file is not already freed
  245. if(status < 0) {
  246. flipper_format_file_close(fff_file);
  247. flipper_format_free(fff_file);
  248. }
  249. // re-send file is paused mid-send
  250. if(status == 1) {
  251. i -= 1;
  252. // errored, skip to next file
  253. } else if(status < 0) {
  254. break;
  255. // exited, exit loop
  256. } else if(status == 2) {
  257. return false;
  258. } else if(status == 3) {
  259. //aqui rebobinamos y avanzamos de nuevo el fichero n-1 veces
  260. //decrementamos el contador de ficheros enviados
  261. worker->meta->current_count--;
  262. if(worker->meta->current_count > 0) {
  263. worker->meta->current_count--;
  264. }
  265. //rebobinamos el fichero
  266. if(!flipper_format_rewind(fff_head)) {
  267. FURI_LOG_E(TAG, "Failed to rewind file");
  268. return false;
  269. }
  270. //avanzamos el fichero n-1 veces
  271. for(int j = 0; j < worker->meta->current_count; j++) {
  272. flipper_format_read_string(fff_head, "sub", data);
  273. }
  274. break;
  275. }
  276. }
  277. } // end of loop
  278. return true;
  279. }
  280. static int32_t playlist_worker_thread(void* ctx) {
  281. Storage* storage = furi_record_open(RECORD_STORAGE);
  282. FlipperFormat* fff_head = flipper_format_file_alloc(storage);
  283. PlaylistWorker* worker = ctx;
  284. if(!flipper_format_file_open_existing(fff_head, furi_string_get_cstr(worker->file_path))) {
  285. FURI_LOG_E(TAG, "Failed to open %s", furi_string_get_cstr(worker->file_path));
  286. worker->is_running = false;
  287. furi_record_close(RECORD_STORAGE);
  288. flipper_format_free(fff_head);
  289. return 0;
  290. }
  291. playlist_worker_wait_pause(worker);
  292. FlipperFormat* fff_data = flipper_format_string_alloc();
  293. FuriString* data;
  294. FuriString* preset;
  295. FuriString* protocol;
  296. data = furi_string_alloc();
  297. preset = furi_string_alloc();
  298. protocol = furi_string_alloc();
  299. for(int i = 0; i < MAX(1, worker->meta->playlist_repetitions); i++) {
  300. // infinite repetitions if playlist_repetitions is 0
  301. if(worker->meta->playlist_repetitions <= 0) {
  302. --i;
  303. }
  304. ++worker->meta->current_playlist_repetition;
  305. // send playlist
  306. worker->meta->current_count = 0;
  307. if(worker->ctl_request_exit) {
  308. break;
  309. }
  310. FURI_LOG_D(
  311. TAG,
  312. "Sending playlist (i %d rep %d b %d)",
  313. i,
  314. worker->meta->current_playlist_repetition,
  315. worker->meta->playlist_repetitions);
  316. if(!playlist_worker_play_playlist_once(
  317. worker, storage, fff_head, fff_data, data, preset, protocol)) {
  318. break;
  319. }
  320. }
  321. furi_record_close(RECORD_STORAGE);
  322. flipper_format_free(fff_head);
  323. furi_string_free(data);
  324. furi_string_free(preset);
  325. furi_string_free(protocol);
  326. flipper_format_free(fff_data);
  327. FURI_LOG_D(TAG, "Done reading. Read %d data lines.", worker->meta->current_count);
  328. worker->is_running = false;
  329. // update state to overview
  330. meta_set_state(worker->meta, STATE_OVERVIEW);
  331. return 0;
  332. }
  333. ////////////////////////////////////////////////////////////////////////////////
  334. void playlist_meta_reset(DisplayMeta* instance) {
  335. instance->current_count = 0;
  336. instance->current_playlist_repetition = 0;
  337. furi_string_reset(instance->prev_0_path);
  338. furi_string_reset(instance->prev_1_path);
  339. furi_string_reset(instance->prev_2_path);
  340. furi_string_reset(instance->prev_3_path);
  341. }
  342. DisplayMeta* playlist_meta_alloc() {
  343. DisplayMeta* instance = malloc(sizeof(DisplayMeta));
  344. instance->prev_0_path = furi_string_alloc();
  345. instance->prev_1_path = furi_string_alloc();
  346. instance->prev_2_path = furi_string_alloc();
  347. instance->prev_3_path = furi_string_alloc();
  348. playlist_meta_reset(instance);
  349. instance->state = STATE_NONE;
  350. instance->playlist_repetitions = 1;
  351. return instance;
  352. }
  353. void playlist_meta_free(DisplayMeta* instance) {
  354. furi_string_free(instance->prev_0_path);
  355. furi_string_free(instance->prev_1_path);
  356. furi_string_free(instance->prev_2_path);
  357. furi_string_free(instance->prev_3_path);
  358. free(instance);
  359. }
  360. ////////////////////////////////////////////////////////////////////////////////
  361. PlaylistWorker* playlist_worker_alloc(DisplayMeta* meta) {
  362. PlaylistWorker* instance = malloc(sizeof(PlaylistWorker));
  363. instance->thread = furi_thread_alloc();
  364. furi_thread_set_name(instance->thread, "PlaylistWorker");
  365. furi_thread_set_stack_size(instance->thread, 2048);
  366. furi_thread_set_context(instance->thread, instance);
  367. furi_thread_set_callback(instance->thread, playlist_worker_thread);
  368. instance->meta = meta;
  369. instance->ctl_pause = true; // require the user to manually start the worker
  370. instance->file_path = furi_string_alloc();
  371. subghz_devices_init();
  372. instance->radio_device =
  373. radio_device_loader_set(instance->radio_device, SubGhzRadioDeviceTypeExternalCC1101);
  374. subghz_devices_reset(instance->radio_device);
  375. subghz_devices_idle(instance->radio_device);
  376. return instance;
  377. }
  378. void playlist_worker_free(PlaylistWorker* instance) {
  379. furi_assert(instance);
  380. furi_thread_free(instance->thread);
  381. furi_string_free(instance->file_path);
  382. subghz_devices_sleep(instance->radio_device);
  383. radio_device_loader_end(instance->radio_device);
  384. subghz_devices_deinit();
  385. free(instance);
  386. }
  387. void playlist_worker_stop(PlaylistWorker* worker) {
  388. furi_assert(worker);
  389. furi_assert(worker->is_running);
  390. worker->ctl_request_exit = true;
  391. furi_thread_join(worker->thread);
  392. }
  393. bool playlist_worker_running(PlaylistWorker* worker) {
  394. furi_assert(worker);
  395. return worker->is_running;
  396. }
  397. void playlist_worker_start(PlaylistWorker* instance, const char* file_path) {
  398. furi_assert(instance);
  399. furi_assert(!instance->is_running);
  400. furi_string_set(instance->file_path, file_path);
  401. instance->is_running = true;
  402. // reset meta (current/total)
  403. playlist_meta_reset(instance->meta);
  404. FURI_LOG_D(TAG, "Starting thread...");
  405. furi_thread_start(instance->thread);
  406. }
  407. ////////////////////////////////////////////////////////////////////////////////
  408. static void render_callback(Canvas* canvas, void* ctx) {
  409. Playlist* app = ctx;
  410. furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
  411. canvas_clear(canvas);
  412. canvas_set_color(canvas, ColorBlack);
  413. canvas_set_font(canvas, FontSecondary);
  414. FuriString* temp_str;
  415. temp_str = furi_string_alloc();
  416. switch(app->meta->state) {
  417. case STATE_NONE:
  418. canvas_set_font(canvas, FontPrimary);
  419. canvas_draw_str_aligned(
  420. canvas, WIDTH / 2, HEIGHT / 2, AlignCenter, AlignCenter, "No playlist loaded");
  421. break;
  422. case STATE_OVERVIEW:
  423. // draw file name
  424. {
  425. path_extract_filename(app->file_path, temp_str, true);
  426. canvas_set_font(canvas, FontPrimary);
  427. draw_centered_boxed_str(canvas, 1, 1, 15, 6, furi_string_get_cstr(temp_str));
  428. }
  429. canvas_set_font(canvas, FontSecondary);
  430. // draw loaded count
  431. {
  432. furi_string_printf(temp_str, "%d Items in playlist", app->meta->total_count);
  433. canvas_draw_str_aligned(
  434. canvas, 1, 19, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  435. if(app->meta->playlist_repetitions <= 0) {
  436. furi_string_set(temp_str, "Repeat: inf");
  437. } else if(app->meta->playlist_repetitions == 1) {
  438. furi_string_set(temp_str, "Repeat: no");
  439. } else {
  440. furi_string_printf(temp_str, "Repeat: %dx", app->meta->playlist_repetitions);
  441. }
  442. canvas_draw_str_aligned(
  443. canvas, 1, 29, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  444. }
  445. // draw buttons
  446. draw_corner_aligned(canvas, 40, 15, AlignCenter, AlignBottom);
  447. canvas_set_color(canvas, ColorWhite);
  448. canvas_draw_str_aligned(canvas, WIDTH / 2 - 7, HEIGHT - 11, AlignLeft, AlignTop, "Start");
  449. canvas_draw_disc(canvas, WIDTH / 2 - 14, HEIGHT - 8, 3);
  450. //
  451. canvas_set_color(canvas, ColorBlack);
  452. draw_corner_aligned(canvas, 20, 15, AlignLeft, AlignBottom);
  453. canvas_set_color(canvas, ColorWhite);
  454. canvas_draw_str_aligned(canvas, 4, HEIGHT - 11, AlignLeft, AlignTop, "R-");
  455. //
  456. canvas_set_color(canvas, ColorBlack);
  457. draw_corner_aligned(canvas, 20, 15, AlignRight, AlignBottom);
  458. canvas_set_color(canvas, ColorWhite);
  459. canvas_draw_str_aligned(canvas, WIDTH - 4, HEIGHT - 11, AlignRight, AlignTop, "R+");
  460. canvas_set_color(canvas, ColorBlack);
  461. break;
  462. case STATE_SENDING:
  463. canvas_set_color(canvas, ColorBlack);
  464. if(app->worker->ctl_pause) {
  465. canvas_draw_icon(canvas, 2, HEIGHT - 8, &I_ButtonRight_4x7);
  466. } else {
  467. canvas_draw_box(canvas, 2, HEIGHT - 8, 2, 7);
  468. canvas_draw_box(canvas, 5, HEIGHT - 8, 2, 7);
  469. }
  470. // draw progress text
  471. {
  472. canvas_set_font(canvas, FontSecondary);
  473. furi_string_printf(
  474. temp_str, "[%d/%d]", app->meta->current_count, app->meta->total_count);
  475. canvas_draw_str_aligned(
  476. canvas, 11, HEIGHT - 8, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  477. int h = canvas_string_width(canvas, furi_string_get_cstr(temp_str));
  478. int xs = 11 + h + 2;
  479. int w = WIDTH - xs - 1;
  480. canvas_draw_box(canvas, xs, HEIGHT - 5, w, 1);
  481. float progress = (float)app->meta->current_count / (float)app->meta->total_count;
  482. int wp = (int)(progress * w);
  483. canvas_draw_box(canvas, xs + wp - 1, HEIGHT - 7, 2, 5);
  484. }
  485. {
  486. if(app->meta->playlist_repetitions <= 0) {
  487. furi_string_printf(temp_str, "[%d/Inf]", app->meta->current_playlist_repetition);
  488. } else {
  489. furi_string_printf(
  490. temp_str,
  491. "[%d/%d]",
  492. app->meta->current_playlist_repetition,
  493. app->meta->playlist_repetitions);
  494. }
  495. canvas_set_color(canvas, ColorBlack);
  496. int w = canvas_string_width(canvas, furi_string_get_cstr(temp_str));
  497. draw_corner_aligned(canvas, w + 6, 13, AlignRight, AlignTop);
  498. canvas_set_color(canvas, ColorWhite);
  499. canvas_draw_str_aligned(
  500. canvas, WIDTH - 3, 3, AlignRight, AlignTop, furi_string_get_cstr(temp_str));
  501. }
  502. // draw last and current file
  503. {
  504. canvas_set_color(canvas, ColorBlack);
  505. canvas_set_font(canvas, FontSecondary);
  506. // current
  507. if(!furi_string_empty(app->meta->prev_0_path)) {
  508. path_extract_filename(app->meta->prev_0_path, temp_str, true);
  509. int w = canvas_string_width(canvas, furi_string_get_cstr(temp_str));
  510. canvas_set_color(canvas, ColorBlack);
  511. canvas_draw_rbox(canvas, 1, 1, w + 4, 12, 2);
  512. canvas_set_color(canvas, ColorWhite);
  513. canvas_draw_str_aligned(
  514. canvas, 3, 3, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  515. }
  516. // last 3
  517. canvas_set_color(canvas, ColorBlack);
  518. if(!furi_string_empty(app->meta->prev_1_path)) {
  519. path_extract_filename(app->meta->prev_1_path, temp_str, true);
  520. canvas_draw_str_aligned(
  521. canvas, 3, 15, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  522. }
  523. if(!furi_string_empty(app->meta->prev_2_path)) {
  524. path_extract_filename(app->meta->prev_2_path, temp_str, true);
  525. canvas_draw_str_aligned(
  526. canvas, 3, 26, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  527. }
  528. if(!furi_string_empty(app->meta->prev_3_path)) {
  529. path_extract_filename(app->meta->prev_3_path, temp_str, true);
  530. canvas_draw_str_aligned(
  531. canvas, 3, 37, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  532. }
  533. }
  534. break;
  535. default:
  536. break;
  537. }
  538. furi_string_free(temp_str);
  539. furi_mutex_release(app->mutex);
  540. }
  541. static void input_callback(InputEvent* event, void* ctx) {
  542. Playlist* app = ctx;
  543. furi_message_queue_put(app->input_queue, event, 0);
  544. }
  545. ////////////////////////////////////////////////////////////////////////////////
  546. Playlist* playlist_alloc(DisplayMeta* meta) {
  547. Playlist* app = malloc(sizeof(Playlist));
  548. app->file_path = furi_string_alloc();
  549. furi_string_set(app->file_path, PLAYLIST_FOLDER);
  550. app->meta = meta;
  551. app->worker = NULL;
  552. app->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  553. app->input_queue = furi_message_queue_alloc(32, sizeof(InputEvent));
  554. // view port
  555. app->view_port = view_port_alloc();
  556. view_port_draw_callback_set(app->view_port, render_callback, app);
  557. view_port_input_callback_set(app->view_port, input_callback, app);
  558. // gui
  559. app->gui = furi_record_open(RECORD_GUI);
  560. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  561. return app;
  562. }
  563. void playlist_start_worker(Playlist* app, DisplayMeta* meta) {
  564. app->worker = playlist_worker_alloc(meta);
  565. // count playlist items
  566. Storage* storage = furi_record_open(RECORD_STORAGE);
  567. app->meta->total_count =
  568. playlist_count_playlist_items(storage, furi_string_get_cstr(app->file_path));
  569. furi_record_close(RECORD_STORAGE);
  570. // start thread
  571. playlist_worker_start(app->worker, furi_string_get_cstr(app->file_path));
  572. }
  573. void playlist_free(Playlist* app) {
  574. furi_string_free(app->file_path);
  575. gui_remove_view_port(app->gui, app->view_port);
  576. furi_record_close(RECORD_GUI);
  577. view_port_free(app->view_port);
  578. furi_message_queue_free(app->input_queue);
  579. furi_mutex_free(app->mutex);
  580. playlist_meta_free(app->meta);
  581. free(app);
  582. }
  583. int32_t playlist_app(void* p) {
  584. UNUSED(p);
  585. // create playlist folder
  586. {
  587. Storage* storage = furi_record_open(RECORD_STORAGE);
  588. if(!storage_simply_mkdir(storage, PLAYLIST_FOLDER)) {
  589. FURI_LOG_E(TAG, "Could not create folder %s", PLAYLIST_FOLDER);
  590. }
  591. furi_record_close(RECORD_STORAGE);
  592. }
  593. // create app
  594. DisplayMeta* meta = playlist_meta_alloc();
  595. Playlist* app = playlist_alloc(meta);
  596. meta->view_port = app->view_port;
  597. furi_hal_power_suppress_charge_enter();
  598. // select playlist file
  599. {
  600. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  601. DialogsFileBrowserOptions browser_options;
  602. dialog_file_browser_set_basic_options(&browser_options, PLAYLIST_EXT, &I_sub1_10px);
  603. browser_options.hide_ext = false;
  604. const bool res =
  605. dialog_file_browser_show(dialogs, app->file_path, app->file_path, &browser_options);
  606. furi_record_close(RECORD_DIALOGS);
  607. // check if a file was selected
  608. if(!res) {
  609. FURI_LOG_E(TAG, "No file selected");
  610. goto exit_cleanup;
  611. }
  612. }
  613. ////////////////////////////////////////////////////////////////////////////////
  614. playlist_start_worker(app, meta);
  615. meta_set_state(app->meta, STATE_OVERVIEW);
  616. bool exit_loop = false;
  617. InputEvent input;
  618. while(1) { // close application if no file was selected
  619. furi_check(
  620. furi_message_queue_get(app->input_queue, &input, FuriWaitForever) == FuriStatusOk);
  621. switch(input.key) {
  622. case InputKeyLeft:
  623. if(app->meta->state == STATE_OVERVIEW) {
  624. if(input.type == InputTypeShort && app->meta->playlist_repetitions > 0) {
  625. --app->meta->playlist_repetitions;
  626. }
  627. } else if(app->meta->state == STATE_SENDING) {
  628. if(input.type == InputTypeShort) {
  629. app->worker->ctl_request_prev = true;
  630. }
  631. }
  632. break;
  633. case InputKeyRight:
  634. if(app->meta->state == STATE_OVERVIEW) {
  635. if(input.type == InputTypeShort) {
  636. ++app->meta->playlist_repetitions;
  637. }
  638. } else if(app->meta->state == STATE_SENDING) {
  639. if(input.type == InputTypeShort) {
  640. app->worker->ctl_request_skip = true;
  641. }
  642. }
  643. break;
  644. case InputKeyOk:
  645. if(input.type == InputTypeShort) {
  646. // toggle pause state
  647. if(!app->worker->is_running) {
  648. app->worker->ctl_pause = false;
  649. app->worker->ctl_request_exit = false;
  650. playlist_worker_start(app->worker, furi_string_get_cstr(app->file_path));
  651. } else {
  652. app->worker->ctl_pause = !app->worker->ctl_pause;
  653. }
  654. }
  655. break;
  656. case InputKeyBack:
  657. FURI_LOG_D(TAG, "Pressed Back button. Application will exit");
  658. exit_loop = true;
  659. break;
  660. default:
  661. break;
  662. }
  663. furi_mutex_release(app->mutex);
  664. // exit application
  665. if(exit_loop == true) {
  666. break;
  667. }
  668. view_port_update(app->view_port);
  669. }
  670. exit_cleanup:
  671. furi_hal_power_suppress_charge_exit();
  672. if(app->worker != NULL) {
  673. if(playlist_worker_running(app->worker)) {
  674. FURI_LOG_D(TAG, "Thread is still running. Requesting thread to finish ...");
  675. playlist_worker_stop(app->worker);
  676. }
  677. FURI_LOG_D(TAG, "Freeing Worker ...");
  678. playlist_worker_free(app->worker);
  679. }
  680. FURI_LOG_D(TAG, "Freeing Playlist ...");
  681. playlist_free(app);
  682. return 0;
  683. }