virtual_portal.c 22 KB

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