virtual_portal.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #include "virtual_portal.h"
  2. #include <furi_hal.h>
  3. #include <stm32wbxx_ll_dma.h>
  4. #include "g72x.h"
  5. #include "string.h"
  6. #include "wav_player_hal.h"
  7. #define TAG "VirtualPortal"
  8. #define BLOCK_SIZE 16
  9. #define PORTAL_SIDE_RING 0
  10. #define PORTAL_SIDE_RIGHT 0
  11. #define PORTAL_SIDE_TRAP 1
  12. #define PORTAL_SIDE_LEFT 2
  13. const NotificationSequence sequence_set_backlight = {
  14. &message_display_backlight_on,
  15. &message_do_not_reset,
  16. NULL,
  17. };
  18. const NotificationSequence sequence_set_leds = {
  19. &message_red_0,
  20. &message_blue_0,
  21. &message_green_0,
  22. &message_do_not_reset,
  23. NULL,
  24. };
  25. static float lerp(float start, float end, float t) {
  26. return start + (end - start) * t;
  27. }
  28. static void wav_player_dma_isr(void* ctx) {
  29. VirtualPortal* virtual_portal = (VirtualPortal*)ctx;
  30. // half of transfer
  31. if (LL_DMA_IsActiveFlag_HT1(DMA1)) {
  32. LL_DMA_ClearFlag_HT1(DMA1);
  33. if (virtual_portal->count < SAMPLES_COUNT / 2) {
  34. for (int i = 0; i < SAMPLES_COUNT / 2; i++) {
  35. virtual_portal->audio_buffer[i] = 0;
  36. }
  37. return;
  38. }
  39. // fill first half of buffer
  40. for (int i = 0; i < SAMPLES_COUNT / 2; i++) {
  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->count < SAMPLES_COUNT / 2) {
  52. for (int i = SAMPLES_COUNT / 2; i < SAMPLES_COUNT; i++) {
  53. virtual_portal->audio_buffer[i] = 0;
  54. }
  55. return;
  56. }
  57. // fill second half of buffer
  58. for (int i = SAMPLES_COUNT / 2; i < SAMPLES_COUNT; i++) {
  59. virtual_portal->audio_buffer[i] = *virtual_portal->tail;
  60. if (++virtual_portal->tail == virtual_portal->end) {
  61. virtual_portal->tail = virtual_portal->current_audio_buffer;
  62. }
  63. virtual_portal->count--;
  64. }
  65. }
  66. }
  67. void virtual_portal_tick(void* ctx) {
  68. VirtualPortal* virtual_portal = (VirtualPortal*)ctx;
  69. (void)virtual_portal;
  70. VirtualPortalLed* led = &virtual_portal->right;
  71. if (!led->running) {
  72. return;
  73. }
  74. uint32_t elapsed = furi_get_tick() - led->start_time;
  75. if (elapsed < led->delay) {
  76. float t_phase = fminf((float)elapsed / (float)led->delay, 1);
  77. if (led->two_phase) {
  78. if (led->current_phase == 0) {
  79. // Phase 1: Increase channels that need to go up, hold others constant
  80. if (led->target_r > led->last_r) {
  81. led->r = lerp(led->last_r, led->target_r, t_phase);
  82. }
  83. if (led->target_g > led->last_g) {
  84. led->g = lerp(led->last_g, led->target_g, t_phase);
  85. }
  86. if (led->target_b > led->last_b) {
  87. led->b = lerp(led->last_b, led->target_b, t_phase);
  88. }
  89. } else {
  90. // Phase 2: Decrease channels that need to go down
  91. if (led->target_r < led->last_r) {
  92. led->r = lerp(led->last_r, led->target_r, t_phase);
  93. }
  94. if (led->target_g < led->last_g) {
  95. led->g = lerp(led->last_g, led->target_g, t_phase);
  96. }
  97. if (led->target_b < led->last_b) {
  98. led->b = lerp(led->last_b, led->target_b, t_phase);
  99. }
  100. }
  101. } else {
  102. // Simple one-phase transition: all channels change together
  103. led->r = lerp(led->last_r, led->target_r, t_phase);
  104. led->g = lerp(led->last_g, led->target_g, t_phase);
  105. led->b = lerp(led->last_b, led->target_b, t_phase);
  106. }
  107. furi_hal_light_set(LightRed, led->r);
  108. furi_hal_light_set(LightGreen, led->g);
  109. furi_hal_light_set(LightBlue, led->b);
  110. } else if (led->two_phase && led->current_phase == 0) {
  111. // Move to phase 2 - save the current state as our "last" values for phase 2
  112. led->last_r = led->r;
  113. led->last_g = led->g;
  114. led->last_b = led->b;
  115. led->start_time = furi_get_tick();
  116. led->current_phase++;
  117. } else {
  118. // Transition complete - set final values
  119. led->r = led->target_r;
  120. led->g = led->target_g;
  121. led->b = led->target_b;
  122. furi_hal_light_set(LightRed, led->r);
  123. furi_hal_light_set(LightGreen, led->g);
  124. furi_hal_light_set(LightBlue, led->b);
  125. led->running = false;
  126. }
  127. }
  128. void queue_led_command(VirtualPortal* virtual_portal, int side, uint8_t r, uint8_t g, uint8_t b, uint16_t duration) {
  129. VirtualPortalLed* led = &virtual_portal->left;
  130. switch (side) {
  131. case PORTAL_SIDE_RIGHT:
  132. led = &virtual_portal->right;
  133. break;
  134. case PORTAL_SIDE_TRAP:
  135. led = &virtual_portal->trap;
  136. break;
  137. case PORTAL_SIDE_LEFT:
  138. led = &virtual_portal->left;
  139. break;
  140. }
  141. // Store current values as last values
  142. led->last_r = led->r;
  143. led->last_g = led->g;
  144. led->last_b = led->b;
  145. // Set target values
  146. led->target_r = r;
  147. led->target_g = g;
  148. led->target_b = b;
  149. if (duration) {
  150. // Determine if we need a two-phase transition
  151. bool increasing = (r > led->last_r) || (g > led->last_g) || (b > led->last_b);
  152. bool decreasing = (r < led->last_r) || (g < led->last_g) || (b < led->last_b);
  153. led->two_phase = increasing && decreasing;
  154. // Set up transition parameters
  155. led->start_time = furi_get_tick();
  156. if (led->two_phase) {
  157. // If two-phase, each phase gets half the duration
  158. led->delay = duration / 2;
  159. } else {
  160. led->delay = duration;
  161. }
  162. // Start in phase 0
  163. led->current_phase = 0;
  164. led->running = true;
  165. } else {
  166. // Immediate change, no transition
  167. if (side == PORTAL_SIDE_RIGHT) {
  168. led->r = r;
  169. led->g = g;
  170. led->b = b;
  171. furi_hal_light_set(LightRed, r);
  172. furi_hal_light_set(LightGreen, g);
  173. furi_hal_light_set(LightBlue, b);
  174. }
  175. led->running = false;
  176. }
  177. }
  178. VirtualPortal* virtual_portal_alloc(NotificationApp* notifications) {
  179. VirtualPortal* virtual_portal = malloc(sizeof(VirtualPortal));
  180. virtual_portal->notifications = notifications;
  181. notification_message(virtual_portal->notifications, &sequence_set_backlight);
  182. notification_message(virtual_portal->notifications, &sequence_set_leds);
  183. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  184. virtual_portal->tokens[i] = pof_token_alloc();
  185. }
  186. virtual_portal->sequence_number = 0;
  187. virtual_portal->active = false;
  188. virtual_portal->volume = 20.0f;
  189. virtual_portal->led_timer = furi_timer_alloc(virtual_portal_tick,
  190. FuriTimerTypePeriodic, virtual_portal);
  191. virtual_portal->head = virtual_portal->current_audio_buffer;
  192. virtual_portal->tail = virtual_portal->current_audio_buffer;
  193. virtual_portal->end = &virtual_portal->current_audio_buffer[SAMPLES_COUNT_BUFFERED];
  194. furi_timer_start(virtual_portal->led_timer, 10);
  195. return virtual_portal;
  196. }
  197. void virtual_portal_set_type(VirtualPortal* virtual_portal, PoFType type) {
  198. virtual_portal->type = type;
  199. if (furi_hal_speaker_acquire(1000)) {
  200. wav_player_speaker_init(virtual_portal->type == PoFHid ? 8000 : 4000);
  201. wav_player_dma_init((uint32_t)virtual_portal->audio_buffer, SAMPLES_COUNT);
  202. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, wav_player_dma_isr, virtual_portal);
  203. wav_player_speaker_start();
  204. wav_player_dma_start();
  205. }
  206. }
  207. void virtual_portal_cleanup(VirtualPortal* virtual_portal) {
  208. notification_message(virtual_portal->notifications, &sequence_reset_rgb);
  209. notification_message(virtual_portal->notifications, &sequence_display_backlight_on);
  210. }
  211. void virtual_portal_free(VirtualPortal* virtual_portal) {
  212. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  213. pof_token_free(virtual_portal->tokens[i]);
  214. virtual_portal->tokens[i] = NULL;
  215. }
  216. furi_timer_stop(virtual_portal->led_timer);
  217. furi_timer_free(virtual_portal->led_timer);
  218. if (furi_hal_speaker_is_mine()) {
  219. furi_hal_speaker_release();
  220. wav_player_speaker_stop();
  221. wav_player_dma_stop();
  222. }
  223. wav_player_hal_deinit();
  224. furi_hal_interrupt_set_isr(FuriHalInterruptIdDma1Ch1, NULL, NULL);
  225. free(virtual_portal);
  226. }
  227. void virtual_portal_set_leds(uint8_t r, uint8_t g, uint8_t b) {
  228. furi_hal_light_set(LightRed, r);
  229. furi_hal_light_set(LightGreen, g);
  230. furi_hal_light_set(LightBlue, b);
  231. }
  232. void virtual_portal_set_backlight(uint8_t brightness) {
  233. furi_hal_light_set(LightBacklight, brightness);
  234. }
  235. void virtual_portal_load_token(VirtualPortal* virtual_portal, PoFToken* pof_token) {
  236. furi_assert(pof_token);
  237. FURI_LOG_D(TAG, "virtual_portal_load_token");
  238. PoFToken* target = NULL;
  239. uint8_t empty[4] = {0, 0, 0, 0};
  240. // first try to "reload" to the same slot it used before based on UID
  241. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  242. if (memcmp(virtual_portal->tokens[i]->UID, pof_token->UID, sizeof(pof_token->UID)) == 0) {
  243. // Found match
  244. if (virtual_portal->tokens[i]->loaded) {
  245. // already loaded, no-op
  246. return;
  247. } else {
  248. FURI_LOG_D(TAG, "Found matching UID at index %d", i);
  249. target = virtual_portal->tokens[i];
  250. break;
  251. }
  252. }
  253. }
  254. // otherwise load into first slot with no set UID
  255. if (target == NULL) {
  256. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  257. if (memcmp(virtual_portal->tokens[i]->UID, empty, sizeof(empty)) == 0) {
  258. FURI_LOG_D(TAG, "Found empty UID at index %d", i);
  259. // By definition an empty UID slot would not be loaded, so I'm not checking. Fight me.
  260. target = virtual_portal->tokens[i];
  261. break;
  262. }
  263. }
  264. }
  265. // Re-use first unloaded slot
  266. if (target == NULL) {
  267. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  268. if (virtual_portal->tokens[i]->loaded == false) {
  269. FURI_LOG_D(TAG, "Re-using previously used slot %d", i);
  270. target = virtual_portal->tokens[i];
  271. break;
  272. }
  273. }
  274. }
  275. if (target == NULL) {
  276. FURI_LOG_W(TAG, "Failed to find slot to token into");
  277. return;
  278. }
  279. furi_assert(target);
  280. // TODO: make pof_token_copy()
  281. target->change = pof_token->change;
  282. target->loaded = pof_token->loaded;
  283. memcpy(target->dev_name, pof_token->dev_name, sizeof(pof_token->dev_name));
  284. memcpy(target->UID, pof_token->UID, sizeof(pof_token->UID));
  285. furi_string_set(target->load_path, pof_token->load_path);
  286. const NfcDeviceData* data = nfc_device_get_data(pof_token->nfc_device, NfcProtocolMfClassic);
  287. nfc_device_set_data(target->nfc_device, NfcProtocolMfClassic, data);
  288. }
  289. uint8_t virtual_portal_next_sequence(VirtualPortal* virtual_portal) {
  290. if (virtual_portal->sequence_number == 0xff) {
  291. virtual_portal->sequence_number = 0;
  292. }
  293. return virtual_portal->sequence_number++;
  294. }
  295. int virtual_portal_activate(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  296. FURI_LOG_D(TAG, "process %c", message[0]);
  297. virtual_portal->active = message[1] != 0;
  298. response[0] = message[0];
  299. response[1] = message[1];
  300. response[2] = 0xFF;
  301. response[3] = 0x77;
  302. return 4;
  303. }
  304. int virtual_portal_reset(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  305. FURI_LOG_D(TAG, "process %c", message[0]);
  306. virtual_portal->active = false;
  307. // virtual_portal->sequence_number = 0;
  308. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  309. if (virtual_portal->tokens[i]->loaded) {
  310. virtual_portal->tokens[i]->change = true;
  311. }
  312. }
  313. uint8_t index = 0;
  314. response[index++] = 'R';
  315. response[index++] = 0x02; // Trap Team Xbox One
  316. response[index++] = 0x27; // Trap Team Xbox One
  317. // response[index++] = 0x02; // Swap Force 3DS
  318. // response[index++] = 0x02; // Swap Force 3DS
  319. return index;
  320. }
  321. int virtual_portal_status(VirtualPortal* virtual_portal, uint8_t* response) {
  322. response[0] = 'S';
  323. bool update = false;
  324. for (size_t i = 0; i < POF_TOKEN_LIMIT; i++) {
  325. // Can't use bit_lib since it uses the opposite endian
  326. if (virtual_portal->tokens[i]->loaded) {
  327. response[1 + i / 4] |= 1 << ((i % 4) * 2 + 0);
  328. }
  329. if (virtual_portal->tokens[i]->change) {
  330. update = true;
  331. response[1 + i / 4] |= 1 << ((i % 4) * 2 + 1);
  332. }
  333. virtual_portal->tokens[i]->change = false;
  334. }
  335. response[5] = virtual_portal_next_sequence(virtual_portal);
  336. response[6] = 1;
  337. // Let me know when a status that actually has a change is sent
  338. if (update) {
  339. char display[33] = {0};
  340. memset(display, 0, sizeof(display));
  341. for (size_t i = 0; i < BLOCK_SIZE; i++) {
  342. snprintf(display + (i * 2), sizeof(display), "%02x", response[i]);
  343. }
  344. FURI_LOG_I(TAG, "> S %s", display);
  345. }
  346. return 7;
  347. }
  348. int virtual_portal_send_status(VirtualPortal* virtual_portal, uint8_t* response) {
  349. if (virtual_portal->active) {
  350. return virtual_portal_status(virtual_portal, response);
  351. }
  352. return 0;
  353. }
  354. // 4d01ff0000d0077d6c2a77a400000000
  355. int virtual_portal_m(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  356. virtual_portal->speaker = (message[1] == 1);
  357. /*
  358. char display[33] = {0};
  359. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  360. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  361. }
  362. FURI_LOG_I(TAG, "M %s", display);
  363. */
  364. size_t index = 0;
  365. response[index++] = 'M';
  366. response[index++] = message[1];
  367. response[index++] = 0x00;
  368. response[index++] = 0x19;
  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)ulaw2linear(message[i]);
  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. }
  518. }
  519. // 32 byte message, 32 byte response;
  520. int virtual_portal_process_message(
  521. VirtualPortal* virtual_portal,
  522. uint8_t* message,
  523. uint8_t* response) {
  524. memset(response, 0, 32);
  525. switch (message[0]) {
  526. case 'A':
  527. return virtual_portal_activate(virtual_portal, message, response);
  528. case 'C': // Ring color R G B
  529. queue_led_command(virtual_portal, PORTAL_SIDE_RING, message[1], message[2], message[3], 0);
  530. return 0;
  531. case 'J':
  532. // https://github.com/flyandi/flipper_zero_rgb_led
  533. return virtual_portal_j(virtual_portal, message, response);
  534. case 'L':
  535. return virtual_portal_l(virtual_portal, message);
  536. case 'M':
  537. return virtual_portal_m(virtual_portal, message, response);
  538. case 'Q': // Query
  539. return virtual_portal_query(virtual_portal, message, response);
  540. case 'R':
  541. return virtual_portal_reset(virtual_portal, message, response);
  542. case 'S': // Status
  543. return virtual_portal_status(virtual_portal, response);
  544. case 'V':
  545. return 0;
  546. case 'W': // Write
  547. return virtual_portal_write(virtual_portal, message, response);
  548. case 'Z':
  549. return 0;
  550. default:
  551. FURI_LOG_W(TAG, "Unhandled command %c", message[0]);
  552. return 0; // No response
  553. }
  554. return 0;
  555. }