From f28d9b75a4427770ebd0167025af7510919df3a7 Mon Sep 17 00:00:00 2001 From: onexs-xsi <850559025@qq.com> Date: Sun, 8 Feb 2026 12:14:50 +0800 Subject: [PATCH] =?UTF-8?q?1.Added=20M5IOE1=5FI2C=5FWRITE=5FDELAY=5FUS=20a?= =?UTF-8?q?nd=20M5IOE1=5FI2C=5FREAD=5FDELAY=5FUS=20macros=20(default=20500?= =?UTF-8?q?=CE=BCs)=20in=20I2C=20operations=202.Implemented=20clearLedRam(?= =?UTF-8?q?)=20API=20to=20zero=20all=2064=20bytes=20of=20LED=20RAM=20regis?= =?UTF-8?q?ters=203.update=201.0.6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- idf_component.yml | 2 +- library.json | 2 +- library.properties | 2 +- src/M5IOE1.cpp | 36 +++++++++++++++++++++++++++++++++--- src/M5IOE1.h | 9 +++++++++ src/M5IOE1_i2c_compat.h | 16 ++++++++++++++++ 6 files changed, 61 insertions(+), 6 deletions(-) diff --git a/idf_component.yml b/idf_component.yml index 8196e13..c01eb55 100644 --- a/idf_component.yml +++ b/idf_component.yml @@ -13,4 +13,4 @@ dependencies: espressif/i2c_bus: version: "^1.0.0" public: true -version: 1.0.5 +version: 1.0.6 diff --git a/library.json b/library.json index 24cd0e5..a5061d1 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "M5IOE1", - "version": "1.0.5", + "version": "1.0.6", "description": "Library for M5Stack M5IOE1", "keywords": [ "m5stack", diff --git a/library.properties b/library.properties index 7edf0d3..007f66e 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=M5IOE1 -version=1.0.5 +version=1.0.6 author=M5Stack maintainer=M5Stack sentence=Library for M5Stack M5IOE1 diff --git a/src/M5IOE1.cpp b/src/M5IOE1.cpp index c59a009..d20b680 100644 --- a/src/M5IOE1.cpp +++ b/src/M5IOE1.cpp @@ -2383,6 +2383,36 @@ m5ioe1_err_t M5IOE1::disableLeds() return M5IOE1_OK; } +m5ioe1_err_t M5IOE1::clearLedRam() +{ + if (!_initialized) { + M5IOE1_LOG_E(TAG, "Device not initialized"); + return M5IOE1_ERR_NOT_INIT; + } + + // 清除所有 LED RAM (0x30 - 0x6F, 共 64 字节,支持 32 个 LED) + // Clear all LED RAM (0x30 - 0x6F, 64 bytes total, supports 32 LEDs) + uint8_t zeroData[64] = {0}; + + M5IOE1_LOG_I(TAG, "Clearing LED RAM..."); + + // 分块写入,每次写入 8 字节,避免 I2C 缓冲区溢出 + // Write in chunks of 8 bytes to avoid I2C buffer overflow + for (uint8_t offset = 0; offset < 64; offset += 8) { + uint8_t regAddr = M5IOE1_REG_LED_RAM_START + offset; + if (!_writeBytes(regAddr, zeroData, 8)) { + M5IOE1_LOG_E(TAG, "Failed to clear LED RAM at offset %d", offset); + return M5IOE1_ERR_I2C_COMM; + } + // 每次写入后增加延迟,确保设备有时间处理 + // Add delay after each write to ensure device has time to process + M5IOE1_DELAY_MS(2); + } + + M5IOE1_LOG_I(TAG, "LED RAM cleared successfully"); + return M5IOE1_OK; +} + m5ioe1_err_t M5IOE1::setLeds(const m5ioe1_rgb_t* colors, uint8_t count, uint8_t arraySize, bool autoRefresh) { // 步骤 1: 参数验证 @@ -3300,9 +3330,9 @@ m5ioe1_err_t M5IOE1::updateSnapshot() { if (!_initialized) return M5IOE1_ERR_NOT_INIT; - bool gpio = _snapshotPinStates(); - bool pwm = _snapshotPwmStates(); - bool adc = _snapshotAdcState(); + bool gpio = _snapshotPinStates(); + bool pwm = _snapshotPwmStates(); + bool adc = _snapshotAdcState(); bool aw8737a = _snapshotAw8737a(); return (gpio && pwm && adc && aw8737a) ? M5IOE1_OK : M5IOE1_ERR_I2C_COMM; diff --git a/src/M5IOE1.h b/src/M5IOE1.h index 01f1ad2..08da289 100644 --- a/src/M5IOE1.h +++ b/src/M5IOE1.h @@ -1087,6 +1087,15 @@ public: */ m5ioe1_err_t disableLeds(); + /** + * @brief 清除 LED RAM 寄存器(将所有 LED 数据设置为 0) + * Clear LED RAM registers (set all LED data to 0) + * @return M5IOE1_OK if successful, error code otherwise + * @note 此函数会清除所有 32 个 LED 的颜色数据,建议在 begin() 后调用 + * This function clears color data for all 32 LEDs, recommended to call after begin() + */ + m5ioe1_err_t clearLedRam(); + // ======================== // AW8737A 脉冲功能 // AW8737A Pulse Functions diff --git a/src/M5IOE1_i2c_compat.h b/src/M5IOE1_i2c_compat.h index 4d7bab8..d5e1155 100644 --- a/src/M5IOE1_i2c_compat.h +++ b/src/M5IOE1_i2c_compat.h @@ -15,6 +15,18 @@ #include "Wire.h" +// ============================ +// I2C 延迟配置 (可调整以解决总线冲突) +// I2C Delay Configuration (adjustable to resolve bus conflicts) +// ============================ +#ifndef M5IOE1_I2C_WRITE_DELAY_US +#define M5IOE1_I2C_WRITE_DELAY_US 500 // 写操作后延迟(微秒) / Delay after write (microseconds) +#endif + +#ifndef M5IOE1_I2C_READ_DELAY_US +#define M5IOE1_I2C_READ_DELAY_US 500 // 读操作前延迟(微秒) / Delay before read (microseconds) +#endif + // ============================ // Arduino I2C 功能 // Arduino I2C Functions @@ -28,6 +40,7 @@ static inline bool M5IOE1_I2C_READ_BYTE(TwoWire *wire, uint8_t addr, uint8_t reg if (wire->endTransmission(false) != 0) { return false; } + delayMicroseconds(M5IOE1_I2C_READ_DELAY_US); // 增加延迟避免 I2C 总线冲突 if (wire->requestFrom(addr, (uint8_t)1) != 1) { return false; } @@ -44,6 +57,7 @@ static inline bool M5IOE1_I2C_READ_BYTES(TwoWire *wire, uint8_t addr, uint8_t st if (wire->endTransmission(false) != 0) { return false; } + delayMicroseconds(M5IOE1_I2C_READ_DELAY_US); // 增加延迟避免 I2C 总线冲突 if (wire->requestFrom(addr, (uint8_t)len) != len) { return false; } @@ -77,6 +91,7 @@ static inline bool M5IOE1_I2C_WRITE_BYTE(TwoWire *wire, uint8_t addr, uint8_t re if (wire->endTransmission() != 0) { return false; } + delayMicroseconds(M5IOE1_I2C_WRITE_DELAY_US); // 增加延迟给设备处理时间 return true; } #endif @@ -93,6 +108,7 @@ static inline bool M5IOE1_I2C_WRITE_BYTES(TwoWire *wire, uint8_t addr, uint8_t s if (wire->endTransmission() != 0) { return false; } + delayMicroseconds(M5IOE1_I2C_WRITE_DELAY_US); // 增加延迟给设备处理时间 return true; } #endif