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/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..b8a76c7 100644 --- a/src/M5StamPLC.cpp +++ b/src/M5StamPLC.cpp @@ -71,6 +71,14 @@ 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() +{ + return M5.getIOExpander(0); } void M5_STAMPLC::setBacklight(bool on) @@ -138,6 +146,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;