From f3b6c86c18fabc4e39a4ab62472f8a9aa843d5b2 Mon Sep 17 00:00:00 2001 From: Forairaaaaa Date: Mon, 8 Dec 2025 10:00:00 +0800 Subject: [PATCH 1/2] add io expander a\b getter, add PI4IOE5V6408 irq example --- .../PI4IOE5V6408_IRQ/PI4IOE5V6408_IRQ.ino | 40 +++++++++++++++++++ src/M5StamPLC.cpp | 10 +++++ src/M5StamPLC.h | 14 +++++++ 3 files changed, 64 insertions(+) create mode 100644 examples/Basic/PI4IOE5V6408_IRQ/PI4IOE5V6408_IRQ.ino diff --git a/examples/Basic/PI4IOE5V6408_IRQ/PI4IOE5V6408_IRQ.ino b/examples/Basic/PI4IOE5V6408_IRQ/PI4IOE5V6408_IRQ.ino new file mode 100644 index 0000000..2152fd4 --- /dev/null +++ b/examples/Basic/PI4IOE5V6408_IRQ/PI4IOE5V6408_IRQ.ino @@ -0,0 +1,40 @@ +/* + * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD + * + * SPDX-License-Identifier: MIT + */ +#include +#include + +#define PIN_IRQ 14 + +void setup() +{ + /* Init M5StamPLC */ + M5StamPLC.begin(); + + /* Enable PI4IOE5V6408 irq */ + auto& ioe = M5StamPLC.getIOExpanderA(); + ioe.enableIrq(); + + /* Setup irq pin */ + pinMode(PIN_IRQ, INPUT_PULLUP); + + printf("Press button A/B/C to trigger irq\n"); +} + +void loop() +{ + /* Check irq pin */ + if (digitalRead(PIN_IRQ) == LOW) { + printf("PI4IOE5V6408 irq detected\n"); + + /* Clear irq */ + auto& ioe = M5StamPLC.getIOExpanderA(); + ioe.resetIrq(); + + delay(100); + } + + delay(20); +} diff --git a/src/M5StamPLC.cpp b/src/M5StamPLC.cpp index ab78d01..253453e 100644 --- a/src/M5StamPLC.cpp +++ b/src/M5StamPLC.cpp @@ -73,6 +73,11 @@ void M5_STAMPLC::io_expander_a_init() ioe.setHighImpedance(6, true); } +m5::IOExpander_Base& M5_STAMPLC::getIOExpanderA() +{ + return M5.getIOExpander(0); +} + void M5_STAMPLC::setBacklight(bool on) { auto& ioe = M5.getIOExpander(0); @@ -138,6 +143,11 @@ void M5_STAMPLC::io_expander_b_init() } } +AW9523_Class& M5_STAMPLC::getIOExpanderB() +{ + return *_io_expander_b; +} + bool M5_STAMPLC::readPlcInput(const uint8_t& channel) { if (_io_expander_b == nullptr) { diff --git a/src/M5StamPLC.h b/src/M5StamPLC.h index 7ad30c9..2c31b01 100644 --- a/src/M5StamPLC.h +++ b/src/M5StamPLC.h @@ -151,6 +151,20 @@ class M5_STAMPLC { */ void noTone(); + /** + * @brief Get the IOExpander A, this is the IOExpander that controls the status light and button A/B/C + * + * @return m5::IOExpander_Base& + */ + m5::IOExpander_Base& getIOExpanderA(); + + /** + * @brief Get the IOExpander B, this is the IOExpander that controls the plc relays and plc inputs + * + * @return AW9523_Class& + */ + AW9523_Class& getIOExpanderB(); + protected: AW9523_Class* _io_expander_b = nullptr; // Controls plc relays, plc inputs Config_t _config; From b1885a9ff15640261f48fc4f6555c7558fadb94f Mon Sep 17 00:00:00 2001 From: Forairaaaaa Date: Mon, 8 Dec 2025 10:17:25 +0800 Subject: [PATCH 2/2] add ioe a irq disable and clear on init, add AW9523B irq example --- examples/Basic/AW9523B_IRQ/AW9523B_IRQ.ino | 43 ++++++++++++++++++++++ src/M5StamPLC.cpp | 3 ++ 2 files changed, 46 insertions(+) create mode 100644 examples/Basic/AW9523B_IRQ/AW9523B_IRQ.ino diff --git a/examples/Basic/AW9523B_IRQ/AW9523B_IRQ.ino b/examples/Basic/AW9523B_IRQ/AW9523B_IRQ.ino new file mode 100644 index 0000000..1d6878f --- /dev/null +++ b/examples/Basic/AW9523B_IRQ/AW9523B_IRQ.ino @@ -0,0 +1,43 @@ +/* + * SPDX-FileCopyrightText: 2025 M5Stack Technology CO LTD + * + * SPDX-License-Identifier: MIT + */ +#include +#include + +#define PIN_IRQ 14 + +void setup() +{ + /* Init M5StamPLC */ + M5StamPLC.begin(); + + /* Enable AW9523B input pins irq */ + auto& ioe = M5StamPLC.getIOExpanderB(); + std::vector input_pins = {4, 5, 6, 7, 12, 13, 14, 15}; + for (const auto& i : input_pins) { + ioe.enableInterrupt(i, true); + } + + /* Setup irq pin */ + pinMode(PIN_IRQ, INPUT_PULLUP); + + printf("Change PLC input state to trigger irq\n"); +} + +void loop() +{ + /* Check irq pin */ + if (digitalRead(PIN_IRQ) == LOW) { + printf("AW9523B irq detected\n"); + + /* Clear irq */ + auto& ioe = M5StamPLC.getIOExpanderB(); + ioe.resetIrq(); + + delay(100); + } + + delay(20); +} diff --git a/src/M5StamPLC.cpp b/src/M5StamPLC.cpp index 253453e..b8a76c7 100644 --- a/src/M5StamPLC.cpp +++ b/src/M5StamPLC.cpp @@ -71,6 +71,9 @@ void M5_STAMPLC::io_expander_a_init() ioe.setDirection(6, true); ioe.setPullMode(6, false); ioe.setHighImpedance(6, true); + + ioe.resetIrq(); + ioe.disableIrq(); } m5::IOExpander_Base& M5_STAMPLC::getIOExpanderA()