Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
  • Loading branch information
Gitshaoxiang committed Oct 10, 2023
1 parent 402f0d1 commit 2fe3b4c
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 676 deletions.
3 changes: 3 additions & 0 deletions examples/Basic/keyboard/multiPress/multiPress.ino
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ void setup() {
M5Cardputer.Display.setTextDatum(middle_center);
M5Cardputer.Display.setTextFont(&fonts::FreeSerifBoldItalic18pt7b);
M5Cardputer.Display.setTextSize(1);
M5Cardputer.Display.drawString("Press Any Key",
M5Cardputer.Display.width() / 2,
M5Cardputer.Display.height() / 2);
}

void loop() {
Expand Down
46 changes: 46 additions & 0 deletions examples/Basic/keyboard/singlePress/singlePress.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @file singlePress.ino
* @author SeanKwok (shaoxiang@m5stack.com)
* @brief M5Cardputer single key test
* @version 0.1
* @date 2023-10-09
*
*
* @Hardwares: M5Cardputer
* @Platform Version: Arduino M5Stack Board Manager v2.0.7
* @Dependent Library:
* M5GFX: https://github.com/m5stack/M5GFX
* M5Unified: https://github.com/m5stack/M5Unified
*/

#include "M5Cardputer.h"

void setup() {
auto cfg = M5.config();
M5Cardputer.begin(cfg, true);
M5Cardputer.Display.setRotation(1);
M5Cardputer.Display.setTextColor(GREEN);
M5Cardputer.Display.setTextDatum(middle_center);
M5Cardputer.Display.setTextFont(&fonts::FreeSerifBoldItalic18pt7b);
M5Cardputer.Display.setTextSize(1);
M5Cardputer.Display.drawString("Press m Key",
M5Cardputer.Display.width() / 2,
M5Cardputer.Display.height() / 2);
}

void loop() {
M5Cardputer.update();
if (M5Cardputer.Keyboard.isChange()) {
if (M5Cardputer.Keyboard.isKeyPressed(KB_KEY_L_M)) {
M5Cardputer.Display.clear();
M5Cardputer.Display.drawString("m Pressed",
M5Cardputer.Display.width() / 2,
M5Cardputer.Display.height() / 2);
} else {
M5Cardputer.Display.clear();
M5Cardputer.Display.drawString("Press m Key",
M5Cardputer.Display.width() / 2,
M5Cardputer.Display.height() / 2);
}
}
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ category=Display
url=https://github.com/m5stack/M5Cardputer.git
architectures=esp32
includes=M5Cardputer.h
depends=M5Unified,M5GFX
depends=M5Unified,M5GFX
6 changes: 2 additions & 4 deletions src/M5Cardputer.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#ifndef M5DIAL_H
#define M5DIAL_H
#ifndef M5CARDPUTER_H
#define M5CARDPUTER_H

#include "M5Unified.h"

// #include "utility/Encoder.h"
#include "utility/Keyboard.h"

namespace m5 {
Expand Down
68 changes: 17 additions & 51 deletions src/utility/Keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,50 +55,20 @@ void Keyboard_Class::begin() {
_set_output(output_list, 0);
}

Point2D_t Keyboard_Class::getKey() {
Point2D_t coor;
coor.x = -1;
coor.y = -1;

uint8_t input_value = 0;

for (int i = 0; i < 8; i++) {
_set_output(output_list, i);
input_value = _get_input(input_list);

/* If key pressed */
if (input_value) {
/* Get X */
for (int j = 0; j < 7; j++) {
if (input_value == X_map_chart[j].value) {
coor.x = (i > 3) ? X_map_chart[j].x_1 : X_map_chart[j].x_2;
break;
}
}

/* Get Y */
coor.y = (i > 3) ? (i - 4) : i;

/* Keep the same as picture */
coor.y = -coor.y;
coor.y = coor.y + 3;

break;
}
}

// printf("%d,%d\n", x, y);
return coor;
}

uint8_t Keyboard_Class::getKeyNum(Point2D_t keyCoor) {
uint8_t ret = 0;
int Keyboard_Class::getKeyCode(Point2D_t keyCoor) {
int ret = 0;

if ((keyCoor.x < 0) || (keyCoor.y < 0)) {
return 0;
}

ret = (keyCoor.y * 14) + (keyCoor.x + 1);
// ret = (keyCoor.y * 14) + (keyCoor.x + 1);
if (_keys_state_buffer.ctrl || _keys_state_buffer.shift ||
_is_caps_locked) {
ret = _key_value_map[keyCoor.y][keyCoor.x].value_num_second;
} else {
ret = _key_value_map[keyCoor.y][keyCoor.x].value_num_first;
}

return ret;
}
Expand Down Expand Up @@ -147,18 +117,14 @@ bool Keyboard_Class::isChange() {
}
}

// bool Keyboard_Class::isPressed(uint16_t key) {
// return _key_list_buffer.size();
// }

// bool Keyboard_Class::isKeyPressed(int keyNum) {
// if (_key_list_buffer.size()) {
// for (const auto& i : _key_list_buffer) {
// if (getKeyNum(i) == keyNum) return true;
// }
// }
// return false;
// }
bool Keyboard_Class::isKeyPressed(int keyCode) {
if (_key_list_buffer.size()) {
for (const auto& i : _key_list_buffer) {
if (getKeyCode(i) == keyCode) return true;
}
}
return false;
}

#include <cstring>

Expand Down
125 changes: 63 additions & 62 deletions src/utility/Keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <iostream>
#include <vector>
#include "Arduino.h"
#include "Keyboard_def.h"

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

struct KeyValue_t {
const char* value_first;
const int value_num_first;
const char* value_second;
const int value_num_second;
};

const KeyValue_t _key_value_map[4][14] = {{{"`", "~"},
{"1", "!"},
{"2", "@"},
{"3", "#"},
{"4", "$"},
{"5", "%"},
{"6", "^"},
{"7", "&"},
{"8", "*"},
{"9", "("},
{"0", ")"},
{"-", "_"},
{"=", "+"},
{"del", "del"}},
{{"tab", "tab"},
{"q", "Q"},
{"w", "W"},
{"e", "E"},
{"r", "R"},
{"t", "T"},
{"y", "Y"},
{"u", "U"},
{"i", "I"},
{"o", "O"},
{"p", "P"},
{"[", "{"},
{"]", "}"},
{"\\", "|"}},
{{"fn", "fn"},
{"shift", "shift"},
{"a", "A"},
{"s", "S"},
{"d", "D"},
{"f", "F"},
{"g", "G"},
{"h", "H"},
{"j", "J"},
{"k", "K"},
{"l", "L"},
{";", ":"},
{"'", "\""},
{"enter", "enter"}},
{{"ctrl", "ctrl"},
{"opt", "opt"},
{"alt", "alt"},
{"z", "Z"},
{"x", "X"},
{"c", "C"},
{"v", "V"},
{"b", "B"},
{"n", "N"},
{"m", "M"},
{",", "<"},
{".", ">"},
{"/", "?"},
{"space", "space"}}};
const KeyValue_t _key_value_map[4][14] = {
{{"`", KB_KEY_BACK_QUOTE, "~", KB_KEY_TILDE},
{"1", KB_KEY_1, "!", KB_KEY_BANG},
{"2", KB_KEY_2, "@", KB_KEY_AT},
{"3", KB_KEY_3, "#", KB_KEY_HASH},
{"4", KB_KEY_4, "$", KB_KEY_DOLLAR},
{"5", KB_KEY_5, "%", KB_KEY_PERCENT},
{"6", KB_KEY_6, "^", KB_KEY_CARET},
{"7", KB_KEY_7, "&", KB_KEY_AND},
{"8", KB_KEY_8, "*", KB_KEY_ASTERISK},
{"9", KB_KEY_9, "(", KB_KEY_L_PARENTHESES},
{"0", KB_KEY_0, ")", KB_KEY_R_PARENTHESES},
{"-", KB_KEY_MINUS, "_", KB_KEY_UNDERSCORE},
{"=", KB_KEY_EQUAL, "+", KB_KEY_PLUS},
{"del", KB_KEY_DEL, "del", KB_KEY_DEL}},
{{"tab", KB_KEY_TAB, "tab", KB_KEY_TAB},
{"q", KB_KEY_L_Q, "Q", KB_KEY_U_Q},
{"w", KB_KEY_L_W, "W", KB_KEY_U_W},
{"e", KB_KEY_L_E, "E", KB_KEY_U_E},
{"r", KB_KEY_L_R, "R", KB_KEY_U_R},
{"t", KB_KEY_L_T, "T", KB_KEY_U_T},
{"y", KB_KEY_L_Y, "Y", KB_KEY_U_Y},
{"u", KB_KEY_L_U, "U", KB_KEY_U_U},
{"i", KB_KEY_L_I, "I", KB_KEY_U_I},
{"o", KB_KEY_L_O, "O", KB_KEY_U_O},
{"p", KB_KEY_L_P, "P", KB_KEY_U_P},
{"[", KB_KEY_L_PARENTHESES, "{", KB_KEY_L_CURLY_BRACKETS},
{"]", KB_KEY_R_PARENTHESES, "}", KB_KEY_R_CURLY_BRACKETS},
{"\\", KB_KEY_BACK_SLASK, "|", KB_KEY_BAR}},
{{"fn", KB_KEY_FN, "fn", KB_KEY_FN},
{"shift", KB_KEY_SHIFT, "shift", KB_KEY_SHIFT},
{"a", KB_KEY_L_A, "A", KB_KEY_U_A},
{"s", KB_KEY_L_S, "S", KB_KEY_U_S},
{"d", KB_KEY_L_D, "D", KB_KEY_U_D},
{"f", KB_KEY_L_F, "F", KB_KEY_U_F},
{"g", KB_KEY_L_G, "G", KB_KEY_U_G},
{"h", KB_KEY_L_H, "H", KB_KEY_U_H},
{"j", KB_KEY_L_J, "J", KB_KEY_U_J},
{"k", KB_KEY_L_K, "K", KB_KEY_U_K},
{"l", KB_KEY_L_L, "L", KB_KEY_U_L},
{";", KB_KEY_SEMICOLON, ":", KB_KEY_COLON},
{"'", KB_KEY_SIGLE_QUOTE, "\"", KB_KEY_QUOTE},
{"enter", KB_KEY_ENTER, "enter", KB_KEY_ENTER}},
{{"ctrl", KB_KEY_CTRL, "ctrl", KB_KEY_CTRL},
{"opt", KB_KEY_OPT, "opt", KB_KEY_OPT},
{"alt", KB_KEY_ALT, "alt", KB_KEY_ALT},
{"z", KB_KEY_L_Z, "Z", KB_KEY_U_Z},
{"x", KB_KEY_L_X, "X", KB_KEY_U_X},
{"c", KB_KEY_L_C, "C", KB_KEY_U_C},
{"v", KB_KEY_L_V, "V", KB_KEY_U_V},
{"b", KB_KEY_L_B, "B", KB_KEY_U_B},
{"n", KB_KEY_L_N, "N", KB_KEY_U_N},
{"m", KB_KEY_L_M, "M", KB_KEY_U_M},
{",", KB_KEY_COMMA, "<", KB_KEY_L_ANGLE_BRACKETS},
{".", KB_KEY_DOT, ">", KB_KEY_R_ANGLE_BRACKETS},
{"/", KB_KEY_SLASH, "?", KB_KEY_QUESTION},
{"space", KB_KEY_SPACE, "space", KB_KEY_SPACE}}};

class Keyboard_Class {
public:
Expand Down Expand Up @@ -138,9 +142,7 @@ class Keyboard_Class {
}

void begin();

Point2D_t getKey();
uint8_t getKeyNum(Point2D_t keyCoor);
int getKeyCode(Point2D_t keyCoor);

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

uint8_t isPressed();
bool isChange();
bool isKeyPressing(int keyNum);
void clearInputHistory();
String getInputHistory();
bool isKeyPressed(int keyCode);

void updateKeysState();
inline KeysState& keysState() {
return _keys_state_buffer;
Expand Down
Loading

0 comments on commit 2fe3b4c

Please sign in to comment.