subghz_file_encoder_worker.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "subghz_file_encoder_worker.h"
  2. #include <stream_buffer.h>
  3. #include "file-worker.h"
  4. #define SUBGHZ_FILE_ENCODER_LOAD 512
  5. struct SubGhzFileEncoderWorker {
  6. FuriThread* thread;
  7. StreamBufferHandle_t stream;
  8. FileWorker* file_worker;
  9. volatile bool worker_running;
  10. bool level;
  11. int16_t duration;
  12. string_t str_data;
  13. string_t file_path;
  14. };
  15. void subghz_file_encoder_worker_add_livel_duration(
  16. SubGhzFileEncoderWorker* instance,
  17. int16_t duration) {
  18. bool res = true;
  19. if(duration < 0 && !instance->level) {
  20. instance->duration += duration;
  21. res = false;
  22. } else if(duration > 0 && instance->level) {
  23. instance->duration += duration;
  24. res = false;
  25. } else if(duration == 0) {
  26. instance->duration = 0;
  27. }
  28. if(res) {
  29. instance->level = !instance->level;
  30. instance->duration += duration;
  31. xStreamBufferSend(instance->stream, &instance->duration, sizeof(int16_t), 10);
  32. instance->duration = 0;
  33. }
  34. }
  35. bool subghz_file_encoder_worker_data_parse(
  36. SubGhzFileEncoderWorker* instance,
  37. const char* strStart,
  38. size_t len) {
  39. char* str1;
  40. size_t ind_start = (size_t)strStart; //store the start address of the beginning of the line
  41. bool res = false;
  42. str1 = strstr(
  43. strStart, "RAW_Data: "); //looking for the beginning of the desired title in the line
  44. if(str1 != NULL) {
  45. str1 = strchr(
  46. str1,
  47. ' '); //if found, shift the pointer by 1 element per line "RAW_Data: -1, 2, -2..."
  48. subghz_file_encoder_worker_add_livel_duration(instance, atoi(str1));
  49. while(
  50. strchr(str1, ',') != NULL &&
  51. ((size_t)str1 <
  52. (len +
  53. ind_start))) { //check that there is still an element in the line and that it has not gone beyond the line
  54. str1 = strchr(str1, ',');
  55. str1 += 2; //if found, shift the pointer by next element per line
  56. subghz_file_encoder_worker_add_livel_duration(instance, atoi(str1));
  57. }
  58. res = true;
  59. }
  60. return res;
  61. }
  62. LevelDuration subghz_file_encoder_worker_get_level_duration(void* context) {
  63. furi_assert(context);
  64. SubGhzFileEncoderWorker* instance = context;
  65. int16_t duration;
  66. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  67. int ret = xStreamBufferReceiveFromISR(
  68. instance->stream, &duration, sizeof(int16_t), &xHigherPriorityTaskWoken);
  69. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  70. if(ret == sizeof(int16_t)) {
  71. LevelDuration level_duration = {.level = LEVEL_DURATION_RESET};
  72. if(duration < 0) {
  73. level_duration = level_duration_make(false, duration * -1);
  74. } else if(duration > 0) {
  75. level_duration = level_duration_make(true, duration);
  76. } else if(duration == 0) {
  77. level_duration = level_duration_reset();
  78. FURI_LOG_I("SubGhzFileEncoderWorker", "Stop transmission");
  79. }
  80. return level_duration;
  81. } else {
  82. FURI_LOG_E("SubGhzFileEncoderWorker", "Slow flash read");
  83. return level_duration_wait();
  84. }
  85. }
  86. /** Worker thread
  87. *
  88. * @param context
  89. * @return exit code
  90. */
  91. static int32_t subghz_file_encoder_worker_thread(void* context) {
  92. SubGhzFileEncoderWorker* instance = context;
  93. FURI_LOG_I("SubGhzFileEncoderWorker", "Worker start");
  94. bool res = false;
  95. do {
  96. if(!file_worker_open(
  97. instance->file_worker,
  98. string_get_cstr(instance->file_path),
  99. FSAM_READ,
  100. FSOM_OPEN_EXISTING)) {
  101. break;
  102. }
  103. //todo skips 3 lines file header
  104. if(!file_worker_read_until(instance->file_worker, instance->str_data, '\n')) {
  105. break;
  106. }
  107. if(!file_worker_read_until(instance->file_worker, instance->str_data, '\n')) {
  108. break;
  109. }
  110. if(!file_worker_read_until(instance->file_worker, instance->str_data, '\n')) {
  111. break;
  112. }
  113. res = true;
  114. FURI_LOG_I("SubGhzFileEncoderWorker", "Start transmission");
  115. } while(0);
  116. while(res && instance->worker_running) {
  117. size_t stream_free_byte = xStreamBufferSpacesAvailable(instance->stream);
  118. if((stream_free_byte / sizeof(int16_t)) >= SUBGHZ_FILE_ENCODER_LOAD) {
  119. if(file_worker_read_until(instance->file_worker, instance->str_data, '\n')) {
  120. if(!subghz_file_encoder_worker_data_parse(
  121. instance,
  122. string_get_cstr(instance->str_data),
  123. strlen(string_get_cstr(instance->str_data)))) {
  124. //to stop DMA correctly
  125. subghz_file_encoder_worker_add_livel_duration(instance, LEVEL_DURATION_RESET);
  126. subghz_file_encoder_worker_add_livel_duration(instance, LEVEL_DURATION_RESET);
  127. break;
  128. }
  129. } else {
  130. subghz_file_encoder_worker_add_livel_duration(instance, LEVEL_DURATION_RESET);
  131. subghz_file_encoder_worker_add_livel_duration(instance, LEVEL_DURATION_RESET);
  132. break;
  133. }
  134. }
  135. }
  136. //waiting for the end of the transfer
  137. while(instance->worker_running) {
  138. osDelay(50);
  139. }
  140. file_worker_close(instance->file_worker);
  141. FURI_LOG_I("SubGhzFileEncoderWorker", "Worker stop");
  142. return 0;
  143. }
  144. SubGhzFileEncoderWorker* subghz_file_encoder_worker_alloc() {
  145. SubGhzFileEncoderWorker* instance = furi_alloc(sizeof(SubGhzFileEncoderWorker));
  146. instance->thread = furi_thread_alloc();
  147. furi_thread_set_name(instance->thread, "subghz_file_encoder_worker");
  148. furi_thread_set_stack_size(instance->thread, 2048);
  149. furi_thread_set_context(instance->thread, instance);
  150. furi_thread_set_callback(instance->thread, subghz_file_encoder_worker_thread);
  151. instance->stream = xStreamBufferCreate(sizeof(int16_t) * 4096, sizeof(int16_t));
  152. instance->file_worker = file_worker_alloc(false);
  153. string_init(instance->str_data);
  154. string_init(instance->file_path);
  155. instance->level = false;
  156. return instance;
  157. }
  158. void subghz_file_encoder_worker_free(SubGhzFileEncoderWorker* instance) {
  159. furi_assert(instance);
  160. vStreamBufferDelete(instance->stream);
  161. furi_thread_free(instance->thread);
  162. string_clear(instance->str_data);
  163. string_clear(instance->file_path);
  164. file_worker_free(instance->file_worker);
  165. free(instance);
  166. }
  167. bool subghz_file_encoder_worker_start(SubGhzFileEncoderWorker* instance, const char* file_path) {
  168. furi_assert(instance);
  169. furi_assert(!instance->worker_running);
  170. xStreamBufferReset(instance->stream);
  171. string_set(instance->file_path, file_path);
  172. instance->worker_running = true;
  173. bool res = furi_thread_start(instance->thread);
  174. return res;
  175. }
  176. void subghz_file_encoder_worker_stop(SubGhzFileEncoderWorker* instance) {
  177. furi_assert(instance);
  178. furi_assert(instance->worker_running);
  179. instance->worker_running = false;
  180. furi_thread_join(instance->thread);
  181. }
  182. bool subghz_file_encoder_worker_is_running(SubGhzFileEncoderWorker* instance) {
  183. furi_assert(instance);
  184. return instance->worker_running;
  185. }