Skip to content

Commit

Permalink
Update CH422G and add a new example
Browse files Browse the repository at this point in the history
Closes #7
Closes #5
  • Loading branch information
H-sw123 authored and Lzw655 committed Oct 18, 2024
1 parent e5378da commit a077be5
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 69 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# ChangeLog

## v0.0.4 - 2024-10-18

### Enhancements:

* Update CH422G and add a new example to show how to use it (@H-sw123)

## v0.0.3 - 2024-05-07

### Enhancements:
Expand Down
102 changes: 102 additions & 0 deletions examples/TestCH422G/TestCH422G.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* | Supported IO Expanders | CH422G |
* | ------------------------- | ------ |
*
* # CH422G Test Example
*
* The hardware device used in this example is waveshare ESP32-S3-Touch-LCD-4.3B-BOX. To test the simultaneous use of I/O input and OC output, connect DO0 to DI0, and connect DO1 to DI1.
*
* ## How to use
*
* 1. Enable USB CDC.
* 2. Verify and upload the example to your board.
*
* ## Serial Output
*
* ```
* ...
* Test begin
* Set the OC pin to push-pull output mode.
* Set the IO0-7 pin to input mode.
* Set pint 8 and 9 to:0, 1
*
* Read pin 0 and 5 level: 0, 1
*
* Set pint 8 and 9 to:1, 0
*
* Read pin 0 and 5 level: 1, 0
* ...
* ```
*
* ## Troubleshooting
*
* The driver initialization by default sets CH422G's IO0-7 to output high-level mode.
Since the input/output mode of CH422G's IO0-7 must remain consistent, the driver will only set IO0-7 to
input mode when it determines that all pins are configured as input.
Using pinMode and multiPinMode will be invalid. You can only set the pin working mode through enableAllIO_Input, enableAllIO_Output, enableOC_PushPull and enableOC_OpenDrain
*
*/

#include <Arduino.h>
#include <ESP_IOExpander_Library.h>

#define EXAMPLE_I2C_NUM (0)
#define EXAMPLE_I2C_SDA_PIN (8)
#define EXAMPLE_I2C_SCL_PIN (9)
#define EXAMPLE_I2C_ADDR (ESP_IO_EXPANDER_I2C_CH422G_ADDRESS)

ESP_IOExpander_CH422G *expander = NULL;

void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Test begin");

expander = new ESP_IOExpander_CH422G((i2c_port_t)EXAMPLE_I2C_NUM, EXAMPLE_I2C_ADDR, EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN);
expander->init();
expander->begin();

/* For CH422G */
Serial.println("Set the OC pin to push-pull output mode.");
expander->enableOC_PushPull();

// Serial.println("Set the OC pin to open_drain output mode.");
// expander->enableOC_OpenDrain();

Serial.println("Set the IO0-7 pin to input mode.");
expander->enableAllIO_Input();

// Serial.println("Set the IO0-7 pin to output mode.");
// expander->enableAllIO_Output();
}

int level[2] = { 0, 0 };

void loop() {
for (int i = 0; i < 100; i++) {
bool toggle = i % 2;

Serial.print("Set pint 8 and 9 to:");
Serial.print(toggle);
Serial.print(", ");
Serial.println(!toggle);
Serial.println();

// Set pin 8 and 9 level
expander->digitalWrite(8, toggle);
expander->digitalWrite(9, !toggle);
delay(1);

// Read pin 0 and 5 level
level[0] = expander->digitalRead(0);
level[1] = expander->digitalRead(5);

Serial.print("Read pin 0 and 5 level: ");
Serial.print(level[0]);
Serial.print(", ");
Serial.println(level[1]);
Serial.println();

delay(1000);
}
}
8 changes: 8 additions & 0 deletions examples/TestFunctions/TestFunctions.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#define EXAMPLE_I2C_NUM (0)
#define EXAMPLE_I2C_SDA_PIN (8)
#define EXAMPLE_I2C_SCL_PIN (18)
#define EXAMPLE_I2C_ADDR (ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000) // Modify this value according to the
// hardware address

#define _EXAMPLE_CHIP_CLASS(name, ...) ESP_IOExpander_##name(__VA_ARGS__)
#define EXAMPLE_CHIP_CLASS(name, ...) _EXAMPLE_CHIP_CLASS(name, ##__VA_ARGS__)
Expand All @@ -29,6 +31,12 @@ void setup()
expander->init();
expander->begin();

/* For CH422G */
// static_cast<ESP_IOExpander_CH422G *>(expander)->enableOC_PushPull();
// static_cast<ESP_IOExpander_CH422G *>(expander)->enableOC_OpenDrain();
// static_cast<ESP_IOExpander_CH422G *>(expander)->enableAllIO_Input();
// static_cast<ESP_IOExpander_CH422G *>(expander)->enableAllIO_Output();

Serial.println("Original status:");
expander->printStatus();

Expand Down
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=ESP32_IO_Expander
version=0.0.3
author=lzw655
version=0.0.4
author=espressif
maintainer=espressif
sentence=ESP32_IO_Expander is a library designed for driving IO expander chips using ESP32 SoCs
paragraph=Currently support TCA95xx(8bit), TCA95xx(16bit), HT8574, CH422G
Expand Down
Loading

0 comments on commit a077be5

Please sign in to comment.