Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Draft
wants to merge 27 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
b5a3449
add PCF8574(A) port expander as passive 4x4 keyboard
caveman99 Mar 25, 2024
8817b71
50msec is a bit steep
caveman99 Mar 25, 2024
4ff27e0
add PCF8574(A) port expander as passive 4x4 keyboard
caveman99 Mar 25, 2024
4c229df
50msec is a bit steep
caveman99 Mar 25, 2024
1532e8c
Merge branch 'portexpander-keyboard' of https://github.com/meshtastic…
caveman99 May 4, 2024
a4efa30
add PCF8574(A) port expander as passive 4x4 keyboard
caveman99 Mar 25, 2024
3768429
50msec is a bit steep
caveman99 Mar 25, 2024
99431b1
Merge branch 'master' into portexpander-keyboard
caveman99 Jun 16, 2024
96bcc78
Merge branch 'master' into portexpander-keyboard
caveman99 Jun 20, 2024
d36c02f
Merge branch 'portexpander-keyboard' of https://github.com/meshtastic…
caveman99 Aug 23, 2024
e78cd12
Merge branch 'master' into portexpander-keyboard
caveman99 Sep 4, 2024
0a09d9a
Merge branch 'master' into portexpander-keyboard
caveman99 Sep 29, 2024
12bd04c
Merge branch 'master' into portexpander-keyboard
caveman99 Oct 8, 2024
b22046a
Merge branch 'master' into portexpander-keyboard
caveman99 Oct 16, 2024
7b06229
Merge branch 'portexpander-keyboard' of https://github.com/meshtastic…
caveman99 Oct 16, 2024
d571271
dafuq?
caveman99 Oct 16, 2024
4b5cb7c
don't check this in dude
caveman99 Oct 16, 2024
8f86269
strange days...
caveman99 Oct 16, 2024
cfc3fac
Merge branch 'master' into portexpander-keyboard
caveman99 Oct 23, 2024
379ad9b
Merge branch 'master' into portexpander-keyboard
caveman99 Oct 25, 2024
a6ea4c5
fix STM32 builds
caveman99 Oct 25, 2024
413a579
Merge branch 'master' into portexpander-keyboard
caveman99 Oct 26, 2024
6239171
sheesh
caveman99 Oct 26, 2024
9fc8a93
Merge branch 'master' into portexpander-keyboard
caveman99 Oct 26, 2024
17948fb
Merge branch 'master' into portexpander-keyboard
caveman99 Nov 2, 2024
82f9cde
Merge branch 'master' into portexpander-keyboard
caveman99 Nov 11, 2024
aba2bff
Merge branch 'master' into portexpander-keyboard
caveman99 Nov 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions arch/stm32/stm32.ini
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,22 @@ build_flags =
-fdata-sections

