virtual_portal.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. #include "virtual_portal.h"
  2. #define TAG "VirtualPortal"
  3. #define BLOCK_SIZE 16
  4. #define PORTAL_SIDE_RING 0
  5. #define PORTAL_SIDE_RIGHT 0
  6. #define PORTAL_SIDE_TRAP 1
  7. #define PORTAL_SIDE_LEFT 2
  8. const NotificationSequence sequence_set_backlight = {
  9. &message_display_backlight_on,
  10. &message_do_not_reset,
  11. NULL,
  12. };
  13. const NotificationSequence sequence_set_leds = {
  14. &message_red_0,
  15. &message_blue_0,
  16. &message_green_0,
  17. &message_do_not_reset,
  18. NULL,
  19. };
  20. static float lerp(float start, float end, float t) {
  21. return start + (end - start) * t;
  22. }
  23. void virtual_portal_tick(void* ctx) {
  24. VirtualPortal* virtual_portal = (VirtualPortal*)ctx;
  25. (void)virtual_portal;
  26. VirtualPortalLed* led = &virtual_portal->right;
  27. if (!led->running) {
  28. return;
  29. }
  30. uint32_t elapsed = furi_get_tick() - led->start_time;
  31. if (elapsed < led->delay) {
  32. float t_phase = fminf((float)elapsed / (float)led->delay, 1);
  33. if (led->two_phase) {
  34. if (led->current_phase == 0) {
  35. // Phase 1: Increase channels that need to go up, hold others constant
  36. if (led->target_r > led->last_r) {
  37. led->r = lerp(led->last_r, led->target_r, t_phase);
  38. }
  39. if (led->target_g > led->last_g) {
  40. led->g = lerp(led->last_g, led->target_g, t_phase);
  41. }
  42. if (led->target_b > led->last_b) {
  43. led->b = lerp(led->last_b, led->target_b, t_phase);
  44. }
  45. } else {
  46. // Phase 2: Decrease channels that need to go down
  47. if (led->target_r < led->last_r) {
  48. led->r = lerp(led->last_r, led->target_r, t_phase);
  49. }
  50. if (led->target_g < led->last_g) {
  51. led->g = lerp(led->last_g, led->target_g, t_phase);
  52. }
  53. if (led->target_b < led->last_b) {
  54. led->b = lerp(led->last_b, led->target_b, t_phase);
  55. }
  56. }
  57. } else {
  58. // Simple one-phase transition: all channels change together
  59. led->r = lerp(led->last_r, led->target_r, t_phase);
  60. led->g = lerp(led->last_g, led->target_g, t_phase);
  61. led->b = lerp(led->last_b, led->target_b, t_phase);
  62. }
  63. furi_hal_light_set(LightRed, led->r);
  64. furi_hal_light_set(LightGreen, led->g);
  65. furi_hal_light_set(LightBlue, led->b);
  66. } else if (led->two_phase && led->current_phase == 0) {
  67. // Move to phase 2 - save the current state as our "last" values for phase 2
  68. led->last_r = led->r;
  69. led->last_g = led->g;
  70. led->last_b = led->b;
  71. led->start_time = furi_get_tick();
  72. led->current_phase++;
  73. } else {
  74. // Transition complete - set final values
  75. led->r = led->target_r;
  76. led->g = led->target_g;
  77. led->b = led->target_b;
  78. furi_hal_light_set(LightRed, led->r);
  79. furi_hal_light_set(LightGreen, led->g);
  80. furi_hal_light_set(LightBlue, led->b);
  81. led->running = false;
  82. }
  83. }
  84. void queue_led_command(VirtualPortal* virtual_portal, int side, uint8_t r, uint8_t g, uint8_t b, uint16_t duration) {
  85. VirtualPortalLed* led = &virtual_portal->left;
  86. switch (side) {
  87. case PORTAL_SIDE_RIGHT:
  88. led = &virtual_portal->right;
  89. break;
  90. case PORTAL_SIDE_TRAP:
  91. led = &virtual_portal->trap;
  92. break;
  93. case PORTAL_SIDE_LEFT:
  94. led = &virtual_portal->left;
  95. break;
  96. }
  97. // Store current values as last values
  98. led->last_r = led->r;
  99. led->last_g = led->g;
  100. led->last_b = led->b;
  101. // Set target values
  102. led->target_r = r;
  103. led->target_g = g;
  104. led->target_b = b;
  105. if (duration) {
  106. // Determine if we need a two-phase transition
  107. bool increasing = (r > led->last_r) || (g > led->last_g) || (b > led->last_b);
  108. bool decreasing = (r < led->last_r) || (g < led->last_g) || (b < led->last_b);
  109. led->two_phase = increasing && decreasing;
  110. // Set up transition parameters
  111. led->start_time = furi_get_tick();
  112. if (led->two_phase) {
  113. // If two-phase, each phase gets half the duration
  114. led->delay = duration / 2;
  115. } else {
  116. led->delay = duration;
  117. }
  118. // Start in phase 0
  119. led->current_phase = 0;
  120. led->running = true;
  121. } else {
  122. // Immediate change, no transition
  123. if (side == PORTAL_SIDE_RIGHT) {
  124. led->r = r;
  125. led->g = g;
  126. led->b = b;
  127. furi_hal_light_set(LightRed, r);
  128. furi_hal_light_set(LightGreen, g);
  129. furi_hal_light_set(LightBlue, b);
  130. }
  131. led->running = false;
  132. }
  133. }
  134. VirtualPortal* virtual_portal_alloc(NotificationApp* notifications) {
  135. VirtualPortal* virtual_portal = malloc(sizeof(VirtualPortal));
  136. virtual_portal->notifications = notifications;
  137. notification_message(virtual_portal->notifications, &sequence_set_backlight);
  138. notification_message(virtual_portal->notifications, &sequence_set_leds);
  139. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  140. virtual_portal->tokens[i] = pof_token_alloc();
  141. }
  142. virtual_portal->sequence_number = 0;
  143. virtual_portal->active = false;
  144. virtual_portal->led_timer = furi_timer_alloc(virtual_portal_tick,
  145. FuriTimerTypePeriodic, virtual_portal);
  146. furi_timer_start(virtual_portal->led_timer, 10);
  147. return virtual_portal;
  148. }
  149. void virtual_portal_cleanup(VirtualPortal* virtual_portal) {
  150. notification_message(virtual_portal->notifications, &sequence_reset_rgb);
  151. notification_message(virtual_portal->notifications, &sequence_display_backlight_on);
  152. }
  153. void virtual_portal_free(VirtualPortal* virtual_portal) {
  154. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  155. pof_token_free(virtual_portal->tokens[i]);
  156. virtual_portal->tokens[i] = NULL;
  157. }
  158. furi_timer_stop(virtual_portal->led_timer);
  159. furi_timer_free(virtual_portal->led_timer);
  160. free(virtual_portal);
  161. }
  162. void virtaul_portal_set_leds(uint8_t r, uint8_t g, uint8_t b) {
  163. furi_hal_light_set(LightRed, r);
  164. furi_hal_light_set(LightGreen, g);
  165. furi_hal_light_set(LightBlue, b);
  166. }
  167. void virtaul_portal_set_backlight(uint8_t brightness) {
  168. furi_hal_light_set(LightBacklight, brightness);
  169. }
  170. void virtual_portal_load_token(VirtualPortal* virtual_portal, PoFToken* pof_token) {
  171. furi_assert(pof_token);
  172. FURI_LOG_D(TAG, "virtual_portal_load_token");
  173. PoFToken* target = NULL;
  174. uint8_t empty[4] = {0, 0, 0, 0};
  175. // first try to "reload" to the same slot it used before based on UID
  176. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  177. if (memcmp(virtual_portal->tokens[i]->UID, pof_token->UID, sizeof(pof_token->UID)) == 0) {
  178. // Found match
  179. if (virtual_portal->tokens[i]->loaded) {
  180. // already loaded, no-op
  181. return;
  182. } else {
  183. FURI_LOG_D(TAG, "Found matching UID at index %d", i);
  184. target = virtual_portal->tokens[i];
  185. break;
  186. }
  187. }
  188. }
  189. // otherwise load into first slot with no set UID
  190. if (target == NULL) {
  191. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  192. if (memcmp(virtual_portal->tokens[i]->UID, empty, sizeof(empty)) == 0) {
  193. FURI_LOG_D(TAG, "Found empty UID at index %d", i);
  194. // By definition an empty UID slot would not be loaded, so I'm not checking. Fight me.
  195. target = virtual_portal->tokens[i];
  196. break;
  197. }
  198. }
  199. }
  200. // Re-use first unloaded slot
  201. if (target == NULL) {
  202. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  203. if (virtual_portal->tokens[i]->loaded == false) {
  204. FURI_LOG_D(TAG, "Re-using previously used slot %d", i);
  205. target = virtual_portal->tokens[i];
  206. break;
  207. }
  208. }
  209. }
  210. if (target == NULL) {
  211. FURI_LOG_W(TAG, "Failed to find slot to token into");
  212. return;
  213. }
  214. furi_assert(target);
  215. // TODO: make pof_token_copy()
  216. target->change = pof_token->change;
  217. target->loaded = pof_token->loaded;
  218. memcpy(target->dev_name, pof_token->dev_name, sizeof(pof_token->dev_name));
  219. memcpy(target->UID, pof_token->UID, sizeof(pof_token->UID));
  220. const NfcDeviceData* data = nfc_device_get_data(pof_token->nfc_device, NfcProtocolMfClassic);
  221. nfc_device_set_data(target->nfc_device, NfcProtocolMfClassic, data);
  222. }
  223. uint8_t virtual_portal_next_sequence(VirtualPortal* virtual_portal) {
  224. if (virtual_portal->sequence_number == 0xff) {
  225. virtual_portal->sequence_number = 0;
  226. }
  227. return virtual_portal->sequence_number++;
  228. }
  229. int virtual_portal_activate(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  230. FURI_LOG_D(TAG, "process %c", message[0]);
  231. virtual_portal->active = message[1] != 0;
  232. response[0] = message[0];
  233. response[1] = message[1];
  234. response[2] = 0xFF;
  235. response[3] = 0x77;
  236. return 4;
  237. }
  238. int virtual_portal_reset(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  239. FURI_LOG_D(TAG, "process %c", message[0]);
  240. virtual_portal->active = false;
  241. // virtual_portal->sequence_number = 0;
  242. for (int i = 0; i < POF_TOKEN_LIMIT; i++) {
  243. if (virtual_portal->tokens[i]->loaded) {
  244. virtual_portal->tokens[i]->change = true;
  245. }
  246. }
  247. uint8_t index = 0;
  248. response[index++] = 'R';
  249. response[index++] = 0x02;
  250. response[index++] = 0x1B;
  251. // response[index++] = 0x0a;
  252. // response[index++] = 0x03;
  253. // response[index++] = 0x02;
  254. // https://github.com/tresni/PoweredPortals/wiki/USB-Protocols
  255. // Wii Wireless: 01 29 00 00
  256. // Wii Wired: 02 0a 03 02 (Giants: works)
  257. // Arduboy: 02 19 (Trap team: works)
  258. return index;
  259. }
  260. int virtual_portal_status(VirtualPortal* virtual_portal, uint8_t* response) {
  261. response[0] = 'S';
  262. bool update = false;
  263. for (size_t i = 0; i < POF_TOKEN_LIMIT; i++) {
  264. // Can't use bit_lib since it uses the opposite endian
  265. if (virtual_portal->tokens[i]->loaded) {
  266. response[1 + i / 4] |= 1 << ((i % 4) * 2 + 0);
  267. }
  268. if (virtual_portal->tokens[i]->change) {
  269. update = true;
  270. response[1 + i / 4] |= 1 << ((i % 4) * 2 + 1);
  271. }
  272. virtual_portal->tokens[i]->change = false;
  273. }
  274. response[5] = virtual_portal_next_sequence(virtual_portal);
  275. response[6] = 1;
  276. // Let me know when a status that actually has a change is sent
  277. if (update) {
  278. char display[33] = {0};
  279. memset(display, 0, sizeof(display));
  280. for (size_t i = 0; i < BLOCK_SIZE; i++) {
  281. snprintf(display + (i * 2), sizeof(display), "%02x", response[i]);
  282. }
  283. FURI_LOG_I(TAG, "> S %s", display);
  284. }
  285. return 7;
  286. }
  287. int virtual_portal_send_status(VirtualPortal* virtual_portal, uint8_t* response) {
  288. if (virtual_portal->active) {
  289. return virtual_portal_status(virtual_portal, response);
  290. }
  291. return 0;
  292. }
  293. // 4d01ff0000d0077d6c2a77a400000000
  294. int virtual_portal_m(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  295. virtual_portal->speaker = (message[1] == 1);
  296. /*
  297. char display[33] = {0};
  298. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  299. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  300. }
  301. FURI_LOG_I(TAG, "M %s", display);
  302. */
  303. size_t index = 0;
  304. response[index++] = 'M';
  305. response[index++] = message[1];
  306. response[index++] = 0x00;
  307. response[index++] = 0x19;
  308. return index;
  309. }
  310. int virtual_portal_l(VirtualPortal* virtual_portal, uint8_t* message) {
  311. UNUSED(virtual_portal);
  312. /*
  313. char display[33] = {0};
  314. memset(display, 0, sizeof(display));
  315. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  316. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  317. }
  318. FURI_LOG_I(TAG, "L %s", display);
  319. */
  320. uint8_t side = message[1]; // 0: left, 2: right
  321. uint8_t brightness = 0;
  322. switch (side) {
  323. case 0:
  324. case 2:
  325. queue_led_command(virtual_portal, side, message[2], message[3], message[4], 0);
  326. break;
  327. case 1:
  328. brightness = message[2];
  329. virtaul_portal_set_backlight(brightness);
  330. break;
  331. case 3:
  332. brightness = 0xff;
  333. virtaul_portal_set_backlight(brightness);
  334. break;
  335. }
  336. return 0;
  337. }
  338. int virtual_portal_j(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  339. /*
  340. char display[33] = {0};
  341. memset(display, 0, sizeof(display));
  342. for(size_t i = 0; i < BLOCK_SIZE; i++) {
  343. snprintf(display + (i * 2), sizeof(display), "%02x", message[i]);
  344. }
  345. FURI_LOG_I(TAG, "J %s", display);
  346. */
  347. uint8_t side = message[1];
  348. uint16_t delay = message[6] << 8 | message[5];
  349. queue_led_command(virtual_portal, side, message[2], message[3], message[4], delay);
  350. // Delay response
  351. // furi_delay_ms(delay); // causes issues
  352. // UNUSED(delay);
  353. // https://marijnkneppers.dev/posts/reverse-engineering-skylanders-toys-to-life-mechanics/
  354. size_t index = 0;
  355. response[index++] = 'J';
  356. return index;
  357. }
  358. int virtual_portal_query(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  359. int index = message[1];
  360. int blockNum = message[2];
  361. int arrayIndex = index & 0x0f;
  362. FURI_LOG_I(TAG, "Query %d %d", arrayIndex, blockNum);
  363. PoFToken* pof_token = virtual_portal->tokens[arrayIndex];
  364. if (!pof_token->loaded) {
  365. response[0] = 'Q';
  366. response[1] = 0x00 | arrayIndex;
  367. response[2] = blockNum;
  368. return 3;
  369. }
  370. NfcDevice* nfc_device = pof_token->nfc_device;
  371. const MfClassicData* data = nfc_device_get_data(nfc_device, NfcProtocolMfClassic);
  372. const MfClassicBlock block = data->block[blockNum];
  373. response[0] = 'Q';
  374. response[1] = 0x10 | arrayIndex;
  375. response[2] = blockNum;
  376. memcpy(response + 3, block.data, BLOCK_SIZE);
  377. return 3 + BLOCK_SIZE;
  378. }
  379. int virtual_portal_write(VirtualPortal* virtual_portal, uint8_t* message, uint8_t* response) {
  380. int index = message[1];
  381. int blockNum = message[2];
  382. int arrayIndex = index & 0x0f;
  383. char display[33] = {0};
  384. for (size_t i = 0; i < BLOCK_SIZE; i++) {
  385. snprintf(display + (i * 2), sizeof(display), "%02x", message[3 + i]);
  386. }
  387. FURI_LOG_I(TAG, "Write %d %d %s", arrayIndex, blockNum, display);
  388. PoFToken* pof_token = virtual_portal->tokens[arrayIndex];
  389. if (!pof_token->loaded) {
  390. response[0] = 'W';
  391. response[1] = 0x00 | arrayIndex;
  392. response[2] = blockNum;
  393. return 3;
  394. }
  395. NfcDevice* nfc_device = pof_token->nfc_device;
  396. MfClassicData* data = mf_classic_alloc();
  397. nfc_device_copy_data(nfc_device, NfcProtocolMfClassic, data);
  398. MfClassicBlock* block = &data->block[blockNum];
  399. memcpy(block->data, message + 3, BLOCK_SIZE);
  400. nfc_device_set_data(nfc_device, NfcProtocolMfClassic, data);
  401. mf_classic_free(data);
  402. response[0] = 'W';
  403. response[1] = 0x10 | arrayIndex;
  404. response[2] = blockNum;
  405. return 3;
  406. }
  407. // 32 byte message, 32 byte response;
  408. int virtual_portal_process_message(
  409. VirtualPortal* virtual_portal,
  410. uint8_t* message,
  411. uint8_t* response) {
  412. memset(response, 0, 32);
  413. switch (message[0]) {
  414. case 'A':
  415. return virtual_portal_activate(virtual_portal, message, response);
  416. case 'C': // Ring color R G B
  417. queue_led_command(virtual_portal, PORTAL_SIDE_RING, message[1], message[2], message[3], 0);
  418. return 0;
  419. case 'J':
  420. // https://github.com/flyandi/flipper_zero_rgb_led
  421. return virtual_portal_j(virtual_portal, message, response);
  422. case 'L':
  423. return virtual_portal_l(virtual_portal, message);
  424. case 'M':
  425. return virtual_portal_m(virtual_portal, message, response);
  426. case 'Q': // Query
  427. return virtual_portal_query(virtual_portal, message, response);
  428. case 'R':
  429. return virtual_portal_reset(virtual_portal, message, response);
  430. case 'S': // Status
  431. return virtual_portal_status(virtual_portal, response);
  432. case 'V':
  433. return 0;
  434. case 'W': // Write
  435. return virtual_portal_write(virtual_portal, message, response);
  436. case 'Z':
  437. return 0;
  438. default:
  439. FURI_LOG_W(TAG, "Unhandled command %c", message[0]);
  440. return 0; // No response
  441. }
  442. return 0;
  443. }