From e34ada3ff10daf531c822f2ba99b9cfcb6b195e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Thu, 6 Oct 2022 16:00:33 +0200 Subject: [PATCH] support RAK14004 in Canned Messages --- src/input/InputBroker.h | 1 + src/input/kbI2cBase.cpp | 132 +++++++++++++++++++--------- src/modules/CannedMessageModule.cpp | 16 +++- src/modules/CannedMessageModule.h | 1 + 4 files changed, 105 insertions(+), 45 deletions(-) diff --git a/src/input/InputBroker.h b/src/input/InputBroker.h index 5736a8a6..ab264d19 100644 --- a/src/input/InputBroker.h +++ b/src/input/InputBroker.h @@ -2,6 +2,7 @@ #include "Observer.h" #define ANYKEY 0xFF +#define MATRIXKEY 0xFE typedef struct _InputEvent { const char* source; diff --git a/src/input/kbI2cBase.cpp b/src/input/kbI2cBase.cpp index 14e17b7a..2066e12a 100644 --- a/src/input/kbI2cBase.cpp +++ b/src/input/kbI2cBase.cpp @@ -3,12 +3,38 @@ #include extern uint8_t cardkb_found; +extern uint8_t kb_model; KbI2cBase::KbI2cBase(const char *name) : concurrency::OSThread(name) { this->_originName = name; } +uint8_t read_from_14004(uint8_t reg, uint8_t *data, uint8_t length) +{ + uint8_t readflag = 0; + Wire.beginTransmission(CARDKB_ADDR); + Wire.write(reg); + Wire.endTransmission(); // stop transmitting + delay(20); + Wire.requestFrom(CARDKB_ADDR, length); + int i = 0; + while ( Wire.available() ) // slave may send less than requested + { + data[i++] = Wire.read(); // receive a byte as a proper uint8_t + readflag = 1; + } + return readflag; +} + +void write_to_14004(uint8_t reg, uint8_t data) +{ + Wire.beginTransmission(CARDKB_ADDR); + Wire.write(reg); + Wire.write(data); + Wire.endTransmission(); // stop transmitting +} + int32_t KbI2cBase::runOnce() { if (cardkb_found != CARDKB_ADDR){ @@ -16,50 +42,74 @@ int32_t KbI2cBase::runOnce() return INT32_MAX; } - Wire.requestFrom(CARDKB_ADDR, 1); - - while (Wire.available()) { - char c = Wire.read(); - InputEvent e; - e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_NONE; - e.source = this->_originName; - switch (c) { - case 0x1b: // ESC - e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_CANCEL; - break; - case 0x08: // Back - e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_BACK; - e.kbchar = c; - break; - case 0xb5: // Up - e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_UP; - break; - case 0xb6: // Down - e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_DOWN; - break; - case 0xb4: // Left - e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_LEFT; - e.kbchar = c; - break; - case 0xb7: // Right - e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_RIGHT; - e.kbchar = c; - break; - case 0x0d: // Enter - e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_SELECT; - break; - case 0x00: //nopress - e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_NONE; - break; - default: // all other keys - e.inputEvent = ANYKEY; - e.kbchar = c; - break; + if (kb_model == 0x02) { + // RAK14004 + uint8_t rDataBuf[8] = {0}; + uint8_t PrintDataBuf = 0; + if (read_from_14004(0x01, rDataBuf, 0x04) == 1) { + for (uint8_t aCount = 0; aCount < 0x04; aCount++) { + for (uint8_t bCount = 0; bCount < 0x04; bCount++ ) { + if (((rDataBuf[aCount] >> bCount) & 0x01) == 0x01) { + PrintDataBuf = aCount * 0x04 + bCount + 1; + } + } + } } - - if (e.inputEvent != ModuleConfig_CannedMessageConfig_InputEventChar_NONE) { + if (PrintDataBuf != 0) { + DEBUG_MSG("RAK14004 key 0x%x pressed\n", PrintDataBuf); + InputEvent e; + e.inputEvent = MATRIXKEY; + e.source = this->_originName; + e.kbchar = PrintDataBuf; this->notifyObservers(&e); } + } else { + // m5 cardkb + Wire.requestFrom(CARDKB_ADDR, 1); + + while (Wire.available()) { + char c = Wire.read(); + InputEvent e; + e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_NONE; + e.source = this->_originName; + switch (c) { + case 0x1b: // ESC + e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_CANCEL; + break; + case 0x08: // Back + e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_BACK; + e.kbchar = c; + break; + case 0xb5: // Up + e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_UP; + break; + case 0xb6: // Down + e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_DOWN; + break; + case 0xb4: // Left + e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_LEFT; + e.kbchar = c; + break; + case 0xb7: // Right + e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_RIGHT; + e.kbchar = c; + break; + case 0x0d: // Enter + e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_SELECT; + break; + case 0x00: //nopress + e.inputEvent = ModuleConfig_CannedMessageConfig_InputEventChar_NONE; + break; + default: // all other keys + e.inputEvent = ANYKEY; + e.kbchar = c; + break; + } + + if (e.inputEvent != ModuleConfig_CannedMessageConfig_InputEventChar_NONE) { + this->notifyObservers(&e); + } + } } return 500; } diff --git a/src/modules/CannedMessageModule.cpp b/src/modules/CannedMessageModule.cpp index 41f62062..d91a4045 100644 --- a/src/modules/CannedMessageModule.cpp +++ b/src/modules/CannedMessageModule.cpp @@ -171,7 +171,15 @@ int CannedMessageModule::handleInputEvent(const InputEvent *event) this->lastTouchMillis = millis(); validEvent = true; } - + if (event->inputEvent == static_cast(MATRIXKEY)) { + DEBUG_MSG("Canned message event Matrix key pressed\n"); + // this will send the text immediately on matrix press + this->runState = CANNED_MESSAGE_RUN_STATE_ACTION_SELECT; + this->payload = event->kbchar; + this->currentMessageIndex = event->kbchar -1; + this->lastTouchMillis = millis(); + validEvent = true; + } if (validEvent) { // Let runOnce to be called immediately. @@ -203,7 +211,7 @@ int32_t CannedMessageModule::runOnce() { if ((!moduleConfig.canned_message.enabled) || (this->runState == CANNED_MESSAGE_RUN_STATE_DISABLED) || (this->runState == CANNED_MESSAGE_RUN_STATE_INACTIVE)) { - return 30000; // TODO: should return MAX_VAL + return INT32_MAX; } DEBUG_MSG("Check status\n"); UIFrameEvent e = {false, true}; @@ -235,7 +243,7 @@ int32_t CannedMessageModule::runOnce() e.frameChanged = true; } } else { - if (strlen(this->messages[this->currentMessageIndex]) > 0) { + if ((this->messagesCount > this->currentMessageIndex) && (strlen(this->messages[this->currentMessageIndex]) > 0)) { sendText(NODENUM_BROADCAST, this->messages[this->currentMessageIndex], true); this->runState = CANNED_MESSAGE_RUN_STATE_SENDING_ACTIVE; } else { @@ -316,7 +324,7 @@ int32_t CannedMessageModule::runOnce() return INACTIVATE_AFTER_MS; } - return 30000; // TODO: should return MAX_VAL + return INT32_MAX; } const char *CannedMessageModule::getCurrentMessage() diff --git a/src/modules/CannedMessageModule.h b/src/modules/CannedMessageModule.h index 784e40fc..8858714e 100644 --- a/src/modules/CannedMessageModule.h +++ b/src/modules/CannedMessageModule.h @@ -9,6 +9,7 @@ enum cannedMessageModuleRunState CANNED_MESSAGE_RUN_STATE_INACTIVE, CANNED_MESSAGE_RUN_STATE_ACTIVE, CANNED_MESSAGE_RUN_STATE_FREETEXT, + CANNED_MESSAGE_RUN_STATE_MATRIX, CANNED_MESSAGE_RUN_STATE_SENDING_ACTIVE, CANNED_MESSAGE_RUN_STATE_ACTION_SELECT, CANNED_MESSAGE_RUN_STATE_ACTION_UP,