summaryrefslogtreecommitdiff
path: root/keyboards/converter/xmk/xmk_shell.c
blob: d8f146316ebbaef99a9a345e0cd147168a92f8e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2022 Manna Harbour (@manna-harbour)
// https://github.com/manna-harbour/xmk

// SPDX-License-Identifier: GPL-2.0-or-later

#include QMK_KEYBOARD_H
#include <stdio.h>
#include <string.h>

#include "xmk_matrix.h"

#define XMK_SHELL_LINE_LEN 64

#define XMK_SHELL_KEY "key "
#define XMK_SHELL_KEY_PRESS "press "
#define XMK_SHELL_KEY_RELEASE "release "
#define XMK_SHELL_BOOT "boot"
#define XMK_SHELL_RESET "reset"

void xmk_shell(char *line) {
  dprintf("xmk_shell: line: '%s'\n", line);
  if (strncmp(line, XMK_SHELL_KEY, strlen(XMK_SHELL_KEY)) == 0) {
    dprintf("xmk_shell: XMK_SHELL_KEY\n");
    if (strncmp(line + strlen(XMK_SHELL_KEY), XMK_SHELL_KEY_PRESS, strlen(XMK_SHELL_KEY_PRESS)) == 0) {
      uint8_t key = strtol(line + strlen(XMK_SHELL_KEY) + strlen(XMK_SHELL_KEY_PRESS), NULL, 10);
      dprintf("xmk_shell: XMK_SHELL_KEY_PRESS: key: %u\n", key);
      xmk_matrix_key(true, key);
    } else if (strncmp(line + strlen(XMK_SHELL_KEY), XMK_SHELL_KEY_RELEASE, strlen(XMK_SHELL_KEY_RELEASE)) == 0) {
      uint8_t key = strtol(line + strlen(XMK_SHELL_KEY) + strlen(XMK_SHELL_KEY_RELEASE), NULL, 10);
      dprintf("xmk_shell: XMK_SHELL_KEY_RELEASE: key: %u\n", key);
      xmk_matrix_key(false, key);
    }
  } else if (strcmp(line, XMK_SHELL_BOOT) == 0) {
    dprintf("xmk_shell: XMK_SHELL_BOOT\n");
    reset_keyboard();
  } else if (strcmp(line, XMK_SHELL_RESET) == 0) {
    dprintf("xmk_shell: XMK_SHELL_RESET\n");
    soft_reset_keyboard();
  }
}

void virtser_recv(const uint8_t ch) {
  static char line[XMK_SHELL_LINE_LEN];
  static uint8_t line_index = 0;
  if (ch == '\r') {
    dprintf("virtser_recv: i: %3u, ch: %3u '\\r' \n", line_index, ch);
    line[line_index] = '\0';
    xmk_shell(line);
    line_index = 0;
  } else if (ch == '\n') {
    dprintf("virtser_recv: i: %3u, ch: %3u '\\n' \n", line_index, ch);
  } else {
    dprintf("virtser_recv: i: %3u, ch: %3u '%c'\n", line_index, ch, ch);
    if (line_index < (XMK_SHELL_LINE_LEN - 1)) {
      line[line_index] = ch;
      line_index++;
    }
  }
}