deck_input.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #include "deck_input.h"
  2. #include "triton.h"
  3. #include "ctrl_bonds.h"
  4. #include "ctrl_link.h"
  5. #include "radio.h"
  6. #include <Adafruit_TinyUSB.h>
  7. #include <Arduino.h>
  8. #include <string.h>
  9. static bool g_forwarding = false;
  10. static unsigned long g_lastInputMs = 0;
  11. static unsigned long g_lastStatusMs = 0;
  12. bool deckForwarding()
  13. {
  14. return g_forwarding;
  15. }
  16. unsigned long deckLastInputMs()
  17. {
  18. return g_lastInputMs;
  19. }
  20. // ---- little-endian payload readers ----
  21. static inline int16_t rd_s16(const uint8_t *p)
  22. {
  23. return (int16_t)((uint16_t)p[0] | ((uint16_t)p[1] << 8));
  24. }
  25. static inline uint32_t rd_u32(const uint8_t *p)
  26. {
  27. return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) |
  28. ((uint32_t)p[3] << 24);
  29. }
  30. // INPUT payload = buttons u32, lx ly rx ry s16, lt rt u8, lpx lpy rpx rpy s16, ax ay az gx gy gz s16.
  31. #define DECK_INPUT_LEN 34
  32. static void applyInput(const uint8_t *p, uint8_t n)
  33. {
  34. if (n < DECK_INPUT_LEN)
  35. return;
  36. g_in.buttons = rd_u32(p + 0);
  37. g_in.lx = rd_s16(p + 4);
  38. g_in.ly = rd_s16(p + 6);
  39. g_in.rx = rd_s16(p + 8);
  40. g_in.ry = rd_s16(p + 10);
  41. g_in.lt = p[12];
  42. g_in.rt = p[13];
  43. g_in.lpx = rd_s16(p + 14);
  44. g_in.lpy = rd_s16(p + 16);
  45. g_in.rpx = rd_s16(p + 18);
  46. g_in.rpy = rd_s16(p + 20);
  47. g_in.ax = rd_s16(p + 22);
  48. g_in.ay = rd_s16(p + 24);
  49. g_in.az = rd_s16(p + 26);
  50. g_in.gx = rd_s16(p + 28);
  51. g_in.gy = rd_s16(p + 30);
  52. g_in.gz = rd_s16(p + 32);
  53. g_lastInputMs = millis();
  54. }
  55. static void handleFrame(uint8_t type, const uint8_t *p, uint8_t n)
  56. {
  57. switch (type) {
  58. case DECK_T_INPUT:
  59. applyInput(p, n);
  60. break;
  61. case DECK_T_CONTROL:
  62. if (n >= 2 && p[0] == 0x01)
  63. g_forwarding = (p[1] != 0);
  64. break;
  65. case DECK_T_REQ_STATUS:
  66. g_lastStatusMs = 0; // force an immediate status emit
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. // ---- RX state machine: resync on the 2-byte sync word ----
  73. void deckInputPoll()
  74. {
  75. // state: 0 wait sync0, 1 wait sync1, 2 type, 3 len, 4 payload, 5 sum
  76. static uint8_t st = 0;
  77. static uint8_t type = 0, len = 0, idx = 0, sum = 0;
  78. static uint8_t buf[64];
  79. while (Serial.available()) {
  80. uint8_t c = (uint8_t)Serial.read();
  81. switch (st) {
  82. case 0:
  83. if (c == DECK_SYNC0)
  84. st = 1;
  85. break;
  86. case 1:
  87. st = (c == DECK_SYNC1) ? 2 : (c == DECK_SYNC0 ? 1 : 0);
  88. break;
  89. case 2:
  90. type = c;
  91. sum = c;
  92. st = 3;
  93. break;
  94. case 3:
  95. len = c;
  96. sum += c;
  97. idx = 0;
  98. if (len > sizeof buf) { // oversized -> drop, resync
  99. st = 0;
  100. break;
  101. }
  102. st = (len == 0) ? 5 : 4;
  103. break;
  104. case 4:
  105. buf[idx++] = c;
  106. sum += c;
  107. if (idx >= len)
  108. st = 5;
  109. break;
  110. case 5:
  111. if (c == sum)
  112. handleFrame(type, buf, len);
  113. st = 0;
  114. break;
  115. }
  116. }
  117. }
  118. // ---- frame TX ----
  119. static void sendFrame(uint8_t type, const uint8_t *p, uint8_t n)
  120. {
  121. // need room for the whole frame or skip (never block the RF loop on CDC backpressure)
  122. if ((int)Serial.availableForWrite() < n + 5)
  123. return;
  124. uint8_t hdr[4] = { DECK_SYNC0, DECK_SYNC1, type, n };
  125. uint8_t sum = (uint8_t)(type + n);
  126. for (uint8_t i = 0; i < n; i++)
  127. sum += p[i];
  128. Serial.write(hdr, 4);
  129. if (n)
  130. Serial.write(p, n);
  131. Serial.write(&sum, 1);
  132. }
  133. void deckText(const char *s)
  134. {
  135. size_t n = strlen(s);
  136. if (n > 60)
  137. n = 60;
  138. sendFrame(DECK_T_TEXT, (const uint8_t *)s, (uint8_t)n);
  139. }
  140. // STATUS payload: [flags][linkSlot][sessCh][nbond] + per-bond [used][alive][puuid4][iuuid4][serial16]
  141. void deckStatusTask()
  142. {
  143. if (millis() - g_lastStatusMs < 250)
  144. return;
  145. g_lastStatusMs = millis();
  146. uint8_t p[4 + NBOND * 26];
  147. memset(p, 0, sizeof p);
  148. bool linkUp = ctrlLinkUp();
  149. p[0] = (uint8_t)((g_forwarding ? 1 : 0) | (linkUp ? 2 : 0));
  150. p[1] = (g_linkSlot >= 0) ? (uint8_t)g_linkSlot : 0xFF;
  151. p[2] = g_sessCh;
  152. p[3] = NBOND;
  153. uint8_t *q = p + 4;
  154. for (int i = 0; i < NBOND; i++) {
  155. CtrlBond &b = g_bond[i];
  156. q[0] = b.used ? 1 : 0;
  157. q[1] = (b.used && i == g_linkSlot && linkUp) ? 1 : 0;
  158. memcpy(q + 2, b.rec + 0, 4); // proteus_uuid
  159. memcpy(q + 6, b.rec + 4, 4); // ibex_uuid
  160. memcpy(q + 10, b.rec + 8, 16); // puck serial
  161. q += 26;
  162. }
  163. sendFrame(DECK_T_STATUS, p, sizeof p);
  164. }