Skip to content

Commit a4dd9f6

Browse files
author
Bluebie
committed
DigiKeyboard: Added print/println stuff so you can easily type in strings and numbers and bits like that. scancode-ascii-table could be compressed better, it has a bunch of zeros in it currently, all near the start!
1 parent 257c92a commit a4dd9f6

File tree

3 files changed

+153
-28
lines changed

3 files changed

+153
-28
lines changed

libraries/DigisparkKeyboard/DigiKeyboard.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <string.h>
1515

1616
#include "usbdrv.h"
17+
#include "scancode-ascii-table.h"
1718

1819
// TODO: Work around Arduino 12 issues better.
1920
//#include <WConstants.h>
@@ -128,7 +129,7 @@ PROGMEM char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = { /*
128129
#define KEY_ARROW_LEFT 0x50
129130

130131

131-
class DigiKeyboardDevice {
132+
class DigiKeyboardDevice : public Print {
132133
public:
133134
DigiKeyboardDevice () {
134135
cli();
@@ -194,9 +195,16 @@ class DigiKeyboardDevice {
194195
memset(reportBuffer, 0, sizeof(reportBuffer));
195196
usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
196197
}
198+
199+
size_t write(uint8_t chr) {
200+
uint8_t data = pgm_read_byte_near(ascii_to_scan_code_table + (chr - 8));
201+
sendKeyStroke(data & 0b01111111, data >> 7 ? MOD_SHIFT_RIGHT : 0);
202+
return 1;
203+
}
197204

198205
//private: TODO: Make friend?
199206
uchar reportBuffer[2]; // buffer for HID reports [ 1 modifier byte + (len-1) key strokes]
207+
using Print::write;
200208
};
201209

202210
DigiKeyboardDevice DigiKeyboard = DigiKeyboardDevice();

libraries/DigisparkKeyboard/examples/Keyboard/Keyboard.ino

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,16 @@ void setup() {
66

77

88
void loop() {
9-
// running DigiKeyboard.update() from time to time is a good idea - it talks to the computer, answering any
10-
// requests the computer has made recently. If you use DigiKeyboard.delay() plentifully you can omit this
11-
// as DigiKeyboard.delay() calls update internally.
12-
DigiKeyboard.update();
13-
14-
// this is generally not necessary but with some older systems it seems to prevent missing the first character after a delay:
9+
// this is generally not necessary but with some older systems it seems to
10+
// prevent missing the first character after a delay:
1511
DigiKeyboard.sendKeyStroke(0);
16-
17-
// send out some keys
18-
DigiKeyboard.sendKeyStroke(KEY_H, MOD_SHIFT_RIGHT);
19-
DigiKeyboard.sendKeyStroke(KEY_E);
20-
DigiKeyboard.sendKeyStroke(KEY_L);
21-
DigiKeyboard.sendKeyStroke(KEY_L);
22-
DigiKeyboard.sendKeyStroke(KEY_O);
23-
DigiKeyboard.sendKeyStroke(KEY_SPACE);
24-
DigiKeyboard.sendKeyStroke(KEY_D);
25-
DigiKeyboard.sendKeyStroke(KEY_I);
26-
DigiKeyboard.sendKeyStroke(KEY_G);
27-
DigiKeyboard.sendKeyStroke(KEY_I);
28-
DigiKeyboard.sendKeyStroke(KEY_S);
29-
DigiKeyboard.sendKeyStroke(KEY_P);
30-
DigiKeyboard.sendKeyStroke(KEY_A);
31-
DigiKeyboard.sendKeyStroke(KEY_R);
32-
DigiKeyboard.sendKeyStroke(KEY_K);
33-
DigiKeyboard.sendKeyStroke(KEY_ENTER);
34-
35-
// It's better to use DigiKeyboard.delay() over the regular Arduino delay() because it keeps
36-
// talking to the computer to make sure the computer knows the keyboard is alive and connected
12+
13+
// Type out this string letter by letter on the computer (assumes US-style
14+
// keyboard)
15+
DigiKeyboard.println("Hello Digispark!");
16+
17+
// It's better to use DigiKeyboard.delay() over the regular Arduino delay()
18+
// if doing keyboard stuff because it keeps talking to the computer to make
19+
// sure the computer knows the keyboard is alive and connected
3720
DigiKeyboard.delay(5000);
3821
}
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include <avr/pgmspace.h>
2+
// Lookup table to convert ascii characters in to keyboard scan codes
3+
// Format: most signifficant bit indicates if scan code should be sent with shift modifier
4+
// remaining 7 bits are to be used as scan code number.
5+
6+
const unsigned char ascii_to_scan_code_table[] PROGMEM = {
7+
// /* ASCII: 0 */ 0,
8+
// /* ASCII: 1 */ 0,
9+
// /* ASCII: 2 */ 0,
10+
// /* ASCII: 3 */ 0,
11+
// /* ASCII: 4 */ 0,
12+
// /* ASCII: 5 */ 0,
13+
// /* ASCII: 6 */ 0,
14+
// /* ASCII: 7 */ 0,
15+
/* ASCII: 8 */ 42,
16+
/* ASCII: 9 */ 43,
17+
/* ASCII: 10 */ 40,
18+
/* ASCII: 11 */ 0,
19+
/* ASCII: 12 */ 0,
20+
/* ASCII: 13 */ 0,
21+
/* ASCII: 14 */ 0,
22+
/* ASCII: 15 */ 0,
23+
/* ASCII: 16 */ 0,
24+
/* ASCII: 17 */ 0,
25+
/* ASCII: 18 */ 0,
26+
/* ASCII: 19 */ 0,
27+
/* ASCII: 20 */ 0,
28+
/* ASCII: 21 */ 0,
29+
/* ASCII: 22 */ 0,
30+
/* ASCII: 23 */ 0,
31+
/* ASCII: 24 */ 0,
32+
/* ASCII: 25 */ 0,
33+
/* ASCII: 26 */ 0,
34+
/* ASCII: 27 */ 41,
35+
/* ASCII: 28 */ 0,
36+
/* ASCII: 29 */ 0,
37+
/* ASCII: 30 */ 0,
38+
/* ASCII: 31 */ 0,
39+
/* ASCII: 32 */ 44,
40+
/* ASCII: 33 */ 158,
41+
/* ASCII: 34 */ 180,
42+
/* ASCII: 35 */ 160,
43+
/* ASCII: 36 */ 161,
44+
/* ASCII: 37 */ 162,
45+
/* ASCII: 38 */ 164,
46+
/* ASCII: 39 */ 52,
47+
/* ASCII: 40 */ 166,
48+
/* ASCII: 41 */ 167,
49+
/* ASCII: 42 */ 165,
50+
/* ASCII: 43 */ 174,
51+
/* ASCII: 44 */ 54,
52+
/* ASCII: 45 */ 45,
53+
/* ASCII: 46 */ 55,
54+
/* ASCII: 47 */ 56,
55+
/* ASCII: 48 */ 39,
56+
/* ASCII: 49 */ 30,
57+
/* ASCII: 50 */ 31,
58+
/* ASCII: 51 */ 32,
59+
/* ASCII: 52 */ 33,
60+
/* ASCII: 53 */ 34,
61+
/* ASCII: 54 */ 35,
62+
/* ASCII: 55 */ 36,
63+
/* ASCII: 56 */ 37,
64+
/* ASCII: 57 */ 38,
65+
/* ASCII: 58 */ 179,
66+
/* ASCII: 59 */ 51,
67+
/* ASCII: 60 */ 182,
68+
/* ASCII: 61 */ 46,
69+
/* ASCII: 62 */ 183,
70+
/* ASCII: 63 */ 184,
71+
/* ASCII: 64 */ 159,
72+
/* ASCII: 65 */ 132,
73+
/* ASCII: 66 */ 133,
74+
/* ASCII: 67 */ 134,
75+
/* ASCII: 68 */ 135,
76+
/* ASCII: 69 */ 136,
77+
/* ASCII: 70 */ 137,
78+
/* ASCII: 71 */ 138,
79+
/* ASCII: 72 */ 139,
80+
/* ASCII: 73 */ 140,
81+
/* ASCII: 74 */ 141,
82+
/* ASCII: 75 */ 142,
83+
/* ASCII: 76 */ 143,
84+
/* ASCII: 77 */ 144,
85+
/* ASCII: 78 */ 145,
86+
/* ASCII: 79 */ 146,
87+
/* ASCII: 80 */ 147,
88+
/* ASCII: 81 */ 148,
89+
/* ASCII: 82 */ 149,
90+
/* ASCII: 83 */ 150,
91+
/* ASCII: 84 */ 151,
92+
/* ASCII: 85 */ 152,
93+
/* ASCII: 86 */ 153,
94+
/* ASCII: 87 */ 154,
95+
/* ASCII: 88 */ 155,
96+
/* ASCII: 89 */ 156,
97+
/* ASCII: 90 */ 157,
98+
/* ASCII: 91 */ 47,
99+
/* ASCII: 92 */ 49,
100+
/* ASCII: 93 */ 48,
101+
/* ASCII: 94 */ 163,
102+
/* ASCII: 95 */ 173,
103+
/* ASCII: 96 */ 53,
104+
/* ASCII: 97 */ 4,
105+
/* ASCII: 98 */ 5,
106+
/* ASCII: 99 */ 6,
107+
/* ASCII: 100 */ 7,
108+
/* ASCII: 101 */ 8,
109+
/* ASCII: 102 */ 9,
110+
/* ASCII: 103 */ 10,
111+
/* ASCII: 104 */ 11,
112+
/* ASCII: 105 */ 12,
113+
/* ASCII: 106 */ 13,
114+
/* ASCII: 107 */ 14,
115+
/* ASCII: 108 */ 15,
116+
/* ASCII: 109 */ 16,
117+
/* ASCII: 110 */ 17,
118+
/* ASCII: 111 */ 18,
119+
/* ASCII: 112 */ 19,
120+
/* ASCII: 113 */ 20,
121+
/* ASCII: 114 */ 21,
122+
/* ASCII: 115 */ 22,
123+
/* ASCII: 116 */ 23,
124+
/* ASCII: 117 */ 24,
125+
/* ASCII: 118 */ 25,
126+
/* ASCII: 119 */ 26,
127+
/* ASCII: 120 */ 27,
128+
/* ASCII: 121 */ 28,
129+
/* ASCII: 122 */ 29,
130+
/* ASCII: 123 */ 175,
131+
/* ASCII: 124 */ 177,
132+
/* ASCII: 125 */ 176,
133+
/* ASCII: 126 */ 181
134+
};

0 commit comments

Comments
 (0)