xiaoxiang781216 commented on code in PR #3869:
URL: https://github.com/apache/nuttx-apps/pull/3869#discussion_r4307787456
##########
games/NXDoom/src/i_input.c:
##########
@@ -362,6 +378,246 @@ static int get_typed_char(uint32_t sym)
#ifdef CONFIG_GAMES_NXDOOM_KEYBOARD
+#ifdef CONFIG_GAMES_NXDOOM_UARTKBD
+
+/****************************************************************************
+ * Name: uartkbd_now
+ *
+ * Description:
+ * Return the time in milliseconds.
+ *
+ ****************************************************************************/
+
+static uint64_t uartkbd_now(void)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
Review Comment:
remove the cast
##########
games/NXDoom/src/i_input.c:
##########
@@ -63,6 +64,21 @@ struct keyboard_dev g_kbd_dev =
};
#endif
+#ifdef CONFIG_GAMES_NXDOOM_UARTKBD
+# define UARTKBD_MAXHELD 8
+# define UARTKBD_HOLDMS 250 /* Release a key this long after its last byte */
+
+struct uartkbd_held
+{
+ bool active;
+ uint32_t type;
+ uint32_t code;
+ uint64_t deadline;
+};
+
+static struct uartkbd_held g_held[UARTKBD_MAXHELD];
+#endif
+
#if 0
static const int g_scancode_translate_table[] = SCANCODE_TO_KEYS_ARRAY;
Review Comment:
remove the dead code
##########
games/NXDoom/src/i_input.c:
##########
@@ -362,6 +378,246 @@ static int get_typed_char(uint32_t sym)
#ifdef CONFIG_GAMES_NXDOOM_KEYBOARD
+#ifdef CONFIG_GAMES_NXDOOM_UARTKBD
+
+/****************************************************************************
+ * Name: uartkbd_now
+ *
+ * Description:
+ * Return the time in milliseconds.
+ *
+ ****************************************************************************/
+
+static uint64_t uartkbd_now(void)
+{
+ struct timespec ts;
+
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ return (uint64_t)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
+}
+
+/****************************************************************************
+ * Name: uartkbd_hold
+ *
+ * Description:
+ * A serial terminal reports a key press only, never its release, so a
+ * press is turned into a press event now and a release event once the
+ * terminal has stopped repeating the key for UARTKBD_HOLDMS. Returns
+ * true when the key was not already held, so that the caller emits the
+ * press. A key that finds no free slot is ignored, because its release
+ * could never be reported.
+ *
+ ****************************************************************************/
+
+static bool uartkbd_hold(uint32_t type, uint32_t code)
+{
+ uint64_t now = uartkbd_now();
+ int i;
+ int freeslot = -1;
+
+ for (i = 0; i < UARTKBD_MAXHELD; i++)
+ {
+ if (g_held[i].active && g_held[i].type == type &&
+ g_held[i].code == code)
+ {
+ g_held[i].deadline = now + UARTKBD_HOLDMS;
+ return false;
+ }
+
+ if (!g_held[i].active && freeslot < 0)
+ {
+ freeslot = i;
+ }
+ }
+
+ if (freeslot < 0)
+ {
+ return false;
+ }
+
+ g_held[freeslot].active = true;
+ g_held[freeslot].type = type;
+ g_held[freeslot].code = code;
+ g_held[freeslot].deadline = now + UARTKBD_HOLDMS;
+ return true;
+}
+
+/****************************************************************************
+ * Name: uartkbd_decode
+ *
+ * Description:
+ * Turns one byte from the terminal into a key, tracking the arrow key
+ * escape sequences. Returns true when *type and *code are valid.
+ *
+ * w a s d, arrow keys move / turn
+ * j fire (Ctrl)
+ * k run (Shift)
+ * l strafe (Alt)
+ * space, e use
+ * Esc, q menu / back
+ * everything else passed through as the typed character
+ *
+ ****************************************************************************/
+
+static bool uartkbd_decode(uint8_t ch, uint32_t *type, uint32_t *code)
+{
+ static bool in_csi; /* Inside an ESC [ or ESC O sequence */
+
+ if (in_csi)
+ {
+ in_csi = false;
+ *type = KEYBOARD_SPECPRESS;
+ switch (ch)
+ {
+ case 'A':
+ *code = KEYCODE_UP;
+ return true;
+ case 'B':
+ *code = KEYCODE_DOWN;
+ return true;
+ case 'C':
+ *code = KEYCODE_RIGHT;
+ return true;
+ case 'D':
+ *code = KEYCODE_LEFT;
+ return true;
+ default:
+ return false;
+ }
+ }
+
+ if (ch == 0x1b)
+ {
+ char next;
+
+ /* A lone Esc cannot be told from the start of an arrow sequence
+ * without waiting, so the sequence is only assumed when the next
+ * byte of it is already there.
+ */
+
+ if (read(g_kbd_dev.fd, &next, 1) == 1 && (next == '[' || next == 'O'))
+ {
+ in_csi = true;
+ return false;
+ }
+
+ *type = KEYBOARD_PRESS;
+ *code = 0x1b;
+ return true;
+ }
+
+ *type = KEYBOARD_SPECPRESS;
+ switch (ch)
+ {
+ case 'w':
+ case 'W':
+ *code = KEYCODE_UP;
+ return true;
+ case 's':
+ case 'S':
+ *code = KEYCODE_DOWN;
+ return true;
+ case 'a':
+ case 'A':
+ *code = KEYCODE_LEFT;
+ return true;
+ case 'd':
+ case 'D':
+ *code = KEYCODE_RIGHT;
+ return true;
+ case 'j':
+ case 'J':
+ *code = KEYCODE_LCTRL;
+ return true;
+ case 'k':
+ case 'K':
+ *code = KEYCODE_LSHIFT;
+ return true;
+ case 'l':
+ case 'L':
+ *code = KEYCODE_LALT;
+ return true;
+ case 0x7f:
+ *code = KEYCODE_BACKDEL;
+ return true;
+ }
+
+ *type = KEYBOARD_PRESS;
+ switch (ch)
+ {
+ case 'e':
+ case 'E':
+ *code = ' ';
+ return true;
+ case 'q':
+ case 'Q':
+ *code = 0x1b;
+ return true;
+ case '\r':
+ *code = '\n';
+ return true;
+ default:
+ *code = ch;
+ return true;
+ }
+}
+
+/****************************************************************************
+ * Name: get_uartkbd_event
+ *
+ * Description:
+ * Read a single synthesized keyboard event from the serial terminal.
+ *
+ ****************************************************************************/
+
+static int get_uartkbd_event(struct keyboard_event_s *sample)
+{
+ uint64_t now;
+ uint8_t ch;
+ int i;
+ int err;
+
+ err = init_kbd_dev(&g_kbd_dev);
Review Comment:
reuse the code at line 643
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]