virtual_portal.c 22 KB

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