From 1e396990fa827fc2c5fe58b0156b47b387019b04 Mon Sep 17 00:00:00 2001 From: ainyan03 <205502311+ainyan03@users.noreply.github.com> Date: Wed, 14 Oct 2026 03:02:18 +0000 Subject: [PATCH] Fix PI4IOE5V6408 initialization and checked input reads begin() never set the initialized flag, so isEnabled() always returned false. It now records whether the device ID register could be read (and is non-zero) on every call, so a changed port or address is checked again. getInputLevel() was not overridden and always reported failure. It now reads the input status register (0x0F) with error checking and leaves the output untouched on failure, matching M5IOE1. --- src/utility/PI4IOE5V6408_Class.hpp | 2 ++ src/utility/PI4IOE5V6408_Class.inl | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/utility/PI4IOE5V6408_Class.hpp b/src/utility/PI4IOE5V6408_Class.hpp index 69480224..69607eb5 100644 --- a/src/utility/PI4IOE5V6408_Class.hpp +++ b/src/utility/PI4IOE5V6408_Class.hpp @@ -43,6 +43,8 @@ namespace m5 bool digitalRead(uint8_t pin) override; + bool getInputLevel(uint8_t pin, bool* level) override; + bool resetIrq() override; bool disableIrq() override; diff --git a/src/utility/PI4IOE5V6408_Class.inl b/src/utility/PI4IOE5V6408_Class.inl index 6faac6d3..baab5cc3 100644 --- a/src/utility/PI4IOE5V6408_Class.inl +++ b/src/utility/PI4IOE5V6408_Class.inl @@ -17,9 +17,10 @@ namespace m5 { bool PI4IOE5V6408_Class::begin() { - auto id = readRegister8(0x01); - if (id == 0) return false; - return true; + uint8_t id; + // Recheck the current address and port on every begin, including after reconnect. + _init = readRegister(0x01, &id, 1) && id != 0; + return _init; } // false input, true output @@ -83,6 +84,16 @@ bool PI4IOE5V6408_Class::digitalRead(uint8_t pin) return (data & (1 << pin)) != 0; } +bool PI4IOE5V6408_Class::getInputLevel(uint8_t pin, bool* level) +{ + if (pin >= 8 || level == nullptr) { return false; } + uint8_t value; + // Preserve the caller's value when the input register cannot be read. + if (!readRegister(0x0F, &value, 1)) { return false; } + *level = (value & (1 << pin)) != 0; + return true; +} + bool PI4IOE5V6408_Class::resetIrq() { uint8_t value;