SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
sensor_xl9555.cpp
Go to the documentation of this file.
1
30#include "sdkconfig.h"
31#include "esp_log.h"
32#include "esp_err.h"
33#include "i2c_driver.h"
34#include "freertos/FreeRTOS.h"
35
36#ifdef CONFIG_XL9555
37
38#include "IoExpanderDrv.hpp"
39
40static const char *TAG = "XL9555";
41
43
44static bool init_done = false;
45
46esp_err_t xl9555_init()
47{
48 ESP_LOGI(TAG, "----DRIVER XL9555 ----");
49
50 //* Implemented using read and write callback methods, applicable to other platforms
51#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
52
53 uint8_t address = XL9555_SLAVE_ADDRESS0;
54
55 // * Provide the device address to the callback function
56 i2c_drv_device_init(address);
57
58 ESP_LOGI(TAG, "Implemented using read and write callback methods");
59 if (io.begin(i2c_wr_function)) {
60 ESP_LOGI(TAG, "Initializing XL9555 successfully!");
61 } else {
62 ESP_LOGE(TAG, "Failed to initialize XL9555 failed!");
63 return ESP_FAIL;
64 }
65#endif
66
67 //* Use the built-in esp-idf communication method
68#if CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
69#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
70
71 ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use higher version >= 5.0 API)");
72
73 // * Using the new API of esp-idf 5.x, you need to pass the I2C BUS handle,
74 // * which is useful when the bus shares multiple devices.
75 extern i2c_master_bus_handle_t bus_handle;
76
77 if (io.begin(bus_handle)) {
78 ESP_LOGI(TAG, "Initializing XL9555 successfully!");
79 } else {
80 ESP_LOGE(TAG, "Failed to initialize XL9555 failed!");
81 return ESP_FAIL;
82 }
83
84#else
85
86 ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use lower version < 5.0 API)");
87 if (io.begin((i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM, CONFIG_SENSOR_SDA, CONFIG_SENSOR_SCL)) {
88 ESP_LOGI(TAG, "Initializing XL9555 successfully!");
89 } else {
90 ESP_LOGE(TAG, "Failed to initialize XL9555 failed!");
91 return ESP_FAIL;
92 }
93#endif //ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)
94#endif //CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
95
96
97 return ESP_OK;
98}
99
100void xl9555_loop()
101{
102 if (!init_done) {
103 return;
104 }
105}
106
107#endif
@license MIT License
bool begin(SensorCommCustom::CustomCallback callback, uint8_t addr)
Initialize the sensor using custom callback interface.
Concrete driver for the XL9555 16‑bit I2C GPIO expander.
bool i2c_wr_function(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len, bool writeReg, bool isWrite)
@license MIT License