add PCF8574(A) port expander as passive 4x4 keyboard

This commit is contained in:
Thomas Göttgens
2024-03-26 00:18:23 +01:00
parent 5de0c71a3e
commit a4efa30799
12 changed files with 155 additions and 4 deletions

View File

@@ -7,7 +7,7 @@ CardKbI2cImpl::CardKbI2cImpl() : KbI2cBase("cardKB") {}
void CardKbI2cImpl::init()
{
if (cardkb_found.address == 0x00) {
if ((cardkb_found.address == 0x00) || (kb_model == 0x12)) {
disable();
return;
}

View File

@@ -0,0 +1,84 @@
#include "peMatrixBase.h"
#include "configuration.h"
#include "detect/ScanI2C.h"
extern ScanI2C::DeviceAddress cardkb_found;
extern uint8_t kb_model;
I2CKeyPad keyPad(cardkb_found.address);
PeMatrixBase::PeMatrixBase(const char *name) : concurrency::OSThread(name)
{
this->_originName = name;
}
int32_t PeMatrixBase::runOnce()
{
if (kb_model != 0x12) {
// Input device is not detected.
return disable();
}
if (firstTime) {
// This is the first time the OSThread library has called this function, so do port setup
firstTime = 0;
if (!keyPad.begin()) {
LOG_ERROR("Failed to initialize I2C keypad\n");
return disable();
}
keyPad.loadKeyMap(keymap);
} else {
if (keyPad.isPressed()) {
key = keyPad.getChar();
// debounce
if (key != prevkey) {
if (key != 0) {
LOG_DEBUG("Key 0x%x pressed\n", key);
// reset shift now that we have a keypress
InputEvent e;
e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE;
e.source = this->_originName;
switch (key) {
case 0x1b: // ESC
e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_CANCEL;
break;
case 0x08: // Back
e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_BACK;
e.kbchar = key;
break;
case 0xb5: // Up
e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_UP;
break;
case 0xb6: // Down
e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_DOWN;
break;
case 0xb4: // Left
e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_LEFT;
e.kbchar = key;
break;
case 0xb7: // Right
e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_RIGHT;
e.kbchar = key;
break;
case 0x0d: // Enter
e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_SELECT;
break;
case 0x00: // nopress
e.inputEvent = meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE;
break;
default: // all other keys
e.inputEvent = ANYKEY;
e.kbchar = key;
break;
}
if (e.inputEvent != meshtastic_ModuleConfig_CannedMessageConfig_InputEventChar_NONE) {
this->notifyObservers(&e);
}
}
prevkey = key;
}
}
}
return 50; // Keyscan every 50msec to avoid key bounce
}

22
src/input/peMatrixBase.h Normal file
View File

@@ -0,0 +1,22 @@
#pragma once
#include "InputBroker.h"
#include "concurrency/OSThread.h"
#include <I2CKeyPad.h>
class PeMatrixBase : public Observable<const InputEvent *>, public concurrency::OSThread
{
public:
explicit PeMatrixBase(const char *name);
protected:
virtual int32_t runOnce() override;
private:
const char *_originName;
bool firstTime = 1;
// char keymap[19] = "123A456B789C*0#DNF"; // N = NoKey, F = Fail
char keymap[19] = {0x1b, 0xb5, '3', 'A', 0xb4, 0x0d, 0xb7, 'B', '7', 0xb6, '9', 'C', 0x09, '0', 0x08, 'D', 'N', 'F'};
char key = 0;
char prevkey = 0;
};

View File

@@ -0,0 +1,16 @@
#include "peMatrixImpl.h"
#include "InputBroker.h"
PeMatrixImpl *peMatrixImpl;
PeMatrixImpl::PeMatrixImpl() : PeMatrixBase("matrixPE") {}
void PeMatrixImpl::init()
{
if (kb_model != 0x12) {
disable();
return;
}
inputBroker->registerSource(this);
}

19
src/input/peMatrixImpl.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include "main.h"
#include "peMatrixBase.h"
/**
* @brief The idea behind this class to have static methods for the event handlers.
* Check attachInterrupt() at RotaryEncoderInteruptBase.cpp
* Technically you can have as many rotary encoders hardver attached
* to your device as you wish, but you always need to have separate event
* handlers, thus you need to have a RotaryEncoderInterrupt implementation.
*/
class PeMatrixImpl : public PeMatrixBase
{
public:
PeMatrixImpl();
void init();
};
extern PeMatrixImpl *peMatrixImpl;