feat: 启动时检测在线的 I2C 设备,避免不在线时崩溃。
This commit is contained in:
parent
ded155d9b5
commit
a869036d34
@ -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 ".")
|
INCLUDE_DIRS ".")
|
@ -66,7 +66,7 @@ endmenu
|
|||||||
|
|
||||||
menu "Encoder Configuration"
|
menu "Encoder Configuration"
|
||||||
config ENCODER_0_CLK_PIN
|
config ENCODER_0_CLK_PIN
|
||||||
int "encoder 0 click GPIO"
|
int "encoder 0 clock GPIO"
|
||||||
range 0 32
|
range 0 32
|
||||||
default 2
|
default 2
|
||||||
help
|
help
|
||||||
@ -85,7 +85,7 @@ menu "Encoder Configuration"
|
|||||||
Encoder 0 switch pin
|
Encoder 0 switch pin
|
||||||
|
|
||||||
config ENCODER_1_CLK_PIN
|
config ENCODER_1_CLK_PIN
|
||||||
int "encoder 1 click GPIO"
|
int "encoder 1 clock GPIO"
|
||||||
range 0 32
|
range 0 32
|
||||||
default 5
|
default 5
|
||||||
help
|
help
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
#include "driver/i2c.h"
|
#include "driver/i2c.h"
|
||||||
#include "embedded_display.c"
|
#include "embedded_display.c"
|
||||||
|
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "i2c.c"
|
||||||
|
|
||||||
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
|
#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_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
|
||||||
@ -45,6 +45,8 @@
|
|||||||
|
|
||||||
#define AMBIENT_LIGHT_TAG "ambient-light"
|
#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) {
|
esp_err_t apds_9930_write(uint8_t command, uint8_t data) {
|
||||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||||
i2c_master_start(cmd);
|
i2c_master_start(cmd);
|
||||||
@ -184,10 +186,19 @@ void ambient_light_fetch(void* arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ambient_light_auto_fetch() {
|
void ambient_light_auto_fetch() {
|
||||||
|
if (is_apds_9930_online == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
xTaskCreate(ambient_light_fetch, "ambient-light", 2048, NULL, 10, NULL);
|
xTaskCreate(ambient_light_fetch, "ambient-light", 2048, NULL, 10, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ambient_light_init() {
|
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_LOGI(AMBIENT_LIGHT_TAG, "Initializing APDS-9930");
|
||||||
// esp_log_level_set(AMBIENT_LIGHT_TAG, ESP_LOG_ERROR);
|
// esp_log_level_set(AMBIENT_LIGHT_TAG, ESP_LOG_ERROR);
|
||||||
ESP_ERROR_CHECK(apds_9930_write(APDS_9930_CMD_REPEATED | APDS_9930_REG_ATIME,
|
ESP_ERROR_CHECK(apds_9930_write(APDS_9930_CMD_REPEATED | APDS_9930_REG_ATIME,
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#include "config_key.h"
|
#include "config_key.h"
|
||||||
#include "driver/i2c.h"
|
#include "driver/i2c.h"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "i2c.c"
|
||||||
|
|
||||||
#define Brightness 0xCF
|
#define Brightness 0xCF
|
||||||
#define X_WIDTH 128
|
#define X_WIDTH 128
|
||||||
@ -15,6 +16,8 @@
|
|||||||
#define I2C_ADDRESS 0x78
|
#define I2C_ADDRESS 0x78
|
||||||
#define I2C_MASTER_NUM CONFIG_I2C_NUM
|
#define I2C_MASTER_NUM CONFIG_I2C_NUM
|
||||||
|
|
||||||
|
static uint8_t is_embedded_display_online = 0;
|
||||||
|
|
||||||
void i2cWriteByte(uint8_t reg, uint8_t data) {
|
void i2cWriteByte(uint8_t reg, uint8_t data) {
|
||||||
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
||||||
i2c_master_start(cmd);
|
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[]) {
|
void display_print8_str(uint8_t x, uint8_t y, char str[]) {
|
||||||
|
if (!is_embedded_display_online) return;
|
||||||
uint8_t c;
|
uint8_t c;
|
||||||
for (uint8_t ch, ci = 0; ch = str[ci], ch != '\0'; ci++, x += 8) {
|
for (uint8_t ch, ci = 0; ch = str[ci], ch != '\0'; ci++, x += 8) {
|
||||||
c = ch - 0x20;
|
c = ch - 0x20;
|
||||||
@ -74,6 +78,13 @@ void display_print8_str(uint8_t x, uint8_t y, char str[]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void init_display() {
|
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(0xAE); // display off
|
||||||
i2cWriteCommand(0x00); // set lower column address
|
i2cWriteCommand(0x00); // set lower column address
|
||||||
i2cWriteCommand(0x10); // set higher column address
|
i2cWriteCommand(0x10); // set higher column address
|
||||||
|
49
main/i2c.c
Normal file
49
main/i2c.c
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
14
main/light.c
14
main/light.c
@ -119,22 +119,22 @@ void update_desktop_connection_state() {
|
|||||||
case light_mode_desktop_online:
|
case light_mode_desktop_online:
|
||||||
if (beat) {
|
if (beat) {
|
||||||
ESP_ERROR_CHECK(
|
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(
|
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(
|
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;
|
break;
|
||||||
case light_mode_mqtt_connected:
|
case light_mode_mqtt_connected:
|
||||||
if (beat) {
|
if (beat) {
|
||||||
ESP_ERROR_CHECK(
|
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(
|
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(
|
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;
|
break;
|
||||||
case light_mode_idle:
|
case light_mode_idle:
|
||||||
if (beat) {
|
if (beat) {
|
||||||
@ -204,7 +204,7 @@ void light_for_idle() {
|
|||||||
for (uint16_t j = 0, hue = offset; j < STRIP_LED_NUMBER;
|
for (uint16_t j = 0, hue = offset; j < STRIP_LED_NUMBER;
|
||||||
j++, hue += step_length) {
|
j++, hue += step_length) {
|
||||||
// Build RGB values
|
// 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;
|
red = red * display_ambient_light_brightness;
|
||||||
green = green * display_ambient_light_brightness;
|
green = green * display_ambient_light_brightness;
|
||||||
|
35
main/main.c
35
main/main.c
@ -1,46 +1,17 @@
|
|||||||
#include "ambient_light.c"
|
#include "ambient_light.c"
|
||||||
#include "ci_03t.c"
|
#include "ci_03t.c"
|
||||||
#include "driver/i2c.h"
|
#include "embedded_display.c"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
|
#include "i2c.c"
|
||||||
#include "light.c"
|
#include "light.c"
|
||||||
#include "mqtt.c"
|
#include "mqtt.c"
|
||||||
#include "sdkconfig.h"
|
#include "sdkconfig.h"
|
||||||
|
#include "temperature.c"
|
||||||
#include "ui_input.c"
|
#include "ui_input.c"
|
||||||
#include "wifi.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";
|
static const char *TAG = "DisplayAmbientLight";
|
||||||
|
|
||||||
void app_main(void) {
|
void app_main(void) {
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include "driver/i2c.h"
|
#include "driver/i2c.h"
|
||||||
#include "embedded_display.c"
|
#include "embedded_display.c"
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "i2c.c"
|
||||||
|
|
||||||
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
|
#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_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
|
||||||
@ -19,8 +20,10 @@
|
|||||||
#define GX21M15_TOS_POINTER_VALUE 0b11 // 过温关断寄存器
|
#define GX21M15_TOS_POINTER_VALUE 0b11 // 过温关断寄存器
|
||||||
|
|
||||||
#define DEFAULT_TEMPERATURE -999 // 过温关断寄存器
|
#define DEFAULT_TEMPERATURE -999 // 过温关断寄存器
|
||||||
|
|
||||||
#define TEMPERATURE_TAG "temperature"
|
#define TEMPERATURE_TAG "temperature"
|
||||||
|
|
||||||
|
static uint8_t is_temperature_online = 0;
|
||||||
|
|
||||||
void fetch_temperature(void* arg) {
|
void fetch_temperature(void* arg) {
|
||||||
esp_err_t error;
|
esp_err_t error;
|
||||||
float temperature = DEFAULT_TEMPERATURE;
|
float temperature = DEFAULT_TEMPERATURE;
|
||||||
@ -70,6 +73,12 @@ void fetch_temperature(void* arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void auto_fetch_temperature() {
|
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");
|
ESP_LOGI(TEMPERATURE_TAG, "auto_fetch_temperature");
|
||||||
xTaskCreate(fetch_temperature, "temperature", 2048, NULL, 10, NULL);
|
xTaskCreate(fetch_temperature, "temperature", 2048, NULL, 10, NULL);
|
||||||
}
|
}
|
||||||
|
@ -206,6 +206,7 @@ void ui_input_init(void) {
|
|||||||
|
|
||||||
io_conf.mode = GPIO_MODE_INPUT;
|
io_conf.mode = GPIO_MODE_INPUT;
|
||||||
io_conf.pull_up_en = 1;
|
io_conf.pull_up_en = 1;
|
||||||
|
io_conf.pull_down_en = 0;
|
||||||
// interrupt of rising edge
|
// interrupt of rising edge
|
||||||
io_conf.intr_type = GPIO_INTR_ANYEDGE;
|
io_conf.intr_type = GPIO_INTR_ANYEDGE;
|
||||||
io_conf.pin_bit_mask = ENCODER_0_CLK_PIN_MASK | ENCODER_1_CLK_PIN_MASK;
|
io_conf.pin_bit_mask = ENCODER_0_CLK_PIN_MASK | ENCODER_1_CLK_PIN_MASK;
|
||||||
|
Loading…
Reference in New Issue
Block a user