Skip to content

Commit 493df00

Browse files
committed
Add CH422G example, and add CH422G-specific example based on esp-arduino-libs#6
1 parent 2c65877 commit 493df00

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

examples/TestCH422G/TestCH422G.ino

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

0 commit comments

Comments
 (0)