-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswc_control.ino
136 lines (113 loc) · 2.46 KB
/
swc_control.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
#include "TrinketHidCombo.h"
#include "limits.h"
#define VOLTAGE_IN 5
#define RESISTANCE_KNOWN 1000
#define PIN_ANALOG_A A6
#define PIN_ANALOG_B A7
#define TOLERANCE 0.1 // 10%
#define DEBUG
struct MAP
{
int resistance;
int multimedia;
int key;
};
static MAP map_a[] = {
{ 0, 1, MMKEY_VOL_UP }, // (+) Vol Up
{ 50, 1, MMKEY_VOL_DOWN }, // (-) Vol Down
{ 120, 0, 0 }, // (>) Tune Up
{ 230, 0, 0 }, // (<) Tune Down
{ 430, 0, 0 }, // List
{ 1000, 0, KEYCODE_ESC } // Return
};
static MAP map_b[] = {
{ 0, 1, MMKEY_PLAYPAUSE }, // Mute/Enter
{ 50, 0, KEYCODE_M }, // Talk
{ 120, 0, 0 }, // Source
{ 230, 0, KEYCODE_P }, // On Hook
{ 430, 0, KEYCODE_O } // Off Hook
};
static MAP *keyPressed = NULL;
void setup()
{
TrinketHidCombo.begin(); // start the USB device engine and enumerate
}
void write_number(int value)
{
#ifdef DEBUG
char buffer[12];
itoa(value, buffer, 10);
write(buffer);
#endif
}
void write(char* buffer)
{
#ifdef DEBUG
TrinketHidCombo.write(buffer);
#endif
}
int read_value(int pin)
{
int raw = analogRead(pin);
if (!raw)
return 0;
if (raw == 1023) // max value, assume open circuit?
return INT_MAX;
float voltage_out = (raw * VOLTAGE_IN) / 1024.0;
float resistance = (voltage_out * RESISTANCE_KNOWN) / (VOLTAGE_IN - voltage_out);
return (int)resistance;
}
MAP* get_key(int pin, MAP map[], int size)
{
int value = read_value(pin);
for (int i = 0; i < size; i++)
{
if (value >= map[i].resistance * (1 - TOLERANCE)
&& value <= map[i].resistance * (1 + TOLERANCE))
{
return &map[i];
}
}
return NULL;
}
void pressKey(MAP* newKey)
{
if (!newKey || !newKey->key)
return;
write("kd\n");
if (newKey->multimedia)
{
TrinketHidCombo.pressMultimediaKey(newKey->key);
}
else
{
TrinketHidCombo.pressKey(0, newKey->key);
// Immediately release
TrinketHidCombo.pressKey(0, 0);
}
write("\n");
}
void loop()
{
MAP* newKey = get_key(PIN_ANALOG_A, map_a, sizeof(map_a) / sizeof(MAP));
if (!newKey)
newKey = get_key(PIN_ANALOG_B, map_b, sizeof(map_b) / sizeof(MAP));
if (newKey)
{
if (!keyPressed)
{
pressKey(newKey);
delay(5); // debounce delay
keyPressed = newKey;
}
}
else
{
if (keyPressed)
{
delay(5); // debounce delay
keyPressed = NULL;
}
}
TrinketHidCombo.poll(); // check if USB needs anything done
}