Skip to content

Commit 2ca1b78

Browse files
H-sw123Lzw655
authored andcommitted
Update CH422G and add a new example
Closes #7 Closes #5
1 parent e5378da commit 2ca1b78

File tree

7 files changed

+306
-69
lines changed

7 files changed

+306
-69
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# ChangeLog
22

3+
## v0.0.4 - 2024-10-18
4+
5+
### Enhancements:
6+
7+
* Update CH422G and add a new example to show how to use it (@H-sw123)
8+
39
## v0.0.3 - 2024-05-07
410

511
### Enhancements:

examples/TestCH422G/TestCH422G.ino

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* | Supported IO Expanders | CH422G |
3+
* | ------------------------- | ------ |
4+
*
5+
* # CH422G Test Example
6+
*
7+
* 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.
8+
*
9+
* ## How to use
10+
*
11+
* 1. Enable USB CDC.
12+
* 2. Verify and upload the example to your board.
13+
*
14+
* ## Serial Output
15+
*
16+
* ```
17+
* ...
18+
* Test begin
19+
* Set the OC pin to push-pull output mode.
20+
* Set the IO0-7 pin to input mode.
21+
* Set pint 8 and 9 to:0, 1
22+
*
23+
* Read pin 0 and 5 level: 0, 1
24+
*
25+
* Set pint 8 and 9 to:1, 0
26+
*
27+
* Read pin 0 and 5 level: 1, 0
28+
* ...
29+
* ```
30+
*
31+
* ## Troubleshooting
32+
*
33+
* The driver initialization by default sets CH422G's IO0-7 to output high-level mode.
34+
Since the input/output mode of CH422G's IO0-7 must remain consistent, the driver will only set IO0-7 to
35+
input mode when it determines that all pins are configured as input.
36+
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
37+
*
38+
*/
39+
40+
#include <Arduino.h>
41+
#include <ESP_IOExpander_Library.h>
42+
43+
#define EXAMPLE_I2C_NUM (0)
44+
#define EXAMPLE_I2C_SDA_PIN (8)
45+
#define EXAMPLE_I2C_SCL_PIN (9)
46+
#define EXAMPLE_I2C_ADDR (ESP_IO_EXPANDER_I2C_CH422G_ADDRESS)
47+
48+
ESP_IOExpander_CH422G *expander = NULL;
49+
50+
void setup() {
51+
Serial.begin(115200);
52+
delay(1000);
53+
Serial.println("Test begin");
54+
55+
expander = new ESP_IOExpander_CH422G((i2c_port_t)EXAMPLE_I2C_NUM, EXAMPLE_I2C_ADDR, EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN);
56+
expander->init();
57+
expander->begin();
58+
59+
/* For CH422G */
60+
Serial.println("Set the OC pin to push-pull output mode.");
61+
expander->enableOC_PushPull();
62+
63+
// Serial.println("Set the OC pin to open_drain output mode.");
64+
// expander->enableOC_OpenDrain();
65+
66+
Serial.println("Set the IO0-7 pin to input mode.");
67+
expander->enableAllIO_Input();
68+
69+
// Serial.println("Set the IO0-7 pin to output mode.");
70+
// expander->enableAllIO_Output();
71+
}
72+
73+
int level[2] = { 0, 0 };
74+
75+
void loop() {
76+
for (int i = 0; i < 100; i++) {
77+
bool toggle = i % 2;
78+
79+
Serial.print("Set pint 8 and 9 to:");
80+
Serial.print(toggle);
81+
Serial.print(", ");
82+
Serial.println(!toggle);
83+
Serial.println();
84+
85+
// Set pin 8 and 9 level
86+
expander->digitalWrite(8, toggle);
87+
expander->digitalWrite(9, !toggle);
88+
delay(1);
89+
90+
// Read pin 0 and 5 level
91+
level[0] = expander->digitalRead(0);
92+
level[1] = expander->digitalRead(5);
93+
94+
Serial.print("Read pin 0 and 5 level: ");
95+
Serial.print(level[0]);
96+
Serial.print(", ");
97+
Serial.println(level[1]);
98+
Serial.println();
99+
100+
delay(1000);
101+
}
102+
}

examples/TestFunctions/TestFunctions.ino

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#define EXAMPLE_I2C_NUM (0)
1313
#define EXAMPLE_I2C_SDA_PIN (8)
1414
#define EXAMPLE_I2C_SCL_PIN (18)
15+
#define EXAMPLE_I2C_ADDR (ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000) // Modify this value according to the
16+
// hardware address
1517

1618
#define _EXAMPLE_CHIP_CLASS(name, ...) ESP_IOExpander_##name(__VA_ARGS__)
1719
#define EXAMPLE_CHIP_CLASS(name, ...) _EXAMPLE_CHIP_CLASS(name, ##__VA_ARGS__)
@@ -29,6 +31,12 @@ void setup()
2931
expander->init();
3032
expander->begin();
3133

34+
/* For CH422G */
35+
// static_cast<ESP_IOExpander_CH422G *>(expander)->enableOC_PushPull();
36+
// static_cast<ESP_IOExpander_CH422G *>(expander)->enableOC_OpenDrain();
37+
// static_cast<ESP_IOExpander_CH422G *>(expander)->enableAllIO_Input();
38+
// static_cast<ESP_IOExpander_CH422G *>(expander)->enableAllIO_Output();
39+
3240
Serial.println("Original status:");
3341
expander->printStatus();
3442

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name=ESP32_IO_Expander
2-
version=0.0.3
3-
author=lzw655
2+
version=0.0.4
3+
author=espressif
44
maintainer=espressif
55
sentence=ESP32_IO_Expander is a library designed for driving IO expander chips using ESP32 SoCs
66
paragraph=Currently support TCA95xx(8bit), TCA95xx(16bit), HT8574, CH422G

0 commit comments

Comments
 (0)