Skip to content

Commit 2fe3b4c

Browse files
committed
update example
1 parent 402f0d1 commit 2fe3b4c

File tree

7 files changed

+260
-676
lines changed

7 files changed

+260
-676
lines changed

examples/Basic/keyboard/multiPress/multiPress.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ void setup() {
2323
M5Cardputer.Display.setTextDatum(middle_center);
2424
M5Cardputer.Display.setTextFont(&fonts::FreeSerifBoldItalic18pt7b);
2525
M5Cardputer.Display.setTextSize(1);
26+
M5Cardputer.Display.drawString("Press Any Key",
27+
M5Cardputer.Display.width() / 2,
28+
M5Cardputer.Display.height() / 2);
2629
}
2730

2831
void loop() {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @file singlePress.ino
3+
* @author SeanKwok (shaoxiang@m5stack.com)
4+
* @brief M5Cardputer single key test
5+
* @version 0.1
6+
* @date 2023-10-09
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+
18+
void setup() {
19+
auto cfg = M5.config();
20+
M5Cardputer.begin(cfg, true);
21+
M5Cardputer.Display.setRotation(1);
22+
M5Cardputer.Display.setTextColor(GREEN);
23+
M5Cardputer.Display.setTextDatum(middle_center);
24+
M5Cardputer.Display.setTextFont(&fonts::FreeSerifBoldItalic18pt7b);
25+
M5Cardputer.Display.setTextSize(1);
26+
M5Cardputer.Display.drawString("Press m Key",
27+
M5Cardputer.Display.width() / 2,
28+
M5Cardputer.Display.height() / 2);
29+
}
30+
31+
void loop() {
32+
M5Cardputer.update();
33+
if (M5Cardputer.Keyboard.isChange()) {
34+
if (M5Cardputer.Keyboard.isKeyPressed(KB_KEY_L_M)) {
35+
M5Cardputer.Display.clear();
36+
M5Cardputer.Display.drawString("m Pressed",
37+
M5Cardputer.Display.width() / 2,
38+
M5Cardputer.Display.height() / 2);
39+
} else {
40+
M5Cardputer.Display.clear();
41+
M5Cardputer.Display.drawString("Press m Key",
42+
M5Cardputer.Display.width() / 2,
43+
M5Cardputer.Display.height() / 2);
44+
}
45+
}
46+
}

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ category=Display
88
url=https://github.com/m5stack/M5Cardputer.git
99
architectures=esp32
1010
includes=M5Cardputer.h
11-
depends=M5Unified,M5GFX
11+
depends=M5Unified,M5GFX

src/M5Cardputer.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
#ifndef M5DIAL_H
2-
#define M5DIAL_H
1+
#ifndef M5CARDPUTER_H
2+
#define M5CARDPUTER_H
33

44
#include "M5Unified.h"
5-
6-
// #include "utility/Encoder.h"
75
#include "utility/Keyboard.h"
86

97
namespace m5 {

src/utility/Keyboard.cpp

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -55,50 +55,20 @@ void Keyboard_Class::begin() {
5555
_set_output(output_list, 0);
5656
}
5757

58-
Point2D_t Keyboard_Class::getKey() {
59-
Point2D_t coor;
60-
coor.x = -1;
61-
coor.y = -1;
62-
63-
uint8_t input_value = 0;
64-
65-
for (int i = 0; i < 8; i++) {
66-
_set_output(output_list, i);
67-
input_value = _get_input(input_list);
68-
69-
/* If key pressed */
70-
if (input_value) {
71-
/* Get X */
72-
for (int j = 0; j < 7; j++) {
73-
if (input_value == X_map_chart[j].value) {
74-
coor.x = (i > 3) ? X_map_chart[j].x_1 : X_map_chart[j].x_2;
75-
break;
76-
}
77-
}
78-
79-
/* Get Y */
80-
coor.y = (i > 3) ? (i - 4) : i;
81-
82-
/* Keep the same as picture */
83-
coor.y = -coor.y;
84-
coor.y = coor.y + 3;
85-
86-
break;
87-
}
88-
}
89-
90-
// printf("%d,%d\n", x, y);
91-
return coor;
92-
}
93-
94-
uint8_t Keyboard_Class::getKeyNum(Point2D_t keyCoor) {
95-
uint8_t ret = 0;
58+
int Keyboard_Class::getKeyCode(Point2D_t keyCoor) {
59+
int ret = 0;
9660

9761
if ((keyCoor.x < 0) || (keyCoor.y < 0)) {
9862
return 0;
9963
}
10064

101-
ret = (keyCoor.y * 14) + (keyCoor.x + 1);
65+
// ret = (keyCoor.y * 14) + (keyCoor.x + 1);
66+
if (_keys_state_buffer.ctrl || _keys_state_buffer.shift ||
67+
_is_caps_locked) {
68+
ret = _key_value_map[keyCoor.y][keyCoor.x].value_num_second;
69+
} else {
70+
ret = _key_value_map[keyCoor.y][keyCoor.x].value_num_first;
71+
}
10272

10373
return ret;
10474
}
@@ -147,18 +117,14 @@ bool Keyboard_Class::isChange() {
147117
}
148118
}
149119

150-
// bool Keyboard_Class::isPressed(uint16_t key) {
151-
// return _key_list_buffer.size();
152-
// }
153-
154-
// bool Keyboard_Class::isKeyPressed(int keyNum) {
155-
// if (_key_list_buffer.size()) {
156-
// for (const auto& i : _key_list_buffer) {
157-
// if (getKeyNum(i) == keyNum) return true;
158-
// }
159-
// }
160-
// return false;
161-
// }
120+
bool Keyboard_Class::isKeyPressed(int keyCode) {
121+
if (_key_list_buffer.size()) {
122+
for (const auto& i : _key_list_buffer) {
123+
if (getKeyCode(i) == keyCode) return true;
124+
}
125+
}
126+
return false;
127+
}
162128

163129
#include <cstring>
164130

src/utility/Keyboard.h

Lines changed: 63 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <iostream>
1313
#include <vector>
1414
#include "Arduino.h"
15+
#include "Keyboard_def.h"
1516

1617
struct Chart_t {
1718
uint8_t value;
@@ -33,65 +34,68 @@ const Chart_t X_map_chart[7] = {{1, 0, 1}, {2, 2, 3}, {4, 4, 5},
3334

3435
struct KeyValue_t {
3536
const char* value_first;
37+
const int value_num_first;
3638
const char* value_second;
39+
const int value_num_second;
3740
};
3841

39-
const KeyValue_t _key_value_map[4][14] = {{{"`", "~"},
40-
{"1", "!"},
41-
{"2", "@"},
42-
{"3", "#"},
43-
{"4", "$"},
44-
{"5", "%"},
45-
{"6", "^"},
46-
{"7", "&"},
47-
{"8", "*"},
48-
{"9", "("},
49-
{"0", ")"},
50-
{"-", "_"},
51-
{"=", "+"},
52-
{"del", "del"}},
53-
{{"tab", "tab"},
54-
{"q", "Q"},
55-
{"w", "W"},
56-
{"e", "E"},
57-
{"r", "R"},
58-
{"t", "T"},
59-
{"y", "Y"},
60-
{"u", "U"},
61-
{"i", "I"},
62-
{"o", "O"},
63-
{"p", "P"},
64-
{"[", "{"},
65-
{"]", "}"},
66-
{"\\", "|"}},
67-
{{"fn", "fn"},
68-
{"shift", "shift"},
69-
{"a", "A"},
70-
{"s", "S"},
71-
{"d", "D"},
72-
{"f", "F"},
73-
{"g", "G"},
74-
{"h", "H"},
75-
{"j", "J"},
76-
{"k", "K"},
77-
{"l", "L"},
78-
{";", ":"},
79-
{"'", "\""},
80-
{"enter", "enter"}},
81-
{{"ctrl", "ctrl"},
82-
{"opt", "opt"},
83-
{"alt", "alt"},
84-
{"z", "Z"},
85-
{"x", "X"},
86-
{"c", "C"},
87-
{"v", "V"},
88-
{"b", "B"},
89-
{"n", "N"},
90-
{"m", "M"},
91-
{",", "<"},
92-
{".", ">"},
93-
{"/", "?"},
94-
{"space", "space"}}};
42+
const KeyValue_t _key_value_map[4][14] = {
43+
{{"`", KB_KEY_BACK_QUOTE, "~", KB_KEY_TILDE},
44+
{"1", KB_KEY_1, "!", KB_KEY_BANG},
45+
{"2", KB_KEY_2, "@", KB_KEY_AT},
46+
{"3", KB_KEY_3, "#", KB_KEY_HASH},
47+
{"4", KB_KEY_4, "$", KB_KEY_DOLLAR},
48+
{"5", KB_KEY_5, "%", KB_KEY_PERCENT},
49+
{"6", KB_KEY_6, "^", KB_KEY_CARET},
50+
{"7", KB_KEY_7, "&", KB_KEY_AND},
51+
{"8", KB_KEY_8, "*", KB_KEY_ASTERISK},
52+
{"9", KB_KEY_9, "(", KB_KEY_L_PARENTHESES},
53+
{"0", KB_KEY_0, ")", KB_KEY_R_PARENTHESES},
54+
{"-", KB_KEY_MINUS, "_", KB_KEY_UNDERSCORE},
55+
{"=", KB_KEY_EQUAL, "+", KB_KEY_PLUS},
56+
{"del", KB_KEY_DEL, "del", KB_KEY_DEL}},
57+
{{"tab", KB_KEY_TAB, "tab", KB_KEY_TAB},
58+
{"q", KB_KEY_L_Q, "Q", KB_KEY_U_Q},
59+
{"w", KB_KEY_L_W, "W", KB_KEY_U_W},
60+
{"e", KB_KEY_L_E, "E", KB_KEY_U_E},
61+
{"r", KB_KEY_L_R, "R", KB_KEY_U_R},
62+
{"t", KB_KEY_L_T, "T", KB_KEY_U_T},
63+
{"y", KB_KEY_L_Y, "Y", KB_KEY_U_Y},
64+
{"u", KB_KEY_L_U, "U", KB_KEY_U_U},
65+
{"i", KB_KEY_L_I, "I", KB_KEY_U_I},
66+
{"o", KB_KEY_L_O, "O", KB_KEY_U_O},
67+
{"p", KB_KEY_L_P, "P", KB_KEY_U_P},
68+
{"[", KB_KEY_L_PARENTHESES, "{", KB_KEY_L_CURLY_BRACKETS},
69+
{"]", KB_KEY_R_PARENTHESES, "}", KB_KEY_R_CURLY_BRACKETS},
70+
{"\\", KB_KEY_BACK_SLASK, "|", KB_KEY_BAR}},
71+
{{"fn", KB_KEY_FN, "fn", KB_KEY_FN},
72+
{"shift", KB_KEY_SHIFT, "shift", KB_KEY_SHIFT},
73+
{"a", KB_KEY_L_A, "A", KB_KEY_U_A},
74+
{"s", KB_KEY_L_S, "S", KB_KEY_U_S},
75+
{"d", KB_KEY_L_D, "D", KB_KEY_U_D},
76+
{"f", KB_KEY_L_F, "F", KB_KEY_U_F},
77+
{"g", KB_KEY_L_G, "G", KB_KEY_U_G},
78+
{"h", KB_KEY_L_H, "H", KB_KEY_U_H},
79+
{"j", KB_KEY_L_J, "J", KB_KEY_U_J},
80+
{"k", KB_KEY_L_K, "K", KB_KEY_U_K},
81+
{"l", KB_KEY_L_L, "L", KB_KEY_U_L},
82+
{";", KB_KEY_SEMICOLON, ":", KB_KEY_COLON},
83+
{"'", KB_KEY_SIGLE_QUOTE, "\"", KB_KEY_QUOTE},
84+
{"enter", KB_KEY_ENTER, "enter", KB_KEY_ENTER}},
85+
{{"ctrl", KB_KEY_CTRL, "ctrl", KB_KEY_CTRL},
86+
{"opt", KB_KEY_OPT, "opt", KB_KEY_OPT},
87+
{"alt", KB_KEY_ALT, "alt", KB_KEY_ALT},
88+
{"z", KB_KEY_L_Z, "Z", KB_KEY_U_Z},
89+
{"x", KB_KEY_L_X, "X", KB_KEY_U_X},
90+
{"c", KB_KEY_L_C, "C", KB_KEY_U_C},
91+
{"v", KB_KEY_L_V, "V", KB_KEY_U_V},
92+
{"b", KB_KEY_L_B, "B", KB_KEY_U_B},
93+
{"n", KB_KEY_L_N, "N", KB_KEY_U_N},
94+
{"m", KB_KEY_L_M, "M", KB_KEY_U_M},
95+
{",", KB_KEY_COMMA, "<", KB_KEY_L_ANGLE_BRACKETS},
96+
{".", KB_KEY_DOT, ">", KB_KEY_R_ANGLE_BRACKETS},
97+
{"/", KB_KEY_SLASH, "?", KB_KEY_QUESTION},
98+
{"space", KB_KEY_SPACE, "space", KB_KEY_SPACE}}};
9599

96100
class Keyboard_Class {
97101
public:
@@ -138,9 +142,7 @@ class Keyboard_Class {
138142
}
139143

140144
void begin();
141-
142-
Point2D_t getKey();
143-
uint8_t getKeyNum(Point2D_t keyCoor);
145+
int getKeyCode(Point2D_t keyCoor);
144146

145147
void updateKeyList();
146148
inline std::vector<Point2D_t>& keyList() {
@@ -153,9 +155,8 @@ class Keyboard_Class {
153155

154156
uint8_t isPressed();
155157
bool isChange();
156-
bool isKeyPressing(int keyNum);
157-
void clearInputHistory();
158-
String getInputHistory();
158+
bool isKeyPressed(int keyCode);
159+
159160
void updateKeysState();
160161
inline KeysState& keysState() {
161162
return _keys_state_buffer;

0 commit comments

Comments
 (0)