Skip to content

Commit 95f1bed

Browse files
committed
Add examples for CH422G
1 parent a0afad3 commit 95f1bed

File tree

5 files changed

+501
-11
lines changed

5 files changed

+501
-11
lines changed

examples/TestCH422G/TestCH422G.ino

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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) // Modify this value according to the \
47+
// hardware address
48+
49+
ESP_IOExpander_CH422G *expander = NULL;
50+
51+
void setup() {
52+
Serial.begin(115200);
53+
delay(1000);
54+
Serial.println("Test begin");
55+
56+
expander = new ESP_IOExpander_CH422G((i2c_port_t)EXAMPLE_I2C_NUM, EXAMPLE_I2C_ADDR,
57+
EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN);
58+
expander->init();
59+
expander->begin();
60+
61+
/* For CH422G */
62+
Serial.println("Set the OC pin to push-pull output mode.");
63+
static_cast<ESP_IOExpander_CH422G *>(expander)->enableOC_PushPull();
64+
65+
// Serial.println("Set the OC pin to open_drain output mode.");
66+
// static_cast<ESP_IOExpander_CH422G *>(expander)->enableOC_OpenDrain();
67+
68+
Serial.println("Set the IO0-7 pin to input mode.");
69+
static_cast<ESP_IOExpander_CH422G *>(expander)->enableAllIO_Input();
70+
71+
// Serial.println("Set the IO0-7 pin to output mode.");
72+
//static_cast<ESP_IOExpander_CH422G *>(expander)->enableAllIO_Output();
73+
}
74+
75+
int level[2] = { 0, 0 };
76+
77+
void loop() {
78+
for (int i = 0; i < 100; i++) {
79+
bool toggle = i % 2;
80+
81+
Serial.print("Set pint 8 and 9 to:");
82+
Serial.print(toggle);
83+
Serial.print(", ");
84+
Serial.println(!toggle);
85+
Serial.println();
86+
87+
// Set pin 8 and 9 level
88+
expander->digitalWrite(8, toggle);
89+
expander->digitalWrite(9, !toggle);
90+
delay(1);
91+
92+
// Read pin 0 and 5 level
93+
level[0] = expander->digitalRead(0);
94+
level[1] = expander->digitalRead(5);
95+
96+
Serial.print("Read pin 0 and 5 level: ");
97+
Serial.print(level[0]);
98+
Serial.print(", ");
99+
Serial.println(level[1]);
100+
Serial.println();
101+
102+
delay(1000);
103+
}
104+
}

examples/TestFunctions/TestFunctions.ino

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#define EXAMPLE_I2C_NUM (0)
55
#define EXAMPLE_I2C_SDA_PIN (8)
66
#define EXAMPLE_I2C_SCL_PIN (18)
7+
#define EXAMPLE_I2C_ADDR (ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000) // Modify this value according to the
8+
// hardware address
79

810
/**
911
* Create an ESP_IOExpander object, Currently supports:
@@ -18,9 +20,18 @@ void setup()
1820
Serial.begin(115200);
1921
Serial.println("Test begin");
2022

23+
<<<<<<< HEAD
24+
=======
25+
expander = new EXAMPLE_CHIP_CLASS(EXAMPLE_CHIP_NAME, (i2c_port_t)EXAMPLE_I2C_NUM, EXAMPLE_I2C_ADDR,
26+
EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN);
27+
>>>>>>> a426a76 (Add examples for CH422G)
2128
expander->init();
2229
expander->begin();
2330

31+
/* For CH422G */
32+
// static_cast<ESP_IOExpander_CH422G *>(expander)->enableOC_PushPull();
33+
// static_cast<ESP_IOExpander_CH422G *>(expander)->enableOC_OpenDrain();
34+
2435
Serial.println("Original status:");
2536
expander->printStatus();
2637

0 commit comments

Comments
 (0)