Skip to content

Commit

Permalink
Update the example to show how to use the CH422G
Browse files Browse the repository at this point in the history
  • Loading branch information
Lzw655 committed May 7, 2024
1 parent 443980c commit 57a23cb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 26 deletions.
15 changes: 11 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
# ChangeLog

## v0.0.1 - 2023-09-20
## v0.0.3 - 2024-05-07

### Enhancements:

* Support for various IO expander chips.
* Support to control individual IO in the same way as Arduino
* Support to control multiple IOs at the same time.
* Add support for CH422G from Waveshare (@lboue)
* Update the example to show how to use the CH422G

## v0.0.2 - 2023-10-07

### Bug Fixes:

* Correct library name in `sentence` field of metadata

## v0.0.1 - 2023-09-20

### Enhancements:

* Support for various IO expander chips.
* Support to control individual IO in the same way as Arduino
* Support to control multiple IOs at the same time.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ ESP32_IO_Expander encapsulates various components from the [Espressif Components
| [TCA95xx (8bit)](https://components.espressif.com/components/espressif/esp_io_expander_tca9554) | 1.0.1 |
| [TCA95xx (16bit)](https://components.espressif.com/components/espressif/esp_io_expander_tca95xx_16bit) | 1.0.0 |
| [HT8574](https://components.espressif.com/components/espressif/esp_io_expander_ht8574) | 1.0.0 |
| CH422G | x |

## Dependencies Version

| **Name** | **Version** |
| ----------------------------------------------------------- | ----------- |
| ESP32_IO_Expander | v0.x.x |
| [arduino-esp32](https://github.com/espressif/arduino-esp32) | >= v2.0.9 |

## How to Use
Expand Down
24 changes: 16 additions & 8 deletions examples/TestFunctions/TestFunctions.ino
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
#include <Arduino.h>
#include <ESP_IOExpander_Library.h>

#define EXAMPLE_I2C_NUM (0)
#define EXAMPLE_I2C_SDA_PIN (8)
#define EXAMPLE_I2C_SCL_PIN (18)

/**
* Create an ESP_IOExpander object, Currently supports:
* - TCA95xx (8bit)
* - TCA95xx (16bit)
* - TCA95xx_8bit
* - TCA95xx_16bit
* - HT8574
* - CH422G
*/
ESP_IOExpander *expander = new ESP_IOExpander_TCA95xx_8bit(EXAMPLE_I2C_NUM, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000, EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN);
#define EXAMPLE_CHIP_NAME TCA95xx_8bit
#define EXAMPLE_I2C_NUM (0)
#define EXAMPLE_I2C_SDA_PIN (8)
#define EXAMPLE_I2C_SCL_PIN (18)

#define _EXAMPLE_CHIP_CLASS(name, ...) ESP_IOExpander_##name(__VA_ARGS__)
#define EXAMPLE_CHIP_CLASS(name, ...) _EXAMPLE_CHIP_CLASS(name, ##__VA_ARGS__)

ESP_IOExpander *expander = NULL;

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

expander = new EXAMPLE_CHIP_CLASS(EXAMPLE_CHIP_NAME,
(i2c_port_t)EXAMPLE_I2C_NUM, ESP_IO_EXPANDER_I2C_TCA9554_ADDRESS_000,
EXAMPLE_I2C_SCL_PIN, EXAMPLE_I2C_SDA_PIN);
expander->init();
expander->begin();

Expand Down Expand Up @@ -68,5 +76,5 @@ void loop()
Serial.print(", ");
Serial.println(level[3]);

sleep(1);
delay(1000);
}
4 changes: 2 additions & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name=ESP32_IO_Expander
version=0.0.2
version=0.0.3
author=lzw655
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
paragraph=Currently support TCA95xx(8bit), TCA95xx(16bit), HT8574, CH422G
category=Other
architectures=esp32
url=https://github.com/esp-arduino-libs/ESP32_IO_Expander
Expand Down
22 changes: 12 additions & 10 deletions src/chip/CH422G.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -21,6 +21,10 @@

#define IO_COUNT (8)

/* Register address */
#define CH422G_REG_IN (0x26)
#define CH422G_REG_OUT (0x38)

/* Default register value on power-up */
#define DIR_REG_DEFAULT_VAL (0xff)
#define OUT_REG_DEFAULT_VAL (0xdf)
Expand Down Expand Up @@ -98,17 +102,16 @@ static esp_err_t esp_io_expander_new_i2c_ch422g(i2c_port_t i2c_num, uint32_t i2c
return ret;
}

#define CH422G_REG_IN 0x26
static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value)
{
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);

uint8_t temp = 0;
ESP_RETURN_ON_ERROR(

ESP_RETURN_ON_ERROR(
i2c_master_read_from_device(ch422g->i2c_num, ch422g->i2c_address, &temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
TAG, "Read input reg failed");

// *INDENT-OFF*
ESP_RETURN_ON_ERROR(
i2c_master_read_from_device(ch422g->i2c_num, CH422G_REG_IN, &temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
Expand All @@ -118,17 +121,16 @@ static esp_err_t read_input_reg(esp_io_expander_handle_t handle, uint32_t *value
return ESP_OK;
}

#define CH422G_REG_OUT 0x38
static esp_err_t write_output_reg(esp_io_expander_handle_t handle, uint32_t value)
{
esp_io_expander_ch422g_t *ch422g = (esp_io_expander_ch422g_t *)__containerof(handle, esp_io_expander_ch422g_t, base);
value &= 0xff;

uint8_t out_temp = 0x01;
ESP_RETURN_ON_ERROR(
uint8_t out_temp = 0x01;
ESP_RETURN_ON_ERROR(
i2c_master_write_to_device(ch422g->i2c_num, ch422g->i2c_address, &out_temp, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
TAG, "Write output reg failed");

uint8_t data = (uint8_t)value;
ESP_RETURN_ON_ERROR(
i2c_master_write_to_device(ch422g->i2c_num, CH422G_REG_OUT, &data, 1, pdMS_TO_TICKS(I2C_TIMEOUT_MS)),
Expand Down
2 changes: 1 addition & 1 deletion src/chip/CH422G.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down

0 comments on commit 57a23cb

Please sign in to comment.