feat: 启动时检测在线的 I2C 设备,避免不在线时崩溃。

This commit is contained in:
Ivan Li 2023-02-26 23:43:17 +08:00
parent ded155d9b5
commit a869036d34
9 changed files with 96 additions and 44 deletions

View File

@ -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 ".")

View File

@ -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

View File

@ -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,

View File

@ -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

49
main/i2c.c Normal file
View 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;
}
}

View File

@ -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;

View File

@ -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) {

View File

@ -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);
}

View File

@ -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;