forked from jamesfowkes/cryptology-4x5keypad-keys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptology-4x5keypad-keys.ino
144 lines (120 loc) · 3.15 KB
/
cryptology-4x5keypad-keys.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
#include <Keyboard.h>
#include <AStar32U4.h>
#include <TaskAction.h>
#include <Keypad.h>
#include <EEPROMex.h>
#include "game_keypad.h"
static byte s_new_key = false;
static const uint8_t SERIAL_BUFFER_LENGTH = 32;
static char s_serial_buffer[SERIAL_BUFFER_LENGTH];
static uint8_t s_serial_idx = 0;
static const uint8_t LED_PIN = 13;
static const uint8_t LAYOUT_SELECT_PIN = 9;
static LAYOUT s_layout;
static int s_debounce_time_address;
static uint16_t s_debounce_time;
static void process_debounce_time_setting(char const * const cmd)
{
char * end;
long debounce_time = strtol(cmd, &end, 10);
if (end > cmd)
{
if ((debounce_time > 10) && (debounce_time < 500))
{
s_debounce_time = debounce_time;
EEPROM.writeInt(s_debounce_time_address, s_debounce_time);
keypad_set_debounce_time(debounce_time);
Serial.print("Set debounce time to ");
Serial.println(s_debounce_time);
}
}
}
static void reset_buffer()
{
s_serial_buffer[0] = '\0';
s_serial_idx = 0;
}
static void process_command(char const * const cmd)
{
if ((cmd[0] == 'S') && (cmd[1] == '0'))
{
process_debounce_time_setting(&cmd[2]);
}
}
static void debug_task_fn(TaskAction * pThisTask)
{
(void)pThisTask;
static bool s_led = false;
digitalWrite(LED_PIN, s_led = !s_led);
}
static TaskAction s_debug_task(debug_task_fn, 500, INFINITE_TICKS);
static void post_setup_task_fn(TaskAction * pThisTask)
{
(void)pThisTask;
Serial.print("Using layout ");
Serial.println(s_layout+1);
Serial.print("Debounce time: ");
Serial.print(s_debounce_time);
Serial.print("ms");
}
static TaskAction s_post_setup_task(post_setup_task_fn, 5000, 1);
static byte check_and_clear(byte& key)
{
byte value = key;
cli();
key = '\0';
sei();
return value;
}
void setup()
{
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
pinMode(LAYOUT_SELECT_PIN, INPUT_PULLUP);
Keyboard.begin();
delay(300);
s_layout = digitalRead(LAYOUT_SELECT_PIN) == HIGH ? LAYOUT_1 : LAYOUT_2;
s_debounce_time_address = EEPROM.getAddress(sizeof(uint16_t));
s_debounce_time = EEPROM.readInt(s_debounce_time_address);
keypad_setup(s_new_key, s_layout);
keypad_set_debounce_time(s_debounce_time);
reset_buffer();
}
void loop()
{
keypad_tick();
char key;
if ((key = check_and_clear(s_new_key)))
{
Serial.println(key);
Keyboard.press(key);
Keyboard.release(key);
}
s_debug_task.tick();
s_post_setup_task.tick();
}
void serialEvent()
{
while(Serial.available())
{
char c = Serial.read();
if (c != '\n')
{
if (s_serial_idx < SERIAL_BUFFER_LENGTH)
{
s_serial_buffer[s_serial_idx++] = c;
s_serial_buffer[s_serial_idx] = '\0';
}
}
else
{
Serial.print("Got command ");
Serial.println(s_serial_buffer);
process_command(s_serial_buffer);
reset_buffer();
}
}
}
void serialEventRun(void) {
if (Serial.available()) serialEvent();
}