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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include "board.h"
#include "usb_stdio.h"
#include "keymat.h"
#include "keysym.h"
#include "split.h"
#include "led.h"
#include "hid.h"
#include "keymap.h"
#include "ws2812.h"
#include "util.h"
#include "hardware/gpio.h"
#include "class/cdc/cdc_device.h"
#include "class/hid/hid.h"
#include "device/usbd.h"
#include "pico/stdlib.h"
#include "bsp/board.h"
#include "pico/time.h"
#include "tusb.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void cdc_task(void);
int
main(void)
{
uint32_t start, stop;
board_init();
tud_init(BOARD_TUD_RHPORT);
usb_stdio_init();
led_init();
keymat_init();
split_init();
hid_init();
start = board_millis();
while (true) {
tud_task();
cdc_task();
led_task();
split_task();
hid_task();
stop = board_millis();
DEBUG("Main loop: %i ms", stop - start);
start = stop;
}
return 0;
}
void
tud_mount_cb(void)
{
led_rgb = WS2812_U32RGB(100, 0, 100);
led_mode = LED_ON;
led_reset = true;
}
void
tud_umount_cb(void)
{
led_blink_ms = 500;
led_rgb = WS2812_U32RGB(100, 100, 100);
led_mode = LED_BLINK;
led_reset = true;
}
void
tud_suspend_cb(bool remote_wakeup_en)
{
led_rgb = WS2812_U32RGB(100, 100, 100);
led_mode = LED_ON;
led_reset = true;
}
void
tud_resume_cb(void)
{
led_rgb = WS2812_U32RGB(100, 0, 100);
led_mode = LED_ON;
led_reset = true;
}
void
tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts)
{
}
void
tud_cdc_rx_cb(uint8_t itf)
{
}
uint16_t
tud_hid_get_report_cb(uint8_t itf, uint8_t report_id,
hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
{
return 0;
}
void
tud_hid_set_report_cb(uint8_t itf, uint8_t report_id,
hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
{
}
void
process_cmd(char *cmd)
{
char *arg, *tok;
tok = strchr(cmd, ' ');
if (tok) {
*tok = '\0';
arg = tok + 1;
} else {
arg = cmd + strlen(cmd);
}
if (!strcmp(cmd, "log")) {
if (!strcmp(arg, "")) {
printf("Levels: debug, info, warn, err\n");
} else if (!strcmp(arg, "debug")) {
loglevel = LOG_DEBUG;
} else if (!strcmp(arg, "info")) {
loglevel = LOG_INFO;
} else if (!strcmp(arg, "warn")) {
loglevel = LOG_WARN;
} else if (!strcmp(arg, "warn")) {
loglevel = LOG_ERR;
} else {
printf("Invalid log level: %s\n", arg);
}
} else {
printf("Invalid command: %s\n", cmd);
}
}
void
cdc_task(void)
{
static char cmdbuf[256];
static int cmdlen = 0;
char c;
do {
if (cmdlen)
tud_task();
if (tud_cdc_connected() && tud_cdc_available()
&& tud_cdc_read(&c, 1)) {
if (c == '\r' || c == '\n') {
printf("\n");
tud_cdc_write_flush();
if (cmdlen) {
cmdbuf[cmdlen] = 0;
process_cmd(cmdbuf);
cmdlen = 0;
}
} else if (c == 4) {
printf("ALIVE!\n");
} else if (cmdlen == ARRLEN(cmdbuf)) {
printf("\n--- cmd too long ---\n");
cmdlen = 0;
} else {
printf("%c", c);
tud_cdc_write_flush();
cmdbuf[cmdlen++] = c;
}
}
} while (cmdlen);
}
|