Skip to content

Commit c038b15

Browse files
committed
Add support for Unit CardKB2.
1 parent b8ad444 commit c038b15

9 files changed

Lines changed: 749 additions & 16 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
Library for KEYBOARD using [M5UnitUnified](https://github.com/m5stack/M5UnitUnified).
66
M5UnitUnified is a library for unified handling of various M5 units products.
77

8+
### SKU:U215
9+
CardKB v2 is a card-size '42 key' QWERTY keyboard.
810

911
### SKU:U035-B
1012
CardKB v1.1 is a card-size '50 key' QWERTY keyboard. Adpots ATMega8A as the MCU, communication port I2C, and one 'RGB-LED' indicator.

examples/UnitUnified/SimpleDisplay/main/SimpleDisplay.cpp

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/*
2-
* SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD
2+
* SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
33
*
44
* SPDX-License-Identifier: MIT
55
*/
66
/*
7-
Display example using M5UnitUnified for UnitCardKB/UnitFacesQWERTY
7+
Display example using M5UnitUnified for UnitCardKB/UnitCardKB2/UnitFacesQWERTY
88
*/
99
#include <M5Unified.h>
1010
#include <M5UnitUnified.h>
@@ -16,9 +16,17 @@
1616
// *************************************************************
1717
// Choose one define symbol to match the unit you are using
1818
// *************************************************************
19-
#if !defined(USING_UNIT_CARDKB) && !defined(USING_UNIT_FACES_QWERTY)
19+
#if !defined(USING_UNIT_CARDKB) && !defined(USING_UNIT_CARDKB2) && !defined(USING_UNIT_FACES_QWERTY)
2020
// For CardKB
2121
// #define USING_UNIT_CARDKB
22+
23+
// For CardKB2
24+
#define USING_UNIT_CARDKB2
25+
#if defined(USING_UNIT_CARDKB2)
26+
// #define USE_I2C_FOR_CARDKB2
27+
#define USE_UART_FOR_CARDKB2
28+
#endif
29+
2230
// For FacesQWERTY
2331
// #define USING_UNIT_FACES_QWERTY
2432
#endif
@@ -29,10 +37,12 @@ auto& lcd = M5.Display;
2937
m5::unit::UnitUnified Units;
3038
#if defined(USING_UNIT_CARDKB)
3139
m5::unit::UnitCardKB unit;
40+
#elif defined(USING_UNIT_CARDKB2)
41+
m5::unit::UnitCardKB2 unit;
3242
#elif defined(USING_UNIT_FACES_QWERTY)
3343
m5::unit::UnitFacesQWERTY unit;
3444
#else
35-
#error Must choose unit define, USING_UNIT_CARDKB or USING_UNIT_FACES_QWERTY
45+
#error Must choose unit define, USING_UNIT_CARDKB or USING_UNIT_CARDKB2 or USING_UNIT_FACES_QWERTY
3646
#endif
3747

3848
bool small_display{};
@@ -54,13 +64,33 @@ void setup()
5464
small_display = lcd.width() < 240;
5565
lcd.fillScreen(TFT_LIGHTGRAY);
5666

57-
auto pin_num_sda = M5.getPin(m5::pin_name_t::port_a_sda);
58-
auto pin_num_scl = M5.getPin(m5::pin_name_t::port_a_scl);
59-
M5_LOGI("getPin: SDA:%u SCL:%u", pin_num_sda, pin_num_scl);
67+
auto pin_num_sda_tx = M5.getPin(m5::pin_name_t::port_a_sda);
68+
auto pin_num_scl_rx = M5.getPin(m5::pin_name_t::port_a_scl);
69+
M5_LOGI("getPin: SDA/TX:%u SCL/RX:%u", pin_num_sda_tx, pin_num_scl_rx);
70+
#ifdef USE_I2C_FOR_CARDKB2
6071
Wire.end();
61-
Wire.begin(pin_num_sda, pin_num_scl, 100 * 1000U);
72+
Wire.begin(pin_num_sda_tx, pin_num_scl_rx, 100 * 1000U);
6273

6374
if (!Units.add(unit, Wire) || !Units.begin()) {
75+
#elif defined(USE_UART_FOR_CARDKB2)
76+
M5.Power.setExtPower(false); // Turn off port power to reset the sym status.
77+
m5::utility::delay(100);
78+
M5.Power.setExtPower(true);
79+
m5::utility::delay(100);
80+
#if defined(CONFIG_IDF_TARGET_ESP32C6)
81+
auto& serial = Serial1;
82+
#elif SOC_UART_NUM > 2
83+
auto& serial = Serial2;
84+
#elif SOC_UART_NUM > 1
85+
auto& serial = Serial1;
86+
#else
87+
#error "Not enough Serial"
88+
#endif
89+
90+
serial.begin(115200, SERIAL_8N1, pin_num_scl_rx, pin_num_sda_tx);
91+
92+
if (!Units.add(unit, serial) || !Units.begin()) {
93+
#endif
6494
M5_LOGE("Failed to begin");
6595
lcd.fillScreen(TFT_RED);
6696
while (true) {
@@ -72,6 +102,9 @@ void setup()
72102
#if defined(USING_UNIT_CARDKB)
73103
M5.Log.printf("Hardware:%02X Firmware:%02X\n", unit.hardwareType(), unit.firmwareVersion());
74104
#endif
105+
#if defined(USING_UNIT_CARDKB2)
106+
M5.Log.printf("Firmware:%02X\n", unit.firmwareVersion());
107+
#endif
75108
#if defined(USING_UNIT_FACES_QWERTY)
76109
M5.Log.printf("FacesType:%02X Firmware:%02X\n", unit.facesType(), unit.firmwareVersion());
77110
#endif
@@ -89,6 +122,7 @@ void loop()
89122
auto touch = M5.Touch.getDetail();
90123
Units.update();
91124

125+
#if !defined(USING_UNIT_CARDKB2)
92126
// Toggle behavior if using M5Unit-KEYBOARD firmware
93127
if (unit.firmwareVersion() && (M5.BtnA.wasClicked() || touch.wasClicked())) {
94128
scan_mode = !scan_mode;
@@ -97,7 +131,7 @@ void loop()
97131
str = "";
98132
dirty = true;
99133
}
100-
134+
#endif
101135
auto prev_str = str.size();
102136
static auto prev_mod = unit.modifierBits();
103137

@@ -108,10 +142,10 @@ void loop()
108142
if (unit.updated()) {
109143
while (unit.available()) {
110144
ch = unit.getchar();
111-
M5.Log.printf("Char:[%02X %c]\n", ch, std::isprint(ch) ? ch : ' ');
145+
M5.Log.printf("Char:[0x%02X=%d %c]\n", ch, ch, std::isprint(ch) ? ch : ' ');
112146
if (std::isprint(ch)) {
113147
str += ch;
114-
} else if (ch == 0x0D) {
148+
} else if (ch == '\r' || ch == '\n') {
115149
str += '\n';
116150
} else if (ch == 0x08 && !str.empty()) {
117151
str.pop_back();
@@ -123,6 +157,7 @@ void loop()
123157

124158
dirty |= (unit.nowBits() ^ unit.previousBits());
125159

160+
#if !defined(USING_UNIT_CARDKB2)
126161
if (scan_mode) {
127162
// For Scan mode
128163

@@ -200,9 +235,12 @@ void loop()
200235
#endif
201236

202237
} else {
238+
#endif
203239
// For Released mode
204240
lcd.drawString("Conventional", 0, 0);
241+
#if !defined(USING_UNIT_CARDKB2)
205242
}
243+
#endif
206244

207245
// String
208246
if (str.size() != prev_str) {

src/M5UnitUnifiedKEYBOARD.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "unit/unit_Keyboard.hpp"
1717
#include "unit/unit_CardKB.hpp"
18+
#include "unit/unit_CardKB2.hpp"
1819
#include "unit/unit_FacesQWERTY.hpp"
1920

2021
/*!

src/unit/unit_CardKB.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ bool UnitCardKB::update_new_firmware(const types::elapsed_time_t at)
325325
auto prev_holding = _holding;
326326

327327
uint8_t rbuf[(NUMBER_OF_KEYS + 7) / 8 + 1]{};
328-
if (!readRegister(CMD_SCAN_REG, rbuf, m5::stl::size(rbuf), 0)) {
328+
if (!readRegister(scan_reg(), rbuf, m5::stl::size(rbuf), 0)) {
329329
M5_LIB_LOGE("Failed to read");
330330
return false;
331331
}

0 commit comments

Comments
 (0)