virtual_portal.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. #include "virtual_portal.h"
  2. #include <furi_hal.h>
  3. #include <stm32wbxx_ll_dma.h>
  4. #include "string.h"
  5. #include "audio/wav_player_hal.h"
  6. #define TAG "VirtualPortal"
  7. #define BLOCK_SIZE 16
  8. #define PORTAL_SIDE_RING 0
  9. #define PORTAL_SIDE_RIGHT 0
  10. #define PORTAL_SIDE_TRAP 1
  11. #define PORTAL_SIDE_LEFT 2
  12. const NotificationSequence sequence_set_backlight = {
  13. &message_display_backlight_on,
  14. &message_do_not_reset,
  15. NULL,
  16. };
  17. const NotificationSequence sequence_set_leds = {
  18. &message_red_0,
  19. &message_blue_0,
  20. &message_green_0,
  21. &message_do_not_reset,
  22. NULL,
  23. };
  24. static float lerp(float start, float end, float t) {
  25. return start + (end - start) * t;
  26. }
  27. static void wav_player_dma_isr(void* ctx) {
  28. VirtualPortal* virtual_portal = (VirtualPortal*)ctx;
  29. // half of transfer
  30. if (LL_DMA_IsActiveFlag_HT1(DMA1)) {
  31. LL_DMA_ClearFlag_HT1(DMA1);
  32. if (!virtual_portal->playing_audio) {
  33. return;
  34. }
  35. // fill first half of buffer
  36. for (int i = 0; i < SAMPLES_COUNT / 2; i++) {
  37. if (!virtual_portal->count) {
  38. virtual_portal->audio_buffer[i] = 0;
  39. continue;
  40. }
  41. virtual_portal->audio_buffer[i] = *virtual_portal->tail;
  42. if (++virtual_portal->tail == virtual_portal->end) {
  43. virtual_portal->tail = virtual_portal->current_audio_buffer;
  44. }
  45. virtual_portal->count--;
  46. }
  47. }
  48. // transfer complete
  49. if (LL_DMA_IsActiveFlag_TC1(DMA1)) {
  50. LL_DMA_ClearFlag_TC1(DMA1);
  51. if (!virtual_portal->playing_audio) {
  52. return;
  53. }
  54. // fill second half of buffer
  55. for (int i = SAMPLES_COUNT / 2; i < SAMPLES_COUNT; i++) {
  56. if (!virtual_portal->count) {
  57. virtual_portal->audio_buffer[i] = 0;
  58. continue;
  59. }
  60. virtual_portal->audio_buffer[i] = *virtual_portal->tail;
  61. if (++virtual_portal->tail == virtual_portal->end) {
  62. virtual_portal->tail = virtual_portal->current_audio_buffer;
  63. }
  64. virtual_portal->count--;
  65. }
  66. }
  67. }
  68. void virtual_portal_tick(void* ctx) {
  69. VirtualPortal* virtual_portal = (VirtualPortal*)ctx;
  70. (void)virtual_portal;
  71. VirtualPortalLed* led = &virtual_portal->right;
  72. if (!led->running) {
  73. return;
  74. }
  75. uint32_t elapsed = furi_get_tick() - led->start_time;
  76. if (elapsed < led->delay) {
  77. float t_phase = fminf((float)elapsed / (float)led->delay, 1);
  78. if (led->two_phase) {
  79. if (led->current_phase == 0) {
  80. // Phase 1: Increase channels that need to go up, hold others constant
  81. if (led->target_r > led->last_r) {
  82. led->r = lerp(led->last_r, led->target_r, t_phase);
  83. }
  84. if (led->target_g > led->last_g) {
  85. led->g = lerp(led->last_g, led->target_g, t_phase);
  86. }
  87. if (led->target_b > led->last_b) {
  88. led->b = lerp(led->last_b, led->target_b, t_phase);
  89. }
  90. } else {
  91. // Phase 2: Decrease channels that need to go down
  92. if (led->target_r < led->last_r) {
  93. led->r = lerp(led->last_r, led->target_r, t_phase);
  94. }
  95. if (led->target_g < led->last_g) {
  96. led->g = lerp(led->last_g, led->target_g, t_phase);
  97. }
  98. if (led->target_b < led->last_b) {
  99. led->b = lerp(led->last_b, led->target_b, t_phase);
  100. }
  101. }
  102. } else {
  103. // Simple one-phase transition: all channels change together
  104. led->r = lerp(led->last_r, led->target_r, t_phase);
  105. led->g = lerp(led->last_g, led->target_g, t_phase);
  106. led->b = lerp(led->last_b, led->target_b, t_phase);
  107. }
  108. furi_hal_light_set(LightRed, led->r);
  109. furi_hal_light_set(LightGreen, led->g);
  110. furi_hal_light_set(LightBlue, led->b);
  111. } else if (led->two_phase && led->current_phase == 0) {
  112. // Move to phase 2 - save the current state as our "last" values for phase 2
  113. led->last_r = led->r;
  114. led->last_g = led->g;
  115. led->last_b = led->b;
  116. led->start_time = furi_get_tick();
  117. led->current_phase++;
  118. } else {
  119. // Transition complete - set final values
  120. led->r = led->target_r;
  121. led->g = led->target_g;
  122. led->b = led->target_b;
  123. furi_hal_light_set(LightRed, led->r);
  124. furi_hal_light_set(LightGreen, led->g);
  125. furi_hal_light_set(LightBlue, led->b);
  126. led->running = false;
  127. }
  128. }
  129. void queue_led_command(VirtualPortal* virtual_portal, int side, uint8_t r, uint8_t g, uint8_t b, uint16_t duration) {
  130. VirtualPortalLed* led = &virtual_portal->left;
  131. switch (side) {
  132. case PORTAL_SIDE_RIGHT:
  133. led = &virtual_portal->right;
  134. break;
  135. case PORTAL_SIDE_TRAP:
  136. led = &virtual_portal->trap;
  137. break;
  138. case PORTAL_SIDE_LEFT:
  139. led = &virtual_portal->left;
  140. break;
  141. }
  142. // Store current values as last values
  143. led->last_r = led->r;
  144. led->last_g = led->g;
  145. led->last_b = led->b;
  146. // Set target values
  147. led->target_r = r;
  148. led->target_g = g;
  149. led->target_b = b;
  150. if (duration) {
  151. // Determine if we need a two-phase transition
  152. bool increasing = (r > led->last_r) || (g > led->last_g) || (b > led->last_b);
  153. bool decreasing = (r < led->last_r) || (g < led->last_g) || (b < led->last_b);
  154. led->two_phase = increasing && decreasing;
  155. // Set up transition parameters
  156. led->start_time = furi_get_tick();
  157. if (led->two_phase) {
  158. // If two-phase, each phase gets half the duration
  159. led->delay = duration / 2;
  160. } else {
  161. led->delay = duration;
  162. }
  163. // Start in phase 0
  164. led->current_phase = 0;
  165. led->running = true;
  166. } else {
  167. // Immediate change, no transition
  168. if (side == PORTAL_SIDE_RIGHT) {
  169. led->r = r;
  170. led->g = g;
  171. led->b = b;
  172. furi_hal_light_set(LightRed, r);
  173. furi_hal_light_set(LightGreen, g);
  174. furi_hal_light_set(LightBlue, b);
  175. }
  176. led->running = false;
  177. }
  178. }
  179. VirtualPortal* virtual_portal_alloc(NotificationApp* notifications) {
  180. VirtualPortal* virtual_portal = malloc(sizeof(VirtualPortal));
  181. virtual_portal->notifications = notifications;
  182. notification_message(virtual_portal->notifications, &sequence_set_backlight);
  183. notification_message(virtual_portal->notifications, &sequence_set_leds);
  184. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  185. virtual_portal->tokens[i] = pof_token_alloc();
  186. }
  187. virtual_portal->sequence_number = 0;
  188. virtual_portal->active = false;
  189. virtual_portal->volume = 20.0f;
  190. virtual_portal->led_timer = furi_timer_alloc(virtual_portal_tick,
  191. FuriTimerTypePeriodic, virtual_portal);
  192. virtual_portal->head = virtual_portal->current_audio_buffer;
  193. virtual_portal->tail = virtual_portal->current_audio_buffer;
  194. virtual_portal->end = &virtual_portal->current_audio_buffer[SAMPLES_COUNT_BUFFERED];
  195. furi_timer_start(virtual_portal->led_timer, 10);
  196. if (furi_hal_speaker_acquire(1000)) {
  197. wav_player_speaker_init(8000);
  198. wav_player_dma_init((uint32_t)virtual_portal->audio_buffer, SAMPLES_COUNT);
  199. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, wav_player_dma_isr, virtual_portal);
  200. wav_player_speaker_start();
  201. wav_player_dma_start();
  202. }
  203. return virtual_portal;
  204. }
  205. void virtual_portal_set_type(VirtualPortal* virtual_portal, PoFType type) {
  206. virtual_portal->type = type;
  207. }
  208. void virtual_portal_cleanup(VirtualPortal* virtual_portal) {
  209. notification_message(virtual_portal->notifications, &sequence_reset_rgb);
  210. notification_message(virtual_portal->notifications, &sequence_display_backlight_on);
  211. }
  212. void virtual_portal_free(VirtualPortal* virtual_portal) {
  213. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  214. pof_token_free(virtual_portal->tokens[i]);
  215. virtual_portal->tokens[i] = NULL;
  216. }
  217. furi_timer_stop(virtual_portal->led_timer);
  218. furi_timer_free(virtual_portal->led_timer);
  219. if (furi_hal_speaker_is_mine()) {
  220. furi_hal_speaker_release();
  221. wav_player_speaker_stop();
  222. wav_player_dma_stop();
  223. }
  224. wav_player_hal_deinit();
  225. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, NULL, NULL);
  226. free(virtual_portal);
  227. }
  228. void virtual_portal_set_leds(uint8_t r, uint8_t g, uint8_t b) {
  229. furi_hal_light_set(LightRed, r);
  230. furi_hal_light_set(LightGreen, g);
  231. furi_hal_light_set(LightBlue, b);
  232. }
  233. void virtual_portal_set_backlight(uint8_t brightness) {
  234. furi_hal_light_set(LightBacklight, brightness);
  235. }
  236. void virtual_portal_load_token(VirtualPortal* virtual_portal, PoFToken* pof_token) {
  237. furi_assert(pof_token);
  238. FURI_LOG_D(TAG, "virtual_portal_load_token");
  239. PoFToken* target = NULL;
  240. uint8_t empty[4] = {0, 0, 0, 0};
  241. // first try to "reload" to the same slot it used before based on UID
  242. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  243. if (memcmp(virtual_portal->tokens[i]->UID, pof_token->UID, sizeof(pof_token->UID)) == 0) {
  244. // Found match
  245. if (virtual_portal->tokens[i]->loaded) {
  246. // already loaded, no-op
  247. return;
  248. } else {
  249. FURI_LOG_D(TAG, "Found matching UID at index %d", i);
  250. target = virtual_portal->tokens[i];
  251. break;
  252. }
  253. }
  254. }
  255. // otherwise load into first slot with no set UID
  256. if (target == NULL) {
  257. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  258. if (memcmp(virtual_portal->tokens[i]->UID, empty, sizeof(empty)) == 0) {
  259. FURI_LOG_D(TAG, "Found empty UID at index %d", i);
  260. // By definition an empty UID slot would not be loaded, so I'm not checking. Fight me.
  261. target = virtual_portal->tokens[i];
  262. break;
  263. }
  264. }
  265. }
  266. // Re-use first unloaded slot
  267. if (target == NULL) {
  268. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  269. if (virtual_portal->tokens[i]->loaded == false) {
  270. FURI_LOG_D(TAG, "Re-using previously used slot %d", i);
  271. target = virtual_portal->tokens[i];
  272. break;
  273. }
  274. }
  275. }
  276. if (target == NULL) {
  277. FURI_LOG_W(TAG, "Failed to find slot to token into");
  278. return;
  279. }
  280. furi_assert(target);
  281. // TODO: make pof_token_copy()
  282. target->change = pof_token->change;
  283. target->loaded = pof_token->loaded;
  284. memcpy(target->dev_name, pof_token->dev_name, sizeof(pof_token->dev_name));
  285. memcpy(target->UID, pof_token->UID, sizeof(pof_token->UID));
  286. furi_string_set(target->load_path, pof_token->load_path);
  287. const NfcDeviceData* data = nfc_device_get_data(pof_token->nfc_device, NfcProtocolMfClassic);
  288. nfc_device_set_data(target->nfc_device, NfcProtocolMfClassic, data);
  289. }
  290. uint8_t virtual_portal_next_sequence(VirtualPortal* virtual_portal) {
  291. if (virtual_portal->sequence_number == 0xff) {
  292. virtual_portal->sequence_number = 0;
  293. }
  294. return virtual_portal->sequence_number++;
  295. }
  296. int virtual_portal_activate(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  297. FURI_LOG_D(TAG, "process %c", message[0]);
  298. virtual_portal->active = message[1] != 0;
  299. response[0] = message[0];
  300. response[1] = message[1];
  301. response[2] = 0xFF;
  302. response[3] = 0x77;
  303. return 4;
  304. }
  305. int virtual_portal_reset(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  306. FURI_LOG_D(TAG, "process %c", message[0]);
  307. virtual_portal->active = false;
  308. // virtual_portal->sequence_number = 0;
  309. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  310. if (virtual_portal->tokens[i]->loaded) {
  311. virtual_portal->tokens[i]->change = true;
  312. }
  313. }
  314. uint8_t index = 0;
  315. response[index++] = 'R';
  316. response[index++] = 0x02; // Trap Team Xbox One
  317. response[index++] = 0x27; // Trap Team Xbox One
  318. // response[index++] = 0x02; // Swap Force 3DS
  319. // response[index++] = 0x02; // Swap Force 3DS
  320. return index;
  321. }
  322. int virtual_portal_status(VirtualPortal* virtual_portal, uint8_t* response) {
  323. response[0] = 'S';
  324. bool update = false;
  325. for (size_t i = 0; i < POF_TOKEN_LIMIT; i++) {
  326. // Can't use bit_lib since it uses the opposite endian
  327. if (virtual_portal->tokens[i]->loaded) {
  328. response[1 + i / 4] |= 1 << ((i % 4) * 2 + 0);
  329. }
  330. if (virtual_portal->tokens[i]->change) {
  331. update = true;
  332. response[1 + i / 4] |= 1 << ((i % 4) * 2 + 1);
  333. }
  334. virtual_portal->tokens[i]->change = false;
  335. }
  336. response[5] = virtual_portal_next_sequence(virtual_portal);
  337. response[6] = 1;
  338. // Let me know when a status that actually has a change is sent
  339. if (update) {
  340. char display[33] = {0};
  341. memset(display, 0, sizeof(display));
  342. for (size_t i = 0; i < BLOCK_SIZE; i++) {
  343. snprintf(display + (i * 2), sizeof(display), "%02x", response[i]);
  344. }
  345. FURI_LOG_I(TAG, "> S %s", display);
  346. }
  347. return 7;
  348. }
  349. int virtual_portal_send_status(VirtualPortal* virtual_portal, uint8_t* response) {
  350. if (virtual_portal->active) {
  351. return virtual_portal_status(virtual_portal, response);
  352. }
  353. return 0;
  354. }
  355. // 4d01ff0000d0077d6c2a77a400000000
  356. int virtual_portal_m(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  357. // Activate speaker for any non-zero value in the range 01-FF
  358. virtual_portal->speaker = (message[1] != 0);
  359. virtual_portal->count = 0;
  360. virtual_portal->head = virtual_portal->tail = virtual_portal->audio_buffer;
  361. virtual_portal->playing_audio = false;
  362. if (virtual_portal->speaker) {
  363. wav_player_speaker_start();
  364. } else {
  365. wav_player_speaker_stop();
  366. }
  367. /*
  368. char display[33] = {0};
  369. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  370. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  371. }
  372. FURI_LOG_I(TAG, "M %s", display);
  373. */
  374. size_t index = 0;
  375. response[index++] = 'M';
  376. // Always respond with 01 if active, 00 if not
  377. response[index++] = virtual_portal->speaker ? 0x01 : 0x00;
  378. response[index++] = 0x00;
  379. response[index++] = virtual_portal->m;
  380. g72x_init_state(&virtual_portal->state);
  381. return index;
  382. }
  383. int virtual_portal_l(VirtualPortal* virtual_portal, uint8_t* message) {
  384. UNUSED(virtual_portal);
  385. /*
  386. char display[33] = {0};
  387. memset(display, 0, sizeof(display));
  388. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  389. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  390. }
  391. FURI_LOG_I(TAG, "L %s", display);
  392. */
  393. uint8_t side = message[1]; // 0: left, 2: right
  394. uint8_t brightness = 0;
  395. switch (side) {
  396. case 0:
  397. case 2:
  398. queue_led_command(virtual_portal, side, message[2], message[3], message[4], 0);
  399. break;
  400. case 1:
  401. brightness = message[2];
  402. virtual_portal_set_backlight(brightness);
  403. break;
  404. case 3:
  405. brightness = 0xff;
  406. virtual_portal_set_backlight(brightness);
  407. break;
  408. }
  409. return 0;
  410. }
  411. int virtual_portal_j(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  412. /*
  413. char display[33] = {0};
  414. memset(display, 0, sizeof(display));
  415. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  416. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  417. }
  418. FURI_LOG_I(TAG, "J %s", display);
  419. */
  420. uint8_t side = message[1];
  421. uint16_t delay = message[6] << 8 | message[5];
  422. queue_led_command(virtual_portal, side, message[2], message[3], message[4], delay);
  423. // Delay response
  424. // furi_delay_ms(delay); // causes issues
  425. // UNUSED(delay);
  426. // https://marijnkneppers.dev/posts/reverse-engineering-skylanders-toys-to-life-mechanics/
  427. size_t index = 0;
  428. response[index++] = 'J';
  429. return index;
  430. }
  431. int virtual_portal_query(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  432. int index = message[1];
  433. int blockNum = message[2];
  434. int arrayIndex = index & 0x0f;
  435. FURI_LOG_I(TAG, "Query %d %d", arrayIndex, blockNum);
  436. PoFToken* pof_token = virtual_portal->tokens[arrayIndex];
  437. if (!pof_token->loaded) {
  438. response[0] = 'Q';
  439. response[1] = 0x00 | arrayIndex;
  440. response[2] = blockNum;
  441. return 3;
  442. }
  443. NfcDevice* nfc_device = pof_token->nfc_device;
  444. const MfClassicData* data = nfc_device_get_data(nfc_device, NfcProtocolMfClassic);
  445. const MfClassicBlock block = data->block[blockNum];
  446. response[0] = 'Q';
  447. response[1] = 0x10 | arrayIndex;
  448. response[2] = blockNum;
  449. memcpy(response + 3, block.data, BLOCK_SIZE);
  450. return 3 + BLOCK_SIZE;
  451. }
  452. int virtual_portal_write(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  453. int index = message[1];
  454. int blockNum = message[2];
  455. int arrayIndex = index & 0x0f;
  456. char display[33] = {0};
  457. for (size_t i = 0; i < BLOCK_SIZE; i++) {
  458. snprintf(display + (i * 2), sizeof(display), "%02x", message[3 + i]);
  459. }
  460. FURI_LOG_I(TAG, "Write %d %d %s", arrayIndex, blockNum, display);
  461. PoFToken* pof_token = virtual_portal->tokens[arrayIndex];
  462. if (!pof_token->loaded) {
  463. response[0] = 'W';
  464. response[1] = 0x00 | arrayIndex;
  465. response[2] = blockNum;
  466. return 3;
  467. }
  468. NfcDevice* nfc_device = pof_token->nfc_device;
  469. MfClassicData* data = mf_classic_alloc();
  470. nfc_device_copy_data(nfc_device, NfcProtocolMfClassic, data);
  471. MfClassicBlock* block = &data->block[blockNum];
  472. memcpy(block->data, message + 3, BLOCK_SIZE);
  473. nfc_device_set_data(nfc_device, NfcProtocolMfClassic, data);
  474. mf_classic_free(data);
  475. nfc_device_save(nfc_device, furi_string_get_cstr(pof_token->load_path));
  476. response[0] = 'W';
  477. response[1] = 0x10 | arrayIndex;
  478. response[2] = blockNum;
  479. return 3;
  480. }
  481. // HID portals use send 8000hz 16 bit signed PCM samples
  482. void virtual_portal_process_audio(
  483. VirtualPortal* virtual_portal,
  484. uint8_t* message,
  485. uint8_t len) {
  486. for (size_t i = 0; i < len; i += 2) {
  487. int16_t int_16 =
  488. (((int16_t)message[i + 1] << 8) + ((int16_t)message[i]));
  489. float data = ((float)int_16 / 256.0);
  490. data /= UINT8_MAX / 2; // scale -1..1
  491. data *= virtual_portal->volume; // volume
  492. data = tanhf(data); // hyperbolic tangent limiter
  493. data *= UINT8_MAX / 2; // scale -128..127
  494. data += UINT8_MAX / 2; // to unsigned
  495. if (data < 0) {
  496. data = 0;
  497. }
  498. if (data > 255) {
  499. data = 255;
  500. }
  501. *virtual_portal->head = data;
  502. virtual_portal->count++;
  503. if (++virtual_portal->head == virtual_portal->end) {
  504. virtual_portal->head = virtual_portal->current_audio_buffer;
  505. }
  506. if (virtual_portal->count > SAMPLES_COUNT) {
  507. virtual_portal->playing_audio = true;
  508. }
  509. }
  510. }
  511. // 360 portals didn't have the bandwith, so they use CCITT G.721 ADPCM coding
  512. // to encode the audio so it uses less bandwith.
  513. void virtual_portal_process_audio_360(
  514. VirtualPortal* virtual_portal,
  515. uint8_t* message,
  516. uint8_t len) {
  517. for (size_t i = 0; i < len; i++) {
  518. int16_t int_16 = (int16_t)g721_decoder(message[i], &virtual_portal->state);
  519. float data = ((float)int_16 / 256.0);
  520. data /= UINT8_MAX / 2; // scale -1..1
  521. data *= virtual_portal->volume; // volume
  522. data = tanhf(data); // hyperbolic tangent limiter
  523. data *= UINT8_MAX / 2; // scale -128..127
  524. data += UINT8_MAX / 2; // to unsigned
  525. if (data < 0) {
  526. data = 0;
  527. }
  528. if (data > 255) {
  529. data = 255;
  530. }
  531. *virtual_portal->head = data;
  532. virtual_portal->count++;
  533. if (++virtual_portal->head == virtual_portal->end) {
  534. virtual_portal->head = virtual_portal->current_audio_buffer;
  535. }
  536. int_16 = (int16_t)g721_decoder(message[i] >> 4, &virtual_portal->state);
  537. data = ((float)int_16 / 256.0);
  538. data /= UINT8_MAX / 2; // scale -1..1
  539. data *= virtual_portal->volume; // volume
  540. data = tanhf(data); // hyperbolic tangent limiter
  541. data *= UINT8_MAX / 2; // scale -128..127
  542. data += UINT8_MAX / 2; // to unsigned
  543. if (data < 0) {
  544. data = 0;
  545. }
  546. if (data > 255) {
  547. data = 255;
  548. }
  549. *virtual_portal->head = data;
  550. virtual_portal->count++;
  551. if (++virtual_portal->head == virtual_portal->end) {
  552. virtual_portal->head = virtual_portal->current_audio_buffer;
  553. }
  554. if (virtual_portal->count > SAMPLES_COUNT) {
  555. virtual_portal->playing_audio = true;
  556. }
  557. }
  558. }
  559. // 32 byte message, 32 byte response;
  560. int virtual_portal_process_message(
  561. VirtualPortal* virtual_portal,
  562. uint8_t* message,
  563. uint8_t* response) {
  564. memset(response, 0, 32);
  565. switch (message[0]) {
  566. case 'A':
  567. return virtual_portal_activate(virtual_portal, message, response);
  568. case 'C': // Ring color R G B
  569. queue_led_command(virtual_portal, PORTAL_SIDE_RING, message[1], message[2], message[3], 0);
  570. return 0;
  571. case 'J':
  572. // https://github.com/flyandi/flipper_zero_rgb_led
  573. return virtual_portal_j(virtual_portal, message, response);
  574. case 'L':
  575. return virtual_portal_l(virtual_portal, message);
  576. case 'M':
  577. return virtual_portal_m(virtual_portal, message, response);
  578. case 'Q': // Query
  579. if (!virtual_portal->active) {
  580. return 0; // No response if portal is not active
  581. }
  582. return virtual_portal_query(virtual_portal, message, response);
  583. case 'R':
  584. return virtual_portal_reset(virtual_portal, message, response);
  585. case 'S': // Status
  586. if (!virtual_portal->active) {
  587. return 0; // No response if portal is not active
  588. }
  589. return virtual_portal_status(virtual_portal, response);
  590. case 'V':
  591. virtual_portal->m = message[3];
  592. return 0;
  593. case 'W': // Write
  594. if (!virtual_portal->active) {
  595. return 0; // No response if portal is not active
  596. }
  597. return virtual_portal_write(virtual_portal, message, response);
  598. case 'Z':
  599. return 0;
  600. default:
  601. FURI_LOG_W(TAG, "Unhandled command %c", message[0]);
  602. return 0; // No response
  603. }
  604. return 0;
  605. }