Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
113 lines (91 loc) · 2.23 KB
/
Copy pathmain.c
File metadata and controls
113 lines (91 loc) · 2.23 KB
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
// Pico stuff
#include "pico/stdlib.h"
#include "tusb.h"
#include "config.h"
#include "hid_report.h"
#include "key.h"
#include "key_config.h"
#include "key_display.h"
#include "logger.h"
#include "macro.h"
#include "macropad_config.h"
#include "oled.h"
#include "rndis.h"
#include "usb_descriptors.h"
#include "ws2812.h"
typedef enum { MODE_KEYBOARD, MODE_CONFIG } DeviceMode;
DeviceMode get_device_mode(void) {
gpio_pull_up(CONFIG_BTN_PIN);
busy_wait_us(3); // settle time
bool pressed = !gpio_get(CONFIG_BTN_PIN);
return pressed ? MODE_CONFIG : MODE_KEYBOARD;
}
void keyboard_mode() {
#if !OLED_DEBUG_LOG
logger_set_display(false);
key_display_init();
#endif
absolute_time_t next_scan = get_absolute_time();
while (true) {
tud_task();
if (absolute_time_diff_us(get_absolute_time(), next_scan) <= 0) {
next_scan = delayed_by_us(next_scan, 1000);
key_scan();
#if !OLED_DEBUG_LOG
key_display_task();
#endif
process_key_events();
macro_task();
hid_report_task();
}
}
}
void config_mode() {
rndis_init("macropad");
log_info("Starting at");
log_info("192.168.7.1");
while (true) {
rndis_task();
}
}
// checks if there is a previous config or if its first boot
// If first boot it saves config, otherwise it loads it from flash
void check_config_flash(void) {
ConfigStorage stored_config;
load_config(&stored_config);
if (config_is_valid(&stored_config)) {
config = stored_config.config;
} else {
config = default_config;
ConfigStorage new_config = {
.header =
{
.magic = CONFIG_MAGIC,
.size = sizeof(Config),
},
.config = config,
};
new_config.header.crc32 =
crc32((const uint8_t *)&new_config.config, sizeof(Config));
save_config(&new_config);
}
}
int main() {
// Initialize Hardware
oled_init();
logger_init();
key_init();
ws2812_init();
check_config_flash();
log_info("Macropad starting");
DeviceMode mode = get_device_mode();
usb_descriptors_set_network_mode(mode == MODE_CONFIG);
tusb_init();
if (mode == MODE_CONFIG) {
// blue led to indicate config mode
ws2812_set_rgb(0, 0, 255);
config_mode();
} else {
keyboard_mode();
}
}