build_src_filter =
${arduino_base.build_src_filter} -<platform/esp32/> -<nimble/> -<mesh/api/> -<mesh/wifi/> -<mesh/http/> -<modules/esp32> -<mesh/eth/> -<input> -<buzz> -<modules/RemoteHardwareModule.cpp> -<platform/nrf52> -<platform/portduino> -<platform/rp2xx0> -<mesh/raspihttp>
${arduino_base.build_src_filter}
-<platform/esp32/>
-<nimble/>
-<mesh/api/>
-<mesh/wifi/>
-<mesh/http/>
-<modules/esp32>
-<mesh/eth/>
-<input>
-<buzz>
-<modules/RemoteHardwareModule.cpp>
-<platform/nrf52>
-<platform/portduino>
-<platform/rp2xx0>
-<mesh/raspihttp>
-<.pio/*/*/I2CKeyPad/*>

board_upload.offset_address = 0x08000000
upload_protocol = stlink
Expand All @@ -34,5 +49,5 @@ lib_deps =
https://github.com/caveman99/Crypto.git#f61ae26a53f7a2d0ba5511625b8bf8eff3a35d5e

lib_ignore =
mathertel/[email protected]
Wire
Wire
I2CKeyPad
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ lib_deps =
https://github.com/meshtastic/ArduinoThread.git#1ae8778c85d0a2a729f989e0b1e7d7c4dc84eef0
nanopb/[email protected]
erriez/[email protected]
robtillaart/[email protected]

; Used for the code analysis in PIO Home / Inspect
check_tool = cppcheck
Expand Down
3 changes: 2 additions & 1 deletion src/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
// Define if screen should be mirrored left to right
// #define SCREEN_MIRROR

// I2C Keyboards (M5Stack, RAK14004, T-Deck)
// I2C Keyboards (M5Stack, RAK14004, T-Deck, PCF8574A passive)
#define CARDKB_ADDR 0x5F
#define TDECK_KB_ADDR 0x55
#define BBQ10_KB_ADDR 0x1F
#define MPR121_KB_ADDR 0x5A
#define PCF8574A_ADDRESS 0x20

// -----------------------------------------------------------------------------
// SENSOR
Expand Down
4 changes: 2 additions & 2 deletions src/detect/ScanI2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ ScanI2C::FoundDevice ScanI2C::firstRTC() const

ScanI2C::FoundDevice ScanI2C::firstKeyboard() const
{
ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004, MPR121KB};
return firstOfOrNONE(5, types);
ScanI2C::DeviceType types[] = {CARDKB, TDECKKB, BBQ10KB, RAK14004, PCF8574A, MPR121KB};
return firstOfOrNONE(6, types);
}

ScanI2C::FoundDevice ScanI2C::firstAccelerometer() const
Expand Down
1 change: 1 addition & 0 deletions src/detect/ScanI2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ScanI2C
TDECKKB,
BBQ10KB,
RAK14004,
PCF8574A,
PMU_AXP192_AXP2101,
BME_680,
BME_280,
Expand Down
5 changes: 5 additions & 0 deletions src/detect/ScanI2CTwoWire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,9 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize)
SCAN_SIMPLE_CASE(BBQ10_KB_ADDR, BBQ10KB, "BB Q10 keyboard found");

SCAN_SIMPLE_CASE(ST7567_ADDRESS, SCREEN_ST7567, "st7567 display found");
#ifndef HAS_TCA9535
SCAN_SIMPLE_CASE(PCF8574A_ADDRESS, PCF8574A, "PCF8574A based keyboard found");
#endif
#ifdef HAS_NCP5623
SCAN_SIMPLE_CASE(NCP5623_ADDR, NCP5623, "NCP5623 RGB LED found");
#endif
Expand Down Expand Up @@ -437,7 +440,9 @@ void ScanI2CTwoWire::scanPort(I2CPort port, uint8_t *address, uint8_t asize)
break;

SCAN_SIMPLE_CASE(LSM6DS3_ADDR, LSM6DS3, "LSM6DS3 accelerometer found at address 0x%x", (uint8_t)addr.address);
#ifdef HAS_TCA9535
SCAN_SIMPLE_CASE(TCA9535_ADDR, TCA9535, "TCA9535 I2C expander found");
#endif
SCAN_SIMPLE_CASE(TCA9555_ADDR, TCA9555, "TCA9555 I2C expander found");
SCAN_SIMPLE_CASE(VEML7700_ADDR, VEML7700, "VEML7700 light sensor found");
SCAN_SIMPLE_CASE(TSL25911_ADDR, TSL2591, "TSL2591 light sensor found");
Expand Down
4 changes: 4 additions & 0 deletions src/input/cardKbI2cImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ CardKbI2cImpl::CardKbI2cImpl() : KbI2cBase("cardKB") {}

void CardKbI2cImpl::init()
{
if (kb_model == 0x12) {
disable();
return;
}
#if !MESHTASTIC_EXCLUDE_I2C && !defined(ARCH_PORTDUINO) && !defined(I2C_NO_RESCAN)
if (cardkb_found.address == 0x00) {
LOG_DEBUG("Rescan for I2C keyboard");
Expand Down
88 changes: 88 additions & 0 deletions src/input/peMatrixBase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include "peMatrixBase.h"

#include "configuration.h"
#include "detect/ScanI2C.h"

extern ScanI2C::DeviceAddress cardkb_found;
extern uint8_t kb_model;

#if WIRE_INTERFACES_COUNT == 2 // defined in architecture.h
I2CKeyPad keyPad(cardkb_found.address, cardkb_found.port == ScanI2C::WIRE1 ? &Wire1 : &Wire);
#else
I2CKeyPad keyPad(cardkb_found.address, &Wire);
#endif

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");
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 100; // Keyscan every 100msec to avoid key bounce
}
22 changes: 22 additions & 0 deletions src/input/peMatrixBase.h
Original file line number Diff line number Diff line change
@@ -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;
};
16 changes: 16 additions & 0 deletions src/input/peMatrixImpl.cpp
Original file line number Diff line number Diff line change
@@ -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 changes: 19 additions & 0 deletions src/input/peMatrixImpl.h
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@ void setup()
// assign an arbitrary value to distinguish from other models
kb_model = 0x11;
break;
case ScanI2C::DeviceType::PCF8574A:
kb_model = 0x12;
break;
case ScanI2C::DeviceType::MPR121KB:
// assign an arbitrary value to distinguish from other models
kb_model = 0x37;
Expand Down
6 changes: 6 additions & 0 deletions src/modules/Modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "input/UpDownInterruptImpl1.h"
#include "input/cardKbI2cImpl.h"
#include "input/kbMatrixImpl.h"
#include "input/peMatrixImpl.h"
#endif
#if !MESHTASTIC_EXCLUDE_ADMIN
#include "modules/AdminModule.h"
Expand Down Expand Up @@ -168,10 +169,15 @@ void setupModules()
kbMatrixImpl = new KbMatrixImpl();
kbMatrixImpl->init();
#endif // INPUTBROKER_MATRIX_TYPE

peMatrixImpl = new PeMatrixImpl();
peMatrixImpl->init();

#ifdef INPUTBROKER_SERIAL_TYPE
aSerialKeyboardImpl = new SerialKeyboardImpl();
aSerialKeyboardImpl->init();
#endif // INPUTBROKER_MATRIX_TYPE

#endif // HAS_BUTTON
#if ARCH_PORTDUINO
aLinuxInputImpl = new LinuxInputImpl();
Expand Down
3 changes: 3 additions & 0 deletions variants/seeed-sensecap-indicator/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
// #define ADC_CHANNEL ADC1_GPIO27_CHANNEL
// #define ADC_MULTIPLIER 2

// Portexpander
#define HAS_TCA9535

// ST7701 TFT LCD
#define ST7701_CS (4 | IO_EXPANDER)
#define ST7701_RS -1 // DC
Expand Down