From a869036d34963d6d0cdc6194adc9d3c874112949 Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sun, 26 Feb 2023 23:43:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=AF=E5=8A=A8=E6=97=B6=E6=A3=80?= =?UTF-8?q?=E6=B5=8B=E5=9C=A8=E7=BA=BF=E7=9A=84=20I2C=20=E8=AE=BE=E5=A4=87?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E4=B8=8D=E5=9C=A8=E7=BA=BF=E6=97=B6?= =?UTF-8?q?=E5=B4=A9=E6=BA=83=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/CMakeLists.txt | 2 +- main/Kconfig.projbuild | 4 ++-- main/ambient_light.c | 13 ++++++++++- main/embedded_display.c | 11 +++++++++ main/i2c.c | 49 +++++++++++++++++++++++++++++++++++++++++ main/light.c | 14 ++++++------ main/main.c | 35 +++-------------------------- main/temperature.c | 11 ++++++++- main/ui_input.c | 1 + 9 files changed, 96 insertions(+), 44 deletions(-) create mode 100644 main/i2c.c diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index a18d2df..52c06b7 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,2 @@ -idf_component_register(SRCS "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 "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/Kconfig.projbuild b/main/Kconfig.projbuild index ab9cd5f..4498b5e 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -66,7 +66,7 @@ endmenu menu "Encoder Configuration" config ENCODER_0_CLK_PIN - int "encoder 0 click GPIO" + int "encoder 0 clock GPIO" range 0 32 default 2 help @@ -85,7 +85,7 @@ menu "Encoder Configuration" Encoder 0 switch pin config ENCODER_1_CLK_PIN - int "encoder 1 click GPIO" + int "encoder 1 clock GPIO" range 0 32 default 5 help diff --git a/main/ambient_light.c b/main/ambient_light.c index 142b0e7..d358a1f 100644 --- a/main/ambient_light.c +++ b/main/ambient_light.c @@ -3,8 +3,8 @@ #include "driver/i2c.h" #include "embedded_display.c" - #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 */ @@ -45,6 +45,8 @@ #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); @@ -184,10 +186,19 @@ void ambient_light_fetch(void* arg) { } void ambient_light_auto_fetch() { + if (is_apds_9930_online == 0) { + return; + } xTaskCreate(ambient_light_fetch, "ambient-light", 2048, NULL, 10, NULL); } void ambient_light_init() { + if (i2c_check_slave_exists(APDS_9930_ADDRESS)) { + is_apds_9930_online = 1; + } else { + is_apds_9930_online = 0; + return; + } ESP_LOGI(AMBIENT_LIGHT_TAG, "Initializing APDS-9930"); // esp_log_level_set(AMBIENT_LIGHT_TAG, ESP_LOG_ERROR); ESP_ERROR_CHECK(apds_9930_write(APDS_9930_CMD_REPEATED | APDS_9930_REG_ATIME, diff --git a/main/embedded_display.c b/main/embedded_display.c index 217fa7b..9a60bf8 100644 --- a/main/embedded_display.c +++ b/main/embedded_display.c @@ -7,6 +7,7 @@ #include "config_key.h" #include "driver/i2c.h" #include "esp_log.h" +#include "i2c.c" #define Brightness 0xCF #define X_WIDTH 128 @@ -15,6 +16,8 @@ #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); @@ -59,6 +62,7 @@ void display_set_pos(uint8_t x, uint8_t y) { } void display_print8_str(uint8_t x, uint8_t y, char str[]) { + if (!is_embedded_display_online) return; uint8_t c; for (uint8_t ch, ci = 0; ch = str[ci], ch != '\0'; ci++, x += 8) { c = ch - 0x20; @@ -74,6 +78,13 @@ 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; + return; + } + i2cWriteCommand(0xAE); // display off i2cWriteCommand(0x00); // set lower column address i2cWriteCommand(0x10); // set higher column address diff --git a/main/i2c.c b/main/i2c.c new file mode 100644 index 0000000..32ed8ef --- /dev/null +++ b/main/i2c.c @@ -0,0 +1,49 @@ +#pragma once +#include "driver/i2c.h" + +#include "esp_log.h" + +#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ +#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ +#define I2C_MASTER_SDA_IO CONFIG_I2C_SDA +#define I2C_MASTER_SCL_IO CONFIG_I2C_SCL + +#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */ +#define I2C_MASTER_NUM CONFIG_I2C_NUM + +static const char *I2C_TAG = "APP_I2C"; + +void init_i2c() { + i2c_config_t conf = { + .mode = I2C_MODE_MASTER, + .sda_io_num = I2C_MASTER_SDA_IO, + .scl_io_num = I2C_MASTER_SCL_IO, + .sda_pullup_en = GPIO_PULLUP_ENABLE, + .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)); + + ESP_LOGI(I2C_TAG, "I2C initialized"); +} + +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_stop(cmd); + esp_err_t ret = i2c_master_cmd_begin(I2C_NUM_0, cmd, 100 / portTICK_RATE_MS); + i2c_cmd_link_delete(cmd); + + if (ret == ESP_OK) { + ESP_LOGW(I2C_TAG, "Slave 0x%2x found", address); + return 1; + } else { + ESP_LOGW(I2C_TAG, "Slave 0x%2x not found", address); + return 0; + } +} \ No newline at end of file diff --git a/main/light.c b/main/light.c index 703b625..4613ac8 100644 --- a/main/light.c +++ b/main/light.c @@ -119,22 +119,22 @@ void update_desktop_connection_state() { case light_mode_desktop_online: if (beat) { ESP_ERROR_CHECK( - light_led_strip->set_pixel(light_led_strip, 0, 77, 77, 77)); + light_led_strip->set_pixel(light_led_strip, 0, 10, 10, 10)); } ESP_ERROR_CHECK( - light_led_strip->set_pixel(light_led_strip, 1, 77, 77, 77)); + light_led_strip->set_pixel(light_led_strip, 1, 10, 10, 10)); ESP_ERROR_CHECK( - light_led_strip->set_pixel(light_led_strip, 2, 77, 77, 77)); + light_led_strip->set_pixel(light_led_strip, 2, 10, 10, 10)); break; case light_mode_mqtt_connected: if (beat) { ESP_ERROR_CHECK( - light_led_strip->set_pixel(light_led_strip, 0, 77, 77, 77)); + light_led_strip->set_pixel(light_led_strip, 0, 10, 10, 10)); ESP_ERROR_CHECK( - light_led_strip->set_pixel(light_led_strip, 1, 77, 77, 77)); + light_led_strip->set_pixel(light_led_strip, 1, 10, 10, 10)); } ESP_ERROR_CHECK( - light_led_strip->set_pixel(light_led_strip, 2, 77, 77, 77)); + light_led_strip->set_pixel(light_led_strip, 2, 22, 22, 22)); break; case light_mode_idle: if (beat) { @@ -204,7 +204,7 @@ void light_for_idle() { for (uint16_t j = 0, hue = offset; j < STRIP_LED_NUMBER; j++, hue += step_length) { // Build RGB values - led_strip_hsv2rgb(hue, 50, 100, &red, &green, &blue); + led_strip_hsv2rgb(hue, 50, 30, &red, &green, &blue); red = red * display_ambient_light_brightness; green = green * display_ambient_light_brightness; diff --git a/main/main.c b/main/main.c index a149d1c..8eabe1b 100644 --- a/main/main.c +++ b/main/main.c @@ -1,46 +1,17 @@ #include "ambient_light.c" #include "ci_03t.c" -#include "driver/i2c.h" +#include "embedded_display.c" #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" +#include "i2c.c" #include "light.c" #include "mqtt.c" #include "sdkconfig.h" +#include "temperature.c" #include "ui_input.c" #include "wifi.c" -#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ -#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */ -#define I2C_MASTER_SDA_IO CONFIG_I2C_SDA -#define I2C_MASTER_SCL_IO CONFIG_I2C_SCL - -#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */ -#define I2C_MASTER_NUM CONFIG_I2C_NUM - -#include "embedded_display.c" -#include "temperature.c" - -void init_i2c() { - int i2c_master_port = I2C_MASTER_NUM; - - i2c_config_t conf = { - .mode = I2C_MODE_MASTER, - .sda_io_num = I2C_MASTER_SDA_IO, - .scl_io_num = I2C_MASTER_SCL_IO, - .sda_pullup_en = GPIO_PULLUP_ENABLE, - .scl_pullup_en = GPIO_PULLUP_ENABLE, - .master.clk_speed = I2C_MASTER_FREQ_HZ, - }; - i2c_param_config(i2c_master_port, &conf); - ESP_LOGI("I2C", "Enabling I2C"); - ESP_ERROR_CHECK(i2c_driver_install(i2c_master_port, conf.mode, - I2C_MASTER_RX_BUF_DISABLE, - I2C_MASTER_TX_BUF_DISABLE, 0)); - - ESP_LOGI("SCR", "I2C initialized successfully"); -} - static const char *TAG = "DisplayAmbientLight"; void app_main(void) { diff --git a/main/temperature.c b/main/temperature.c index 517d6a0..58301c1 100644 --- a/main/temperature.c +++ b/main/temperature.c @@ -4,6 +4,7 @@ #include "driver/i2c.h" #include "embedded_display.c" #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 */ @@ -19,8 +20,10 @@ #define GX21M15_TOS_POINTER_VALUE 0b11 // 过温关断寄存器 #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; @@ -70,6 +73,12 @@ void fetch_temperature(void* arg) { } void auto_fetch_temperature() { + if (i2c_check_slave_exists(GX21M15_ADDRESS)) { + is_temperature_online = 1; + } else { + is_temperature_online = 0; + return; + } ESP_LOGI(TEMPERATURE_TAG, "auto_fetch_temperature"); xTaskCreate(fetch_temperature, "temperature", 2048, NULL, 10, NULL); } diff --git a/main/ui_input.c b/main/ui_input.c index 01b00e4..06bb0c8 100644 --- a/main/ui_input.c +++ b/main/ui_input.c @@ -206,6 +206,7 @@ void ui_input_init(void) { io_conf.mode = GPIO_MODE_INPUT; io_conf.pull_up_en = 1; + io_conf.pull_down_en = 0; // interrupt of rising edge io_conf.intr_type = GPIO_INTR_ANYEDGE; io_conf.pin_bit_mask = ENCODER_0_CLK_PIN_MASK | ENCODER_1_CLK_PIN_MASK;