playlist.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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_EXT ".txt"
  18. #define TAG "Playlist"
  19. #define STATE_NONE 0
  20. #define STATE_OVERVIEW 1
  21. #define STATE_SENDING 2
  22. #define WIDTH 128
  23. #define HEIGHT 64
  24. typedef struct {
  25. int current_count; // number of processed files
  26. int total_count; // number of items in the playlist
  27. int playlist_repetitions; // number of times to repeat the whole playlist
  28. int current_playlist_repetition; // current playlist repetition
  29. // last 3 files
  30. FuriString* prev_0_path; // current file
  31. FuriString* prev_1_path; // previous file
  32. FuriString* prev_2_path; // previous previous file
  33. FuriString* prev_3_path; // you get the idea
  34. int state; // current state
  35. ViewPort* view_port;
  36. } DisplayMeta;
  37. typedef struct {
  38. FuriThread* thread;
  39. Storage* storage;
  40. FlipperFormat* format;
  41. DisplayMeta* meta;
  42. FuriString* file_path; // path to the playlist file
  43. const SubGhzDevice* radio_device;
  44. bool ctl_request_exit; // can be set to true if the worker should exit
  45. bool ctl_pause; // can be set to true if the worker should pause
  46. bool ctl_request_skip; // can be set to true if the worker should skip the current file
  47. bool ctl_request_prev; // can be set to true if the worker should go to the previous file
  48. bool is_running; // indicates if the worker is running
  49. } PlaylistWorker;
  50. typedef struct {
  51. FuriMutex* mutex;
  52. FuriMessageQueue* input_queue;
  53. ViewPort* view_port;
  54. Gui* gui;
  55. DisplayMeta* meta;
  56. PlaylistWorker* worker;
  57. FuriString* file_path; // Path to the playlist file
  58. } Playlist;
  59. ////////////////////////////////////////////////////////////////////////////////
  60. void meta_set_state(DisplayMeta* meta, int state) {
  61. meta->state = state;
  62. view_port_update(meta->view_port);
  63. }
  64. static FuriHalSubGhzPreset str_to_preset(FuriString* preset) {
  65. if(furi_string_cmp_str(preset, "FuriHalSubGhzPresetOok270Async") == 0) {
  66. return FuriHalSubGhzPresetOok270Async;
  67. }
  68. if(furi_string_cmp_str(preset, "FuriHalSubGhzPresetOok650Async") == 0) {
  69. return FuriHalSubGhzPresetOok650Async;
  70. }
  71. if(furi_string_cmp_str(preset, "FuriHalSubGhzPreset2FSKDev238Async") == 0) {
  72. return FuriHalSubGhzPreset2FSKDev238Async;
  73. }
  74. if(furi_string_cmp_str(preset, "FuriHalSubGhzPreset2FSKDev476Async") == 0) {
  75. return FuriHalSubGhzPreset2FSKDev476Async;
  76. }
  77. if(furi_string_cmp_str(preset, "FuriHalSubGhzPresetMSK99_97KbAsync") == 0) {
  78. return FuriHalSubGhzPresetMSK99_97KbAsync;
  79. }
  80. if(furi_string_cmp_str(preset, "FuriHalSubGhzPresetMSK99_97KbAsync") == 0) {
  81. return FuriHalSubGhzPresetMSK99_97KbAsync;
  82. }
  83. return FuriHalSubGhzPresetCustom;
  84. }
  85. // -4: missing protocol
  86. // -3: missing preset
  87. // -2: transmit error
  88. // -1: error
  89. // 0: ok
  90. // 1: resend
  91. // 2: exited
  92. static int playlist_worker_process(
  93. PlaylistWorker* worker,
  94. FlipperFormat* fff_file,
  95. FlipperFormat* fff_data,
  96. const char* path,
  97. FuriString* preset,
  98. FuriString* protocol) {
  99. // actual sending of .sub file
  100. if(!flipper_format_file_open_existing(fff_file, path)) {
  101. FURI_LOG_E(TAG, " (TX) Failed to open %s", path);
  102. return -1;
  103. }
  104. // read frequency or default to 433.92MHz
  105. uint32_t frequency = 0;
  106. if(!flipper_format_read_uint32(fff_file, "Frequency", &frequency, 1)) {
  107. FURI_LOG_W(TAG, " (TX) Missing Frequency, defaulting to 433.92MHz");
  108. frequency = 433920000;
  109. }
  110. if(!subghz_devices_is_frequency_valid(worker->radio_device, frequency)) {
  111. FURI_LOG_E(
  112. TAG, " (TX) The SubGhz device used does not support the frequency %lu", frequency);
  113. return -2;
  114. }
  115. // check if preset is present
  116. if(!flipper_format_read_string(fff_file, "Preset", preset)) {
  117. FURI_LOG_E(TAG, " (TX) Missing Preset");
  118. return -3;
  119. }
  120. // check if protocol is present
  121. if(!flipper_format_read_string(fff_file, "Protocol", protocol)) {
  122. FURI_LOG_E(TAG, " (TX) Missing Protocol");
  123. return -4;
  124. }
  125. if(!furi_string_cmp_str(protocol, "RAW")) {
  126. subghz_protocol_raw_gen_fff_data(
  127. fff_data, path, subghz_devices_get_name(worker->radio_device));
  128. } else {
  129. stream_copy_full(
  130. flipper_format_get_raw_stream(fff_file), flipper_format_get_raw_stream(fff_data));
  131. }
  132. flipper_format_file_close(fff_file);
  133. flipper_format_free(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. subghz_environment_free(environment);
  184. return status;
  185. }
  186. // true - the worker can continue
  187. // false - the worker should exit
  188. static bool playlist_worker_wait_pause(PlaylistWorker* worker) {
  189. // wait if paused
  190. while(worker->ctl_pause && !worker->ctl_request_exit) {
  191. furi_delay_ms(50);
  192. }
  193. // exit loop if requested to stop
  194. if(worker->ctl_request_exit) {
  195. FURI_LOG_D(TAG, "Requested to exit. Exiting loop...");
  196. return false;
  197. }
  198. return true;
  199. }
  200. void updatePlayListView(PlaylistWorker* worker, const char* str) {
  201. furi_string_reset(worker->meta->prev_3_path);
  202. furi_string_set(worker->meta->prev_3_path, furi_string_get_cstr(worker->meta->prev_2_path));
  203. furi_string_reset(worker->meta->prev_2_path);
  204. furi_string_set(worker->meta->prev_2_path, furi_string_get_cstr(worker->meta->prev_1_path));
  205. furi_string_reset(worker->meta->prev_1_path);
  206. furi_string_set(worker->meta->prev_1_path, furi_string_get_cstr(worker->meta->prev_0_path));
  207. furi_string_reset(worker->meta->prev_0_path);
  208. furi_string_set(worker->meta->prev_0_path, str);
  209. view_port_update(worker->meta->view_port);
  210. }
  211. static bool playlist_worker_play_playlist_once(
  212. PlaylistWorker* worker,
  213. Storage* storage,
  214. FlipperFormat* fff_head,
  215. FlipperFormat* fff_data,
  216. FuriString* data,
  217. FuriString* preset,
  218. FuriString* protocol) {
  219. //
  220. if(!flipper_format_rewind(fff_head)) {
  221. FURI_LOG_E(TAG, "Failed to rewind file");
  222. return false;
  223. }
  224. while(flipper_format_read_string(fff_head, "sub", data)) {
  225. if(!playlist_worker_wait_pause(worker)) {
  226. break;
  227. }
  228. // update state to sending
  229. meta_set_state(worker->meta, STATE_SENDING);
  230. ++worker->meta->current_count;
  231. const char* str = furi_string_get_cstr(data);
  232. // it's not fancy, but it works for now :)
  233. updatePlayListView(worker, str);
  234. for(int i = 0; i < 1; i++) {
  235. if(!playlist_worker_wait_pause(worker)) {
  236. break;
  237. }
  238. view_port_update(worker->meta->view_port);
  239. FURI_LOG_D(TAG, "(worker) Sending %s", str);
  240. FlipperFormat* fff_file = flipper_format_file_alloc(storage);
  241. int status =
  242. playlist_worker_process(worker, fff_file, fff_data, str, preset, protocol);
  243. // if there was an error, fff_file is not already freed
  244. if(status < 0) {
  245. flipper_format_file_close(fff_file);
  246. flipper_format_free(fff_file);
  247. }
  248. // re-send file is paused mid-send
  249. if(status == 1) {
  250. i -= 1;
  251. // errored, skip to next file
  252. } else if(status < 0) {
  253. break;
  254. // exited, exit loop
  255. } else if(status == 2) {
  256. return false;
  257. } else if(status == 3) {
  258. //aqui rebobinamos y avanzamos de nuevo el fichero n-1 veces
  259. //decrementamos el contador de ficheros enviados
  260. worker->meta->current_count--;
  261. if(worker->meta->current_count > 0) {
  262. worker->meta->current_count--;
  263. }
  264. //rebobinamos el fichero
  265. if(!flipper_format_rewind(fff_head)) {
  266. FURI_LOG_E(TAG, "Failed to rewind file");
  267. return false;
  268. }
  269. //avanzamos el fichero n-1 veces
  270. for(int j = 0; j < worker->meta->current_count; j++) {
  271. flipper_format_read_string(fff_head, "sub", data);
  272. }
  273. break;
  274. }
  275. }
  276. } // end of loop
  277. return true;
  278. }
  279. static int32_t playlist_worker_thread(void* ctx) {
  280. Storage* storage = furi_record_open(RECORD_STORAGE);
  281. FlipperFormat* fff_head = flipper_format_file_alloc(storage);
  282. PlaylistWorker* worker = ctx;
  283. if(!flipper_format_file_open_existing(fff_head, furi_string_get_cstr(worker->file_path))) {
  284. FURI_LOG_E(TAG, "Failed to open %s", furi_string_get_cstr(worker->file_path));
  285. worker->is_running = false;
  286. furi_record_close(RECORD_STORAGE);
  287. flipper_format_free(fff_head);
  288. return 0;
  289. }
  290. playlist_worker_wait_pause(worker);
  291. FlipperFormat* fff_data = flipper_format_string_alloc();
  292. FuriString* data;
  293. FuriString* preset;
  294. FuriString* protocol;
  295. data = furi_string_alloc();
  296. preset = furi_string_alloc();
  297. protocol = furi_string_alloc();
  298. for(int i = 0; i < MAX(1, worker->meta->playlist_repetitions); i++) {
  299. // infinite repetitions if playlist_repetitions is 0
  300. if(worker->meta->playlist_repetitions <= 0) {
  301. --i;
  302. }
  303. ++worker->meta->current_playlist_repetition;
  304. // send playlist
  305. worker->meta->current_count = 0;
  306. if(worker->ctl_request_exit) {
  307. break;
  308. }
  309. FURI_LOG_D(
  310. TAG,
  311. "Sending playlist (i %d rep %d b %d)",
  312. i,
  313. worker->meta->current_playlist_repetition,
  314. worker->meta->playlist_repetitions);
  315. if(!playlist_worker_play_playlist_once(
  316. worker, storage, fff_head, fff_data, data, preset, protocol)) {
  317. break;
  318. }
  319. }
  320. furi_record_close(RECORD_STORAGE);
  321. flipper_format_free(fff_head);
  322. furi_string_free(data);
  323. furi_string_free(preset);
  324. furi_string_free(protocol);
  325. flipper_format_free(fff_data);
  326. FURI_LOG_D(TAG, "Done reading. Read %d data lines.", worker->meta->current_count);
  327. worker->is_running = false;
  328. // update state to overview
  329. meta_set_state(worker->meta, STATE_OVERVIEW);
  330. return 0;
  331. }
  332. ////////////////////////////////////////////////////////////////////////////////
  333. void playlist_meta_reset(DisplayMeta* instance) {
  334. instance->current_count = 0;
  335. instance->current_playlist_repetition = 0;
  336. furi_string_reset(instance->prev_0_path);
  337. furi_string_reset(instance->prev_1_path);
  338. furi_string_reset(instance->prev_2_path);
  339. furi_string_reset(instance->prev_3_path);
  340. }
  341. DisplayMeta* playlist_meta_alloc() {
  342. DisplayMeta* instance = malloc(sizeof(DisplayMeta));
  343. instance->prev_0_path = furi_string_alloc();
  344. instance->prev_1_path = furi_string_alloc();
  345. instance->prev_2_path = furi_string_alloc();
  346. instance->prev_3_path = furi_string_alloc();
  347. playlist_meta_reset(instance);
  348. instance->state = STATE_NONE;
  349. instance->playlist_repetitions = 1;
  350. return instance;
  351. }
  352. void playlist_meta_free(DisplayMeta* instance) {
  353. furi_string_free(instance->prev_0_path);
  354. furi_string_free(instance->prev_1_path);
  355. furi_string_free(instance->prev_2_path);
  356. furi_string_free(instance->prev_3_path);
  357. free(instance);
  358. }
  359. ////////////////////////////////////////////////////////////////////////////////
  360. PlaylistWorker* playlist_worker_alloc(DisplayMeta* meta) {
  361. PlaylistWorker* instance = malloc(sizeof(PlaylistWorker));
  362. instance->thread = furi_thread_alloc();
  363. furi_thread_set_name(instance->thread, "PlaylistWorker");
  364. furi_thread_set_stack_size(instance->thread, 2048);
  365. furi_thread_set_context(instance->thread, instance);
  366. furi_thread_set_callback(instance->thread, playlist_worker_thread);
  367. instance->meta = meta;
  368. instance->ctl_pause = true; // require the user to manually start the worker
  369. instance->file_path = furi_string_alloc();
  370. subghz_devices_init();
  371. instance->radio_device =
  372. radio_device_loader_set(instance->radio_device, SubGhzRadioDeviceTypeExternalCC1101);
  373. subghz_devices_reset(instance->radio_device);
  374. subghz_devices_idle(instance->radio_device);
  375. return instance;
  376. }
  377. void playlist_worker_free(PlaylistWorker* instance) {
  378. furi_assert(instance);
  379. furi_thread_free(instance->thread);
  380. furi_string_free(instance->file_path);
  381. subghz_devices_sleep(instance->radio_device);
  382. radio_device_loader_end(instance->radio_device);
  383. subghz_devices_deinit();
  384. free(instance);
  385. }
  386. void playlist_worker_stop(PlaylistWorker* worker) {
  387. furi_assert(worker);
  388. furi_assert(worker->is_running);
  389. worker->ctl_request_exit = true;
  390. furi_thread_join(worker->thread);
  391. }
  392. bool playlist_worker_running(PlaylistWorker* worker) {
  393. furi_assert(worker);
  394. return worker->is_running;
  395. }
  396. void playlist_worker_start(PlaylistWorker* instance, const char* file_path) {
  397. furi_assert(instance);
  398. furi_assert(!instance->is_running);
  399. furi_string_set(instance->file_path, file_path);
  400. instance->is_running = true;
  401. // reset meta (current/total)
  402. playlist_meta_reset(instance->meta);
  403. FURI_LOG_D(TAG, "Starting thread...");
  404. furi_thread_start(instance->thread);
  405. }
  406. ////////////////////////////////////////////////////////////////////////////////
  407. static void render_callback(Canvas* canvas, void* ctx) {
  408. Playlist* app = ctx;
  409. furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
  410. canvas_clear(canvas);
  411. canvas_set_color(canvas, ColorBlack);
  412. canvas_set_font(canvas, FontSecondary);
  413. FuriString* temp_str;
  414. temp_str = furi_string_alloc();
  415. switch(app->meta->state) {
  416. case STATE_NONE:
  417. canvas_set_font(canvas, FontPrimary);
  418. canvas_draw_str_aligned(
  419. canvas, WIDTH / 2, HEIGHT / 2, AlignCenter, AlignCenter, "No playlist loaded");
  420. break;
  421. case STATE_OVERVIEW:
  422. // draw file name
  423. {
  424. path_extract_filename(app->file_path, temp_str, true);
  425. canvas_set_font(canvas, FontPrimary);
  426. draw_centered_boxed_str(canvas, 1, 1, 15, 6, furi_string_get_cstr(temp_str));
  427. }
  428. canvas_set_font(canvas, FontSecondary);
  429. // draw loaded count
  430. {
  431. furi_string_printf(temp_str, "%d Items in playlist", app->meta->total_count);
  432. canvas_draw_str_aligned(
  433. canvas, 1, 19, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  434. if(app->meta->playlist_repetitions <= 0) {
  435. furi_string_set(temp_str, "Repeat: inf");
  436. } else if(app->meta->playlist_repetitions == 1) {
  437. furi_string_set(temp_str, "Repeat: no");
  438. } else {
  439. furi_string_printf(temp_str, "Repeat: %dx", app->meta->playlist_repetitions);
  440. }
  441. canvas_draw_str_aligned(
  442. canvas, 1, 29, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  443. }
  444. // draw buttons
  445. draw_corner_aligned(canvas, 40, 15, AlignCenter, AlignBottom);
  446. canvas_set_color(canvas, ColorWhite);
  447. canvas_draw_str_aligned(canvas, WIDTH / 2 - 7, HEIGHT - 11, AlignLeft, AlignTop, "Start");
  448. canvas_draw_disc(canvas, WIDTH / 2 - 14, HEIGHT - 8, 3);
  449. //
  450. canvas_set_color(canvas, ColorBlack);
  451. draw_corner_aligned(canvas, 20, 15, AlignLeft, AlignBottom);
  452. canvas_set_color(canvas, ColorWhite);
  453. canvas_draw_str_aligned(canvas, 4, HEIGHT - 11, AlignLeft, AlignTop, "R-");
  454. //
  455. canvas_set_color(canvas, ColorBlack);
  456. draw_corner_aligned(canvas, 20, 15, AlignRight, AlignBottom);
  457. canvas_set_color(canvas, ColorWhite);
  458. canvas_draw_str_aligned(canvas, WIDTH - 4, HEIGHT - 11, AlignRight, AlignTop, "R+");
  459. canvas_set_color(canvas, ColorBlack);
  460. break;
  461. case STATE_SENDING:
  462. canvas_set_color(canvas, ColorBlack);
  463. if(app->worker->ctl_pause) {
  464. canvas_draw_icon(canvas, 2, HEIGHT - 8, &I_ButtonRight_4x7);
  465. } else {
  466. canvas_draw_box(canvas, 2, HEIGHT - 8, 2, 7);
  467. canvas_draw_box(canvas, 5, HEIGHT - 8, 2, 7);
  468. }
  469. // draw progress text
  470. {
  471. canvas_set_font(canvas, FontSecondary);
  472. furi_string_printf(
  473. temp_str, "[%d/%d]", app->meta->current_count, app->meta->total_count);
  474. canvas_draw_str_aligned(
  475. canvas, 11, HEIGHT - 8, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  476. int h = canvas_string_width(canvas, furi_string_get_cstr(temp_str));
  477. int xs = 11 + h + 2;
  478. int w = WIDTH - xs - 1;
  479. canvas_draw_box(canvas, xs, HEIGHT - 5, w, 1);
  480. float progress = (float)app->meta->current_count / (float)app->meta->total_count;
  481. int wp = (int)(progress * w);
  482. canvas_draw_box(canvas, xs + wp - 1, HEIGHT - 7, 2, 5);
  483. }
  484. {
  485. if(app->meta->playlist_repetitions <= 0) {
  486. furi_string_printf(temp_str, "[%d/Inf]", app->meta->current_playlist_repetition);
  487. } else {
  488. furi_string_printf(
  489. temp_str,
  490. "[%d/%d]",
  491. app->meta->current_playlist_repetition,
  492. app->meta->playlist_repetitions);
  493. }
  494. canvas_set_color(canvas, ColorBlack);
  495. int w = canvas_string_width(canvas, furi_string_get_cstr(temp_str));
  496. draw_corner_aligned(canvas, w + 6, 13, AlignRight, AlignTop);
  497. canvas_set_color(canvas, ColorWhite);
  498. canvas_draw_str_aligned(
  499. canvas, WIDTH - 3, 3, AlignRight, AlignTop, furi_string_get_cstr(temp_str));
  500. }
  501. // draw last and current file
  502. {
  503. canvas_set_color(canvas, ColorBlack);
  504. canvas_set_font(canvas, FontSecondary);
  505. // current
  506. if(!furi_string_empty(app->meta->prev_0_path)) {
  507. path_extract_filename(app->meta->prev_0_path, temp_str, true);
  508. int w = canvas_string_width(canvas, furi_string_get_cstr(temp_str));
  509. canvas_set_color(canvas, ColorBlack);
  510. canvas_draw_rbox(canvas, 1, 1, w + 4, 12, 2);
  511. canvas_set_color(canvas, ColorWhite);
  512. canvas_draw_str_aligned(
  513. canvas, 3, 3, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  514. }
  515. // last 3
  516. canvas_set_color(canvas, ColorBlack);
  517. if(!furi_string_empty(app->meta->prev_1_path)) {
  518. path_extract_filename(app->meta->prev_1_path, temp_str, true);
  519. canvas_draw_str_aligned(
  520. canvas, 3, 15, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  521. }
  522. if(!furi_string_empty(app->meta->prev_2_path)) {
  523. path_extract_filename(app->meta->prev_2_path, temp_str, true);
  524. canvas_draw_str_aligned(
  525. canvas, 3, 26, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  526. }
  527. if(!furi_string_empty(app->meta->prev_3_path)) {
  528. path_extract_filename(app->meta->prev_3_path, temp_str, true);
  529. canvas_draw_str_aligned(
  530. canvas, 3, 37, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  531. }
  532. }
  533. break;
  534. default:
  535. break;
  536. }
  537. furi_string_free(temp_str);
  538. furi_mutex_release(app->mutex);
  539. }
  540. static void input_callback(InputEvent* event, void* ctx) {
  541. Playlist* app = ctx;
  542. furi_message_queue_put(app->input_queue, event, 0);
  543. }
  544. ////////////////////////////////////////////////////////////////////////////////
  545. Playlist* playlist_alloc(DisplayMeta* meta) {
  546. Playlist* app = malloc(sizeof(Playlist));
  547. app->file_path = furi_string_alloc();
  548. furi_string_set(app->file_path, PLAYLIST_FOLDER);
  549. app->meta = meta;
  550. app->worker = NULL;
  551. app->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  552. app->input_queue = furi_message_queue_alloc(32, sizeof(InputEvent));
  553. // view port
  554. app->view_port = view_port_alloc();
  555. view_port_draw_callback_set(app->view_port, render_callback, app);
  556. view_port_input_callback_set(app->view_port, input_callback, app);
  557. // gui
  558. app->gui = furi_record_open(RECORD_GUI);
  559. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  560. return app;
  561. }
  562. void playlist_start_worker(Playlist* app, DisplayMeta* meta) {
  563. app->worker = playlist_worker_alloc(meta);
  564. // count playlist items
  565. Storage* storage = furi_record_open(RECORD_STORAGE);
  566. app->meta->total_count =
  567. playlist_count_playlist_items(storage, furi_string_get_cstr(app->file_path));
  568. furi_record_close(RECORD_STORAGE);
  569. // start thread
  570. playlist_worker_start(app->worker, furi_string_get_cstr(app->file_path));
  571. }
  572. void playlist_free(Playlist* app) {
  573. furi_string_free(app->file_path);
  574. gui_remove_view_port(app->gui, app->view_port);
  575. furi_record_close(RECORD_GUI);
  576. view_port_free(app->view_port);
  577. furi_message_queue_free(app->input_queue);
  578. furi_mutex_free(app->mutex);
  579. playlist_meta_free(app->meta);
  580. free(app);
  581. }
  582. int32_t playlist_app(char* 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. if(p && strlen(p)) {
  598. furi_string_set(app->file_path, p);
  599. } else {
  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.base_path = PLAYLIST_FOLDER;
  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. }