virtual_portal.c 21 KB

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