-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecmacro02.ino
More file actions
294 lines (262 loc) · 7.04 KB
/
ecmacro02.ino
File metadata and controls
294 lines (262 loc) · 7.04 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* ECmacro02 Firmware
*
* Description:
* This firmware is for ECmacro02, a 2-key USB macro pad using electrostatic capacitive switches.
* It sends Volume Up, Volume Down, and Mute commands via USB HID.
*
* Author: QuadState
* License: MIT License
* Project URL: https://github.com/QuadState/ECmacro
*
* USB descriptor handling is based on CH55xduino's HID keyboard example by Deqing Sun:
* https://github.com/DeqingSun/ch55xduino/tree/ch55xduino/ch55xduino/ch55x/libraries/Generic_Examples/examples/05.USB/HidKeyboard
*
* Media key (Consumer Control) functionality was independently implemented,
* although a similar approach exists in CH55xduino examples.
*
* Application logic and key assignments are original.
*
* Created: 2025
*
* USB VID/PID: 0x1209 / 0xEC02 (pending registration via pid.codes)
*/
__sfr __at(0x8E) CKCON;
#ifdef USER_USB_RAM
#include "src/USBHIDKeyboard.h"
#else
#error "This example needs to be compiled with a USER USB setting"
#endif
#define SWITCH_COUNT 2
#define STABLE_THRESHOLD 15
#define WAIT_THRESHOLD 35
// Button event types
typedef enum {
EVENT_NONE,
EVENT_PRESS_TOP,
EVENT_PRESS_BOTTOM,
EVENT_PRESS_BOTH,
EVENT_RELEASE_TOP,
EVENT_RELEASE_BOTTOM
} ButtonEvent;
// Switch configuration structure
typedef struct {
uint8_t pin;
uint8_t mask;
uint16_t releaseThreshold;
uint16_t pressThreshold;
} SwitchConfig;
// Configuration for switches
const SwitchConfig switchConfigs[SWITCH_COUNT] = {
{ 14, 0x10, 34, 37 }, // Top key
{ 15, 0x20, 40, 48 } // Bottom key
};
// Switch states
typedef enum {
STATE_RELEASED,
STATE_PRESSED,
STATE_WAIT
} SwitchStateType;
// Structure to hold switch states
typedef struct {
SwitchStateType state;
uint8_t stableCount;
uint8_t waitCount;
} SwitchState;
SwitchState switchStates[SWITCH_COUNT] = {
{ STATE_RELEASED, 0, 0 },
{ STATE_RELEASED, 0, 0 }
};
// Operation modes
typedef enum {
MODE_PRESS_TOP,
MODE_PRESS_BOTTOM,
MODE_PRESS_BOTH,
MODE_RELEASE
} OperationMode;
OperationMode currentMode = MODE_RELEASE;
// Timer initialization (16-bit timer)
void initTimer1(void) {
CKCON |= (1 << 4);
CKCON &= 0x0F;
TMOD |= 0x10;
}
// Interrupt controls
void disableInterrupts(void) {
EA = 0;
USB_INT_FG = 0;
}
void enableInterrupts(void) {
EA = 1;
}
// Timer utilities
void resetTimer(void) {
TR1 = 0;
TH1 = 0;
TL1 = 0;
TF1 = 0;
}
uint16_t getTimerCycleCount(void) {
return ((uint16_t)TH1 << 8) | TL1;
}
// Measure the discharge time for capacitive switches
uint16_t measureDischargeCycleCount(uint8_t mask) {
disableInterrupts();
resetTimer();
TR1 = 1;
P1_DIR_PU &= ~mask;
while (P1 & mask)
;
TR1 = 0;
P1_DIR_PU |= mask;
uint16_t count = TF1 ? 0 : getTimerCycleCount();
enableInterrupts();
return count;
}
// Update switch state and detect events
ButtonEvent updateSwitchState(uint8_t index, uint16_t cycleCount) {
SwitchState *currentSwitch = &switchStates[index];
SwitchState *otherSwitch = &switchStates[index == 0 ? 1 : 0];
const SwitchConfig *config = &switchConfigs[index];
if (currentSwitch->state == STATE_WAIT) {
if (currentSwitch->waitCount < WAIT_THRESHOLD) {
currentSwitch->waitCount++;
}
if (otherSwitch->stableCount != 0) {
return EVENT_NONE;
}
if (currentSwitch->waitCount >= WAIT_THRESHOLD) {
currentSwitch->state = STATE_PRESSED;
currentSwitch->waitCount = 0;
return index == 0 ? EVENT_PRESS_TOP : EVENT_PRESS_BOTTOM;
}
} else if (currentSwitch->state == STATE_RELEASED) {
if (cycleCount >= config->pressThreshold) {
currentSwitch->stableCount++;
if (currentSwitch->stableCount >= STABLE_THRESHOLD) {
if (otherSwitch->state == STATE_WAIT) {
currentSwitch->state = STATE_PRESSED;
otherSwitch->state = STATE_PRESSED;
currentSwitch->waitCount = 0;
otherSwitch->waitCount = 0;
currentSwitch->stableCount = 0;
otherSwitch->stableCount = 0;
return EVENT_PRESS_BOTH;
} else {
currentSwitch->state = STATE_WAIT;
currentSwitch->stableCount = 0;
currentSwitch->waitCount = 0;
}
}
} else {
currentSwitch->stableCount = 0;
}
} else if (currentSwitch->state == STATE_PRESSED) {
if (cycleCount <= config->releaseThreshold) {
currentSwitch->stableCount++;
if (currentSwitch->stableCount >= STABLE_THRESHOLD) {
currentSwitch->state = STATE_RELEASED;
currentSwitch->stableCount = 0;
return index == 0 ? EVENT_RELEASE_TOP : EVENT_RELEASE_BOTTOM;
}
} else {
currentSwitch->stableCount = 0;
currentSwitch->waitCount = 0;
}
}
return EVENT_NONE;
}
// Initialize pins and USB
void setup(void) {
for (uint8_t i = 0; i < SWITCH_COUNT; i++) {
pinMode(switchConfigs[i].pin, INPUT_PULLUP);
}
initTimer1();
#ifdef USER_USB_RAM
USBInit();
#endif
}
// Send HID media commands
void sendVolumeUp() {
#ifdef USER_USB_RAM
MediaKey_press(MEDIA_VOLUME_UP);
#endif
}
void sendVolumeDown() {
#ifdef USER_USB_RAM
MediaKey_press(MEDIA_VOLUME_DOWN);
#endif
}
void sendMute() {
#ifdef USER_USB_RAM
MediaKey_press(MEDIA_MUTE);
#endif
}
void releaseMediaKey() {
#ifdef USER_USB_RAM
MediaKey_release();
delay(1);
#endif
}
uint8_t previousMillis = 0;
// Main loop
void loop(void) {
for (uint8_t i = 0; i < SWITCH_COUNT; i++) {
uint16_t cycleCount = measureDischargeCycleCount(switchConfigs[i].mask);
ButtonEvent event = updateSwitchState(i, cycleCount);
switch (currentMode) {
case MODE_RELEASE:
if (event == EVENT_PRESS_TOP) {
currentMode = MODE_PRESS_TOP;
sendVolumeUp();
} else if (event == EVENT_PRESS_BOTTOM) {
currentMode = MODE_PRESS_BOTTOM;
sendVolumeDown();
} else if (event == EVENT_PRESS_BOTH) {
currentMode = MODE_PRESS_BOTH;
sendMute();
}
break;
case MODE_PRESS_TOP:
if (event == EVENT_RELEASE_TOP) {
currentMode = MODE_RELEASE;
releaseMediaKey();
} else if (event == EVENT_PRESS_BOTTOM) {
currentMode = MODE_PRESS_BOTTOM;
releaseMediaKey();
sendVolumeDown();
}
break;
case MODE_PRESS_BOTTOM:
if (event == EVENT_RELEASE_BOTTOM) {
currentMode = MODE_RELEASE;
releaseMediaKey();
} else if (event == EVENT_PRESS_TOP) {
currentMode = MODE_PRESS_TOP;
releaseMediaKey();
sendVolumeUp();
}
break;
case MODE_PRESS_BOTH:
if ((event == EVENT_RELEASE_TOP || event == EVENT_RELEASE_BOTTOM)
&& switchStates[0].state != STATE_PRESSED
&& switchStates[1].state != STATE_PRESSED) {
currentMode = MODE_RELEASE;
releaseMediaKey();
}
break;
}
#ifndef USER_USB_RAM
USBSerial_print(cycleCount);
USBSerial_print((i != SWITCH_COUNT - 1) ? "," : "\r\n");
delay(10);
#endif
}
while (true) {
uint8_t currentMillis = millis();
if (previousMillis != currentMillis) {
previousMillis = currentMillis;
break;
}
}
}