From 3d1503e7173cbf3485b4bf79fe41584efcbcfeaa Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sat, 19 Oct 2024 17:57:44 +1000 Subject: [PATCH 01/24] For testing, switching to t3s3 platform --- platformio.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platformio.ini b/platformio.ini index 2e3ee56f95..7a0c5c4d46 100644 --- a/platformio.ini +++ b/platformio.ini @@ -2,7 +2,7 @@ ; https://docs.platformio.org/page/projectconf.html [platformio] -default_envs = tbeam +;default_envs = tbeam ;default_envs = pico ;default_envs = tbeam-s3-core ;default_envs = tbeam0.7 @@ -16,7 +16,7 @@ default_envs = tbeam ;default_envs = tlora-v2 ;default_envs = tlora-v2-1-1_6 ;default_envs = tlora-v2-1-1_6-tcxo -;default_envs = tlora-t3s3-v1 +default_envs = tlora-t3s3-v1 ;default_envs = t-echo ;default_envs = canaryone ;default_envs = native From dbee1ff04938df3fa760ddf807f14771464d4b58 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 06:23:47 +1000 Subject: [PATCH 02/24] Added flag to use MPR121 on default address, a the expense of MLX90614 --- src/configuration.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/configuration.h b/src/configuration.h index 10a4e0a990..a403bdbdc4 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -114,6 +114,13 @@ along with this program. If not, see . #define CARDKB_ADDR 0x5F #define TDECK_KB_ADDR 0x55 #define BBQ10_KB_ADDR 0x1F +#define MPR121_USE_5A +#ifdef MPR121_USE_5A // Matches common 3x4 button touch boards +#define MPR121_KB_ADDR 0x5A +#endif +#ifndef MPR121_USE_5A +#define MPR121_KB_ADDR 0x5B +#endif // ----------------------------------------------------------------------------- // SENSOR @@ -145,7 +152,12 @@ along with this program. If not, see . #define DFROBOT_LARK_ADDR 0x42 #define NAU7802_ADDR 0x2A #define MAX30102_ADDR 0x57 +#ifdef MPR121_USE_5A +#define MLX90614_ADDR_DEF 0x5B // Can be adjusted by writing a new address to eeprom on the sensor +#endif +#ifndef MPR121_USE_5A #define MLX90614_ADDR_DEF 0x5A +#endif // ----------------------------------------------------------------------------- // ACCELEROMETER From 4c7c59d1639adb17a5acb81e0afa6fffd2af3bd0 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 06:26:53 +1000 Subject: [PATCH 03/24] Added MPR121 keyboard I2C detection --- src/detect/ScanI2C.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/detect/ScanI2C.cpp b/src/detect/ScanI2C.cpp index a9d70edaa9..41ba8257e2 100644 --- a/src/detect/ScanI2C.cpp +++ b/src/detect/ScanI2C.cpp @@ -31,8 +31,8 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const ScanI2C::FoundDevice ScanI2C::firstKeyboard() const { - ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004}; - return firstOfOrNONE(4, types); + ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004, MPR121KB}; + return firstOfOrNONE(5, types); } ScanI2C::FoundDevice ScanI2C::firstAccelerometer() const From 080fcd6ec9b83457d733b008462fe21e19a487f9 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 06:38:59 +1000 Subject: [PATCH 04/24] Adding MPR121 to keyboard detection --- src/detect/ScanI2C.h | 3 ++- src/detect/ScanI2CTwoWire.cpp | 1 + src/input/cardKbI2cImpl.cpp | 9 +++++++-- src/main.cpp | 4 ++++ variants/tlora_t3s3_v1/variant.h | 3 +++ 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/detect/ScanI2C.h b/src/detect/ScanI2C.h index 07db3fd572..b2c482c980 100644 --- a/src/detect/ScanI2C.h +++ b/src/detect/ScanI2C.h @@ -61,7 +61,8 @@ class ScanI2C STK8BAXX, ICM20948, MAX30102, - TPS65233 + TPS65233, + MPR121KB } DeviceType; // typedef uint8_t DeviceAddress; diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index 0401a5ecc8..e8d4ff347a 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -243,6 +243,7 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) SCAN_SIMPLE_CASE(TDECK_KB_ADDR, TDECKKB, "T-Deck keyboard found"); SCAN_SIMPLE_CASE(BBQ10_KB_ADDR, BBQ10KB, "BB Q10 keyboard found"); + SCAN_SIMPLE_CASE(MPR121_KB_ADDR, MPR121KB, "MPR121 keyboard found"); SCAN_SIMPLE_CASE(ST7567_ADDRESS, SCREEN_ST7567, "st7567 display found"); #ifdef HAS_NCP5623 SCAN_SIMPLE_CASE(NCP5623_ADDR, NCP5623, "NCP5623 RGB LED found"); diff --git a/src/input/cardKbI2cImpl.cpp b/src/input/cardKbI2cImpl.cpp index 59ae16866d..2af65f0419 100644 --- a/src/input/cardKbI2cImpl.cpp +++ b/src/input/cardKbI2cImpl.cpp @@ -12,8 +12,8 @@ void CardKbI2cImpl::init() #if !MESHTASTIC_EXCLUDE_I2C && !defined(ARCH_PORTDUINO) if (cardkb_found.address == 0x00) { LOG_DEBUG("Rescanning for I2C keyboard"); - uint8_t i2caddr_scan[] = {CARDKB_ADDR, TDECK_KB_ADDR, BBQ10_KB_ADDR}; - uint8_t i2caddr_asize = 3; + uint8_t i2caddr_scan[] = {CARDKB_ADDR, TDECK_KB_ADDR, BBQ10_KB_ADDR, MPR121_KB_ADDR}; + uint8_t i2caddr_asize = 4; auto i2cScanner = std::unique_ptr(new ScanI2CTwoWire()); #if WIRE_INTERFACES_COUNT == 2 @@ -39,12 +39,17 @@ void CardKbI2cImpl::init() // assign an arbitrary value to distinguish from other models kb_model = 0x11; break; + case ScanI2C::DeviceType::MPR121KB: + // assign an arbitrary value to distinguish from other models + kb_model = 0x37; + break; default: // use this as default since it's also just zero LOG_WARN("kb_info.type is unknown(0x%02x), setting kb_model=0x00", kb_info.type); kb_model = 0x00; } } + LOG_DEBUG("Keyboard Type: 0x%02x Model: 0x%02x Address: 0x%02x\n", kb_info.type, kb_model, cardkb_found.address); if (cardkb_found.address == 0x00) { disable(); return; diff --git a/src/main.cpp b/src/main.cpp index 6abdb18f74..3c1406a65e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -523,6 +523,10 @@ void setup() // assign an arbitrary value to distinguish from other models kb_model = 0x11; break; + case ScanI2C::DeviceType::MPR121KB: + // assign an arbitrary value to distinguish from other models + kb_model = 0x37; + break; default: // use this as default since it's also just zero LOG_WARN("kb_info.type is unknown(0x%02x), setting kb_model=0x00", kb_info.type); diff --git a/variants/tlora_t3s3_v1/variant.h b/variants/tlora_t3s3_v1/variant.h index babe44a584..80abd794ec 100644 --- a/variants/tlora_t3s3_v1/variant.h +++ b/variants/tlora_t3s3_v1/variant.h @@ -1,6 +1,9 @@ #define HAS_SDCARD #define SDCARD_USE_SPI1 +#define CANNED_MESSAGE_MODULE_ENABLE 1 +#define MPR121_USE_5A + #define USE_SSD1306 #define BATTERY_PIN 1 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage From f6a07067b60a4ebb0311b3929d5f24ed5ea2cbe2 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 07:04:30 +1000 Subject: [PATCH 05/24] Added MPR121 implementation v1 --- src/input/MPR121Keyboard.cpp | 362 +++++++++++++++++++++++++++++++++++ src/input/MPR121Keyboard.h | 60 ++++++ src/input/kbI2cBase.cpp | 66 +++++++ 3 files changed, 488 insertions(+) create mode 100644 src/input/MPR121Keyboard.cpp create mode 100644 src/input/MPR121Keyboard.h diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp new file mode 100644 index 0000000000..538daf6898 --- /dev/null +++ b/src/input/MPR121Keyboard.cpp @@ -0,0 +1,362 @@ +// Based on the BBQ10 Keyboard + +#include +#include + +#include "MPR121Keyboard.h" + +#define _REG_CFG 0x5E // Electrode Configuration Register +#define _REG_RST 0x80 // Soft Reset Register +#define _REG_KEY 0x00 // First 0x0FFF (12) bits of register 0 and 1 contain the touch status + +#define _KEY_MASK 0x0FFF // Key mask for the first 12 bits +#define _NUM_KEYS 12 + +#define ECR_CALIBRATION_TRACK_FROM_ZERO (0 << 6) +#define ECR_CALIBRATION_LOCK (1 << 6) +#define ECR_CALIBRATION_TRACK_FROM_PARTIAL_FILTER (2 << 6) // Recommended Typical Mode +#define ECR_CALIBRATION_TRACK_FROM_FULL_FILTER (3 << 6) +#define ECR_PROXIMITY_DETECTION_OFF (0 << 0) // Not using proximity detection +#define ECR_TOUCH_DETECTION_12CH (12 << 0) // Using all 12 channels + +#define MPR121_NONE 0x00 +#define MPR121_REBOOT 0x90 +#define MPR121_LEFT 0xb4 +#define MPR121_UP 0xb5 +#define MPR121_DOWN 0xb6 +#define MPR121_RIGHT 0xb7 +#define MPR121_ESC 0x1b +#define MPR121_BSP 0x08 +#define MPR121_SELECT 0x0d + +#define LONG_PRESS_THRESHOLD 2000 +#define MULTI_TAP_THRESHOLD 2000 + +uint8_t TapMod[12] = {1, 2, 1, 13, 7, 7, 7, 7, 7, 9, 7, 9}; // Num chars per key, Modulus for rotating through characters + +unsigned char TapMap[12][13] = { + {MPR121_BSP}, + {'0', ' '}, + {MPR121_SELECT}, + {'1', '.', ',', '?', '!', ':', ';', '-', '_', '\\', '/', '(', ')'}, + {'2', 'a', 'b', 'c', 'A', 'B', 'C'}, + {'3', 'd', 'e', 'f', 'D', 'E', 'F'}, + {'4', 'g', 'h', 'i', 'G', 'H', 'I'}, + {'5', 'j', 'k', 'l', 'J', 'K', 'L'}, + {'6', 'm', 'n', 'o', 'M', 'N', 'O'}, + {'7', 'p', 'q', 'r', 's', 'P', 'Q', 'R', 'S'}, + {'8', 't', 'u', 'v', 'T', 'U', 'V'}, + {'9', 'w', 'x', 'y', 'z', 'W', 'X', 'Y', 'Z'} +}; + +unsigned char LongMap[12] = { + MPR121_ESC, ' ', MPR121_NONE, + MPR121_NONE, MPR121_UP, MPR121_NONE, + MPR121_LEFT, MPR121_NONE, MPR121_RIGHT, + MPR121_NONE, MPR121_DOWN, MPR121_NONE +}; + +// Translation map from left to right, top to bottom layout to a more convenient layout to manufacture, matching the +// https://www.amazon.com.au/Capacitive-Sensitive-Sensitivity-Replacement-Traditional/dp/B0CTJD5KW9/ref=pd_ci_mcx_mh_mcx_views_0_title?th=1 +/*uint8_t KeyMap[12] = { + 9, 6, 3, 0, + 10, 7, 4, 1, + 11, 8, 5, 2 +};*/ +// Rotated Layout +uint8_t KeyMap[12] = { + 2, 5, 8, 11, + 1, 4, 7, 10, + 0, 3, 6, 9 +}; + + +MPR121Keyboard::MPR121Keyboard() : m_wire(nullptr), m_addr(0), readCallback(nullptr), writeCallback(nullptr) { + LOG_DEBUG("MPR121 @ %02x\n", m_addr); + state = Init; + last_key = -1; + last_tap = 0L; + char_idx = 0; + queue = ""; + +} + +bool MPR121Keyboard::status() +{ + switch (state) { + case Held: + status_toggle = true; + break; + case Idle: + status_toggle = false; + break; + default: + status_toggle = !status_toggle; + break; + } + return status_toggle; +} + +void MPR121Keyboard::begin(uint8_t addr, TwoWire *wire) +{ + m_addr = addr; + m_wire = wire; + + m_wire->begin(); + + reset(); +} + +void MPR121Keyboard::begin(i2c_com_fptr_t r, i2c_com_fptr_t w, uint8_t addr) +{ + m_addr = addr; + m_wire = nullptr; + writeCallback = w; + readCallback = r; + reset(); +} + +bool MPR121Keyboard::ready() +{ + bool ready = false; + + + return ready; +} + +void MPR121Keyboard::reset() +{ + LOG_DEBUG("MPR121 Resetting"); + if (m_wire) { + m_wire->beginTransmission(m_addr); + m_wire->write(_REG_RST); + m_wire->endTransmission(); + } + if (writeCallback) { + uint8_t data = 0; + writeCallback(m_addr, _REG_RST, &data, 0); + } + delay(100); + writeRegister(_REG_CFG, ECR_CALIBRATION_TRACK_FROM_PARTIAL_FILTER | ECR_PROXIMITY_DETECTION_OFF | ECR_TOUCH_DETECTION_12CH); + delay(100); + state = Idle; +} + +void MPR121Keyboard::attachInterrupt(uint8_t pin, void (*func)(void)) const +{ + pinMode(pin, INPUT_PULLUP); + ::attachInterrupt(digitalPinToInterrupt(pin), func, RISING); +} + +void MPR121Keyboard::detachInterrupt(uint8_t pin) const +{ + ::detachInterrupt(pin); +} + +uint8_t MPR121Keyboard::status() const +{ + return readRegister16(_REG_KEY); +} + +uint8_t MPR121Keyboard::keyCount() const +{ + // Read the key register + uint16_t keyRegister = readRegister16(_REG_KEY); + return keyCount(keyRegister); +} + +uint8_t MPR121Keyboard::keyCount(uint16_t value) const +{ + // Mask the first 12 bits + uint16_t buttonState = value & _KEY_MASK; + + // Count how many bits are set to 1 (i.e., how many buttons are pressed) + uint8_t numButtonsPressed = 0; + for (uint8_t i = 0; i < 12; ++i) { + if (buttonState & (1 << i)) { + numButtonsPressed++; + } + } + + return numButtonsPressed; +} + +bool MPR121Keyboard::hasEvent() { + return queue.length() > 0; +} + +void MPR121Keyboard::queueEvent(char next) { + if(next == MPR121_NONE) { return; } + queue.concat(next); +} + +char MPR121Keyboard::dequeueEvent() { + if(queue.length() < 1) { + return MPR121_NONE; + } + char next = queue.charAt(0); + queue.remove(0,1); + return next; +} + +void MPR121Keyboard::trigger() +{ + // Intended to fire in response to an interrupt from the MPR121 or a longpress callback + // Only functional if not in Init state + if (state != Init) { + // Read the key register + uint16_t keyRegister = readRegister16(_REG_KEY); + uint8_t keysPressed = keyCount(keyRegister); + if(keysPressed == 0) + { + // No buttons pressed + if(state == Held) released(); + state = Idle; + return; + } + if(keysPressed == 1) + { + // No buttons pressed + if(state == Held || state == HeldLong) held(keyRegister); + if(state == Idle) pressed(keyRegister); + return; + } + if(keysPressed > 1) + { + // Multipress + state = Busy; + return; + } + } else { + if(!ready()) { + reset(); + } + } +} + +void MPR121Keyboard::pressed(uint16_t keyRegister) { + if(state == Init || state == Busy) { return; } + if(keyCount(keyRegister) != 1) { return; } + uint16_t buttonState = keyRegister & _KEY_MASK; + uint8_t next_key = 0; + for (uint8_t i = 0; i < 12; ++i) { + if (buttonState & (1 << i)) { + next_key = KeyMap[i]; + } + } + uint32_t now = millis(); + int32_t tap_interval = now - last_tap; + if (tap_interval < 0) { + // long running, millis has overflowed. + last_tap = 0; + state = Busy; + return; + } + if (next_key != last_key || tap_interval > MULTI_TAP_THRESHOLD) { + char_idx = 0; + } else { + char_idx += 1; + } + last_key = next_key; + last_tap = now; + return; +} + +void MPR121Keyboard::held(uint16_t keyRegister) { + if(state == Init || state == Busy) { return; } + if(keyCount(keyRegister) != 1) { return; } + uint16_t buttonState = keyRegister & _KEY_MASK; + uint8_t next_key = 0; + for (uint8_t i = 0; i < 12; ++i) { + if (buttonState & (1 << i)) { + next_key = KeyMap[i]; + } + } + uint32_t now = millis(); + int32_t held_interval = now - last_tap; + if (held_interval < 0 || next_key != last_key) { + // long running, millis has overflowed, or a key has been switched quickly... + last_tap = 0; + state = Busy; + return; + } + if (held_interval > LONG_PRESS_THRESHOLD) { + // Set state to heldlong, send a longpress, and reset the timer... + state = HeldLong; // heldlong will allow this function to still fire, but prevent a "release" + queueEvent(LongMap[last_key]); + last_tap = now; + } + return; +} + +void MPR121Keyboard::released() { + if(state != Held) { return; } + // would clear longpress callback... later. + if(last_key < 0 || last_key > _NUM_KEYS) { // reset to idle if last_key out of bounds + last_key = -1; + state = Idle; + return; + } + if(char_idx > 0 && TapMod[last_key] > 1) { + queueEvent(MPR121_BSP); + } + queueEvent(TapMap[last_key][(char_idx % TapMod[last_key])]); +} + +uint8_t MPR121Keyboard::readRegister8(uint8_t reg) const +{ + if (m_wire) { + m_wire->beginTransmission(m_addr); + m_wire->write(reg); + m_wire->endTransmission(); + + m_wire->requestFrom(m_addr, (uint8_t)1); + if (m_wire->available() < 1) + return 0; + + return m_wire->read(); + } + if (readCallback) { + uint8_t data; + readCallback(m_addr, reg, &data, 1); + return data; + } + return 0; +} + +uint16_t MPR121Keyboard::readRegister16(uint8_t reg) const +{ + uint8_t data[2] = {0}; + // uint8_t low = 0, high = 0; + if (m_wire) { + m_wire->beginTransmission(m_addr); + m_wire->write(reg); + m_wire->endTransmission(); + + m_wire->requestFrom(m_addr, (uint8_t)2); + if (m_wire->available() < 2) + return 0; + data[0] = m_wire->read(); + data[1] = m_wire->read(); + } + if (readCallback) { + readCallback(m_addr, reg, data, 2); + } + return (data[1] << 8) | data[0]; +} + +void MPR121Keyboard::writeRegister(uint8_t reg, uint8_t value) +{ + uint8_t data[2]; + data[0] = reg; + data[1] = value; + + if (m_wire) { + m_wire->beginTransmission(m_addr); + m_wire->write(data, sizeof(uint8_t) * 2); + m_wire->endTransmission(); + } + if (writeCallback) { + writeCallback(m_addr, data[0], &(data[1]), 1); + } +} \ No newline at end of file diff --git a/src/input/MPR121Keyboard.h b/src/input/MPR121Keyboard.h new file mode 100644 index 0000000000..2788f2649a --- /dev/null +++ b/src/input/MPR121Keyboard.h @@ -0,0 +1,60 @@ +// Based on the BBQ10 Keyboard + +#include "configuration.h" +#include "concurrency/NotifiedWorkerThread.h" +#include + +class MPR121Keyboard +{ + public: + typedef uint8_t (*i2c_com_fptr_t)(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint8_t len); + + enum MPR121States { Init=0, Idle, Held, HeldLong, Busy }; + + MPR121States state; + + int8_t last_key; + uint32_t last_tap; + uint8_t char_idx; + bool status_toggle; + + String queue; + + MPR121Keyboard(); + + bool status(); + + void begin(uint8_t addr = MPR121_KB_ADDR, TwoWire *wire = &Wire); + + void begin(i2c_com_fptr_t r, i2c_com_fptr_t w, uint8_t addr = MPR121_KB_ADDR); + + void reset(void); + + bool ready(); + + void attachInterrupt(uint8_t pin, void (*func)(void)) const; + void detachInterrupt(uint8_t pin) const; + + void trigger(void); + void pressed(uint16_t value); + void held(uint16_t value); + void released(void); + + uint8_t status(void) const; + uint8_t keyCount(void) const; + uint8_t keyCount(uint16_t value) const; + + bool hasEvent(void); + char dequeueEvent(void); + void queueEvent(char); + + uint8_t readRegister8(uint8_t reg) const; + uint16_t readRegister16(uint8_t reg) const; + void writeRegister(uint8_t reg, uint8_t value); + + private: + TwoWire *m_wire; + uint8_t m_addr; + i2c_com_fptr_t readCallback; + i2c_com_fptr_t writeCallback; +}; \ No newline at end of file diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index 30188bb922..8c9dd001b0 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -157,6 +157,72 @@ int32_t KbI2cBase::runOnce() } break; } + case 0x37: { // MPR121 + LOG_DEBUG("KB_POLL\n"); + MPRkeyboard.trigger(); + InputEvent e; + e.inputEvent = ANYKEY; + e.source = this->_originName; + e.kbchar = MPRkeyboard.status() & INPUT_BROKER_MSG_FN_SYMBOL_ON | INPUT_BROKER_MSG_FN_SYMBOL_OFF; + this->notifyObservers(&e); + while (MPRkeyboard.hasEvent()) { + char nextEvent = MPRkeyboard.dequeueEvent(); + e.inputEvent = ANYKEY; + e.kbchar = 0x00; + e.source = this->_originName; + switch (nextEvent) { + case 0x00: // MPR121_NONE + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE; + e.kbchar = 0x00; + break; + case 0x90: // MPR121_REBOOT + e.inputEvent = ANYKEY; + e.kbchar = INPUT_BROKER_MSG_REBOOT; + break; + case 0xb4: // MPR121_LEFT + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_LEFT; + e.kbchar = 0x00; + break; + case 0xb5: // MPR121_UP + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_UP; + e.kbchar = 0x00; + break; + case 0xb6: // MPR121_DOWN + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_DOWN; + e.kbchar = 0x00; + break; + case 0xb7: // MPR121_RIGHT + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_RIGHT; + e.kbchar = 0x00; + break; + case 0x1b: // MPR121_ESC + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_CANCEL; + e.kbchar = 0x1b; + break; + case 0x08: // MPR121_BSP + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_BACK; + e.kbchar = 0x08; + break; + case 0x0d: // MPR121_SELECT + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_SELECT; + e.kbchar = 0x0d; + break; + default: + if(nextEvent > 127) { + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE; + e.kbchar = 0x00; + break; + } + e.inputEvent = ANYKEY; + e.kbchar = nextEvent; + break; + } + if (e.inputEvent != meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE) { + this->notifyObservers(&e); + } + } + break; + } case 0x02: { // RAK14004 uint8_t rDataBuf[8] = {0}; From a9d69b0631c919c0778c7d29d46eaf02c8043ca3 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 07:07:37 +1000 Subject: [PATCH 06/24] Adding MPR121 instance to kbI2cBase --- src/input/kbI2cBase.cpp | 7 ++++++- src/input/kbI2cBase.h | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index 8c9dd001b0..d06e7d000d 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -40,6 +40,9 @@ int32_t KbI2cBase::runOnce() Q10keyboard.begin(BBQ10_KB_ADDR, &Wire1); Q10keyboard.setBacklight(0); } + if (cardkb_found.address == MPR121_KB_ADDR) { + MPRkeyboard.begin(MPR121_KB_ADDR, &Wire1); + } break; #endif case ScanI2C::WIRE: @@ -49,6 +52,9 @@ int32_t KbI2cBase::runOnce() Q10keyboard.begin(BBQ10_KB_ADDR, &Wire); Q10keyboard.setBacklight(0); } + if (cardkb_found.address == MPR121_KB_ADDR) { + MPRkeyboard.begin(MPR121_KB_ADDR, &Wire); + } break; case ScanI2C::NO_I2C: default: @@ -158,7 +164,6 @@ int32_t KbI2cBase::runOnce() break; } case 0x37: { // MPR121 - LOG_DEBUG("KB_POLL\n"); MPRkeyboard.trigger(); InputEvent e; e.inputEvent = ANYKEY; diff --git a/src/input/kbI2cBase.h b/src/input/kbI2cBase.h index 35b9b09016..59d9a51a87 100644 --- a/src/input/kbI2cBase.h +++ b/src/input/kbI2cBase.h @@ -1,6 +1,7 @@ #pragma once #include "BBQ10Keyboard.h" +#include "MPR121Keyboard.h" #include "InputBroker.h" #include "Wire.h" #include "concurrency/OSThread.h" @@ -19,5 +20,6 @@ class KbI2cBase : public Observable, public concurrency::OST TwoWire *i2cBus = 0; BBQ10Keyboard Q10keyboard; + MPR121Keyboard MPRkeyboard; bool is_sym = false; }; From 76e10d3c7014dac2a65015bd49dfdc52a1263967 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 15:02:35 +1000 Subject: [PATCH 07/24] Completed MPR121 reset method --- src/input/MPR121Keyboard.cpp | 143 +++++++++++++++++++++++++++++------ src/input/MPR121Keyboard.h | 3 + src/input/kbI2cBase.cpp | 6 +- 3 files changed, 125 insertions(+), 27 deletions(-) diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index 538daf6898..ebf1ecd58d 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -1,13 +1,40 @@ // Based on the BBQ10 Keyboard #include -#include - #include "MPR121Keyboard.h" - -#define _REG_CFG 0x5E // Electrode Configuration Register -#define _REG_RST 0x80 // Soft Reset Register -#define _REG_KEY 0x00 // First 0x0FFF (12) bits of register 0 and 1 contain the touch status +#include "configuration.h" + +#ifdef MPR121_USE_5A +#define _MPR121_REG_KEY 0x5a +#endif +#ifndef MPR121_USE_5A +#define _MPR121_REG_KEY 0x5b +#endif + +#define _MPR121_REG_TOUCH_STATUS 0x00 +#define _MPR121_REG_ELECTRODE_FILTERED_DATA +#define _MPR121_REG_BASELINE_VALUE 0x1E + +// Baseline filters +#define _MPR121_REG_MAX_HALF_DELTA_RISING 0x2B +#define _MPR121_REG_NOISE_HALF_DELTA_RISING 0x2C +#define _MPR121_REG_NOISE_COUNT_LIMIT_RISING 0x2D +#define _MPR121_REG_FILTER_DELAY_COUNT_RISING 0x2E +#define _MPR121_REG_MAX_HALF_DELTA_FALLING 0x2F +#define _MPR121_REG_NOISE_HALF_DELTA_FALLING 0x30 +#define _MPR121_REG_NOISE_COUNT_LIMIT_FALLING 0x31 +#define _MPR121_REG_FILTER_DELAY_COUNT_FALLING 0x32 +#define _MPR121_REG_NOISE_HALF_DELTA_TOUCHED 0x33 +#define _MPR121_REG_NOISE_COUNT_LIMIT_TOUCHED 0x34 +#define _MPR121_REG_FILTER_DELAY_COUNT_TOUCHED 0x35 + +#define _MPR121_REG_TOUCH_THRESHOLD 0x41 // First input, +2 for subsequent +#define _MPR121_REG_RELEASE_THRESHOLD 0x42 // First input, +2 for subsequent +#define _MPR121_REG_DEBOUNCE 0x5B +#define _MPR121_REG_CONFIG1 0x5C +#define _MPR121_REG_CONFIG2 0x5D +#define _MPR121_REG_ELECTRODE_CONFIG 0x5E +#define _MPR121_REG_SOFT_RESET 0x80 #define _KEY_MASK 0x0FFF // Key mask for the first 12 bits #define _NUM_KEYS 12 @@ -29,6 +56,9 @@ #define MPR121_BSP 0x08 #define MPR121_SELECT 0x0d +#define MPR121_FN_ON 0xf1 +#define MPR121_FN_OFF 0xf2 + #define LONG_PRESS_THRESHOLD 2000 #define MULTI_TAP_THRESHOLD 2000 @@ -83,6 +113,7 @@ MPR121Keyboard::MPR121Keyboard() : m_wire(nullptr), m_addr(0), readCallback(null bool MPR121Keyboard::status() { + uint32_t now = millis(); switch (state) { case Held: status_toggle = true; @@ -91,7 +122,11 @@ bool MPR121Keyboard::status() status_toggle = false; break; default: - status_toggle = !status_toggle; + if((last_toggle + 1000) < now) + { + status_toggle = !status_toggle; + last_toggle = now; + } break; } return status_toggle; @@ -126,19 +161,65 @@ bool MPR121Keyboard::ready() void MPR121Keyboard::reset() { - LOG_DEBUG("MPR121 Resetting"); - if (m_wire) { - m_wire->beginTransmission(m_addr); - m_wire->write(_REG_RST); - m_wire->endTransmission(); + bool has_reset = false; + while(!has_reset) { + LOG_DEBUG("MPR121 Resetting..."); + // Trigger a MPR121 Soft Reset, sending 0x63 to 0x80 + writeRegister(_MPR121_REG_SOFT_RESET, 0x63); + delay(1000); + // Reset Electrode Configuration to 0x00, Stop Mode + writeRegister(_MPR121_REG_ELECTRODE_CONFIG, 0x00); + delay(100); + // Read MPR121_Config2 0x5d to check if it has been reset to 0x24 + has_reset = readRegister8(_MPR121_REG_CONFIG2) == 0x24; + delay(100); } - if (writeCallback) { - uint8_t data = 0; - writeCallback(m_addr, _REG_RST, &data, 0); + LOG_DEBUG("MPR121 Configuring"); + // Set touch release thresholds + for(uint8_t i = 0; i < 12; i++) { + // Set touch threshold + writeRegister(_MPR121_REG_TOUCH_THRESHOLD + (i * 2), 15); + delay(20); + // Set release threshold + writeRegister(_MPR121_REG_RELEASE_THRESHOLD + (i * 2), 7); + delay(20); } + // Configure filtering and baseline registers + writeRegister(_MPR121_REG_MAX_HALF_DELTA_RISING, 0x01); + delay(20); + writeRegister(_MPR121_REG_MAX_HALF_DELTA_FALLING, 0x01); + delay(20); + writeRegister(_MPR121_REG_NOISE_HALF_DELTA_RISING, 0x01); + delay(20); + writeRegister(_MPR121_REG_NOISE_HALF_DELTA_FALLING, 0x05); + delay(20); + writeRegister(_MPR121_REG_NOISE_HALF_DELTA_TOUCHED, 0x00); + delay(20); + writeRegister(_MPR121_REG_NOISE_COUNT_LIMIT_RISING, 0x0e); + delay(20); + writeRegister(_MPR121_REG_NOISE_COUNT_LIMIT_FALLING, 0x01); + delay(20); + writeRegister(_MPR121_REG_NOISE_COUNT_LIMIT_TOUCHED, 0x00); + delay(20); + writeRegister(_MPR121_REG_FILTER_DELAY_COUNT_RISING, 0x00); + delay(20); + writeRegister(_MPR121_REG_FILTER_DELAY_COUNT_FALLING, 0x00); + delay(20); + writeRegister(_MPR121_REG_FILTER_DELAY_COUNT_TOUCHED, 0x00); + delay(20); + // Set Debounce to 0x02 + writeRegister(_MPR121_REG_DEBOUNCE, 0x00); + delay(20); + // Set Filter1 itterations and discharge current 6x and 16uA respectively (0x10) + writeRegister(_MPR121_REG_CONFIG1, 0x10); + delay(20); + // Set CDT to 0.5us, Filter2 itterations to 4x, and Sample interval = 0 (0x20) + writeRegister(_MPR121_REG_CONFIG2, 0x20); + delay(20); + // Enter run mode by Seting partial filter calibration tracking, disable proximity detection, enable 12 channels + writeRegister(_MPR121_REG_ELECTRODE_CONFIG, ECR_CALIBRATION_TRACK_FROM_PARTIAL_FILTER | ECR_PROXIMITY_DETECTION_OFF | ECR_TOUCH_DETECTION_12CH); delay(100); - writeRegister(_REG_CFG, ECR_CALIBRATION_TRACK_FROM_PARTIAL_FILTER | ECR_PROXIMITY_DETECTION_OFF | ECR_TOUCH_DETECTION_12CH); - delay(100); + LOG_DEBUG("MPR121 Running"); state = Idle; } @@ -155,13 +236,13 @@ void MPR121Keyboard::detachInterrupt(uint8_t pin) const uint8_t MPR121Keyboard::status() const { - return readRegister16(_REG_KEY); + return readRegister16(_MPR121_REG_KEY); } uint8_t MPR121Keyboard::keyCount() const { // Read the key register - uint16_t keyRegister = readRegister16(_REG_KEY); + uint16_t keyRegister = readRegister16(_MPR121_REG_KEY); return keyCount(keyRegister); } @@ -203,9 +284,17 @@ void MPR121Keyboard::trigger() { // Intended to fire in response to an interrupt from the MPR121 or a longpress callback // Only functional if not in Init state + bool next_status = status(); + if(last_status != next_status) { + if(next_status) { + queueEvent(MPR121_FN_ON); + } else { + queueEvent(MPR121_FN_OFF); + }; + } if (state != Init) { // Read the key register - uint16_t keyRegister = readRegister16(_REG_KEY); + uint16_t keyRegister = readRegister16(_MPR121_REG_KEY); uint8_t keysPressed = keyCount(keyRegister); if(keysPressed == 0) { @@ -236,14 +325,19 @@ void MPR121Keyboard::trigger() void MPR121Keyboard::pressed(uint16_t keyRegister) { if(state == Init || state == Busy) { return; } - if(keyCount(keyRegister) != 1) { return; } + if(keyCount(keyRegister) != 1) { + LOG_DEBUG("Multipress"); + return; + } uint16_t buttonState = keyRegister & _KEY_MASK; - uint8_t next_key = 0; + uint8_t next_pin = 0; for (uint8_t i = 0; i < 12; ++i) { if (buttonState & (1 << i)) { - next_key = KeyMap[i]; + next_pin = i; } } + uint8_t next_key = KeyMap[next_pin]; + LOG_DEBUG("MPR121 Pin: %i Key: %i", next_pin, next_key); uint32_t now = millis(); int32_t tap_interval = now - last_tap; if (tap_interval < 0) { @@ -285,6 +379,7 @@ void MPR121Keyboard::held(uint16_t keyRegister) { state = HeldLong; // heldlong will allow this function to still fire, but prevent a "release" queueEvent(LongMap[last_key]); last_tap = now; + LOG_DEBUG("Long Press Key: %i Map: %i", last_key, LongMap[last_key]); } return; } @@ -299,8 +394,10 @@ void MPR121Keyboard::released() { } if(char_idx > 0 && TapMod[last_key] > 1) { queueEvent(MPR121_BSP); + LOG_DEBUG("Multi Press, Backspace"); } queueEvent(TapMap[last_key][(char_idx % TapMod[last_key])]); + LOG_DEBUG("Key Press: %i Index:%i if %i Map: %i", last_key, char_idx, TapMod[last_key], TapMap[last_key][(char_idx % TapMod[last_key])]); } uint8_t MPR121Keyboard::readRegister8(uint8_t reg) const diff --git a/src/input/MPR121Keyboard.h b/src/input/MPR121Keyboard.h index 2788f2649a..926d5ce9e0 100644 --- a/src/input/MPR121Keyboard.h +++ b/src/input/MPR121Keyboard.h @@ -3,6 +3,7 @@ #include "configuration.h" #include "concurrency/NotifiedWorkerThread.h" #include +#include class MPR121Keyboard { @@ -17,6 +18,8 @@ class MPR121Keyboard uint32_t last_tap; uint8_t char_idx; bool status_toggle; + uint32_t last_toggle; + bool last_status; String queue; diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index d06e7d000d..d5dc06c1bd 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -166,10 +166,7 @@ int32_t KbI2cBase::runOnce() case 0x37: { // MPR121 MPRkeyboard.trigger(); InputEvent e; - e.inputEvent = ANYKEY; - e.source = this->_originName; - e.kbchar = MPRkeyboard.status() & INPUT_BROKER_MSG_FN_SYMBOL_ON | INPUT_BROKER_MSG_FN_SYMBOL_OFF; - this->notifyObservers(&e); + while (MPRkeyboard.hasEvent()) { char nextEvent = MPRkeyboard.dequeueEvent(); e.inputEvent = ANYKEY; @@ -223,6 +220,7 @@ int32_t KbI2cBase::runOnce() break; } if (e.inputEvent != meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE) { + LOG_DEBUG("MP121 Notifying: %i Char: %i", e.inputEvent, e.kbchar); this->notifyObservers(&e); } } From b2344044435c8000bd8d38af54a39797227ea16b Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 15:19:57 +1000 Subject: [PATCH 08/24] Switch back to old soft reset method --- src/input/MPR121Keyboard.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index ebf1ecd58d..e8be5d85ea 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -153,10 +153,8 @@ void MPR121Keyboard::begin(i2c_com_fptr_t r, i2c_com_fptr_t w, uint8_t addr) bool MPR121Keyboard::ready() { - bool ready = false; - - - return ready; + // Read MPR121_Config2 0x5d to check if it has been reset to 0x24 + return readRegister8(_MPR121_REG_CONFIG2) == 0x24; } void MPR121Keyboard::reset() @@ -164,14 +162,21 @@ void MPR121Keyboard::reset() bool has_reset = false; while(!has_reset) { LOG_DEBUG("MPR121 Resetting..."); - // Trigger a MPR121 Soft Reset, sending 0x63 to 0x80 - writeRegister(_MPR121_REG_SOFT_RESET, 0x63); - delay(1000); + // Trigger a MPR121 Soft Reset + if (m_wire) { + m_wire->beginTransmission(m_addr); + m_wire->write(_MPR121_REG_SOFT_RESET); + m_wire->endTransmission(); + } + if (writeCallback) { + uint8_t data = 0; + writeCallback(m_addr, _MPR121_REG_SOFT_RESET, &data, 0); + } + delay(100); // Reset Electrode Configuration to 0x00, Stop Mode writeRegister(_MPR121_REG_ELECTRODE_CONFIG, 0x00); delay(100); - // Read MPR121_Config2 0x5d to check if it has been reset to 0x24 - has_reset = readRegister8(_MPR121_REG_CONFIG2) == 0x24; + has_reset = ready(); delay(100); } LOG_DEBUG("MPR121 Configuring"); From bce930d735443885a9d9319db1d68bee30cfe48e Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 15:36:11 +1000 Subject: [PATCH 09/24] Removing config loop. --- src/input/MPR121Keyboard.cpp | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index e8be5d85ea..2bf26cb615 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -159,26 +159,22 @@ bool MPR121Keyboard::ready() void MPR121Keyboard::reset() { - bool has_reset = false; - while(!has_reset) { - LOG_DEBUG("MPR121 Resetting..."); - // Trigger a MPR121 Soft Reset - if (m_wire) { - m_wire->beginTransmission(m_addr); - m_wire->write(_MPR121_REG_SOFT_RESET); - m_wire->endTransmission(); - } - if (writeCallback) { - uint8_t data = 0; - writeCallback(m_addr, _MPR121_REG_SOFT_RESET, &data, 0); - } - delay(100); - // Reset Electrode Configuration to 0x00, Stop Mode - writeRegister(_MPR121_REG_ELECTRODE_CONFIG, 0x00); - delay(100); - has_reset = ready(); - delay(100); + LOG_DEBUG("MPR121 Resetting..."); + // Trigger a MPR121 Soft Reset + if (m_wire) { + m_wire->beginTransmission(m_addr); + m_wire->write(_MPR121_REG_SOFT_RESET); + m_wire->endTransmission(); } + if (writeCallback) { + uint8_t data = 0; + writeCallback(m_addr, _MPR121_REG_SOFT_RESET, &data, 0); + } + delay(100); + // Reset Electrode Configuration to 0x00, Stop Mode + writeRegister(_MPR121_REG_ELECTRODE_CONFIG, 0x00); + delay(100); + LOG_DEBUG("MPR121 Configuring"); // Set touch release thresholds for(uint8_t i = 0; i < 12; i++) { From 8c87e5e4ea3d35dedbdd2c80908a719b735cbe4a Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 15:40:46 +1000 Subject: [PATCH 10/24] Cleanup reset. --- src/input/MPR121Keyboard.cpp | 11 +---------- src/input/MPR121Keyboard.h | 2 -- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index 2bf26cb615..d889dd9849 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -108,7 +108,6 @@ MPR121Keyboard::MPR121Keyboard() : m_wire(nullptr), m_addr(0), readCallback(null last_tap = 0L; char_idx = 0; queue = ""; - } bool MPR121Keyboard::status() @@ -151,12 +150,6 @@ void MPR121Keyboard::begin(i2c_com_fptr_t r, i2c_com_fptr_t w, uint8_t addr) reset(); } -bool MPR121Keyboard::ready() -{ - // Read MPR121_Config2 0x5d to check if it has been reset to 0x24 - return readRegister8(_MPR121_REG_CONFIG2) == 0x24; -} - void MPR121Keyboard::reset() { LOG_DEBUG("MPR121 Resetting..."); @@ -318,9 +311,7 @@ void MPR121Keyboard::trigger() return; } } else { - if(!ready()) { - reset(); - } + reset(); } } diff --git a/src/input/MPR121Keyboard.h b/src/input/MPR121Keyboard.h index 926d5ce9e0..6da7f9d052 100644 --- a/src/input/MPR121Keyboard.h +++ b/src/input/MPR121Keyboard.h @@ -33,8 +33,6 @@ class MPR121Keyboard void reset(void); - bool ready(); - void attachInterrupt(uint8_t pin, void (*func)(void)) const; void detachInterrupt(uint8_t pin) const; From ed295738664519b2907629066bf1a40a396d86ec Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 16:33:54 +1000 Subject: [PATCH 11/24] Fixed "Pressed" not changing state to "Held" --- src/input/MPR121Keyboard.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index d889dd9849..eb19e6bef8 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -286,6 +286,7 @@ void MPR121Keyboard::trigger() queueEvent(MPR121_FN_OFF); }; } + last_status = next_status; if (state != Init) { // Read the key register uint16_t keyRegister = readRegister16(_MPR121_REG_KEY); @@ -320,6 +321,8 @@ void MPR121Keyboard::pressed(uint16_t keyRegister) { if(keyCount(keyRegister) != 1) { LOG_DEBUG("Multipress"); return; + } else { + LOG_DEBUG("Pressed"); } uint16_t buttonState = keyRegister & _KEY_MASK; uint8_t next_pin = 0; @@ -345,12 +348,14 @@ void MPR121Keyboard::pressed(uint16_t keyRegister) { } last_key = next_key; last_tap = now; + state = Held; return; } void MPR121Keyboard::held(uint16_t keyRegister) { if(state == Init || state == Busy) { return; } if(keyCount(keyRegister) != 1) { return; } + LOG_DEBUG("Held"); uint16_t buttonState = keyRegister & _KEY_MASK; uint8_t next_key = 0; for (uint8_t i = 0; i < 12; ++i) { @@ -384,6 +389,7 @@ void MPR121Keyboard::released() { state = Idle; return; } + LOG_DEBUG("Released"); if(char_idx > 0 && TapMod[last_key] > 1) { queueEvent(MPR121_BSP); LOG_DEBUG("Multi Press, Backspace"); From dd2e0951d07103d04c88a5e6775d4462b746a1fd Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 16:39:02 +1000 Subject: [PATCH 12/24] Reset testing config back to match original config --- platformio.ini | 4 ++-- src/configuration.h | 2 +- variants/tlora_t3s3_v1/variant.h | 3 --- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/platformio.ini b/platformio.ini index 7a0c5c4d46..2e3ee56f95 100644 --- a/platformio.ini +++ b/platformio.ini @@ -2,7 +2,7 @@ ; https://docs.platformio.org/page/projectconf.html [platformio] -;default_envs = tbeam +default_envs = tbeam ;default_envs = pico ;default_envs = tbeam-s3-core ;default_envs = tbeam0.7 @@ -16,7 +16,7 @@ ;default_envs = tlora-v2 ;default_envs = tlora-v2-1-1_6 ;default_envs = tlora-v2-1-1_6-tcxo -default_envs = tlora-t3s3-v1 +;default_envs = tlora-t3s3-v1 ;default_envs = t-echo ;default_envs = canaryone ;default_envs = native diff --git a/src/configuration.h b/src/configuration.h index a403bdbdc4..3d5240155d 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -114,7 +114,7 @@ along with this program. If not, see . #define CARDKB_ADDR 0x5F #define TDECK_KB_ADDR 0x55 #define BBQ10_KB_ADDR 0x1F -#define MPR121_USE_5A +// #define MPR121_USE_5A #ifdef MPR121_USE_5A // Matches common 3x4 button touch boards #define MPR121_KB_ADDR 0x5A #endif diff --git a/variants/tlora_t3s3_v1/variant.h b/variants/tlora_t3s3_v1/variant.h index 80abd794ec..babe44a584 100644 --- a/variants/tlora_t3s3_v1/variant.h +++ b/variants/tlora_t3s3_v1/variant.h @@ -1,9 +1,6 @@ #define HAS_SDCARD #define SDCARD_USE_SPI1 -#define CANNED_MESSAGE_MODULE_ENABLE 1 -#define MPR121_USE_5A - #define USE_SSD1306 #define BATTERY_PIN 1 // A battery voltage measurement pin, voltage divider connected here to measure battery voltage From 1b2c47168d5910d715ae12d309a0a22c7d33a027 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 17:03:23 +1000 Subject: [PATCH 13/24] Fix warnings on t-echo re: member variable initialisation --- src/input/MPR121Keyboard.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index eb19e6bef8..993451179d 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -108,6 +108,9 @@ MPR121Keyboard::MPR121Keyboard() : m_wire(nullptr), m_addr(0), readCallback(null last_tap = 0L; char_idx = 0; queue = ""; + status_toggle = false; + last_toggle = 0L; + last_status = false; } bool MPR121Keyboard::status() From ad5175fb11eefbdf925220a4571366ee5c60ea72 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 12:50:31 +0000 Subject: [PATCH 14/24] Linted Codebase --- src/input/MPR121Keyboard.cpp | 163 ++++++++++++++++++----------------- src/input/MPR121Keyboard.h | 4 +- src/input/cardKbI2cImpl.cpp | 2 +- src/input/kbI2cBase.cpp | 86 +++++++++--------- src/input/kbI2cBase.h | 4 +- 5 files changed, 133 insertions(+), 126 deletions(-) diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index 993451179d..e794e2cee6 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -1,8 +1,8 @@ // Based on the BBQ10 Keyboard -#include #include "MPR121Keyboard.h" #include "configuration.h" +#include #ifdef MPR121_USE_5A #define _MPR121_REG_KEY 0x5a @@ -28,8 +28,8 @@ #define _MPR121_REG_NOISE_COUNT_LIMIT_TOUCHED 0x34 #define _MPR121_REG_FILTER_DELAY_COUNT_TOUCHED 0x35 -#define _MPR121_REG_TOUCH_THRESHOLD 0x41 // First input, +2 for subsequent -#define _MPR121_REG_RELEASE_THRESHOLD 0x42 // First input, +2 for subsequent +#define _MPR121_REG_TOUCH_THRESHOLD 0x41 // First input, +2 for subsequent +#define _MPR121_REG_RELEASE_THRESHOLD 0x42 // First input, +2 for subsequent #define _MPR121_REG_DEBOUNCE 0x5B #define _MPR121_REG_CONFIG1 0x5C #define _MPR121_REG_CONFIG2 0x5D @@ -37,14 +37,14 @@ #define _MPR121_REG_SOFT_RESET 0x80 #define _KEY_MASK 0x0FFF // Key mask for the first 12 bits -#define _NUM_KEYS 12 +#define _NUM_KEYS 12 #define ECR_CALIBRATION_TRACK_FROM_ZERO (0 << 6) #define ECR_CALIBRATION_LOCK (1 << 6) #define ECR_CALIBRATION_TRACK_FROM_PARTIAL_FILTER (2 << 6) // Recommended Typical Mode #define ECR_CALIBRATION_TRACK_FROM_FULL_FILTER (3 << 6) #define ECR_PROXIMITY_DETECTION_OFF (0 << 0) // Not using proximity detection -#define ECR_TOUCH_DETECTION_12CH (12 << 0) // Using all 12 channels +#define ECR_TOUCH_DETECTION_12CH (12 << 0) // Using all 12 channels #define MPR121_NONE 0x00 #define MPR121_REBOOT 0x90 @@ -64,27 +64,21 @@ uint8_t TapMod[12] = {1, 2, 1, 13, 7, 7, 7, 7, 7, 9, 7, 9}; // Num chars per key, Modulus for rotating through characters -unsigned char TapMap[12][13] = { - {MPR121_BSP}, - {'0', ' '}, - {MPR121_SELECT}, - {'1', '.', ',', '?', '!', ':', ';', '-', '_', '\\', '/', '(', ')'}, - {'2', 'a', 'b', 'c', 'A', 'B', 'C'}, - {'3', 'd', 'e', 'f', 'D', 'E', 'F'}, - {'4', 'g', 'h', 'i', 'G', 'H', 'I'}, - {'5', 'j', 'k', 'l', 'J', 'K', 'L'}, - {'6', 'm', 'n', 'o', 'M', 'N', 'O'}, - {'7', 'p', 'q', 'r', 's', 'P', 'Q', 'R', 'S'}, - {'8', 't', 'u', 'v', 'T', 'U', 'V'}, - {'9', 'w', 'x', 'y', 'z', 'W', 'X', 'Y', 'Z'} -}; - -unsigned char LongMap[12] = { - MPR121_ESC, ' ', MPR121_NONE, - MPR121_NONE, MPR121_UP, MPR121_NONE, - MPR121_LEFT, MPR121_NONE, MPR121_RIGHT, - MPR121_NONE, MPR121_DOWN, MPR121_NONE -}; +unsigned char TapMap[12][13] = {{MPR121_BSP}, + {'0', ' '}, + {MPR121_SELECT}, + {'1', '.', ',', '?', '!', ':', ';', '-', '_', '\\', '/', '(', ')'}, + {'2', 'a', 'b', 'c', 'A', 'B', 'C'}, + {'3', 'd', 'e', 'f', 'D', 'E', 'F'}, + {'4', 'g', 'h', 'i', 'G', 'H', 'I'}, + {'5', 'j', 'k', 'l', 'J', 'K', 'L'}, + {'6', 'm', 'n', 'o', 'M', 'N', 'O'}, + {'7', 'p', 'q', 'r', 's', 'P', 'Q', 'R', 'S'}, + {'8', 't', 'u', 'v', 'T', 'U', 'V'}, + {'9', 'w', 'x', 'y', 'z', 'W', 'X', 'Y', 'Z'}}; + +unsigned char LongMap[12] = {MPR121_ESC, ' ', MPR121_NONE, MPR121_NONE, MPR121_UP, MPR121_NONE, + MPR121_LEFT, MPR121_NONE, MPR121_RIGHT, MPR121_NONE, MPR121_DOWN, MPR121_NONE}; // Translation map from left to right, top to bottom layout to a more convenient layout to manufacture, matching the // https://www.amazon.com.au/Capacitive-Sensitive-Sensitivity-Replacement-Traditional/dp/B0CTJD5KW9/ref=pd_ci_mcx_mh_mcx_views_0_title?th=1 @@ -94,14 +88,10 @@ unsigned char LongMap[12] = { 11, 8, 5, 2 };*/ // Rotated Layout -uint8_t KeyMap[12] = { - 2, 5, 8, 11, - 1, 4, 7, 10, - 0, 3, 6, 9 -}; - +uint8_t KeyMap[12] = {2, 5, 8, 11, 1, 4, 7, 10, 0, 3, 6, 9}; -MPR121Keyboard::MPR121Keyboard() : m_wire(nullptr), m_addr(0), readCallback(nullptr), writeCallback(nullptr) { +MPR121Keyboard::MPR121Keyboard() : m_wire(nullptr), m_addr(0), readCallback(nullptr), writeCallback(nullptr) +{ LOG_DEBUG("MPR121 @ %02x\n", m_addr); state = Init; last_key = -1; @@ -117,19 +107,18 @@ bool MPR121Keyboard::status() { uint32_t now = millis(); switch (state) { - case Held: - status_toggle = true; - break; - case Idle: - status_toggle = false; - break; - default: - if((last_toggle + 1000) < now) - { - status_toggle = !status_toggle; - last_toggle = now; - } - break; + case Held: + status_toggle = true; + break; + case Idle: + status_toggle = false; + break; + default: + if ((last_toggle + 1000) < now) { + status_toggle = !status_toggle; + last_toggle = now; + } + break; } return status_toggle; } @@ -170,10 +159,10 @@ void MPR121Keyboard::reset() // Reset Electrode Configuration to 0x00, Stop Mode writeRegister(_MPR121_REG_ELECTRODE_CONFIG, 0x00); delay(100); - + LOG_DEBUG("MPR121 Configuring"); // Set touch release thresholds - for(uint8_t i = 0; i < 12; i++) { + for (uint8_t i = 0; i < 12; i++) { // Set touch threshold writeRegister(_MPR121_REG_TOUCH_THRESHOLD + (i * 2), 15); delay(20); @@ -214,7 +203,8 @@ void MPR121Keyboard::reset() writeRegister(_MPR121_REG_CONFIG2, 0x20); delay(20); // Enter run mode by Seting partial filter calibration tracking, disable proximity detection, enable 12 channels - writeRegister(_MPR121_REG_ELECTRODE_CONFIG, ECR_CALIBRATION_TRACK_FROM_PARTIAL_FILTER | ECR_PROXIMITY_DETECTION_OFF | ECR_TOUCH_DETECTION_12CH); + writeRegister(_MPR121_REG_ELECTRODE_CONFIG, + ECR_CALIBRATION_TRACK_FROM_PARTIAL_FILTER | ECR_PROXIMITY_DETECTION_OFF | ECR_TOUCH_DETECTION_12CH); delay(100); LOG_DEBUG("MPR121 Running"); state = Idle; @@ -259,21 +249,26 @@ uint8_t MPR121Keyboard::keyCount(uint16_t value) const return numButtonsPressed; } -bool MPR121Keyboard::hasEvent() { +bool MPR121Keyboard::hasEvent() +{ return queue.length() > 0; } -void MPR121Keyboard::queueEvent(char next) { - if(next == MPR121_NONE) { return; } +void MPR121Keyboard::queueEvent(char next) +{ + if (next == MPR121_NONE) { + return; + } queue.concat(next); } -char MPR121Keyboard::dequeueEvent() { - if(queue.length() < 1) { +char MPR121Keyboard::dequeueEvent() +{ + if (queue.length() < 1) { return MPR121_NONE; } char next = queue.charAt(0); - queue.remove(0,1); + queue.remove(0, 1); return next; } @@ -282,8 +277,8 @@ void MPR121Keyboard::trigger() // Intended to fire in response to an interrupt from the MPR121 or a longpress callback // Only functional if not in Init state bool next_status = status(); - if(last_status != next_status) { - if(next_status) { + if (last_status != next_status) { + if (next_status) { queueEvent(MPR121_FN_ON); } else { queueEvent(MPR121_FN_OFF); @@ -294,22 +289,22 @@ void MPR121Keyboard::trigger() // Read the key register uint16_t keyRegister = readRegister16(_MPR121_REG_KEY); uint8_t keysPressed = keyCount(keyRegister); - if(keysPressed == 0) - { + if (keysPressed == 0) { // No buttons pressed - if(state == Held) released(); + if (state == Held) + released(); state = Idle; return; } - if(keysPressed == 1) - { + if (keysPressed == 1) { // No buttons pressed - if(state == Held || state == HeldLong) held(keyRegister); - if(state == Idle) pressed(keyRegister); + if (state == Held || state == HeldLong) + held(keyRegister); + if (state == Idle) + pressed(keyRegister); return; } - if(keysPressed > 1) - { + if (keysPressed > 1) { // Multipress state = Busy; return; @@ -319,11 +314,14 @@ void MPR121Keyboard::trigger() } } -void MPR121Keyboard::pressed(uint16_t keyRegister) { - if(state == Init || state == Busy) { return; } - if(keyCount(keyRegister) != 1) { +void MPR121Keyboard::pressed(uint16_t keyRegister) +{ + if (state == Init || state == Busy) { + return; + } + if (keyCount(keyRegister) != 1) { LOG_DEBUG("Multipress"); - return; + return; } else { LOG_DEBUG("Pressed"); } @@ -355,9 +353,14 @@ void MPR121Keyboard::pressed(uint16_t keyRegister) { return; } -void MPR121Keyboard::held(uint16_t keyRegister) { - if(state == Init || state == Busy) { return; } - if(keyCount(keyRegister) != 1) { return; } +void MPR121Keyboard::held(uint16_t keyRegister) +{ + if (state == Init || state == Busy) { + return; + } + if (keyCount(keyRegister) != 1) { + return; + } LOG_DEBUG("Held"); uint16_t buttonState = keyRegister & _KEY_MASK; uint8_t next_key = 0; @@ -384,21 +387,25 @@ void MPR121Keyboard::held(uint16_t keyRegister) { return; } -void MPR121Keyboard::released() { - if(state != Held) { return; } +void MPR121Keyboard::released() +{ + if (state != Held) { + return; + } // would clear longpress callback... later. - if(last_key < 0 || last_key > _NUM_KEYS) { // reset to idle if last_key out of bounds + if (last_key < 0 || last_key > _NUM_KEYS) { // reset to idle if last_key out of bounds last_key = -1; state = Idle; return; } LOG_DEBUG("Released"); - if(char_idx > 0 && TapMod[last_key] > 1) { + if (char_idx > 0 && TapMod[last_key] > 1) { queueEvent(MPR121_BSP); LOG_DEBUG("Multi Press, Backspace"); } queueEvent(TapMap[last_key][(char_idx % TapMod[last_key])]); - LOG_DEBUG("Key Press: %i Index:%i if %i Map: %i", last_key, char_idx, TapMod[last_key], TapMap[last_key][(char_idx % TapMod[last_key])]); + LOG_DEBUG("Key Press: %i Index:%i if %i Map: %i", last_key, char_idx, TapMod[last_key], + TapMap[last_key][(char_idx % TapMod[last_key])]); } uint8_t MPR121Keyboard::readRegister8(uint8_t reg) const diff --git a/src/input/MPR121Keyboard.h b/src/input/MPR121Keyboard.h index 6da7f9d052..646d53d838 100644 --- a/src/input/MPR121Keyboard.h +++ b/src/input/MPR121Keyboard.h @@ -1,7 +1,7 @@ // Based on the BBQ10 Keyboard -#include "configuration.h" #include "concurrency/NotifiedWorkerThread.h" +#include "configuration.h" #include #include @@ -10,7 +10,7 @@ class MPR121Keyboard public: typedef uint8_t (*i2c_com_fptr_t)(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint8_t len); - enum MPR121States { Init=0, Idle, Held, HeldLong, Busy }; + enum MPR121States { Init = 0, Idle, Held, HeldLong, Busy }; MPR121States state; diff --git a/src/input/cardKbI2cImpl.cpp b/src/input/cardKbI2cImpl.cpp index 2af65f0419..7ad2eb2aca 100644 --- a/src/input/cardKbI2cImpl.cpp +++ b/src/input/cardKbI2cImpl.cpp @@ -39,7 +39,7 @@ void CardKbI2cImpl::init() // assign an arbitrary value to distinguish from other models kb_model = 0x11; break; - case ScanI2C::DeviceType::MPR121KB: + case ScanI2C::DeviceType::MPR121KB: // assign an arbitrary value to distinguish from other models kb_model = 0x37; break; diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index d5dc06c1bd..d0f36c3868 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -166,59 +166,59 @@ int32_t KbI2cBase::runOnce() case 0x37: { // MPR121 MPRkeyboard.trigger(); InputEvent e; - + while (MPRkeyboard.hasEvent()) { char nextEvent = MPRkeyboard.dequeueEvent(); e.inputEvent = ANYKEY; e.kbchar = 0x00; e.source = this->_originName; switch (nextEvent) { - case 0x00: // MPR121_NONE + case 0x00: // MPR121_NONE + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE; + e.kbchar = 0x00; + break; + case 0x90: // MPR121_REBOOT + e.inputEvent = ANYKEY; + e.kbchar = INPUT_BROKER_MSG_REBOOT; + break; + case 0xb4: // MPR121_LEFT + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_LEFT; + e.kbchar = 0x00; + break; + case 0xb5: // MPR121_UP + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_UP; + e.kbchar = 0x00; + break; + case 0xb6: // MPR121_DOWN + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_DOWN; + e.kbchar = 0x00; + break; + case 0xb7: // MPR121_RIGHT + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_RIGHT; + e.kbchar = 0x00; + break; + case 0x1b: // MPR121_ESC + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_CANCEL; + e.kbchar = 0x1b; + break; + case 0x08: // MPR121_BSP + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_BACK; + e.kbchar = 0x08; + break; + case 0x0d: // MPR121_SELECT + e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_SELECT; + e.kbchar = 0x0d; + break; + default: + if (nextEvent > 127) { e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE; e.kbchar = 0x00; break; - case 0x90: // MPR121_REBOOT - e.inputEvent = ANYKEY; - e.kbchar = INPUT_BROKER_MSG_REBOOT; - break; - case 0xb4: // MPR121_LEFT - e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_LEFT; - e.kbchar = 0x00; - break; - case 0xb5: // MPR121_UP - e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_UP; - e.kbchar = 0x00; - break; - case 0xb6: // MPR121_DOWN - e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_DOWN; - e.kbchar = 0x00; - break; - case 0xb7: // MPR121_RIGHT - e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_RIGHT; - e.kbchar = 0x00; - break; - case 0x1b: // MPR121_ESC - e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_CANCEL; - e.kbchar = 0x1b; - break; - case 0x08: // MPR121_BSP - e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_BACK; - e.kbchar = 0x08; - break; - case 0x0d: // MPR121_SELECT - e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_SELECT; - e.kbchar = 0x0d; - break; - default: - if(nextEvent > 127) { - e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE; - e.kbchar = 0x00; - break; - } - e.inputEvent = ANYKEY; - e.kbchar = nextEvent; - break; } + e.inputEvent = ANYKEY; + e.kbchar = nextEvent; + break; + } if (e.inputEvent != meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE) { LOG_DEBUG("MP121 Notifying: %i Char: %i", e.inputEvent, e.kbchar); this->notifyObservers(&e); diff --git a/src/input/kbI2cBase.h b/src/input/kbI2cBase.h index 59d9a51a87..dc2414fc05 100644 --- a/src/input/kbI2cBase.h +++ b/src/input/kbI2cBase.h @@ -1,8 +1,8 @@ #pragma once #include "BBQ10Keyboard.h" -#include "MPR121Keyboard.h" #include "InputBroker.h" +#include "MPR121Keyboard.h" #include "Wire.h" #include "concurrency/OSThread.h" @@ -22,4 +22,4 @@ class KbI2cBase : public Observable, public concurrency::OST BBQ10Keyboard Q10keyboard; MPR121Keyboard MPRkeyboard; bool is_sym = false; -}; +}; \ No newline at end of file From 5cf67fd7cf759a011defa147ad45fdc268a80096 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 23:03:50 +1000 Subject: [PATCH 15/24] Made keymap and others unique --- src/input/MPR121Keyboard.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index e794e2cee6..31e2dff740 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -64,7 +64,7 @@ uint8_t TapMod[12] = {1, 2, 1, 13, 7, 7, 7, 7, 7, 9, 7, 9}; // Num chars per key, Modulus for rotating through characters -unsigned char TapMap[12][13] = {{MPR121_BSP}, +unsigned char MPR121_TapMap[12][13] = {{MPR121_BSP}, {'0', ' '}, {MPR121_SELECT}, {'1', '.', ',', '?', '!', ':', ';', '-', '_', '\\', '/', '(', ')'}, @@ -77,18 +77,18 @@ unsigned char TapMap[12][13] = {{MPR121_BSP}, {'8', 't', 'u', 'v', 'T', 'U', 'V'}, {'9', 'w', 'x', 'y', 'z', 'W', 'X', 'Y', 'Z'}}; -unsigned char LongMap[12] = {MPR121_ESC, ' ', MPR121_NONE, MPR121_NONE, MPR121_UP, MPR121_NONE, +unsigned char MPR121_LongPressMap[12] = {MPR121_ESC, ' ', MPR121_NONE, MPR121_NONE, MPR121_UP, MPR121_NONE, MPR121_LEFT, MPR121_NONE, MPR121_RIGHT, MPR121_NONE, MPR121_DOWN, MPR121_NONE}; // Translation map from left to right, top to bottom layout to a more convenient layout to manufacture, matching the // https://www.amazon.com.au/Capacitive-Sensitive-Sensitivity-Replacement-Traditional/dp/B0CTJD5KW9/ref=pd_ci_mcx_mh_mcx_views_0_title?th=1 -/*uint8_t KeyMap[12] = { +/*uint8_t MPR121_KeyMap[12] = { 9, 6, 3, 0, 10, 7, 4, 1, 11, 8, 5, 2 };*/ // Rotated Layout -uint8_t KeyMap[12] = {2, 5, 8, 11, 1, 4, 7, 10, 0, 3, 6, 9}; +uint8_t MPR121_KeyMap[12] = {2, 5, 8, 11, 1, 4, 7, 10, 0, 3, 6, 9}; MPR121Keyboard::MPR121Keyboard() : m_wire(nullptr), m_addr(0), readCallback(nullptr), writeCallback(nullptr) { @@ -332,7 +332,7 @@ void MPR121Keyboard::pressed(uint16_t keyRegister) next_pin = i; } } - uint8_t next_key = KeyMap[next_pin]; + uint8_t next_key = MPR121_KeyMap[next_pin]; LOG_DEBUG("MPR121 Pin: %i Key: %i", next_pin, next_key); uint32_t now = millis(); int32_t tap_interval = now - last_tap; @@ -366,7 +366,7 @@ void MPR121Keyboard::held(uint16_t keyRegister) uint8_t next_key = 0; for (uint8_t i = 0; i < 12; ++i) { if (buttonState & (1 << i)) { - next_key = KeyMap[i]; + next_key = MPR121_KeyMap[i]; } } uint32_t now = millis(); @@ -380,9 +380,9 @@ void MPR121Keyboard::held(uint16_t keyRegister) if (held_interval > LONG_PRESS_THRESHOLD) { // Set state to heldlong, send a longpress, and reset the timer... state = HeldLong; // heldlong will allow this function to still fire, but prevent a "release" - queueEvent(LongMap[last_key]); + queueEvent(MPR121_LongPressMap[last_key]); last_tap = now; - LOG_DEBUG("Long Press Key: %i Map: %i", last_key, LongMap[last_key]); + LOG_DEBUG("Long Press Key: %i Map: %i", last_key, MPR121_LongPressMap[last_key]); } return; } @@ -403,9 +403,9 @@ void MPR121Keyboard::released() queueEvent(MPR121_BSP); LOG_DEBUG("Multi Press, Backspace"); } - queueEvent(TapMap[last_key][(char_idx % TapMod[last_key])]); + queueEvent(MPR121_TapMap[last_key][(char_idx % TapMod[last_key])]); LOG_DEBUG("Key Press: %i Index:%i if %i Map: %i", last_key, char_idx, TapMod[last_key], - TapMap[last_key][(char_idx % TapMod[last_key])]); + MPR121_TapMap[last_key][(char_idx % TapMod[last_key])]); } uint8_t MPR121Keyboard::readRegister8(uint8_t reg) const @@ -464,4 +464,4 @@ void MPR121Keyboard::writeRegister(uint8_t reg, uint8_t value) if (writeCallback) { writeCallback(m_addr, data[0], &(data[1]), 1); } -} \ No newline at end of file +} From 5e486a835a5179b47097a4d0f1e4bc3a87a1bc7f Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 13:08:42 +0000 Subject: [PATCH 16/24] trunk fmt --- src/input/MPR121Keyboard.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index 31e2dff740..5ed60b6c75 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -65,20 +65,20 @@ uint8_t TapMod[12] = {1, 2, 1, 13, 7, 7, 7, 7, 7, 9, 7, 9}; // Num chars per key, Modulus for rotating through characters unsigned char MPR121_TapMap[12][13] = {{MPR121_BSP}, - {'0', ' '}, - {MPR121_SELECT}, - {'1', '.', ',', '?', '!', ':', ';', '-', '_', '\\', '/', '(', ')'}, - {'2', 'a', 'b', 'c', 'A', 'B', 'C'}, - {'3', 'd', 'e', 'f', 'D', 'E', 'F'}, - {'4', 'g', 'h', 'i', 'G', 'H', 'I'}, - {'5', 'j', 'k', 'l', 'J', 'K', 'L'}, - {'6', 'm', 'n', 'o', 'M', 'N', 'O'}, - {'7', 'p', 'q', 'r', 's', 'P', 'Q', 'R', 'S'}, - {'8', 't', 'u', 'v', 'T', 'U', 'V'}, - {'9', 'w', 'x', 'y', 'z', 'W', 'X', 'Y', 'Z'}}; + {'0', ' '}, + {MPR121_SELECT}, + {'1', '.', ',', '?', '!', ':', ';', '-', '_', '\\', '/', '(', ')'}, + {'2', 'a', 'b', 'c', 'A', 'B', 'C'}, + {'3', 'd', 'e', 'f', 'D', 'E', 'F'}, + {'4', 'g', 'h', 'i', 'G', 'H', 'I'}, + {'5', 'j', 'k', 'l', 'J', 'K', 'L'}, + {'6', 'm', 'n', 'o', 'M', 'N', 'O'}, + {'7', 'p', 'q', 'r', 's', 'P', 'Q', 'R', 'S'}, + {'8', 't', 'u', 'v', 'T', 'U', 'V'}, + {'9', 'w', 'x', 'y', 'z', 'W', 'X', 'Y', 'Z'}}; unsigned char MPR121_LongPressMap[12] = {MPR121_ESC, ' ', MPR121_NONE, MPR121_NONE, MPR121_UP, MPR121_NONE, - MPR121_LEFT, MPR121_NONE, MPR121_RIGHT, MPR121_NONE, MPR121_DOWN, MPR121_NONE}; + MPR121_LEFT, MPR121_NONE, MPR121_RIGHT, MPR121_NONE, MPR121_DOWN, MPR121_NONE}; // Translation map from left to right, top to bottom layout to a more convenient layout to manufacture, matching the // https://www.amazon.com.au/Capacitive-Sensitive-Sensitivity-Replacement-Traditional/dp/B0CTJD5KW9/ref=pd_ci_mcx_mh_mcx_views_0_title?th=1 From d74cfc7d77323d5113cffd95e34fdc0858c795a8 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 23:45:31 +1000 Subject: [PATCH 17/24] Alt detection mode test --- src/configuration.h | 11 ----------- src/detect/ScanI2CTwoWire.cpp | 15 +++++++++++++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/configuration.h b/src/configuration.h index 3d5240155d..66070eb403 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -114,13 +114,7 @@ along with this program. If not, see . #define CARDKB_ADDR 0x5F #define TDECK_KB_ADDR 0x55 #define BBQ10_KB_ADDR 0x1F -// #define MPR121_USE_5A -#ifdef MPR121_USE_5A // Matches common 3x4 button touch boards #define MPR121_KB_ADDR 0x5A -#endif -#ifndef MPR121_USE_5A -#define MPR121_KB_ADDR 0x5B -#endif // ----------------------------------------------------------------------------- // SENSOR @@ -152,12 +146,7 @@ along with this program. If not, see . #define DFROBOT_LARK_ADDR 0x42 #define NAU7802_ADDR 0x2A #define MAX30102_ADDR 0x57 -#ifdef MPR121_USE_5A -#define MLX90614_ADDR_DEF 0x5B // Can be adjusted by writing a new address to eeprom on the sensor -#endif -#ifndef MPR121_USE_5A #define MLX90614_ADDR_DEF 0x5A -#endif // ----------------------------------------------------------------------------- // ACCELEROMETER diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index e8d4ff347a..fa402837e0 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -243,7 +243,7 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) SCAN_SIMPLE_CASE(TDECK_KB_ADDR, TDECKKB, "T-Deck keyboard found"); SCAN_SIMPLE_CASE(BBQ10_KB_ADDR, BBQ10KB, "BB Q10 keyboard found"); - SCAN_SIMPLE_CASE(MPR121_KB_ADDR, MPR121KB, "MPR121 keyboard found"); + SCAN_SIMPLE_CASE(ST7567_ADDRESS, SCREEN_ST7567, "st7567 display found"); #ifdef HAS_NCP5623 SCAN_SIMPLE_CASE(NCP5623_ADDR, NCP5623, "NCP5623 RGB LED found"); @@ -409,7 +409,18 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) #ifdef HAS_TPS65233 SCAN_SIMPLE_CASE(TPS65233_ADDR, TPS65233, "TPS65233 BIAS-T found"); #endif - SCAN_SIMPLE_CASE(MLX90614_ADDR_DEF, MLX90614, "MLX90614 IR temp sensor found"); + + case MLX90614_ADDR_DEF: + registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x5d), 1); + if (registerValue == 0x24) { + type = MPR121KB; + LOG_INFO("MPR121KB keyboard found"); + break; + } else { + type = MLX90614; + LOG_INFO("MLX90614 IR temp sensor found"); + } + break; case ICM20948_ADDR: // same as BMX160_ADDR case ICM20948_ADDR_ALT: // same as MPU6050_ADDR From 444d3de80d4c5767e1e01863b2f3b510cb19c526 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Mon, 21 Oct 2024 00:05:14 +1000 Subject: [PATCH 18/24] Update ScanI2CTwoWire.cpp --- src/detect/ScanI2CTwoWire.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index fa402837e0..3cd9ccd7ba 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -411,8 +411,8 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) #endif case MLX90614_ADDR_DEF: - registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x5d), 1); - if (registerValue == 0x24) { + registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x5c), 1); + if (registerValue == 0x10) { type = MPR121KB; LOG_INFO("MPR121KB keyboard found"); break; From 8e15b32e42286654e1e14cc047f76c7b75c71fb8 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Mon, 21 Oct 2024 06:33:11 +1000 Subject: [PATCH 19/24] Switching to MLX90614 detection of 0x5a in bus address 0x0e --- src/detect/ScanI2CTwoWire.cpp | 12 ++++++------ src/input/MPR121Keyboard.cpp | 5 ----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index 3cd9ccd7ba..43ea00aaa9 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -411,14 +411,14 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) #endif case MLX90614_ADDR_DEF: - registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x5c), 1); - if (registerValue == 0x10) { - type = MPR121KB; - LOG_INFO("MPR121KB keyboard found"); - break; - } else { + registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x5e), 1); + if (registerValue == 0x5a) { type = MLX90614; LOG_INFO("MLX90614 IR temp sensor found"); + break; + } else { + type = MPR121KB; + LOG_INFO("MPR121KB keyboard found"); } break; diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index 5ed60b6c75..474d0915e5 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -4,12 +4,7 @@ #include "configuration.h" #include -#ifdef MPR121_USE_5A #define _MPR121_REG_KEY 0x5a -#endif -#ifndef MPR121_USE_5A -#define _MPR121_REG_KEY 0x5b -#endif #define _MPR121_REG_TOUCH_STATUS 0x00 #define _MPR121_REG_ELECTRODE_FILTERED_DATA From 432645179faa54892fa4c5627e47db1d070b8c12 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Mon, 21 Oct 2024 06:44:38 +1000 Subject: [PATCH 20/24] Fixed register read address to 0x0e --- src/detect/ScanI2CTwoWire.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index 43ea00aaa9..15feae8d76 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -411,11 +411,10 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) #endif case MLX90614_ADDR_DEF: - registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x5e), 1); + registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x0e), 1); if (registerValue == 0x5a) { type = MLX90614; LOG_INFO("MLX90614 IR temp sensor found"); - break; } else { type = MPR121KB; LOG_INFO("MPR121KB keyboard found"); From 98dfdbff91f76cdd1215662756c6e93f6f37dbf8 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Sun, 20 Oct 2024 20:45:54 +0000 Subject: [PATCH 21/24] Trunked... --- src/detect/ScanI2CTwoWire.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/detect/ScanI2CTwoWire.cpp b/src/detect/ScanI2CTwoWire.cpp index 15feae8d76..71e475904f 100644 --- a/src/detect/ScanI2CTwoWire.cpp +++ b/src/detect/ScanI2CTwoWire.cpp @@ -243,7 +243,7 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) SCAN_SIMPLE_CASE(TDECK_KB_ADDR, TDECKKB, "T-Deck keyboard found"); SCAN_SIMPLE_CASE(BBQ10_KB_ADDR, BBQ10KB, "BB Q10 keyboard found"); - + SCAN_SIMPLE_CASE(ST7567_ADDRESS, SCREEN_ST7567, "st7567 display found"); #ifdef HAS_NCP5623 SCAN_SIMPLE_CASE(NCP5623_ADDR, NCP5623, "NCP5623 RGB LED found"); @@ -409,7 +409,7 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize) #ifdef HAS_TPS65233 SCAN_SIMPLE_CASE(TPS65233_ADDR, TPS65233, "TPS65233 BIAS-T found"); #endif - + case MLX90614_ADDR_DEF: registerValue = getRegisterValue(ScanI2CTwoWire::RegisterLocation(addr, 0x0e), 1); if (registerValue == 0x5a) { From ff256563a29056c021a98a33343c3556b06590e2 Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Mon, 21 Oct 2024 07:00:53 +1000 Subject: [PATCH 22/24] Remove logic to blink the fn symbol. --- src/input/MPR121Keyboard.cpp | 34 +--------------------------------- src/input/MPR121Keyboard.h | 5 ----- 2 files changed, 1 insertion(+), 38 deletions(-) diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index 474d0915e5..e1b32aa542 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -87,35 +87,12 @@ uint8_t MPR121_KeyMap[12] = {2, 5, 8, 11, 1, 4, 7, 10, 0, 3, 6, 9}; MPR121Keyboard::MPR121Keyboard() : m_wire(nullptr), m_addr(0), readCallback(nullptr), writeCallback(nullptr) { - LOG_DEBUG("MPR121 @ %02x\n", m_addr); + // LOG_DEBUG("MPR121 @ %02x\n", m_addr); state = Init; last_key = -1; last_tap = 0L; char_idx = 0; queue = ""; - status_toggle = false; - last_toggle = 0L; - last_status = false; -} - -bool MPR121Keyboard::status() -{ - uint32_t now = millis(); - switch (state) { - case Held: - status_toggle = true; - break; - case Idle: - status_toggle = false; - break; - default: - if ((last_toggle + 1000) < now) { - status_toggle = !status_toggle; - last_toggle = now; - } - break; - } - return status_toggle; } void MPR121Keyboard::begin(uint8_t addr, TwoWire *wire) @@ -271,15 +248,6 @@ void MPR121Keyboard::trigger() { // Intended to fire in response to an interrupt from the MPR121 or a longpress callback // Only functional if not in Init state - bool next_status = status(); - if (last_status != next_status) { - if (next_status) { - queueEvent(MPR121_FN_ON); - } else { - queueEvent(MPR121_FN_OFF); - }; - } - last_status = next_status; if (state != Init) { // Read the key register uint16_t keyRegister = readRegister16(_MPR121_REG_KEY); diff --git a/src/input/MPR121Keyboard.h b/src/input/MPR121Keyboard.h index 646d53d838..6349750cef 100644 --- a/src/input/MPR121Keyboard.h +++ b/src/input/MPR121Keyboard.h @@ -17,16 +17,11 @@ class MPR121Keyboard int8_t last_key; uint32_t last_tap; uint8_t char_idx; - bool status_toggle; - uint32_t last_toggle; - bool last_status; String queue; MPR121Keyboard(); - bool status(); - void begin(uint8_t addr = MPR121_KB_ADDR, TwoWire *wire = &Wire); void begin(i2c_com_fptr_t r, i2c_com_fptr_t w, uint8_t addr = MPR121_KB_ADDR); From 992e2828a4e15cae4636c138ce12403029c4f46c Mon Sep 17 00:00:00 2001 From: aussieklutz Date: Mon, 21 Oct 2024 16:08:54 +1000 Subject: [PATCH 23/24] Update MPR121Keyboard.cpp --- src/input/MPR121Keyboard.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index e1b32aa542..d4463a3e27 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -87,7 +87,7 @@ uint8_t MPR121_KeyMap[12] = {2, 5, 8, 11, 1, 4, 7, 10, 0, 3, 6, 9}; MPR121Keyboard::MPR121Keyboard() : m_wire(nullptr), m_addr(0), readCallback(nullptr), writeCallback(nullptr) { - // LOG_DEBUG("MPR121 @ %02x\n", m_addr); + // LOG_DEBUG("MPR121 @ %02x\n", m_addr); state = Init; last_key = -1; last_tap = 0L; From 24d889c45732ea28d4f79647c3e9863e9401bd1c Mon Sep 17 00:00:00 2001 From: Tom Fifield Date: Mon, 21 Oct 2024 17:41:51 +1100 Subject: [PATCH 24/24] trunk fmt --- src/input/MPR121Keyboard.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/input/MPR121Keyboard.cpp b/src/input/MPR121Keyboard.cpp index d4463a3e27..e1b32aa542 100644 --- a/src/input/MPR121Keyboard.cpp +++ b/src/input/MPR121Keyboard.cpp @@ -87,7 +87,7 @@ uint8_t MPR121_KeyMap[12] = {2, 5, 8, 11, 1, 4, 7, 10, 0, 3, 6, 9}; MPR121Keyboard::MPR121Keyboard() : m_wire(nullptr), m_addr(0), readCallback(nullptr), writeCallback(nullptr) { - // LOG_DEBUG("MPR121 @ %02x\n", m_addr); + // LOG_DEBUG("MPR121 @ %02x\n", m_addr); state = Init; last_key = -1; last_tap = 0L;