-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
253 lines (200 loc) · 6.97 KB
/
main.cpp
File metadata and controls
253 lines (200 loc) · 6.97 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
/*
Keaboard Simulator
Sends one of the supplied messages as keyboard-inputs to the
connected USB host.
The message to be sent can be selected by long-pressing the button
and afterwards cycling through them with normal button presses [starting
from the first one].
The selected message number will be remembered even when powered off.
The circuit:
Lily TTGO USB with button between MISO and GND [see drawing.svg].
This code is in the public domain.
*/
#include "SlowKeyboard.h"
#include <Arduino.h>
#include <EEPROM.h>
#include <Wire.h>
#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include <string.h>
namespace Messages
{
typedef void (*KeyboardFunction)(Keyboard_ & keyboard);
void keyboardFunction0(Keyboard_ & keyboard)
{
keyboard.print(F("String 0"));
keyboard.write(KEY_RETURN);
}
void keyboardFunction1(Keyboard_ & keyboard)
{
keyboard.print(F("String 1"));
keyboard.write(KEY_RETURN);
}
void keyboardFunction2(Keyboard_ & keyboard)
{
keyboard.print(F("String 2"));
keyboard.write(KEY_RETURN);
}
void keyboardFunction3(Keyboard_ & keyboard)
{
keyboard.print(F("String 3"));
keyboard.write(KEY_RETURN);
}
void keyboardFunction4(Keyboard_ & keyboard)
{
keyboard.print(F("String 4"));
keyboard.write(KEY_RETURN);
}
void keyboardFunction5(Keyboard_ & keyboard)
{
keyboard.print(F("String 5"));
keyboard.write(KEY_RETURN);
}
KeyboardFunction constexpr array[] = {keyboardFunction0,
keyboardFunction1,
keyboardFunction2,
keyboardFunction3,
keyboardFunction4,
keyboardFunction5};
static size_t constexpr count = sizeof(array) / sizeof(array[0]);
} // namespace Messages
namespace Pins
{
static uint8_t constexpr button = PIN_SPI_MISO; // PE6, Int4
static uint8_t constexpr led = LED_BUILTIN;
} // namespace Pin
namespace EepromAddresses
{
static uint8_t constexpr selectedMessageIndex = 0;
} // namespace EepromAddresses
static Keyboard_ & slowKeyboard = Keyboard;
static bool volatile buttonPressed = false;
static size_t messageIndex = 0;
ISR(PCINT0_vect)
{
PCICR &= ~PCIE0; // Disable pin change interrupt for PCINT7..0 of Atmega 32u4.
}
void enterSleepMode(void)
{
static_assert(PIN_SPI_MISO == Pins::button); // Make sure we are talking about the same pin here and everywhere else.
PCIFR |= PCIF0; // Clear interrupt flag for PCINT7..0 of Atmega 32u4.
PCICR |= PCIE0; // Enable pin change interrupt for PCINT7..0 of Atmega 32u4.
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_enable();
sleep_mode();
sleep_disable();
}
void setup()
{
PCMSK0 = PCINT3; // Enable pin change interrupt for PB3 [Arduino Pin PIN_SPI_MISO].
pinMode(Pins::button, INPUT_PULLUP);
pinMode(Pins::led, OUTPUT);
EEPROM.get(EepromAddresses::selectedMessageIndex, messageIndex);
if (Messages::count <= messageIndex)
{
messageIndex = 0;
}
// initialize control over the keyboard:
// slowKeyboard.minimumReportDelayUs = 8000;
slowKeyboard.begin(KeyboardLayout_de_DE);
digitalWrite(Pins::led, HIGH);
// Wait for the USB connection to become operational.
delay(600);
digitalWrite(Pins::led, LOW);
while (true)
{
buttonPressed = (LOW == digitalRead(Pins::button));
if (buttonPressed)
{
bool longPress = false;
{
unsigned long const timePressed = millis();
delay(50); // debounce
// Wait for the button to be released.
while (LOW == digitalRead(Pins::button))
{
unsigned long const now = millis();
// If the button is held low for more than 250ms consider it a long press.
if (250 < (now - timePressed))
{
longPress = true;
digitalWrite(Pins::led, HIGH);
}
}
}
if (longPress)
{
// Update the selected message index.
unsigned long constexpr timeout = 2000; // milliseconds
bool timedOut = false;
size_t buttonPresses = 0;
do
{
{
// check whether pressed again
unsigned long const timeReleased = millis();
delay(50); // debounce
while ((HIGH == digitalRead(Pins::button)) && !timedOut)
{
if (timeout < (millis() - timeReleased))
{
timedOut = true;
}
}
}
if (!timedOut)
{
// wait to be released again
unsigned long const timePressed = millis();
digitalWrite(Pins::led, LOW); // Turn off LED temporarily when pressed.
delay(50); // debounce
while ((LOW == digitalRead(Pins::button)))
{
// Turn LED back on after 500ms.
if (500 < (millis() - timePressed))
{
digitalWrite(Pins::led, HIGH);
}
}
digitalWrite(Pins::led, HIGH);
// Note button press.
// Please note that this will overflow ungracefully, so don't press the button more
// than std::numeric_limits<size_t>::max() times, i.e. 65535 for 16-bit.
buttonPresses += 1;
}
}
while (!timedOut);
digitalWrite(Pins::led, LOW); // Selection finished, so turn off LED again.
if (0 < buttonPresses)
{
// Change to zero-based index and confine to available number of messages..
messageIndex = (buttonPresses - 1) % Messages::count;
// Remember messageIndex even after power off.
EEPROM.put(EepromAddresses::selectedMessageIndex, messageIndex);
}
}
else
{
// write out the message for a short press of the button
Messages::array[messageIndex](slowKeyboard);
}
buttonPressed = false;
}
else
{
// intentionally empty
}
// Conserve power by going to sleep now.
enterSleepMode();
}
// finalize
slowKeyboard.end();
}
void loop()
{
// notify error condition
digitalWrite(Pins::led, HIGH);
delay(500);
digitalWrite (Pins::led, LOW);
delay(500);
}