playlist.c 29 KB

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