forked from chrisandreae/keyboard-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro.c
333 lines (285 loc) · 10 KB
/
macro.c
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
Kinesis ergonomic keyboard firmware replacement
Copyright 2012 Chris Andreae (chris (at) andreae.gen.nz)
This file is offered under either of the GNU GPL v2 or MIT licences
below in order that it may be used with either of the V-USB or LUFA
USB libraries.
See Kinesis.h for keyboard hardware documentation.
==========================
If built for V-USB, this program includes library and sample code from:
V-USB, (C) Objective Development Software GmbH
Licensed under the GNU GPL v2 (see GPL2.txt)
==========================
If built for LUFA, this program includes library and sample code from:
LUFA Library
Copyright (C) Dean Camera, 2011.
dean [at] fourwalledcubicle [dot] com
www.lufa-lib.org
Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
#include "hardware.h"
#include "macro.h"
#include "usb.h"
#include "printing.h"
#include "serial_eeprom.h"
#include "buzzer.h"
#include <stdint.h>
#include <stdlib.h>
#include <avr/eeprom.h>
#include <util/delay.h>
#if USE_EEPROM
// The macro lookup index is in internal eeprom
#define MACRO_INDEX_COUNT (MACRO_INDEX_SIZE / sizeof(macro_idx))
static macro_idx macro_index[MACRO_INDEX_COUNT] EEMEM;
// and the macro data itself is in external eeprom
static uint8_t macros_storage[MACROS_SIZE] EEEXT;
static uint16_t *const macros_end_offset = (uint16_t*)macros_storage;
static uint8_t *const macros = macros_storage + sizeof(uint16_t);
////////////////////// Macro Execution ////////////////////////
static macro_data* current_macro;
static hid_keycode* current_macro_cursor;
static macro_idx* current_macro_index;
////////////////////// Macro Management ////////////////////////
uint8_t* macros_get_storage(){
return ¯os_storage[0];
}
uint8_t* macros_get_index(){
return (uint8_t*) ¯o_index[0];
}
void macros_reset_defaults(){
macro_idx tmp;
memset(tmp.keys, NO_KEY, MACRO_MAX_KEYS);
tmp.macro_offset = 0x0;
for(uint8_t i = 0; i < MACRO_INDEX_COUNT; ++i){
eeprom_update_block(&tmp, ¯o_index[i], sizeof(macro_idx));
USB_KeepAlive(true);
}
volatile uint16_t zero = 0x0;
serial_eeprom_write((uint8_t*)macros_end_offset, (uint8_t*)&zero, sizeof(uint16_t));
}
// comparator for a macro key (in ram) and macro_idx (in eeprom)
static int macro_index_cmp(const macro_key* k, const macro_idx* v){
for(uint8_t j = 0; j < MACRO_MAX_KEYS; ++j){
int d = k->keys[j] - eeprom_read_byte(&v->keys[j]);
if(d) return d;
}
return 0;
}
static inline macro_idx* find_macro(macro_key* key){
macro_idx* r = (macro_idx*) bsearch(key,
macro_index,
MACRO_INDEX_COUNT, //nelem
sizeof(macro_idx), // width
(int(*)(const void*, const void*)) macro_index_cmp);
return r;
}
// convenience macro for reading from eeprom and handling errors
#define seeprom_read_var(var, ptr) \
if(serial_eeprom_read((uint8_t*)ptr, (uint8_t*)&var, sizeof(typeof(var))) != sizeof(typeof(var))){ goto err; }
#define seeprom_write_var(ptr, var) \
if(serial_eeprom_write((uint8_t*)ptr, (uint8_t*)&var, sizeof(typeof(var))) != sizeof(typeof(var))){ goto err; }
/**
* internal function: remove the macro data for the given
* macro index entry.
*/
static bool delete_macro_data(macro_idx* entry_idx){
uint16_t entry_offset = eeprom_read_word(&entry_idx->macro_offset);
macro_data* entry = (macro_data*) (¯os[entry_offset]);
// read the macro space end offset
uint16_t end_offset;
seeprom_read_var(end_offset, macros_end_offset);
// Read the length of the macro to be deleted
uint16_t entry_len;
seeprom_read_var(entry_len, &entry->length);
entry_len += 2; // length header
uint16_t rest_offset = entry_offset + entry_len;
uint16_t rest_len = end_offset - rest_offset;
if(rest_len){
// if there is data after the entry, move rest_len bytes of
// macro data down to entry_offset from rest_offset
if(serial_eeprom_memmove(¯os[entry_offset], ¯os[rest_offset], rest_len) != SUCCESS){ goto err; }
}
// and update the saved macro end offset
end_offset -= entry_len;
seeprom_write_var(macros_end_offset, end_offset);
// Now scan the macro index, and move down any entries > entry_offset by entry_len
for(uint8_t i = 0; i < MACRO_INDEX_COUNT; ++i){
if(eeprom_read_byte(¯o_index[i].keys[0]) == NO_KEY) break;
uint16_t ioff = eeprom_read_word(¯o_index[i].macro_offset);
if(ioff > entry_offset){
eeprom_update_word(¯o_index[i].macro_offset, ioff - entry_len);
USB_KeepAlive(true);
}
}
return true;
err:
return false;
}
static void remove_macro_index(macro_idx* mi){
// move macros down into the slot until we hit the end of the array or an empty (keys[0] == NO_KEY) entry
uint8_t mi_offset = mi - macro_index;
for(uint8_t i = mi_offset; i < MACRO_INDEX_COUNT; ++i){
macro_idx tmp;
if(i == MACRO_INDEX_COUNT - 1){
// hit the end, fill with empty
memset(tmp.keys, NO_KEY, MACRO_MAX_KEYS);
tmp.macro_offset = 0;
}
else{
eeprom_read_block(&tmp, ¯o_index[i + 1], sizeof(macro_idx));
}
// and write
eeprom_update_block(&tmp, ¯o_index[i], sizeof(macro_idx));
if(tmp.keys[0] == NO_KEY) break; // done
USB_KeepAlive(true);
}
}
/**
* Looks up a macro based on a set of pressed keys, returns a pointer
* to the macro data or NO_MACRO if not found.
*/
macro_data* macros_lookup(macro_key* key){
if(key->keys[0] == NO_KEY){
return NO_MACRO; // do not allow lookup of empty entry
}
macro_idx* r = find_macro(key);
if(r){
uint16_t off = eeprom_read_word(&r->macro_offset);
return (macro_data*) (¯os[off]);
}
else return NO_MACRO;
}
/**
* Starts recording a macro identified by the given key. Adds it to
* the index, removes any existing data, and returns a pointer to the
* macro data. Only one macro may be being recorded at once.
*/
bool macros_start_macro(macro_key* key){
macro_idx* r = find_macro(key);
if(r){
// macro index exists: remove the old macro data and re-use this slot
if(!delete_macro_data(r)) goto err;
}
else{
// macro index does not exist: move the index up from the end until we reach
// a key lower than us, then insert
if(eeprom_read_byte(¯o_index[MACRO_INDEX_COUNT - 1].keys[0]) != NO_KEY){
// then we're full, error
goto err;
}
for(int i = MACRO_INDEX_COUNT - 1; i >= 0; --i){
// consider each slot i from the end. If it is the correct
// position (first or key is >= the preceding cell) then
// store, otherwise move the value in preceding cell up
// and repeat.
if(i == 0 || macro_index_cmp(key, ¯o_index[i-1]) >= 0){
r = ¯o_index[i];
break;
}
else if(eeprom_read_byte(¯o_index[i-1].keys[0]) == NO_KEY){
continue; // Don't bother to copy empty cells.
}
else{
// copy up (i-1)
macro_idx tmp;
eeprom_read_block (&tmp, ¯o_index[i-1], sizeof(macro_idx));
eeprom_update_block(&tmp, ¯o_index[i], sizeof(macro_idx));
USB_KeepAlive(true);
}
}
}
// we now have a free index cell at r: write in the key part
eeprom_update_block(key, r, sizeof(macro_key)); // macro_key is prefix to macro_idx
USB_KeepAlive(true);
// and now the data
uint16_t new_macro_offset;
seeprom_read_var(new_macro_offset, macros_end_offset);
eeprom_update_word((uint16_t*)&r->macro_offset, (uint16_t)new_macro_offset);
// and set up the new macro for recording content
current_macro = (macro_data*) ¯os[new_macro_offset];
current_macro_cursor = ¤t_macro->events[0];
current_macro_index = r;
return true;
err:
buzzer_start_f(200, BUZZER_FAILURE_TONE);
current_macro = 0;
current_macro_cursor = 0;
current_macro_index = 0;
return false;
}
/**
* Commits the current macro which was started with
* macros_start_macro(). If len is 0, instead rolls back the macro
* creation and removes the entry from the index. This is also used
* to delete a macro.
*/
void macros_commit_macro(){
if(current_macro == 0){
// cannot commit no macro
goto err;
}
uint16_t macro_len = current_macro_cursor - ¤t_macro->events[0];
if(macro_len == 0){
// find the macro in the index and remove it.
remove_macro_index(current_macro_index);
}
else{
seeprom_write_var(¤t_macro->length, macro_len);
uint16_t end_offset;
seeprom_read_var(end_offset, macros_end_offset);
end_offset += macro_len + 2; // length header + data
seeprom_write_var(macros_end_offset, end_offset);
buzzer_start_f(200, BUZZER_SUCCESS_TONE);
}
current_macro = 0;
current_macro_cursor = 0;
current_macro_index = 0;
return;
err:
buzzer_start_f(200, BUZZER_FAILURE_TONE);
}
void macros_abort_macro(){
remove_macro_index(current_macro_index);
current_macro = 0;
current_macro_cursor = 0;
current_macro_index = 0;
}
bool macros_append(hid_keycode event){
seeprom_write_var(current_macro_cursor++, event);
return true;
err:
return false;
}
bool macros_fill_next_report(macro_playback* state, KeyboardReport_Data_t* report){
if(state->remaining){
--state->remaining;
hid_keycode event;
seeprom_read_var(event, state->cursor++);
ExtraKeyboardReport_toggle(&state->report, event);
ExtraKeyboardReport_append(&state->report, report);
return true;
}
else{
return false;
}
err:
buzzer_start_f(200, BUZZER_FAILURE_TONE);
return false;
}
#endif // USE_EEPROM