playlist.c 27 KB

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