Skip to content

Commit 2c0de0e

Browse files
committed
update lib and add usb keyboard demo
1 parent 0fd54b7 commit 2c0de0e

File tree

9 files changed

+386
-260
lines changed

9 files changed

+386
-260
lines changed

examples/Basic/keyboard/inputText/inputText.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author SeanKwok (shaoxiang@m5stack.com)
44
* @brief M5Cardputer input text test
55
* @version 0.1
6-
* @date 2023-10-09
6+
* @date 2023-10-13
77
*
88
*
99
* @Hardwares: M5Cardputer
@@ -47,7 +47,7 @@ void loop() {
4747
if (M5Cardputer.Keyboard.isPressed()) {
4848
Keyboard_Class::KeysState status = M5Cardputer.Keyboard.keysState();
4949

50-
for (auto i : status.values) {
50+
for (auto i : status.word) {
5151
data += i;
5252
}
5353

examples/Basic/keyboard/multiPress/multiPress.ino

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author SeanKwok (shaoxiang@m5stack.com)
44
* @brief M5Cardputer multi key test
55
* @version 0.1
6-
* @date 2023-10-09
6+
* @date 2023-10-13
77
*
88
*
99
* @Hardwares: M5Cardputer
@@ -35,18 +35,13 @@ void loop() {
3535
if (M5Cardputer.Keyboard.isPressed()) {
3636
Keyboard_Class::KeysState status = M5Cardputer.Keyboard.keysState();
3737
String keyStr = "";
38-
for (auto i : status.values) {
38+
for (auto i : status.word) {
3939
if (keyStr != "") {
4040
keyStr = keyStr + "+" + i;
4141
} else {
4242
keyStr += i;
4343
}
4444
}
45-
Serial.println("key num: ");
46-
for (auto i : status.keys) {
47-
Serial.printf("%d ", i);
48-
}
49-
Serial.println("");
5045
M5Cardputer.Display.clear();
5146
M5Cardputer.Display.drawString(keyStr,
5247
M5Cardputer.Display.width() / 2,

examples/Basic/keyboard/singlePress/singlePress.ino

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author SeanKwok (shaoxiang@m5stack.com)
44
* @brief M5Cardputer single key test
55
* @version 0.1
6-
* @date 2023-10-09
6+
* @date 2023-10-13
77
*
88
*
99
* @Hardwares: M5Cardputer
@@ -23,22 +23,22 @@ void setup() {
2323
M5Cardputer.Display.setTextDatum(middle_center);
2424
M5Cardputer.Display.setTextFont(&fonts::FreeSerifBoldItalic18pt7b);
2525
M5Cardputer.Display.setTextSize(1);
26-
M5Cardputer.Display.drawString("Press m Key",
26+
M5Cardputer.Display.drawString("Press a Key",
2727
M5Cardputer.Display.width() / 2,
2828
M5Cardputer.Display.height() / 2);
2929
}
3030

3131
void loop() {
3232
M5Cardputer.update();
3333
if (M5Cardputer.Keyboard.isChange()) {
34-
if (M5Cardputer.Keyboard.isKeyPressed(KB_KEY_L_M)) {
34+
if (M5Cardputer.Keyboard.isKeyPressed('a')) {
3535
M5Cardputer.Display.clear();
36-
M5Cardputer.Display.drawString("m Pressed",
36+
M5Cardputer.Display.drawString("a Pressed",
3737
M5Cardputer.Display.width() / 2,
3838
M5Cardputer.Display.height() / 2);
3939
} else {
4040
M5Cardputer.Display.clear();
41-
M5Cardputer.Display.drawString("Press m Key",
41+
M5Cardputer.Display.drawString("Press a Key",
4242
M5Cardputer.Display.width() / 2,
4343
M5Cardputer.Display.height() / 2);
4444
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @file usbKeyboard.ino
3+
* @author SeanKwok (shaoxiang@m5stack.com)
4+
* @brief M5Cardputer USB Keyboard test
5+
* @version 0.1
6+
* @date 2023-10-13
7+
*
8+
*
9+
* @Hardwares: M5Cardputer
10+
* @Platform Version: Arduino M5Stack Board Manager v2.0.7
11+
* @Dependent Library:
12+
* M5GFX: https://github.com/m5stack/M5GFX
13+
* M5Unified: https://github.com/m5stack/M5Unified
14+
*/
15+
16+
#include "M5Cardputer.h"
17+
#include "USB.h"
18+
#include "USBHIDKeyboard.h"
19+
USBHIDKeyboard Keyboard;
20+
21+
void setup() {
22+
auto cfg = M5.config();
23+
M5Cardputer.begin(cfg, true);
24+
M5Cardputer.Display.setRotation(1);
25+
M5Cardputer.Display.setTextColor(GREEN);
26+
M5Cardputer.Display.setTextDatum(middle_center);
27+
M5Cardputer.Display.setTextFont(&fonts::Orbitron_Light_24);
28+
M5Cardputer.Display.setTextSize(1);
29+
M5Cardputer.Display.drawString("USB Keyboard",
30+
M5Cardputer.Display.width() / 2,
31+
M5Cardputer.Display.height() / 2);
32+
Keyboard.begin();
33+
USB.begin();
34+
}
35+
36+
void loop() {
37+
M5Cardputer.update();
38+
// max press 3 button at the same time
39+
if (M5Cardputer.Keyboard.isChange()) {
40+
if (M5Cardputer.Keyboard.isPressed()) {
41+
Keyboard_Class::KeysState status = M5Cardputer.Keyboard.keysState();
42+
// for (auto i : status.word) {
43+
// Keyboard.press(i);
44+
// }
45+
KeyReport report = {0};
46+
report.modifiers = status.modifiers;
47+
uint8_t index = 0;
48+
for (auto i : status.hid_keys) {
49+
report.keys[index] = i;
50+
index++;
51+
if (index > 5) {
52+
index = 5;
53+
}
54+
}
55+
Keyboard.sendReport(&report);
56+
Keyboard.releaseAll();
57+
58+
// only text for display
59+
String keyStr = "";
60+
for (auto i : status.word) {
61+
if (keyStr != "") {
62+
keyStr = keyStr + "+" + i;
63+
} else {
64+
keyStr += i;
65+
}
66+
}
67+
68+
if (keyStr.length() > 0) {
69+
M5Cardputer.Display.clear();
70+
M5Cardputer.Display.drawString(
71+
keyStr, M5Cardputer.Display.width() / 2,
72+
M5Cardputer.Display.height() / 2);
73+
}
74+
75+
} else {
76+
M5Cardputer.Display.clear();
77+
M5Cardputer.Display.drawString("USB Keyboard",
78+
M5Cardputer.Display.width() / 2,
79+
M5Cardputer.Display.height() / 2);
80+
}
81+
}
82+
}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"type": "git",
1111
"url": "https://github.com/m5stack/M5Cardputer.git"
1212
},
13-
"version": "1.0.1",
13+
"version": "1.0.2",
1414
"frameworks": ["arduino"],
1515
"platforms": ["espressif32", "native"],
1616
"headers": "M5Cardputer.h"

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=M5Cardputer
2-
version=1.0.1
2+
version=1.0.2
33
author=M5Stack
44
maintainer=M5Stack
55
sentence=Library for M5Stack M5Cardputer Board

src/utility/Keyboard.cpp

Lines changed: 58 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,18 @@ void Keyboard_Class::begin() {
5555
_set_output(output_list, 0);
5656
}
5757

58-
int Keyboard_Class::getKeyCode(Point2D_t keyCoor) {
59-
int ret = 0;
58+
uint8_t Keyboard_Class::getKey(Point2D_t keyCoor) {
59+
uint8_t ret = 0;
6060

6161
if ((keyCoor.x < 0) || (keyCoor.y < 0)) {
6262
return 0;
6363
}
64-
65-
// ret = (keyCoor.y * 14) + (keyCoor.x + 1);
6664
if (_keys_state_buffer.ctrl || _keys_state_buffer.shift ||
6765
_is_caps_locked) {
68-
ret = _key_value_map[keyCoor.y][keyCoor.x].value_num_second;
66+
ret = _key_value_map[keyCoor.y][keyCoor.x].value_second;
6967
} else {
70-
ret = _key_value_map[keyCoor.y][keyCoor.x].value_num_first;
68+
ret = _key_value_map[keyCoor.y][keyCoor.x].value_first;
7169
}
72-
7370
return ret;
7471
}
7572

@@ -117,10 +114,10 @@ bool Keyboard_Class::isChange() {
117114
}
118115
}
119116

120-
bool Keyboard_Class::isKeyPressed(int keyCode) {
117+
bool Keyboard_Class::isKeyPressed(char c) {
121118
if (_key_list_buffer.size()) {
122119
for (const auto& i : _key_list_buffer) {
123-
if (getKeyCode(i) == keyCode) return true;
120+
if (getKey(i) == c) return true;
124121
}
125122
}
126123
return false;
@@ -130,66 +127,94 @@ bool Keyboard_Class::isKeyPressed(int keyCode) {
130127

131128
void Keyboard_Class::updateKeysState() {
132129
_keys_state_buffer.reset();
133-
_key_values_without_special_keys.clear();
130+
_key_pos_print_keys.clear();
131+
_key_pos_hid_keys.clear();
132+
_key_pos_modifier_keys.clear();
134133

135134
// Get special keys
136135
for (auto& i : _key_list_buffer) {
137-
if (strcmp(getKeyValue(i).value_first, "tab") == 0) {
138-
_keys_state_buffer.tab = true;
139-
continue;
140-
}
141-
142-
if (strcmp(getKeyValue(i).value_first, "fn") == 0) {
136+
// modifier
137+
if (getKeyValue(i).value_first == KEY_FN) {
143138
_keys_state_buffer.fn = true;
144139
continue;
145140
}
146-
147-
if (strcmp(getKeyValue(i).value_first, "shift") == 0) {
148-
_keys_state_buffer.shift = true;
141+
if (getKeyValue(i).value_first == KEY_OPT) {
142+
_keys_state_buffer.opt = true;
149143
continue;
150144
}
151145

152-
if (strcmp(getKeyValue(i).value_first, "ctrl") == 0) {
146+
if (getKeyValue(i).value_first == KEY_LEFT_CTRL) {
153147
_keys_state_buffer.ctrl = true;
148+
_key_pos_modifier_keys.push_back(i);
154149
continue;
155150
}
156-
if (strcmp(getKeyValue(i).value_first, "opt") == 0) {
157-
_keys_state_buffer.opt = true;
151+
152+
if (getKeyValue(i).value_first == KEY_LEFT_SHIFT) {
153+
_keys_state_buffer.shift = true;
154+
_key_pos_modifier_keys.push_back(i);
158155
continue;
159156
}
160157

161-
if (strcmp(getKeyValue(i).value_first, "alt") == 0) {
158+
if (getKeyValue(i).value_first == KEY_LEFT_ALT) {
162159
_keys_state_buffer.alt = true;
160+
_key_pos_modifier_keys.push_back(i);
163161
continue;
164162
}
165163

166-
if (strcmp(getKeyValue(i).value_first, "del") == 0) {
164+
// function
165+
if (getKeyValue(i).value_first == KEY_TAB) {
166+
_keys_state_buffer.tab = true;
167+
_key_pos_hid_keys.push_back(i);
168+
continue;
169+
}
170+
171+
if (getKeyValue(i).value_first == KEY_BACKSPACE) {
167172
_keys_state_buffer.del = true;
173+
_key_pos_hid_keys.push_back(i);
168174
continue;
169175
}
170176

171-
if (strcmp(getKeyValue(i).value_first, "enter") == 0) {
177+
if (getKeyValue(i).value_first == KEY_ENTER) {
172178
_keys_state_buffer.enter = true;
179+
_key_pos_hid_keys.push_back(i);
173180
continue;
174181
}
175182

176-
if (strcmp(getKeyValue(i).value_first, "space") == 0) {
183+
if (getKeyValue(i).value_first == ' ') {
177184
_keys_state_buffer.space = true;
178-
continue;
179185
}
186+
_key_pos_hid_keys.push_back(i);
187+
_key_pos_print_keys.push_back(i);
188+
}
189+
190+
for (auto& i : _key_pos_modifier_keys) {
191+
uint8_t key = getKeyValue(i).value_first;
192+
_keys_state_buffer.modifier_keys.push_back(key);
193+
}
194+
195+
for (auto& k : _keys_state_buffer.modifier_keys) {
196+
_keys_state_buffer.modifiers |= (1 << (k - 0x80));
197+
}
180198

181-
_key_values_without_special_keys.push_back(i);
199+
for (auto& i : _key_pos_hid_keys) {
200+
uint8_t k = getKeyValue(i).value_first;
201+
if (k == KEY_TAB || k == KEY_BACKSPACE || k == KEY_ENTER) {
202+
_keys_state_buffer.hid_keys.push_back(k);
203+
continue;
204+
}
205+
uint8_t key = _kb_asciimap[k];
206+
if (key) {
207+
_keys_state_buffer.hid_keys.push_back(key);
208+
}
182209
}
183210

184211
// Deal what left
185-
for (auto& i : _key_values_without_special_keys) {
212+
for (auto& i : _key_pos_print_keys) {
186213
if (_keys_state_buffer.ctrl || _keys_state_buffer.shift ||
187214
_is_caps_locked) {
188-
_keys_state_buffer.values.push_back(*getKeyValue(i).value_second);
189-
_keys_state_buffer.keys.push_back(getKeyValue(i).value_num_second);
215+
_keys_state_buffer.word.push_back(getKeyValue(i).value_second);
190216
} else {
191-
_keys_state_buffer.values.push_back(*getKeyValue(i).value_first);
192-
_keys_state_buffer.keys.push_back(getKeyValue(i).value_num_first);
217+
_keys_state_buffer.word.push_back(getKeyValue(i).value_first);
193218
}
194219
}
195220
}

0 commit comments

Comments
 (0)