playlist.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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/subghz_txrx.h"
  11. #include <lib/subghz/blocks/custom_btn.h>
  12. #include <lib/subghz/protocols/raw.h>
  13. #include "flipper_format_stream.h"
  14. #include "flipper_format_stream_i.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. SubGhzTxRx* txrx; // subghz txrx instance
  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. bool raw_file_is_tx;
  52. } PlaylistWorker;
  53. typedef struct {
  54. FuriMutex* mutex;
  55. FuriMessageQueue* input_queue;
  56. ViewPort* view_port;
  57. Gui* gui;
  58. DisplayMeta* meta;
  59. PlaylistWorker* worker;
  60. FuriString* file_path; // Path to the playlist file
  61. } Playlist;
  62. ////////////////////////////////////////////////////////////////////////////////
  63. void meta_set_state(DisplayMeta* meta, int state) {
  64. meta->state = state;
  65. view_port_update(meta->view_port);
  66. }
  67. typedef struct SubGhzNeedSaveContext {
  68. PlaylistWorker* worker;
  69. SubGhzTxRx* txrx;
  70. char* file_path;
  71. } SubGhzNeedSaveContext;
  72. static void playlist_subghz_need_save_callback(void* context) {
  73. FURI_LOG_I(TAG, "Saving updated subghz signal");
  74. SubGhzNeedSaveContext* savectx = (SubGhzNeedSaveContext*)context;
  75. FlipperFormat* ff = subghz_txrx_get_fff_data(savectx->txrx);
  76. Stream* ff_stream = flipper_format_get_raw_stream(ff);
  77. flipper_format_delete_key(ff, "Repeat");
  78. do {
  79. if(!storage_simply_remove(savectx->worker->storage, savectx->file_path)) {
  80. FURI_LOG_E(TAG, "Failed to delete subghz file before re-save");
  81. break;
  82. }
  83. stream_seek(ff_stream, 0, StreamOffsetFromStart);
  84. stream_save_to_file(
  85. ff_stream, savectx->worker->storage, savectx->file_path, FSOM_CREATE_ALWAYS);
  86. if(storage_common_stat(savectx->worker->storage, savectx->file_path, NULL) != FSE_OK) {
  87. FURI_LOG_E(TAG, "Error verifying new subghz file after re-save");
  88. break;
  89. }
  90. } while(0);
  91. }
  92. void playlist_subghz_raw_end_callback(void* context) {
  93. FURI_LOG_I(TAG, "Stopping TX on RAW");
  94. furi_assert(context);
  95. PlaylistWorker* worker = context;
  96. worker->raw_file_is_tx = false;
  97. }
  98. // -4: missing protocol
  99. // -3: missing or wrong preset
  100. // -2: transmit error
  101. // -1: error
  102. // 0: ok
  103. // 1: resend
  104. // 2: exited
  105. static int playlist_worker_process(PlaylistWorker* worker, const char* file_name) {
  106. // actual sending of .sub file
  107. FuriString* preset_name = furi_string_alloc();
  108. FuriString* protocol_name = furi_string_alloc();
  109. FuriString* temp_str = furi_string_alloc();
  110. uint8_t status = 0;
  111. do {
  112. FlipperFormat* fff_data_file = flipper_format_file_alloc(worker->storage);
  113. SubGhzNeedSaveContext save_context = {worker, worker->txrx, (char*)file_name};
  114. subghz_txrx_set_need_save_callback(
  115. worker->txrx, playlist_subghz_need_save_callback, &save_context);
  116. Stream* fff_data_stream =
  117. flipper_format_get_raw_stream(subghz_txrx_get_fff_data(worker->txrx));
  118. stream_clean(fff_data_stream);
  119. furi_string_reset(temp_str);
  120. furi_string_reset(preset_name);
  121. furi_string_reset(protocol_name);
  122. worker->raw_file_is_tx = false;
  123. subghz_custom_btns_reset();
  124. uint32_t temp_data32;
  125. uint32_t frequency = 0;
  126. if(!flipper_format_file_open_existing(fff_data_file, file_name)) {
  127. FURI_LOG_E(TAG, "Error opening %s", file_name);
  128. flipper_format_free(fff_data_file);
  129. furi_string_free(preset_name);
  130. furi_string_free(protocol_name);
  131. furi_string_free(temp_str);
  132. return -1;
  133. }
  134. if(!flipper_format_read_header(fff_data_file, temp_str, &temp_data32)) {
  135. FURI_LOG_E(TAG, "Missing or incorrect header");
  136. flipper_format_file_close(fff_data_file);
  137. flipper_format_free(fff_data_file);
  138. furi_string_free(preset_name);
  139. furi_string_free(protocol_name);
  140. furi_string_free(temp_str);
  141. return -1;
  142. }
  143. if(((!strcmp(furi_string_get_cstr(temp_str), SUBGHZ_KEY_FILE_TYPE)) ||
  144. (!strcmp(furi_string_get_cstr(temp_str), SUBGHZ_RAW_FILE_TYPE))) &&
  145. temp_data32 == SUBGHZ_KEY_FILE_VERSION) {
  146. } else {
  147. FURI_LOG_E(TAG, "Type or version mismatch");
  148. flipper_format_file_close(fff_data_file);
  149. flipper_format_free(fff_data_file);
  150. furi_string_free(preset_name);
  151. furi_string_free(protocol_name);
  152. furi_string_free(temp_str);
  153. return -1;
  154. }
  155. SubGhzSetting* setting = subghz_txrx_get_setting(worker->txrx);
  156. if(!flipper_format_read_uint32(fff_data_file, "Frequency", &frequency, 1)) {
  157. FURI_LOG_W(TAG, "Missing Frequency. Setting default frequency");
  158. frequency = subghz_setting_get_default_frequency(setting);
  159. } else if(!subghz_txrx_radio_device_is_frequecy_valid(worker->txrx, frequency)) {
  160. FURI_LOG_E(TAG, "Frequency not supported on the chosen radio module");
  161. flipper_format_file_close(fff_data_file);
  162. flipper_format_free(fff_data_file);
  163. furi_string_free(preset_name);
  164. furi_string_free(protocol_name);
  165. furi_string_free(temp_str);
  166. return -1;
  167. }
  168. if(!flipper_format_read_string(fff_data_file, "Preset", temp_str)) {
  169. FURI_LOG_E(TAG, "Missing Preset");
  170. flipper_format_file_close(fff_data_file);
  171. flipper_format_free(fff_data_file);
  172. furi_string_free(preset_name);
  173. furi_string_free(protocol_name);
  174. furi_string_free(temp_str);
  175. return -3;
  176. }
  177. furi_string_set_str(
  178. temp_str, subghz_txrx_get_preset_name(worker->txrx, furi_string_get_cstr(temp_str)));
  179. if(!strcmp(furi_string_get_cstr(temp_str), "")) {
  180. FURI_LOG_E(TAG, "Unknown preset");
  181. flipper_format_file_close(fff_data_file);
  182. flipper_format_free(fff_data_file);
  183. furi_string_free(preset_name);
  184. furi_string_free(protocol_name);
  185. furi_string_free(temp_str);
  186. return -3;
  187. }
  188. if(!strcmp(furi_string_get_cstr(temp_str), "CUSTOM")) {
  189. subghz_setting_delete_custom_preset(setting, furi_string_get_cstr(temp_str));
  190. if(!subghz_setting_load_custom_preset(
  191. setting, furi_string_get_cstr(temp_str), fff_data_file)) {
  192. FURI_LOG_E(TAG, "Missing Custom preset");
  193. flipper_format_file_close(fff_data_file);
  194. flipper_format_free(fff_data_file);
  195. furi_string_free(preset_name);
  196. furi_string_free(protocol_name);
  197. furi_string_free(temp_str);
  198. return -1;
  199. }
  200. }
  201. furi_string_set(preset_name, temp_str);
  202. size_t preset_index =
  203. subghz_setting_get_inx_preset_by_name(setting, furi_string_get_cstr(preset_name));
  204. subghz_txrx_set_preset(
  205. worker->txrx,
  206. furi_string_get_cstr(preset_name),
  207. frequency,
  208. subghz_setting_get_preset_data(setting, preset_index),
  209. subghz_setting_get_preset_data_size(setting, preset_index));
  210. // Load Protocol
  211. if(!flipper_format_read_string(fff_data_file, "Protocol", protocol_name)) {
  212. FURI_LOG_E(TAG, "Missing protocol");
  213. flipper_format_file_close(fff_data_file);
  214. flipper_format_free(fff_data_file);
  215. furi_string_free(preset_name);
  216. furi_string_free(protocol_name);
  217. furi_string_free(temp_str);
  218. return -4;
  219. }
  220. FlipperFormat* fff_data = subghz_txrx_get_fff_data(worker->txrx);
  221. if(!strcmp(furi_string_get_cstr(protocol_name), "RAW")) {
  222. subghz_protocol_raw_gen_fff_data(
  223. fff_data, file_name, subghz_txrx_radio_device_get_name(worker->txrx));
  224. } else {
  225. stream_copy_full(
  226. flipper_format_get_raw_stream(fff_data_file),
  227. flipper_format_get_raw_stream(fff_data));
  228. }
  229. uint32_t repeat = 200;
  230. if(!flipper_format_insert_or_update_uint32(fff_data, "Repeat", &repeat, 1)) {
  231. FURI_LOG_E(TAG, "Unable Repeat");
  232. //
  233. }
  234. if(subghz_txrx_load_decoder_by_name_protocol(
  235. worker->txrx, furi_string_get_cstr(protocol_name))) {
  236. SubGhzProtocolStatus status = subghz_protocol_decoder_base_deserialize(
  237. subghz_txrx_get_decoder(worker->txrx), fff_data);
  238. if(status != SubGhzProtocolStatusOk) {
  239. flipper_format_file_close(fff_data_file);
  240. flipper_format_free(fff_data_file);
  241. furi_string_free(preset_name);
  242. furi_string_free(protocol_name);
  243. furi_string_free(temp_str);
  244. return -2;
  245. }
  246. } else {
  247. FURI_LOG_E(TAG, "Protocol not found: %s", furi_string_get_cstr(protocol_name));
  248. flipper_format_file_close(fff_data_file);
  249. flipper_format_free(fff_data_file);
  250. furi_string_free(preset_name);
  251. furi_string_free(protocol_name);
  252. furi_string_free(temp_str);
  253. return -2;
  254. }
  255. flipper_format_file_close(fff_data_file);
  256. flipper_format_free(fff_data_file);
  257. FURI_LOG_I(TAG, "Starting TX");
  258. if(subghz_txrx_tx_start(worker->txrx, subghz_txrx_get_fff_data(worker->txrx)) !=
  259. SubGhzTxRxStartTxStateOk) {
  260. FURI_LOG_E(TAG, "Failed to start TX");
  261. }
  262. bool skip_extra_stop = false;
  263. FURI_LOG_D(TAG, "Checking if file is RAW...");
  264. if(!strcmp(furi_string_get_cstr(protocol_name), "RAW")) {
  265. subghz_txrx_set_raw_file_encoder_worker_callback_end(
  266. worker->txrx, playlist_subghz_raw_end_callback, worker);
  267. worker->raw_file_is_tx = true;
  268. skip_extra_stop = true;
  269. }
  270. do {
  271. if(worker->ctl_request_exit) {
  272. FURI_LOG_D(TAG, " (TX) Requested to exit. Cancelling sending...");
  273. status = 2;
  274. break;
  275. }
  276. if(worker->ctl_pause) {
  277. FURI_LOG_D(TAG, " (TX) Requested to pause. Cancelling...");
  278. status = 1;
  279. break;
  280. }
  281. if(worker->ctl_request_skip) {
  282. worker->ctl_request_skip = false;
  283. FURI_LOG_D(TAG, " (TX) Requested to skip. Cancelling and skipping...");
  284. status = 0;
  285. break;
  286. }
  287. if(worker->ctl_request_prev) {
  288. worker->ctl_request_prev = false;
  289. FURI_LOG_D(TAG, " (TX) Requested to prev. Cancelling and resending...");
  290. status = 3;
  291. break;
  292. }
  293. furi_delay_ms(1);
  294. } while(worker->raw_file_is_tx);
  295. if(!worker->raw_file_is_tx && !skip_extra_stop) {
  296. if(worker->ctl_request_exit) {
  297. FURI_LOG_D(TAG, " (TX) Requested to exit. Cancelling sending...");
  298. status = 2;
  299. }
  300. if(worker->ctl_pause) {
  301. FURI_LOG_D(TAG, " (TX) Requested to pause. Cancelling...");
  302. status = 1;
  303. }
  304. if(worker->ctl_request_skip) {
  305. worker->ctl_request_skip = false;
  306. FURI_LOG_D(TAG, " (TX) Requested to skip. Cancelling and skipping...");
  307. status = 0;
  308. }
  309. if(worker->ctl_request_prev) {
  310. worker->ctl_request_prev = false;
  311. FURI_LOG_D(TAG, " (TX) Requested to prev. Cancelling and resending...");
  312. status = 3;
  313. }
  314. furi_delay_ms(1600);
  315. subghz_txrx_stop(worker->txrx);
  316. } else {
  317. furi_delay_ms(20);
  318. subghz_txrx_stop(worker->txrx);
  319. }
  320. skip_extra_stop = false;
  321. } while(false);
  322. FURI_LOG_D(TAG, "TX Finished");
  323. subghz_custom_btns_reset();
  324. furi_string_free(preset_name);
  325. furi_string_free(protocol_name);
  326. furi_string_free(temp_str);
  327. return status;
  328. }
  329. // true - the worker can continue
  330. // false - the worker should exit
  331. static bool playlist_worker_wait_pause(PlaylistWorker* worker) {
  332. // wait if paused
  333. while(worker->ctl_pause && !worker->ctl_request_exit) {
  334. furi_delay_ms(50);
  335. }
  336. // exit loop if requested to stop
  337. if(worker->ctl_request_exit) {
  338. FURI_LOG_D(TAG, "Requested to exit. Exiting loop...");
  339. return false;
  340. }
  341. return true;
  342. }
  343. void updatePlayListView(PlaylistWorker* worker, const char* str) {
  344. furi_check(furi_mutex_acquire(worker->meta->mutex, FuriWaitForever) == FuriStatusOk);
  345. furi_string_set(worker->meta->prev_3_text, furi_string_get_cstr(worker->meta->prev_2_text));
  346. furi_string_set(worker->meta->prev_2_text, furi_string_get_cstr(worker->meta->prev_1_text));
  347. furi_string_set(worker->meta->prev_1_text, furi_string_get_cstr(worker->meta->prev_0_text));
  348. furi_string_set(worker->meta->prev_0_text, str);
  349. furi_mutex_release(worker->meta->mutex);
  350. view_port_update(worker->meta->view_port);
  351. }
  352. static bool playlist_worker_play_playlist_once(
  353. PlaylistWorker* worker,
  354. FlipperFormat* fff_head,
  355. FuriString* data) {
  356. //
  357. if(!flipper_format_rewind(fff_head)) {
  358. FURI_LOG_E(TAG, "Failed to rewind file");
  359. return false;
  360. }
  361. while(flipper_format_read_string(fff_head, "sub", data)) {
  362. if(!playlist_worker_wait_pause(worker)) {
  363. break;
  364. }
  365. // update state to sending
  366. meta_set_state(worker->meta, STATE_SENDING);
  367. ++worker->meta->current_count;
  368. const char* str = furi_string_get_cstr(data);
  369. // it's not fancy, but it works for now :)
  370. FuriString* filename = furi_string_alloc();
  371. path_extract_filename(data, filename, true);
  372. updatePlayListView(worker, furi_string_get_cstr(filename));
  373. furi_string_free(filename);
  374. for(int i = 0; i < 1; i++) {
  375. if(!playlist_worker_wait_pause(worker)) {
  376. break;
  377. }
  378. view_port_update(worker->meta->view_port);
  379. FURI_LOG_D(TAG, "(worker) Sending %s", str);
  380. int status = playlist_worker_process(worker, str);
  381. FURI_LOG_D(TAG, "Finished file, status %d", status);
  382. // if there was an error, fff_file is not already freed
  383. // why do you do this with numbers without meaning x_x
  384. if(status < 0) {
  385. if(status > -5) {
  386. //
  387. }
  388. furi_check(
  389. furi_mutex_acquire(worker->meta->mutex, FuriWaitForever) == FuriStatusOk);
  390. furi_string_cat(worker->meta->prev_0_text, " FAIL");
  391. furi_mutex_release(worker->meta->mutex);
  392. view_port_update(worker->meta->view_port);
  393. }
  394. // re-send file is paused mid-send
  395. if(status == 1) {
  396. i -= 1;
  397. // errored, skip to next file
  398. } else if(status < 0) {
  399. break;
  400. // exited, exit loop
  401. } else if(status == 2) {
  402. return false;
  403. } else if(status == 3) {
  404. //aqui rebobinamos y avanzamos de nuevo el fichero n-1 veces
  405. //decrementamos el contador de ficheros enviados
  406. worker->meta->current_count--;
  407. if(worker->meta->current_count > 0) {
  408. worker->meta->current_count--;
  409. }
  410. //rebobinamos el fichero
  411. if(!flipper_format_rewind(fff_head)) {
  412. FURI_LOG_E(TAG, "Failed to rewind file");
  413. return false;
  414. }
  415. //avanzamos el fichero n-1 veces
  416. for(int j = 0; j < worker->meta->current_count; j++) {
  417. flipper_format_read_string(fff_head, "sub", data);
  418. }
  419. break;
  420. }
  421. }
  422. } // end of loop
  423. return true;
  424. }
  425. static int32_t playlist_worker_thread(void* ctx) {
  426. PlaylistWorker* worker = ctx;
  427. FlipperFormat* fff_head = flipper_format_file_alloc(worker->storage);
  428. if(!flipper_format_file_open_existing(fff_head, furi_string_get_cstr(worker->file_path))) {
  429. FURI_LOG_E(TAG, "Failed to open %s", furi_string_get_cstr(worker->file_path));
  430. worker->is_running = false;
  431. flipper_format_free(fff_head);
  432. return 0;
  433. }
  434. playlist_worker_wait_pause(worker);
  435. FuriString* data = furi_string_alloc();
  436. for(int i = 0; i < MAX(1, worker->meta->playlist_repetitions); i++) {
  437. // infinite repetitions if playlist_repetitions is 0
  438. if(worker->meta->playlist_repetitions <= 0) {
  439. --i;
  440. }
  441. ++worker->meta->current_playlist_repetition;
  442. // send playlist
  443. worker->meta->current_count = 0;
  444. if(worker->ctl_request_exit) {
  445. break;
  446. }
  447. FURI_LOG_D(
  448. TAG,
  449. "Sending playlist (i %d rep %d b %d)",
  450. i,
  451. worker->meta->current_playlist_repetition,
  452. worker->meta->playlist_repetitions);
  453. if(!playlist_worker_play_playlist_once(worker, fff_head, data)) {
  454. break;
  455. }
  456. }
  457. flipper_format_free(fff_head);
  458. furi_string_free(data);
  459. FURI_LOG_D(TAG, "Done reading. Read %d data lines.", worker->meta->current_count);
  460. worker->is_running = false;
  461. // update state to overview
  462. meta_set_state(worker->meta, STATE_OVERVIEW);
  463. return 0;
  464. }
  465. ////////////////////////////////////////////////////////////////////////////////
  466. void playlist_meta_reset(DisplayMeta* instance) {
  467. instance->current_count = 0;
  468. instance->current_playlist_repetition = 0;
  469. furi_string_reset(instance->prev_0_text);
  470. furi_string_reset(instance->prev_1_text);
  471. furi_string_reset(instance->prev_2_text);
  472. furi_string_reset(instance->prev_3_text);
  473. }
  474. DisplayMeta* playlist_meta_alloc() {
  475. DisplayMeta* instance = malloc(sizeof(DisplayMeta));
  476. instance->prev_0_text = furi_string_alloc();
  477. instance->prev_1_text = furi_string_alloc();
  478. instance->prev_2_text = furi_string_alloc();
  479. instance->prev_3_text = furi_string_alloc();
  480. playlist_meta_reset(instance);
  481. instance->state = STATE_NONE;
  482. instance->playlist_repetitions = 1;
  483. return instance;
  484. }
  485. void playlist_meta_free(DisplayMeta* instance) {
  486. furi_string_free(instance->prev_0_text);
  487. furi_string_free(instance->prev_1_text);
  488. furi_string_free(instance->prev_2_text);
  489. furi_string_free(instance->prev_3_text);
  490. free(instance);
  491. }
  492. ////////////////////////////////////////////////////////////////////////////////
  493. PlaylistWorker* playlist_worker_alloc(DisplayMeta* meta) {
  494. PlaylistWorker* instance = malloc(sizeof(PlaylistWorker));
  495. instance->thread = furi_thread_alloc();
  496. furi_thread_set_name(instance->thread, "PlaylistWorker");
  497. furi_thread_set_stack_size(instance->thread, 6 * 1024);
  498. furi_thread_set_context(instance->thread, instance);
  499. furi_thread_set_callback(instance->thread, playlist_worker_thread);
  500. instance->storage = furi_record_open(RECORD_STORAGE);
  501. instance->meta = meta;
  502. instance->ctl_pause = true; // require the user to manually start the worker
  503. instance->file_path = furi_string_alloc();
  504. instance->txrx = subghz_txrx_alloc();
  505. return instance;
  506. }
  507. void playlist_worker_free(PlaylistWorker* instance) {
  508. furi_assert(instance);
  509. furi_thread_free(instance->thread);
  510. furi_string_free(instance->file_path);
  511. subghz_txrx_free(instance->txrx);
  512. furi_record_close(RECORD_STORAGE);
  513. free(instance);
  514. }
  515. void playlist_worker_stop(PlaylistWorker* worker) {
  516. furi_assert(worker);
  517. furi_assert(worker->is_running);
  518. worker->ctl_request_exit = true;
  519. furi_thread_join(worker->thread);
  520. }
  521. bool playlist_worker_running(PlaylistWorker* worker) {
  522. furi_assert(worker);
  523. return worker->is_running;
  524. }
  525. void playlist_worker_start(PlaylistWorker* instance, const char* file_path) {
  526. furi_assert(instance);
  527. furi_assert(!instance->is_running);
  528. furi_string_set(instance->file_path, file_path);
  529. instance->is_running = true;
  530. // reset meta (current/total)
  531. playlist_meta_reset(instance->meta);
  532. FURI_LOG_D(TAG, "Starting thread...");
  533. furi_thread_start(instance->thread);
  534. }
  535. ////////////////////////////////////////////////////////////////////////////////
  536. static void render_callback(Canvas* canvas, void* ctx) {
  537. Playlist* app = ctx;
  538. furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
  539. canvas_clear(canvas);
  540. canvas_set_color(canvas, ColorBlack);
  541. canvas_set_font(canvas, FontSecondary);
  542. FuriString* temp_str;
  543. temp_str = furi_string_alloc();
  544. switch(app->meta->state) {
  545. case STATE_NONE:
  546. canvas_set_font(canvas, FontPrimary);
  547. canvas_draw_str_aligned(
  548. canvas, WIDTH / 2, HEIGHT / 2, AlignCenter, AlignCenter, "No playlist loaded");
  549. break;
  550. case STATE_OVERVIEW:
  551. // draw file name
  552. {
  553. path_extract_filename(app->file_path, temp_str, true);
  554. canvas_set_font(canvas, FontPrimary);
  555. draw_centered_boxed_str(canvas, 1, 1, 15, 6, furi_string_get_cstr(temp_str));
  556. }
  557. canvas_set_font(canvas, FontSecondary);
  558. // draw loaded count
  559. {
  560. furi_string_printf(temp_str, "%d Items in playlist", app->meta->total_count);
  561. canvas_draw_str_aligned(
  562. canvas, 1, 19, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  563. if(app->meta->playlist_repetitions <= 0) {
  564. furi_string_set(temp_str, "Repeat: inf");
  565. } else if(app->meta->playlist_repetitions == 1) {
  566. furi_string_set(temp_str, "Repeat: no");
  567. } else {
  568. furi_string_printf(temp_str, "Repeat: %dx", app->meta->playlist_repetitions);
  569. }
  570. canvas_draw_str_aligned(
  571. canvas, 1, 29, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  572. }
  573. // draw buttons
  574. draw_corner_aligned(canvas, 40, 15, AlignCenter, AlignBottom);
  575. canvas_set_color(canvas, ColorWhite);
  576. canvas_draw_str_aligned(canvas, WIDTH / 2 - 7, HEIGHT - 11, AlignLeft, AlignTop, "Start");
  577. canvas_draw_disc(canvas, WIDTH / 2 - 14, HEIGHT - 8, 3);
  578. //
  579. canvas_set_color(canvas, ColorBlack);
  580. draw_corner_aligned(canvas, 20, 15, AlignLeft, AlignBottom);
  581. canvas_set_color(canvas, ColorWhite);
  582. canvas_draw_str_aligned(canvas, 4, HEIGHT - 11, AlignLeft, AlignTop, "R-");
  583. //
  584. canvas_set_color(canvas, ColorBlack);
  585. draw_corner_aligned(canvas, 20, 15, AlignRight, AlignBottom);
  586. canvas_set_color(canvas, ColorWhite);
  587. canvas_draw_str_aligned(canvas, WIDTH - 4, HEIGHT - 11, AlignRight, AlignTop, "R+");
  588. canvas_set_color(canvas, ColorBlack);
  589. break;
  590. case STATE_SENDING:
  591. canvas_set_color(canvas, ColorBlack);
  592. if(app->worker->ctl_pause) {
  593. canvas_draw_icon(canvas, 2, HEIGHT - 8, &I_ButtonRight_4x7);
  594. } else {
  595. canvas_draw_box(canvas, 2, HEIGHT - 8, 2, 7);
  596. canvas_draw_box(canvas, 5, HEIGHT - 8, 2, 7);
  597. }
  598. // draw progress text
  599. {
  600. canvas_set_font(canvas, FontSecondary);
  601. furi_string_printf(
  602. temp_str, "[%d/%d]", app->meta->current_count, app->meta->total_count);
  603. canvas_draw_str_aligned(
  604. canvas, 11, HEIGHT - 8, AlignLeft, AlignTop, furi_string_get_cstr(temp_str));
  605. int h = canvas_string_width(canvas, furi_string_get_cstr(temp_str));
  606. int xs = 11 + h + 2;
  607. int w = WIDTH - xs - 1;
  608. canvas_draw_box(canvas, xs, HEIGHT - 5, w, 1);
  609. float progress = (float)app->meta->current_count / (float)app->meta->total_count;
  610. int wp = (int)(progress * w);
  611. canvas_draw_box(canvas, xs + wp - 1, HEIGHT - 7, 2, 5);
  612. }
  613. {
  614. if(app->meta->playlist_repetitions <= 0) {
  615. furi_string_printf(temp_str, "[%d/Inf]", app->meta->current_playlist_repetition);
  616. } else {
  617. furi_string_printf(
  618. temp_str,
  619. "[%d/%d]",
  620. app->meta->current_playlist_repetition,
  621. app->meta->playlist_repetitions);
  622. }
  623. canvas_set_color(canvas, ColorBlack);
  624. int w = canvas_string_width(canvas, furi_string_get_cstr(temp_str));
  625. draw_corner_aligned(canvas, w + 6, 13, AlignRight, AlignTop);
  626. canvas_set_color(canvas, ColorWhite);
  627. canvas_draw_str_aligned(
  628. canvas, WIDTH - 3, 3, AlignRight, AlignTop, furi_string_get_cstr(temp_str));
  629. }
  630. // draw last and current file
  631. {
  632. canvas_set_color(canvas, ColorBlack);
  633. canvas_set_font(canvas, FontSecondary);
  634. // current
  635. if(!furi_string_empty(app->meta->prev_0_text)) {
  636. int w = canvas_string_width(canvas, furi_string_get_cstr(app->meta->prev_0_text));
  637. canvas_set_color(canvas, ColorBlack);
  638. canvas_draw_rbox(canvas, 1, 1, w + 4, 12, 2);
  639. canvas_set_color(canvas, ColorWhite);
  640. canvas_draw_str_aligned(
  641. canvas,
  642. 3,
  643. 3,
  644. AlignLeft,
  645. AlignTop,
  646. furi_string_get_cstr(app->meta->prev_0_text));
  647. }
  648. // last 3
  649. canvas_set_color(canvas, ColorBlack);
  650. if(!furi_string_empty(app->meta->prev_1_text)) {
  651. canvas_draw_str_aligned(
  652. canvas,
  653. 3,
  654. 15,
  655. AlignLeft,
  656. AlignTop,
  657. furi_string_get_cstr(app->meta->prev_1_text));
  658. }
  659. if(!furi_string_empty(app->meta->prev_2_text)) {
  660. canvas_draw_str_aligned(
  661. canvas,
  662. 3,
  663. 26,
  664. AlignLeft,
  665. AlignTop,
  666. furi_string_get_cstr(app->meta->prev_2_text));
  667. }
  668. if(!furi_string_empty(app->meta->prev_3_text)) {
  669. canvas_draw_str_aligned(
  670. canvas,
  671. 3,
  672. 37,
  673. AlignLeft,
  674. AlignTop,
  675. furi_string_get_cstr(app->meta->prev_3_text));
  676. }
  677. }
  678. break;
  679. default:
  680. break;
  681. }
  682. furi_string_free(temp_str);
  683. furi_mutex_release(app->mutex);
  684. }
  685. static void input_callback(InputEvent* event, void* ctx) {
  686. Playlist* app = ctx;
  687. furi_message_queue_put(app->input_queue, event, 0);
  688. }
  689. ////////////////////////////////////////////////////////////////////////////////
  690. Playlist* playlist_alloc(DisplayMeta* meta) {
  691. Playlist* app = malloc(sizeof(Playlist));
  692. app->file_path = furi_string_alloc();
  693. furi_string_set(app->file_path, PLAYLIST_FOLDER);
  694. app->meta = meta;
  695. app->worker = NULL;
  696. app->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  697. app->input_queue = furi_message_queue_alloc(32, sizeof(InputEvent));
  698. // view port
  699. app->view_port = view_port_alloc();
  700. view_port_draw_callback_set(app->view_port, render_callback, app);
  701. view_port_input_callback_set(app->view_port, input_callback, app);
  702. // gui
  703. app->gui = furi_record_open(RECORD_GUI);
  704. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  705. return app;
  706. }
  707. void playlist_start_worker(Playlist* app, DisplayMeta* meta) {
  708. app->worker = playlist_worker_alloc(meta);
  709. // count playlist items
  710. app->meta->total_count =
  711. playlist_count_playlist_items(app->worker->storage, furi_string_get_cstr(app->file_path));
  712. // start thread
  713. playlist_worker_start(app->worker, furi_string_get_cstr(app->file_path));
  714. }
  715. void playlist_free(Playlist* app) {
  716. furi_string_free(app->file_path);
  717. gui_remove_view_port(app->gui, app->view_port);
  718. furi_record_close(RECORD_GUI);
  719. view_port_free(app->view_port);
  720. furi_message_queue_free(app->input_queue);
  721. furi_mutex_free(app->mutex);
  722. playlist_meta_free(app->meta);
  723. free(app);
  724. }
  725. int32_t playlist_app(char* p) {
  726. // create playlist folder
  727. {
  728. Storage* storage = furi_record_open(RECORD_STORAGE);
  729. if(!storage_simply_mkdir(storage, PLAYLIST_FOLDER)) {
  730. FURI_LOG_E(TAG, "Could not create folder %s", PLAYLIST_FOLDER);
  731. }
  732. furi_record_close(RECORD_STORAGE);
  733. }
  734. // create app
  735. DisplayMeta* meta = playlist_meta_alloc();
  736. Playlist* app = playlist_alloc(meta);
  737. meta->view_port = app->view_port;
  738. meta->mutex = app->mutex;
  739. furi_hal_power_suppress_charge_enter();
  740. // select playlist file
  741. if(p && strlen(p)) {
  742. furi_string_set(app->file_path, p);
  743. } else {
  744. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  745. DialogsFileBrowserOptions browser_options;
  746. dialog_file_browser_set_basic_options(&browser_options, PLAYLIST_EXT, &I_sub1_10px);
  747. browser_options.hide_ext = false;
  748. const bool res =
  749. dialog_file_browser_show(dialogs, app->file_path, app->file_path, &browser_options);
  750. furi_record_close(RECORD_DIALOGS);
  751. // check if a file was selected
  752. if(!res) {
  753. FURI_LOG_E(TAG, "No file selected");
  754. goto exit_cleanup;
  755. }
  756. }
  757. ////////////////////////////////////////////////////////////////////////////////
  758. playlist_start_worker(app, meta);
  759. meta_set_state(app->meta, STATE_OVERVIEW);
  760. bool exit_loop = false;
  761. InputEvent input;
  762. while(1) { // close application if no file was selected
  763. furi_check(
  764. furi_message_queue_get(app->input_queue, &input, FuriWaitForever) == FuriStatusOk);
  765. furi_check(furi_mutex_acquire(app->mutex, FuriWaitForever) == FuriStatusOk);
  766. switch(input.key) {
  767. case InputKeyLeft:
  768. if(app->meta->state == STATE_OVERVIEW) {
  769. if(input.type == InputTypeShort && app->meta->playlist_repetitions > 0) {
  770. --app->meta->playlist_repetitions;
  771. }
  772. } else if(app->meta->state == STATE_SENDING) {
  773. if(input.type == InputTypeShort) {
  774. app->worker->ctl_request_prev = true;
  775. }
  776. }
  777. break;
  778. case InputKeyRight:
  779. if(app->meta->state == STATE_OVERVIEW) {
  780. if(input.type == InputTypeShort) {
  781. ++app->meta->playlist_repetitions;
  782. }
  783. } else if(app->meta->state == STATE_SENDING) {
  784. if(input.type == InputTypeShort) {
  785. app->worker->ctl_request_skip = true;
  786. }
  787. }
  788. break;
  789. case InputKeyOk:
  790. if(input.type == InputTypeShort) {
  791. // toggle pause state
  792. if(!app->worker->is_running) {
  793. app->worker->ctl_pause = false;
  794. app->worker->ctl_request_exit = false;
  795. playlist_worker_start(app->worker, furi_string_get_cstr(app->file_path));
  796. } else {
  797. app->worker->ctl_pause = !app->worker->ctl_pause;
  798. }
  799. }
  800. break;
  801. case InputKeyBack:
  802. FURI_LOG_D(TAG, "Pressed Back button. Application will exit");
  803. exit_loop = true;
  804. break;
  805. default:
  806. break;
  807. }
  808. furi_mutex_release(app->mutex);
  809. // exit application
  810. if(exit_loop == true) {
  811. break;
  812. }
  813. view_port_update(app->view_port);
  814. }
  815. exit_cleanup:
  816. furi_hal_power_suppress_charge_exit();
  817. if(app->worker != NULL) {
  818. if(playlist_worker_running(app->worker)) {
  819. FURI_LOG_D(TAG, "Thread is still running. Requesting thread to finish ...");
  820. playlist_worker_stop(app->worker);
  821. }
  822. FURI_LOG_D(TAG, "Freeing Worker ...");
  823. playlist_worker_free(app->worker);
  824. }
  825. FURI_LOG_D(TAG, "Freeing Playlist ...");
  826. playlist_free(app);
  827. return 0;
  828. }