forked from chrisandreae/keyboard-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro.c
271 lines (218 loc) · 8.25 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
/*
Kinesis ergonomic keyboard firmware replacement
Copyright 2012 Chris Andreae (chris (at) andreae.gen.nz)
Licensed under the GNU GPL v2 (see GPL2.txt).
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_index.h"
#include "macro.h"
#include "usb.h"
#include "printing.h"
#include "storage.h"
#include "buzzer.h"
#include <stdint.h>
#include <stdlib.h>
#include <util/delay.h>
// The macro data itself is in external eeprom
static uint8_t macros_storage[MACROS_SIZE] STORAGE(MACROS_STORAGE);
static uint16_t *const macros_end_offset = (uint16_t*)macros_storage;
static uint8_t *const macros = macros_storage + sizeof(uint16_t);
/////////////// Recording and Playback Data ////////////////////
static struct _macro_recording_state {
macro_data* macro;
hid_keycode* cursor;
macro_idx_entry* index_entry;
} recording_state;
static struct _macro_playback_state {
uint16_t remaining;
hid_keycode* cursor; // pointer to serial eeprom memory
ExtraKeyboardReport report;
} playback_state;
////////////////////// Macro Management ////////////////////////
uint8_t* macros_get_storage(){
return ¯os_storage[0];
}
void macros_reset_defaults(){
volatile uint16_t zero = 0x0;
storage_write(MACROS_STORAGE, (uint8_t*)macros_end_offset, (uint8_t*)&zero, sizeof(uint16_t));
}
static macro_data* macros_get_macro_pointer(uint16_t offset){
return (macro_data*) ¯os[offset];
}
// convenience macro for reading from eeprom and handling errors
#define macro_storage_read_var(var, ptr) \
if(storage_read(MACROS_STORAGE, (uint8_t*)ptr, (uint8_t*)&var, sizeof(typeof(var))) != sizeof(typeof(var))){ goto err; }
#define macro_storage_write_var(ptr, var) \
if(storage_write(MACROS_STORAGE, (uint8_t*)ptr, (uint8_t*)&var, sizeof(typeof(var))) != sizeof(typeof(var))){ goto err; }
typedef struct {
uint16_t offset;
uint16_t len;
} macro_range;
static void macro_shift_down_iterator(macro_idx_entry* entry, macro_range* range){
macro_idx_entry_data d = macro_idx_get_data(entry);
if(d.type != MACRO) return;
// offset of start of macro is in d.data: if it's greater than the
// removed range, subtract the removed length
if(d.data > range->offset){
d.data -= range->len;
macro_idx_set_data(entry, d);
}
}
/**
* internal function: remove any macro data for the given macro index
* entry. Returns true if no error, false if error.
*/
static bool delete_macro_data(macro_idx_entry* idx_entry){
macro_idx_entry_data idx_data = macro_idx_get_data(idx_entry);
if(idx_data.type != MACRO) return true; // no data to delete, trivial success
uint16_t entry_offset = idx_data.data;
macro_data* entry = (macro_data*) (¯os[entry_offset]);
// read the macro space end offset
uint16_t end_offset;
macro_storage_read_var(end_offset, macros_end_offset);
// Read the length of the macro to be deleted
uint16_t entry_len;
macro_storage_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(storage_memmove(MACROS_STORAGE, ¯os[entry_offset], ¯os[rest_offset], rest_len) != SUCCESS){ goto err; }
// Now scan the macro index, and move down any entries > entry_offset by entry_len
macro_range deleted_range;
deleted_range.offset = entry_offset;
deleted_range.len = entry_len;
macro_idx_iterate((macro_idx_iterator)macro_shift_down_iterator, &deleted_range);
}
// and update the saved macro end offset
end_offset -= entry_len;
macro_storage_write_var(macros_end_offset, end_offset);
return true;
err:
return false;
}
/////////// Macro Recording /////////////
/**
* 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_idx_key* key){
// Find or create a free entry:
macro_idx_entry* entry = macro_idx_lookup(key);
if(entry){
// There's already an entry for this key in the index: delete
// the old macro data if necessary and re-use this slot
if(!delete_macro_data(entry)) goto err;
}
else{
entry = macro_idx_create(key);
if(!entry) goto err;
}
// Now store the data in the entry:
macro_idx_entry_data new_entry_data;
new_entry_data.type = MACRO;
macro_storage_read_var(new_entry_data.data, macros_end_offset);
macro_idx_set_data(entry, new_entry_data);
// and set up the new macro for recording content
recording_state.macro = macros_get_macro_pointer(new_entry_data.data);
recording_state.cursor = &recording_state.macro->events[0];
recording_state.index_entry = entry;
return true;
err:
buzzer_start_f(200, BUZZER_FAILURE_TONE);
memset(&recording_state, 0x0, sizeof(recording_state));
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(!recording_state.macro){
// cannot commit no macro
goto err;
}
uint16_t macro_len = recording_state.cursor - &recording_state.macro->events[0];
if(macro_len == 0){
// find the macro in the index and remove it.
macro_idx_remove(recording_state.index_entry);
}
else{
macro_storage_write_var(&recording_state.macro->length, macro_len);
uint16_t end_offset;
macro_storage_read_var(end_offset, macros_end_offset);
end_offset += macro_len + 2; // length header + data
macro_storage_write_var(macros_end_offset, end_offset);
buzzer_start_f(200, BUZZER_SUCCESS_TONE);
}
memset(&recording_state, 0x0, sizeof(recording_state));
return;
err:
buzzer_start_f(200, BUZZER_FAILURE_TONE);
}
void macros_abort_macro(){
macro_idx_remove(recording_state.index_entry);
memset(&recording_state, 0x0, sizeof(recording_state));
}
bool macros_append(hid_keycode event){
macro_storage_write_var(recording_state.cursor++, event);
return true;
err:
return false;
}
////// Macro Playback /////
bool macros_start_playback(uint16_t macro_offset){
macro_data* macro = macros_get_macro_pointer(macro_offset);
ExtraKeyboardReport_clear(&playback_state.report);
playback_state.cursor = ¯o->events[0];
macro_storage_read_var(playback_state.remaining, ¯o->length);
return true;
err:
return false;
}
bool macros_fill_next_report(KeyboardReport_Data_t* report){
if(playback_state.remaining){
--playback_state.remaining;
hid_keycode event;
macro_storage_read_var(event, playback_state.cursor++);
ExtraKeyboardReport_toggle(&playback_state.report, event);
ExtraKeyboardReport_append(&playback_state.report, report);
}
return playback_state.remaining ? true : false;
err:
buzzer_start_f(200, BUZZER_FAILURE_TONE);
return false;
}