diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 52c06b7..904f7fe 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "i2c.c" "asr_pro.c" "ci_03t.c" "ui_input.c" "ambient_light.c" "temperature.c" "embedded_display.c" "mqtt.c" "main.c" "wifi.c" "light.c" "mqtt.c" +idf_component_register(SRCS "pca9555.c" "i2c.c" "asr_pro.c" "ci_03t.c" "ui_input.c" "ambient_light.c" "temperature.c" "embedded_display.c" "mqtt.c" "main.c" "wifi.c" "light.c" "mqtt.c" INCLUDE_DIRS ".") \ No newline at end of file diff --git a/main/ambient_light.c b/main/ambient_light.c index d358a1f..77e1915 100644 --- a/main/ambient_light.c +++ b/main/ambient_light.c @@ -6,15 +6,6 @@ #include "esp_log.h" #include "i2c.c" -#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/ -#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */ -#define ACK_VAL 0x0 /*!< I2C ack value */ -#define NACK_VAL 0x1 /*!< I2C nack value */ - -#define I2C_MASTER_NUM CONFIG_I2C_NUM - -#define APDS_9930_ADDRESS 0x39 - #define APDS_9930_CMD_REPEATED 0x80 // 命令重复地址 #define APDS_9930_CMD_AUTO_INCREMENT 0x90 // 命令自动递增地址 @@ -45,8 +36,6 @@ #define AMBIENT_LIGHT_TAG "ambient-light" -static int8_t is_apds_9930_online = 0; - esp_err_t apds_9930_write(uint8_t command, uint8_t data) { i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_master_start(cmd); @@ -193,10 +182,8 @@ void ambient_light_auto_fetch() { } void ambient_light_init() { - if (i2c_check_slave_exists(APDS_9930_ADDRESS)) { - is_apds_9930_online = 1; - } else { - is_apds_9930_online = 0; + if (is_apds_9930_online == 0) { + ESP_LOGI(AMBIENT_LIGHT_TAG, "AMG8833 is offline"); return; } ESP_LOGI(AMBIENT_LIGHT_TAG, "Initializing APDS-9930"); diff --git a/main/embedded_display.c b/main/embedded_display.c index 9a60bf8..0bc2ad7 100644 --- a/main/embedded_display.c +++ b/main/embedded_display.c @@ -13,15 +13,10 @@ #define X_WIDTH 128 #define Y_WIDTH 64 -#define I2C_ADDRESS 0x78 -#define I2C_MASTER_NUM CONFIG_I2C_NUM - -static uint8_t is_embedded_display_online = 0; - void i2cWriteByte(uint8_t reg, uint8_t data) { i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_master_start(cmd); - i2c_master_write_byte(cmd, I2C_ADDRESS | I2C_MASTER_WRITE, true); + i2c_master_write_byte(cmd, I2C_ADDRESS << 1 | I2C_MASTER_WRITE, true); i2c_master_write_byte(cmd, reg, true); i2c_master_write_byte(cmd, data, true); i2c_master_stop(cmd); @@ -78,10 +73,8 @@ void display_print8_str(uint8_t x, uint8_t y, char str[]) { } void init_display() { - if (i2c_check_slave_exists(I2C_ADDRESS >> 1)) { - is_embedded_display_online = 1; - } else { - is_embedded_display_online = 0; + if (is_embedded_display_online == 0) { + ESP_LOGE("display", "display is offline"); return; } diff --git a/main/i2c.c b/main/i2c.c index 32ed8ef..1d316d6 100644 --- a/main/i2c.c +++ b/main/i2c.c @@ -8,11 +8,27 @@ #define I2C_MASTER_SDA_IO CONFIG_I2C_SDA #define I2C_MASTER_SCL_IO CONFIG_I2C_SCL +#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/ +#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */ +#define ACK_VAL 0x0 /*!< I2C ack value */ +#define NACK_VAL 0x1 /*!< I2C nack value */ + #define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */ #define I2C_MASTER_NUM CONFIG_I2C_NUM +// 0 1 0 0 A2 A1 A0 +#define PCA9555_ADDRESS 0x20 +#define GX21M15_ADDRESS 0x48 +#define APDS_9930_ADDRESS 0x39 +#define I2C_ADDRESS 0x3c + static const char *I2C_TAG = "APP_I2C"; +static uint8_t is_temperature_online = 0; +static uint8_t is_pca9555_online = 0; +static uint8_t is_apds_9930_online = 0; +static uint8_t is_embedded_display_online = 0; + void init_i2c() { i2c_config_t conf = { .mode = I2C_MODE_MASTER, @@ -22,10 +38,10 @@ void init_i2c() { .scl_pullup_en = GPIO_PULLUP_ENABLE, .master.clk_speed = I2C_MASTER_FREQ_HZ, }; - i2c_param_config(I2C_MASTER_NUM, &conf); ESP_ERROR_CHECK(i2c_driver_install(I2C_MASTER_NUM, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0)); + i2c_param_config(I2C_MASTER_NUM, &conf); ESP_LOGI(I2C_TAG, "I2C initialized"); } @@ -34,16 +50,26 @@ uint8_t i2c_check_slave_exists(uint8_t address) { ESP_LOGI(I2C_TAG, "Checking if slave 0x%2x exists", address); i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_master_start(cmd); - i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, 1); + i2c_master_write_byte(cmd, (address << 1) | I2C_MASTER_WRITE, ACK_CHECK_EN); i2c_master_stop(cmd); - esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 100 / portTICK_RATE_MS); + esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 50 / portTICK_RATE_MS); i2c_cmd_link_delete(cmd); if (ret == ESP_OK) { ESP_LOGW(I2C_TAG, "Slave 0x%2x found", address); return 1; + } else if (ret == ESP_ERR_TIMEOUT) { + ESP_LOGW(I2C_TAG, "Slave 0x%2x TIMEOUT", address); + return 1; } else { ESP_LOGW(I2C_TAG, "Slave 0x%2x not found", address); return 0; } +} + +void i2c_check_slaves() { + is_temperature_online = i2c_check_slave_exists(GX21M15_ADDRESS); + is_pca9555_online = i2c_check_slave_exists(PCA9555_ADDRESS); + is_apds_9930_online = i2c_check_slave_exists(APDS_9930_ADDRESS); + is_embedded_display_online = i2c_check_slave_exists(I2C_ADDRESS); } \ No newline at end of file diff --git a/main/main.c b/main/main.c index 8eabe1b..3a06fb1 100644 --- a/main/main.c +++ b/main/main.c @@ -7,6 +7,7 @@ #include "i2c.c" #include "light.c" #include "mqtt.c" +#include "pca9555.c" #include "sdkconfig.h" #include "temperature.c" #include "ui_input.c" @@ -17,12 +18,14 @@ static const char *TAG = "DisplayAmbientLight"; void app_main(void) { light_init_strip(); init_i2c(); + i2c_check_slaves(); init_display(); display_print8_str(0, 0, "Ambient Light"); ci_03t_init(); ambient_light_init(); ambient_light_auto_fetch(); auto_fetch_temperature(); + pca9555_init(); ui_input_init(); xTaskCreate(mqtt_publish_ui_input, "mqtt_publish_ui_input", 2048, NULL, 10, NULL); diff --git a/main/pca9555.c b/main/pca9555.c new file mode 100644 index 0000000..d08a3c2 --- /dev/null +++ b/main/pca9555.c @@ -0,0 +1,32 @@ +#include +#include + +#include "driver/i2c.h" +#include "embedded_display.c" +#include "esp_log.h" +#include "i2c.c" + +#define PCA_9555_TAG "PCA9555" + + +// Register of command byte +#define PCA95555_CMD_INPUT_PORT_0 0x00 +#define PCA95555_CMD_INPUT_PORT_1 0x01 +#define PCA95555_CMD_OUTPUT_PORT_0 0x02 +#define PCA95555_CMD_OUTPUT_PORT_1 0x03 +#define PCA95555_CMD_POLARITY_INVERSION_PORT_0 0x04 +#define PCA95555_CMD_POLARITY_INVERSION_PORT_1 0x05 +#define PCA95555_CMD_CONFIGURATION_PORT_0 0x06 +#define PCA95555_CMD_CONFIGURATION_PORT_1 0x07 + + + + +void pca9555_init() { + if (is_pca9555_online == 0) { + ESP_LOGE(PCA_9555_TAG, "salve offline"); + return; + } + ESP_LOGI(PCA_9555_TAG, "Initializing PCA9555"); + // esp_log_level_set(PCA_9555_TAG, ESP_LOG_ERROR); +} diff --git a/main/temperature.c b/main/temperature.c index 3a3f486..aebb889 100644 --- a/main/temperature.c +++ b/main/temperature.c @@ -11,9 +11,6 @@ #define ACK_VAL 0x0 /*!< I2C ack value */ #define NACK_VAL 0x1 /*!< I2C nack value */ -#define I2C_MASTER_NUM CONFIG_I2C_NUM - -#define GX21M15_ADDRESS 0x90 #define GX21M15_TEMP_POINTER_VALUE 0b00 // 温度寄存器 #define GX21M15_CONF_POINTER_VALUE 0b01 // 配置寄存器 #define GX21M15_THYST_POINTER_VALUE 0b10 // 回滞寄存器 @@ -22,8 +19,6 @@ #define DEFAULT_TEMPERATURE -999 // 过温关断寄存器 #define TEMPERATURE_TAG "temperature" -static uint8_t is_temperature_online = 0; - void fetch_temperature(void* arg) { esp_err_t error; float temperature = DEFAULT_TEMPERATURE; @@ -33,11 +28,11 @@ void fetch_temperature(void* arg) { for (;;) { i2c_cmd_handle_t cmd = i2c_cmd_link_create(); i2c_master_start(cmd); - i2c_master_write_byte(cmd, GX21M15_ADDRESS | I2C_MASTER_WRITE, + i2c_master_write_byte(cmd, GX21M15_ADDRESS << 1 | I2C_MASTER_WRITE, ACK_CHECK_EN); i2c_master_write_byte(cmd, 0, ACK_CHECK_EN); i2c_master_start(cmd); - i2c_master_write_byte(cmd, GX21M15_ADDRESS | I2C_MASTER_READ, ACK_VAL); + i2c_master_write_byte(cmd, GX21M15_ADDRESS << 1 | I2C_MASTER_READ, ACK_VAL); i2c_master_read_byte(cmd, &(temperature_buffer[0]), ACK_VAL); i2c_master_read_byte(cmd, &(temperature_buffer[1]), NACK_VAL); i2c_master_stop(cmd); @@ -73,12 +68,10 @@ void fetch_temperature(void* arg) { } void auto_fetch_temperature() { - // if (i2c_check_slave_exists(GX21M15_ADDRESS >> 1)) { - // is_temperature_online = 1; - // } else { - // is_temperature_online = 0; - // return; - // } + if (is_temperature_online == 0) { + ESP_LOGW(TEMPERATURE_TAG, "temperature is offline"); + return; + } ESP_LOGI(TEMPERATURE_TAG, "auto_fetch_temperature"); xTaskCreate(fetch_temperature, "temperature", 2048, NULL, 10, NULL); }