-
Notifications
You must be signed in to change notification settings - Fork 25
/
printing.c
279 lines (259 loc) · 7.69 KB
/
printing.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
/*
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 "printing.h"
#include "Keyboard.h"
#include "keystate.h"
#include "storage.h"
static storage_type print_buffer_type;
static const char* print_buffer;
void printing_set_buffer(const char* buf, storage_type typ){
print_buffer = buf;
print_buffer_type = typ;
}
char print_buffer_get(void){
switch(print_buffer_type){
case sram:
return (char)sram_read_byte((uint8_t*)print_buffer);
case avr_pgm:
return (char)avr_pgm_read_byte((uint8_t*)print_buffer);
case avr_eeprom:
return (char)avr_pgm_read_byte((uint8_t*)print_buffer);
case i2c_eeprom:
return (char)avr_pgm_read_byte((uint8_t*)print_buffer);
default:
return 0; // unsupported;
}
}
bool printing_buffer_empty(void){
return print_buffer_get() == '\0';
}
void printing_Fill_KeyboardReport(KeyboardReport_Data_t* ReportData){
// if the last report was a key, send empty. Otherwise send the
// next character from print_buffer
if(PrevKeyboardHIDReportBuffer.Modifier || PrevKeyboardHIDReportBuffer.KeyCode[0]){
return; // empty report
}
else{
char nextchar = print_buffer_get();
print_buffer++;
uint8_t key, mod;
char_to_keys(nextchar, &key, &mod);
ReportData->Modifier = mod;
ReportData->KeyCode[0] = key;
}
}
void char_to_keys(const char nextchar, hid_keycode* nextkey, hid_keycode* nextmod){
*nextkey = 0;
*nextmod = 0;
// letters:
uint8_t l = nextchar | 0x20;
if('a' <= l && 'z' >= l){
*nextkey = HID_KEYBOARD_SC_A + (l - 'a');
if(!(nextchar & 0x20)) *nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
}
else{
switch(nextchar){
case ' ':
*nextkey = HID_KEYBOARD_SC_SPACE;
break;
case '!':
*nextkey = HID_KEYBOARD_SC_1_AND_EXCLAMATION;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '"':
*nextkey = HID_KEYBOARD_SC_APOSTROPHE_AND_QUOTE;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '#':
*nextkey = HID_KEYBOARD_SC_3_AND_HASHMARK;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '$':
*nextkey = HID_KEYBOARD_SC_4_AND_DOLLAR;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '%':
*nextkey = HID_KEYBOARD_SC_5_AND_PERCENTAGE;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '&':
*nextkey = HID_KEYBOARD_SC_7_AND_AND_AMPERSAND;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '\'':
*nextkey = HID_KEYBOARD_SC_APOSTROPHE_AND_QUOTE;
break;
case '(':
*nextkey = HID_KEYBOARD_SC_9_AND_OPENING_PARENTHESIS;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case ')':
*nextkey = HID_KEYBOARD_SC_0_AND_CLOSING_PARENTHESIS;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '*':
*nextkey = HID_KEYBOARD_SC_8_AND_ASTERISK;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '+':
*nextkey = HID_KEYBOARD_SC_EQUAL_AND_PLUS;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case ',':
*nextkey = HID_KEYBOARD_SC_COMMA_AND_LESS_THAN_SIGN;
break;
case '-':
*nextkey = HID_KEYBOARD_SC_MINUS_AND_UNDERSCORE;
break;
case '.':
*nextkey = HID_KEYBOARD_SC_DOT_AND_GREATER_THAN_SIGN;
break;
case '/':
*nextkey = HID_KEYBOARD_SC_SLASH_AND_QUESTION_MARK;
break;
case '0':
*nextkey = HID_KEYBOARD_SC_0_AND_CLOSING_PARENTHESIS;
break;
case '1':
*nextkey = HID_KEYBOARD_SC_1_AND_EXCLAMATION;
break;
case '2':
*nextkey = HID_KEYBOARD_SC_2_AND_AT;
break;
case '3':
*nextkey = HID_KEYBOARD_SC_3_AND_HASHMARK;
break;
case '4':
*nextkey = HID_KEYBOARD_SC_4_AND_DOLLAR;
break;
case '5':
*nextkey = HID_KEYBOARD_SC_5_AND_PERCENTAGE;
break;
case '6':
*nextkey = HID_KEYBOARD_SC_6_AND_CARET;
break;
case '7':
*nextkey = HID_KEYBOARD_SC_7_AND_AND_AMPERSAND;
break;
case '8':
*nextkey = HID_KEYBOARD_SC_8_AND_ASTERISK;
break;
case '9':
*nextkey = HID_KEYBOARD_SC_9_AND_OPENING_PARENTHESIS;
break;
case ':':
*nextkey = HID_KEYBOARD_SC_SEMICOLON_AND_COLON;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case ';':
*nextkey = HID_KEYBOARD_SC_SEMICOLON_AND_COLON;
break;
case '<':
*nextkey = HID_KEYBOARD_SC_COMMA_AND_LESS_THAN_SIGN;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '=':
*nextkey = HID_KEYBOARD_SC_EQUAL_AND_PLUS;
break;
case '>':
*nextkey = HID_KEYBOARD_SC_DOT_AND_GREATER_THAN_SIGN;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '?':
*nextkey = HID_KEYBOARD_SC_SLASH_AND_QUESTION_MARK;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '@':
*nextkey = HID_KEYBOARD_SC_2_AND_AT;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '[':
*nextkey = HID_KEYBOARD_SC_OPENING_BRACKET_AND_OPENING_BRACE;
break;
case '\\':
*nextkey = HID_KEYBOARD_SC_BACKSLASH_AND_PIPE;
break;
case ']':
*nextkey = HID_KEYBOARD_SC_CLOSING_BRACKET_AND_CLOSING_BRACE;
break;
case '^':
*nextkey = HID_KEYBOARD_SC_6_AND_CARET;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '_':
*nextkey = HID_KEYBOARD_SC_MINUS_AND_UNDERSCORE;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '`':
*nextkey = HID_KEYBOARD_SC_GRAVE_ACCENT_AND_TILDE;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '{':
*nextkey = HID_KEYBOARD_SC_OPENING_BRACKET_AND_OPENING_BRACE;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '|':
*nextkey = HID_KEYBOARD_SC_BACKSLASH_AND_PIPE;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '}':
*nextkey = HID_KEYBOARD_SC_CLOSING_BRACKET_AND_CLOSING_BRACE;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
case '~':
*nextkey = HID_KEYBOARD_SC_GRAVE_ACCENT_AND_TILDE;
break;
case '\n':
*nextkey = HID_KEYBOARD_SC_ENTER;
break;
case '\t':
*nextkey = HID_KEYBOARD_SC_TAB;
break;
default:
*nextkey = HID_KEYBOARD_SC_SLASH_AND_QUESTION_MARK;
*nextmod = HID_KEYBOARD_MODIFER_LEFTSHIFT;
break;
}
}
}
// For serial eeprom testing, to allow the eeprom contents to be dumped
const char* byte_to_str(uint8_t byte){
static char buf[4] = {'x', 'x', ' ', '\0'};
for(int8_t i = 1; i >= 0; --i){
uint8_t n = byte & 0xF;
buf[i] = n >= 0xA ? ('A' + n - 0xA) : ('0' + n);
byte >>= 4;
}
return buf;
}