playlist.c 30 KB

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