xmk_shell.c (2040B)
1 // Copyright 2022 Manna Harbour (@manna-harbour) 2 // https://github.com/manna-harbour/xmk 3 4 // SPDX-License-Identifier: GPL-2.0-or-later 5 6 #include QMK_KEYBOARD_H 7 #include <stdio.h> 8 #include <string.h> 9 10 #include "xmk_matrix.h" 11 12 #define XMK_SHELL_LINE_LEN 64 13 14 #define XMK_SHELL_KEY "key " 15 #define XMK_SHELL_KEY_PRESS "press " 16 #define XMK_SHELL_KEY_RELEASE "release " 17 #define XMK_SHELL_BOOT "boot" 18 #define XMK_SHELL_RESET "reset" 19 20 void xmk_shell(char *line) { 21 dprintf("xmk_shell: line: '%s'\n", line); 22 if (strncmp(line, XMK_SHELL_KEY, strlen(XMK_SHELL_KEY)) == 0) { 23 dprintf("xmk_shell: XMK_SHELL_KEY\n"); 24 if (strncmp(line + strlen(XMK_SHELL_KEY), XMK_SHELL_KEY_PRESS, strlen(XMK_SHELL_KEY_PRESS)) == 0) { 25 uint8_t key = strtol(line + strlen(XMK_SHELL_KEY) + strlen(XMK_SHELL_KEY_PRESS), NULL, 10); 26 dprintf("xmk_shell: XMK_SHELL_KEY_PRESS: key: %u\n", key); 27 xmk_matrix_key(true, key); 28 } else if (strncmp(line + strlen(XMK_SHELL_KEY), XMK_SHELL_KEY_RELEASE, strlen(XMK_SHELL_KEY_RELEASE)) == 0) { 29 uint8_t key = strtol(line + strlen(XMK_SHELL_KEY) + strlen(XMK_SHELL_KEY_RELEASE), NULL, 10); 30 dprintf("xmk_shell: XMK_SHELL_KEY_RELEASE: key: %u\n", key); 31 xmk_matrix_key(false, key); 32 } 33 } else if (strcmp(line, XMK_SHELL_BOOT) == 0) { 34 dprintf("xmk_shell: XMK_SHELL_BOOT\n"); 35 reset_keyboard(); 36 } else if (strcmp(line, XMK_SHELL_RESET) == 0) { 37 dprintf("xmk_shell: XMK_SHELL_RESET\n"); 38 soft_reset_keyboard(); 39 } 40 } 41 42 void virtser_recv(const uint8_t ch) { 43 static char line[XMK_SHELL_LINE_LEN]; 44 static uint8_t line_index = 0; 45 if (ch == '\r') { 46 dprintf("virtser_recv: i: %3u, ch: %3u '\\r' \n", line_index, ch); 47 line[line_index] = '\0'; 48 xmk_shell(line); 49 line_index = 0; 50 } else if (ch == '\n') { 51 dprintf("virtser_recv: i: %3u, ch: %3u '\\n' \n", line_index, ch); 52 } else { 53 dprintf("virtser_recv: i: %3u, ch: %3u '%c'\n", line_index, ch, ch); 54 if (line_index < (XMK_SHELL_LINE_LEN - 1)) { 55 line[line_index] = ch; 56 line_index++; 57 } 58 } 59 }