forked from djnugent/BLE_WASD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.ino
157 lines (140 loc) · 3.05 KB
/
Controller.ino
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
/*
* Controller responsible for tracking whether we are in wired or wireless mode and routing key presses
* accordingly
*/
// Stores the state of the cable / bluetooth switch
volatile boolean bluetooth = true;
volatile boolean usb = false;
/**
* Sends the current key report either over bluetooth or USB cable
*/
void send_key_report() {
KeyReport keyReport = getKeyReport();
if (bluetooth) {
send_ble_report(keyReport);
} else if (usb) {
send_usb_report(keyReport);
} else {
clear_key_report();
}
}
/**
* Sends the current consumer key reports either over bluetooth or USB cable
*/
void send_media_control(uint8_t mediaHidKey) {
if (bluetooth) {
send_ble_media(mediaHidKey);
} else if (usb) {
send_usb_media(mediaHidKey);
}
}
/**
* Clears all currently set modifiers - not sure if this is really needed
*/
void clear_all() {
clear_key_report();
send_key_report();
clear_LEDs();
}
/**
* Starts wireless mode ensuring that we don't process key presses until we are ready
*/
void startup_BLE(boolean reset) {
start_BLE();
if (reset) {
reconfigure_BLE();
}
// Show two LEDs - a broken line = wireless
bluetooth = true;
clear_all();
flash_LEDs(3);
start_PS2();
}
/**
* Stops wireless mode ensuring that further key presses are ignored
*/
void shutdown_BLE() {
stop_PS2();
clear_all();
delay(250); // delay needed or control key can get stuck
bluetooth = false;
stop_BLE();
}
/**
* Starts wired mode ensuring that we don't process key presses until we are ready
*/
void startup_USB(boolean reset) {
start_USB();
if (reset) {
reconfigure_USB();
}
// Show three LEDs - a full line = wired
usb = true;
clear_all();
flash_LEDs(7);
start_PS2();
}
/**
* Stops wired mode ensuring that further key presses are ignored
*/
void shutdown_USB() {
stop_PS2();
clear_all();
delay(250); // delay needed or control key can get stuck
usb = false;
stop_USB();
}
/**
* Switches mode from bluetooth to USB and vice versa
*/
void switch_mode() {
if (bluetooth) {
#if defined (DEBUG)
Serial.println(F("Switching to USB keyboard"));
#endif
shutdown_BLE();
startup_USB(false);
} else {
#if defined (DEBUG)
Serial.println(F("Switching to Bluetooth keyboard"));
#endif
shutdown_USB();
startup_BLE(false);
}
}
/**
* Performs a hard reset for the current mode - USB or bluetooth
*/
void reconfigure() {
if (bluetooth) {
#if defined (DEBUG)
Serial.println(F("Reconfiguring Bluetooth keyboard"));
#endif
shutdown_BLE();
startup_BLE(true);
} else {
#if defined (DEBUG)
Serial.println(F("Reconfiguring USB keyboard"));
#endif
shutdown_USB();
startup_USB(true);
}
}
/**
* Method to perform the initial keyboard start
*
* Currently this defaults to bluetooth
*/
void start_keyboard() {
if (bluetooth) {
#if defined (DEBUG)
Serial.println(F("Starting as Bluetooth keyboard"));
#endif
startup_BLE(false);
} else {
#if defined (DEBUG)
Serial.println(F("Starting as USB keyboard"));
#endif
startup_USB(false);
}
}