Skip to content

Commit 96443c5

Browse files
committed
Fixes update return value, BS/DEL key mapping, removes unused variables, and adds Doxygen
1 parent b03f5bb commit 96443c5

10 files changed

Lines changed: 69 additions & 57 deletions

src/unit/unit_CardKB.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ constexpr uint8_t key_map[][4 /* mode: normal, shift, sym, fn */] = {
3232
{'8', '8', '*', 136}, // 8
3333
{'9', '9', '(', 137}, // 9
3434
{'0', '0', ')', 138}, // 0
35-
{8, 127, 8, 139}, // bs/del
35+
{8, 8, 127, 139}, // bs/del (Sym+BS => DEL)
3636
{9, 9, 9, 140}, // tab
3737
{'q', 'Q', '{', 141}, // q
3838
{'w', 'W', '}', 142}, // w
@@ -72,9 +72,6 @@ constexpr uint8_t key_map[][4 /* mode: normal, shift, sym, fn */] = {
7272
};
7373
static_assert(m5::stl::size(key_map) == UnitCardKB::NUMBER_OF_KEYS, "Invalid size");
7474

75-
// modifier bit to key_map mode index
76-
constexpr uint8_t mod_table[] = {1, 0, 3, 2}; // 0x01:Shift, 0x80:Symbol 0x40:Function
77-
7875
// ASCII to mode bit and key_index_t
7976
// 1:normal 2:shift 4:symbol 8:function
8077
constexpr std::pair<uint8_t, key_index_t> character_map[] = {
@@ -86,7 +83,7 @@ constexpr std::pair<uint8_t, key_index_t> character_map[] = {
8683
{0x00, 0xFF}, // ENG
8784
{0x00, 0xFF}, // ACK
8885
{0x00, 0xFF}, // BEL
89-
{1 + 4, UnitCardKB::KEY_BS}, // BS
86+
{1 + 2, UnitCardKB::KEY_BS}, // BS
9087
{1 + 2 + 4, UnitCardKB::KEY_TAB}, // HT
9188
{1 + 2 + 4, UnitCardKB::KEY_ENTER}, // LF
9289
{0x00, 0xFF}, // VT
@@ -205,7 +202,7 @@ constexpr std::pair<uint8_t, key_index_t> character_map[] = {
205202
{4, UnitCardKB::KEY_U}, // |
206203
{4, UnitCardKB::KEY_W}, // }
207204
{4, UnitCardKB::KEY_I}, // ~
208-
{2, UnitCardKB::KEY_BS}, // DEL
205+
{4, UnitCardKB::KEY_BS}, // DEL
209206
};
210207
static_assert(m5::stl::size(character_map) == 128, "Invalid size");
211208

@@ -232,7 +229,7 @@ key_index_t UnitCardKB::character_to_key_index(const char ch)
232229
unsigned char uc = ch;
233230
// function (>= 0x80)
234231
if (uc & 0x80) {
235-
key_index_t kidx = (key_index_t)(uc - 0x80);
232+
key_index_t kidx = static_cast<key_index_t>(uc - 0x80);
236233
// Special key?
237234
if (uc >= SCHAR_LEFT && uc <= SCHAR_RIGHT) {
238235
return special_character_map[uc - SCHAR_LEFT].second;
@@ -248,7 +245,7 @@ uint8_t UnitCardKB::character_to_mode_bits(const char ch)
248245
unsigned char uc = ch;
249246
// function? (>= 0x80)
250247
if (uc & 0x80) {
251-
key_index_t kidx = (key_index_t)(uc - 0x80);
248+
key_index_t kidx = static_cast<key_index_t>(uc - 0x80);
252249
// Special key?
253250
if (uc >= SCHAR_LEFT && uc <= SCHAR_RIGHT) {
254251
// M5_LIB_LOGI("%c => %02X", ch, special_character_map[uc - SCHAR_LEFT].first);
@@ -365,7 +362,7 @@ bool UnitCardKB::update_new_firmware(const types::elapsed_time_t at)
365362
}
366363
_wasHold = (prev_holding ^ _holding) & _holding;
367364

368-
return true; // Always true
365+
return (_wasPressed | _wasReleased | _repeating);
369366
}
370367

371368
void UnitCardKB::push_back(m5::container::CircularBuffer<uint8_t>* container, const uint8_t kidx, const uint8_t mod8)

src/unit/unit_CardKB.hpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,10 @@ class UnitCardKB : public UnitKeyboardBitwise {
9393

9494
///@name Character code for special keys
9595
///@{
96-
static constexpr char SCHAR_LEFT{(char)180};
97-
static constexpr char SCHAR_UP{(char)181};
98-
static constexpr char SCHAR_DOWN{(char)182};
99-
static constexpr char SCHAR_RIGHT{(char)183};
96+
static constexpr char SCHAR_LEFT{static_cast<char>(180)};
97+
static constexpr char SCHAR_UP{static_cast<char>(181)};
98+
static constexpr char SCHAR_DOWN{static_cast<char>(182)};
99+
static constexpr char SCHAR_RIGHT{static_cast<char>(183)};
100100
///@}
101101

102102
/*!
@@ -124,7 +124,9 @@ class UnitCardKB : public UnitKeyboardBitwise {
124124
_repeat_start_at.resize(NUMBER_OF_KEYS);
125125
_hold_start_at.resize(NUMBER_OF_KEYS);
126126
}
127+
//! @copydoc Component::begin
127128
virtual bool begin() override;
129+
//! @copydoc Component::update
128130
virtual void update(const bool force = false) override;
129131

130132
///@name Settings for begin
@@ -141,6 +143,7 @@ class UnitCardKB : public UnitKeyboardBitwise {
141143
}
142144
///@}
143145

146+
//! @copydoc UnitKeyboardBitwise::toKeyIndex
144147
inline virtual keyboard::key_index_t toKeyIndex(const char ch) const override
145148
{
146149
return character_to_key_index(ch);

src/unit/unit_CardKB2.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ class UnitCardKB2 : public UnitKeyboard {
5353
explicit UnitCardKB2(const uint8_t addr = DEFAULT_ADDRESS) : UnitKeyboard(addr)
5454
{
5555
}
56+
//! @copydoc Component::begin
5657
virtual bool begin() override;
58+
//! @copydoc Component::update
5759
virtual void update(const bool force = false) override;
5860

5961
///@name Settings for begin

src/unit/unit_CardKB2UART.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -301,20 +301,18 @@ void UnitCardKB2UART::update_uart(const bool force)
301301
_repeating |= bit;
302302

303303
// Push repeat character
304-
if (i != cardkb2::KEY_AA && i != cardkb2::KEY_FN && i != cardkb2::KEY_SYM) {
305-
const bool fn_active = _now & (1ULL << cardkb2::KEY_FN);
306-
const bool caps_active = (_caps_lock || _caps_hold_active || _caps_shift_once);
307-
uint8_t ch;
308-
if (fn_active) {
309-
ch = key_map[i][3];
310-
} else if (_sym_was_pressed) {
311-
ch = key_map[i][2];
312-
} else {
313-
ch = key_map[i][caps_active ? 1 : 0];
314-
}
315-
if (ch) {
316-
_data->push_back(ch);
317-
}
304+
const bool fn_active = _now & (1ULL << cardkb2::KEY_FN);
305+
const bool caps_active = (_caps_lock || _caps_hold_active || _caps_shift_once);
306+
uint8_t ch;
307+
if (fn_active) {
308+
ch = key_map[i][3];
309+
} else if (_sym_was_pressed) {
310+
ch = key_map[i][2];
311+
} else {
312+
ch = key_map[i][caps_active ? 1 : 0];
313+
}
314+
if (ch) {
315+
_data->push_back(ch);
318316
}
319317
}
320318
// Hold?

src/unit/unit_CardKB2UART.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ class UnitCardKB2UART : public UnitKeyboardBitwise {
5959
_repeat_start_at.resize(cardkb2::NUMBER_OF_KEYS);
6060
_hold_start_at.resize(cardkb2::NUMBER_OF_KEYS);
6161
}
62+
//! @copydoc Component::begin
6263
virtual bool begin() override;
64+
//! @copydoc Component::update
6365
virtual void update(const bool force = false) override;
6466

6567
///@name Settings for begin
@@ -76,6 +78,7 @@ class UnitCardKB2UART : public UnitKeyboardBitwise {
7678
}
7779
///@}
7880

81+
//! @copydoc UnitKeyboardBitwise::toKeyIndex
7982
inline virtual keyboard::key_index_t toKeyIndex(const char ch) const override
8083
{
8184
return cardkb2::character_to_key_index(ch);

src/unit/unit_CardKB2_defs.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,6 @@ constexpr uint8_t key_map[][4 /* mode: normal, shift, sym, fn */] = {
6666
};
6767
static_assert(m5::stl::size(key_map) == m5::unit::cardkb2::NUMBER_OF_KEYS, "Invalid size");
6868

69-
// modifier bit to key_map mode index
70-
constexpr uint8_t mod_table[] = {1, 0, 3, 2}; // 0x01:Shift, 0x80:Symbol 0x40:Function
71-
7269
// ASCII to mode bit and key_index_t
7370
// 1:normal 2:shift 4:symbol 8:function
7471
// 0x0: no key, 0xFF: invalid char
@@ -100,7 +97,7 @@ constexpr std::pair<uint8_t, key_index_t> character_map[] = {
10097
{0x00, 0xFF}, // CAN (24)
10198
{0x00, 0xFF}, // EM (25)
10299
{0x00, 0xFF}, // SUB (26)
103-
{0x00, 0xFF}, // ESC (27)
100+
{0x08, m5::unit::cardkb2::KEY_1}, // ESC (27) — Fn+1
104101
{0x00, 0xFF}, // FS (28)
105102
{0x00, 0xFF}, // GS (29)
106103
{0x00, 0xFF}, // RS (30)
@@ -251,7 +248,7 @@ uint8_t character_to_mode_bits(const char ch)
251248
unsigned char uc = ch;
252249
// function? (>= 0x80)
253250
if (uc & 0x80) {
254-
key_index_t kidx = (key_index_t)(uc - 0x80);
251+
key_index_t kidx = static_cast<key_index_t>(uc - 0x80);
255252
// Special key?
256253
if (uc >= (unsigned char)SCHAR_LEFT && uc <= (unsigned char)SCHAR_RIGHT) {
257254
// M5_LIB_LOGI("%c => %02X", ch, special_character_map[uc - (unsigned char)SCHAR_LEFT].first);

src/unit/unit_CardKB2_defs.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ constexpr keyboard::key_index_t KEY_SPACE{42};
6666

6767
///@name Character code for special keys
6868
///@{
69-
constexpr char SCHAR_LEFT{(char)180};
70-
constexpr char SCHAR_UP{(char)181};
71-
constexpr char SCHAR_DOWN{(char)182};
72-
constexpr char SCHAR_RIGHT{(char)183};
69+
constexpr char SCHAR_LEFT{static_cast<char>(180)};
70+
constexpr char SCHAR_UP{static_cast<char>(181)};
71+
constexpr char SCHAR_DOWN{static_cast<char>(182)};
72+
constexpr char SCHAR_RIGHT{static_cast<char>(183)};
7373
///@}
7474

7575
/*!

src/unit/unit_FacesQWERTY.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ constexpr uint8_t key_map[][5 /* mode: normal, shift, sym, fn, alt */] = {
3939
{'j', 'J', ';', 182, 160}, // j
4040
{'k', 'K', '\'', 183, 161}, // k
4141
{'l', 'L', '"', 184, 162}, // l
42-
{8, 127, 8, 8, 163}, // bs/del Old: {8 , 8, 127, 8, 163} Fixes same as CARDKB (Shift+BS => DEL)
42+
{8, 8, 127, 8, 163}, // bs/del (Sym+BS => DEL)
4343
{}, // no key (alt)
4444
{'z', 'Z', '7', 9, 165}, // z Old: {'z', 'Z', '7', 186, 165}, Fixes Fn+Z => TAB
4545
{'x', 'X', '8', 187, 166}, // x
@@ -238,7 +238,7 @@ key_index_t UnitFacesQWERTY::character_to_key_index(const char ch)
238238
}
239239
// alt? (>= 0x90)
240240
if (uc >= 0x90) {
241-
key_index_t kidx = (key_index_t)(uc - 0x90);
241+
key_index_t kidx = static_cast<key_index_t>(uc - 0x90);
242242
return static_cast<key_index_t>((kidx < m5::stl::size(key_map)) ? kidx : 0xFF);
243243
}
244244
// normal,shift,symbol and function
@@ -255,7 +255,7 @@ uint8_t UnitFacesQWERTY::character_to_mode_bits(const char ch)
255255
}
256256
// alt? (>= 0x90)
257257
if (uc >= 0x90) {
258-
key_index_t kidx = (key_index_t)(uc - 0x90);
258+
key_index_t kidx = static_cast<key_index_t>(uc - 0x90);
259259
// M5_LIB_LOGI("%c => %02X", ch, (uc - 0x90));
260260
return (kidx < m5::stl::size(key_map)) ? 0x10 : 0x00;
261261
}
@@ -408,7 +408,7 @@ bool UnitFacesQWERTY::update_new_firmware(const types::elapsed_time_t at)
408408
}
409409
_wasHold = (prev_holding ^ _holding) & _holding;
410410

411-
return _repeating; // Any key pressed?
411+
return (_wasPressed | _wasReleased | _repeating);
412412
}
413413

414414
void UnitFacesQWERTY::push_back(m5::container::CircularBuffer<uint8_t>* container, const uint8_t kidx,

src/unit/unit_FacesQWERTY.hpp

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ class UnitFacesQWERTY : public UnitKeyboardBitwise {
7878

7979
///@name Character code for special keys
8080
///@{
81-
static constexpr char SCHAR_NOMARK_G{(char)180};
82-
static constexpr char SCHAR_NOMARK_H{(char)181};
83-
static constexpr char SCHAR_NOMARK_J{(char)182};
84-
static constexpr char SCHAR_UP{(char)183};
85-
static constexpr char SCHAR_INS{(char)184};
86-
static constexpr char SCHAR_HOME{(char)187};
87-
static constexpr char SCHAR_END{(char)188};
88-
static constexpr char SCHAR_PAGE_UP{(char)189};
89-
static constexpr char SCHAR_PAGE_DOWN{(char)190};
90-
static constexpr char SCHAR_LEFT{(char)191};
91-
static constexpr char SCHAR_DOWN{(char)192};
92-
static constexpr char SCHAR_RIGHT{(char)193};
93-
static constexpr char SCHAR_SPEAKER{(char)194};
81+
static constexpr char SCHAR_NOMARK_G{static_cast<char>(180)};
82+
static constexpr char SCHAR_NOMARK_H{static_cast<char>(181)};
83+
static constexpr char SCHAR_NOMARK_J{static_cast<char>(182)};
84+
static constexpr char SCHAR_UP{static_cast<char>(183)};
85+
static constexpr char SCHAR_INS{static_cast<char>(184)};
86+
static constexpr char SCHAR_HOME{static_cast<char>(187)};
87+
static constexpr char SCHAR_END{static_cast<char>(188)};
88+
static constexpr char SCHAR_PAGE_UP{static_cast<char>(189)};
89+
static constexpr char SCHAR_PAGE_DOWN{static_cast<char>(190)};
90+
static constexpr char SCHAR_LEFT{static_cast<char>(191)};
91+
static constexpr char SCHAR_DOWN{static_cast<char>(192)};
92+
static constexpr char SCHAR_RIGHT{static_cast<char>(193)};
93+
static constexpr char SCHAR_SPEAKER{static_cast<char>(194)};
9494
///@}
9595

9696
/*!
@@ -120,7 +120,9 @@ class UnitFacesQWERTY : public UnitKeyboardBitwise {
120120
_repeat_start_at.resize(NUMBER_OF_KEYS);
121121
_hold_start_at.resize(NUMBER_OF_KEYS);
122122
}
123+
//! @copydoc Component::begin
123124
virtual bool begin() override;
125+
//! @copydoc Component::update
124126
virtual void update(const bool force = false) override;
125127

126128
///@name Settings for begin
@@ -137,6 +139,7 @@ class UnitFacesQWERTY : public UnitKeyboardBitwise {
137139
}
138140
///@}
139141

142+
//! @copydoc UnitKeyboardBitwise::toKeyIndex
140143
inline virtual keyboard::key_index_t toKeyIndex(const char ch) const override
141144
{
142145
return character_to_key_index(ch);

src/unit/unit_Keyboard.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ constexpr inline key_status_bits_t modifier_bits(const key_status_bits_t kbs)
4545

4646
/*!
4747
@enum Mode
48-
@brief Operation mode for M5UnitU-KEYBOARD firmware
48+
@brief Operation mode for M5Unit-KEYBOARD firmware
4949
*/
5050
enum class Mode : uint8_t {
5151
/*!
@@ -54,7 +54,7 @@ enum class Mode : uint8_t {
5454
*/
5555
Conventional,
5656
/*!
57-
M5Unit-KEYBOARD mode behavior
57+
M5Unit-KEYBOARD mode behavior
5858
@details CardKB, FacesQWERTY:Gets the pressed key status
5959
@warning M5Unit-KEYBOARD firmware must be written
6060
*/
@@ -82,11 +82,13 @@ class UnitKeyboard : public Component {
8282
{
8383
}
8484

85+
//! @copydoc Component::begin
8586
virtual bool begin() override;
87+
//! @copydoc Component::update
8688
virtual void update(const bool force = false) override;
8789

8890
/*!
89-
@brief Gets the character if input
91+
@brief Gets the input character
9092
@retval != 0 Character
9193
@retval == 0 Not input or invalid character
9294
@note Whether the input is a released or pressed key depends on the derived class
@@ -127,12 +129,13 @@ class UnitKeyboardBitwise : public UnitKeyboard, public PeriodicMeasurementAdapt
127129
{
128130
}
129131

132+
//! @copydoc Component::update
130133
virtual void update(const bool force = false) override;
131134

132135
///@name Measurement data by periodic
133136
///@{
134137
/*!
135-
@brief Gets the character if input
138+
@brief Gets the input character
136139
@retval != 0 Character
137140
@retval == 0 Not input or invalid character
138141
*/
@@ -524,18 +527,24 @@ class UnitKeyboardBitwise : public UnitKeyboard, public PeriodicMeasurementAdapt
524527
///@warning API valid only if using M5Unit-KEYBOARD firmware
525528
///@name Repeat/Hold threshold
526529
///@{
530+
//! @brief Gets the holding threshold (ms)
527531
inline uint32_t holdingThreshold() const
528532
{
529533
return _holding_threshold;
530534
}
535+
//! @brief Gets the repeating threshold (ms)
531536
inline uint32_t repeatingThreshold() const
532537
{
533538
return _repeating_threshold;
534539
}
540+
//! @brief Sets the holding threshold
541+
//! @param ms Threshold in milliseconds
535542
inline void setHoldingThreshold(const uint32_t ms)
536543
{
537544
_holding_threshold = ms;
538545
}
546+
//! @brief Sets the repeating threshold
547+
//! @param ms Threshold in milliseconds
539548
inline void setRepeatingThreshold(const uint32_t ms)
540549
{
541550
_repeating_threshold = ms;

0 commit comments

Comments
 (0)