feat: 测试中断功能(接近)

This commit is contained in:
Ivan Li 2023-03-08 14:19:35 +08:00
parent f6b7a398cd
commit e426829aa6
5 changed files with 327 additions and 88 deletions

View File

@ -146,3 +146,12 @@ menu "UART Configuration"
help
UART NUM
endmenu
menu "ADPS 9960"
config APDS_9960_INT_GPIO
int "APDS 996O INT GPIO"
range 0 32
default 2
help
APDS 996O INT GPIO
endmenu

View File

@ -1,14 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
#include "common.h"
#include "driver/i2c.h"
#include "embedded_display.c"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "i2c.c"
// #define APDS_9930_CMD_REPEATED 0x80 // 命令重复地址
// #define APDS_9930_CMD_AUTO_INCREMENT 0x90 // 命令自动递增地址
#define APDS_9960_INT_GPIO CONFIG_APDS_9960_INT_GPIO
// ===============================================
// Register addresses
// ===============================================
// 0x80 ENABLE R/W Enable states and interrupts 0x00
// 0x81 ATIME R/W ADC integration time 0xFF
// 0x83 WTIME R/W Wait time (non-gesture) 0xFF
@ -109,13 +113,37 @@
#define APDS_9960_REG_GFIFO_L 0xFE // 手势 FIFO 数据左 0x00
#define APDS_9960_REG_GFIFO_R 0xFF // 手势 FIFO 数据右 0x00
#define APDS_9960_CONTROL_VALUE \
0b00001010 // 50 mA LED, Reserved, 2x PGAIN, 4x ALS/Cain GAIN
#define APDS_9960_ENABLE_VALUE 0b00000111
#define APDS_9930_OFFSET_VALUE 0x8f
#define APDS_9960_GVALID 0b00000001 // GVALID
#define APDS_9960_GFOV 0b00000010 // GFOV
#define APDS_9960_PINT 0x20 // PINT
#define APDS_9960_AINT 0x10 // AINT
#define APDS_9960_GINT 0x04 // GINT
// 50 mA LED, Reserved, 2x PGAIN, 4x ALS/Cain GAIN
#define APDS_9960_CONTROL_VALUE 0b00001010
// Enable Gesture, Proximity, ALS, Power
#define APDS_9960_ENABLE_VALUE 0b01000111
#define APDS_9960_TAG "APDS-9960"
static xQueueHandle apds_9960_int_evt_queue = NULL;
esp_err_t apds_9960_write_empty(uint8_t command) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, APDS_9960_ADDRESS << 1 | I2C_MASTER_WRITE,
ACK_CHECK_EN);
i2c_master_write_byte(cmd, command, ACK_CHECK_DIS);
i2c_master_stop(cmd);
esp_err_t error =
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 10 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "write failed. %d", error);
}
return error;
}
esp_err_t apds_9960_write(uint8_t command, uint8_t data) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
@ -125,7 +153,7 @@ esp_err_t apds_9960_write(uint8_t command, uint8_t data) {
i2c_master_write_byte(cmd, data, ACK_CHECK_EN);
i2c_master_stop(cmd);
esp_err_t error =
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_PERIOD_MS);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 10 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "write failed. %d", error);
@ -145,10 +173,10 @@ esp_err_t apds_9960_read_byte(uint8_t command, uint8_t* data) {
i2c_master_read_byte(cmd, data, NACK_VAL);
i2c_master_stop(cmd);
esp_err_t error =
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_PERIOD_MS);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 10 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "write failed. %d", error);
ESP_LOGW(APDS_9960_TAG, "read failed. %d", error);
}
return error;
}
@ -167,29 +195,182 @@ esp_err_t apds_9960_read_word(uint8_t command, uint16_t* data) {
i2c_master_read_byte(cmd, (uint8_t*)data + 1, NACK_VAL);
i2c_master_stop(cmd);
esp_err_t error =
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_PERIOD_MS);
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 10 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "write failed. %d", error);
ESP_LOGW(APDS_9960_TAG, "read failed. %d", error);
}
return error;
}
void apes_9960_fetch(void* arg) {
ESP_LOGI(APDS_9960_TAG, "apes_9960_fetch");
// read bytes by length
esp_err_t apds_9960_read_bytes_len(uint8_t command, uint8_t* data,
uint8_t len) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, APDS_9960_ADDRESS << 1 | I2C_MASTER_WRITE,
ACK_CHECK_EN);
i2c_master_write_byte(cmd, command, ACK_CHECK_EN);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, APDS_9960_ADDRESS << 1 | I2C_MASTER_READ,
ACK_CHECK_EN);
for (int i = 0; i < len; i++) {
if (i == len - 1) {
i2c_master_read_byte(cmd, (uint8_t*)data + i, NACK_VAL);
} else {
i2c_master_read_byte(cmd, (uint8_t*)data + i, ACK_VAL);
}
}
i2c_master_stop(cmd);
esp_err_t error =
i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 100 / portTICK_PERIOD_MS);
i2c_cmd_link_delete(cmd);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read failed. %d", error);
}
return error;
}
void apds_9960_fetch(void* arg) {
ESP_LOGI(APDS_9960_TAG, "apds_9960_fetch");
esp_err_t error;
uint16_t red_raw;
uint16_t green_raw;
uint16_t blue_raw;
uint16_t clear_raw;
uint8_t proximity_raw;
uint8_t gesture_fifo_level_raw;
uint8_t gesture_status_raw;
uint8_t status_raw;
char red_str[10];
char green_str[10];
char blue_str[10];
char clear_str[10];
char proximity_str[10];
char status_str[10];
uint8_t interrupt = 0;
display_fill_rect(0, 2, 128, 8, 0x00);
for (;;) {
// display_fill_rect(0, 0, 128, 8, 0x00);
interrupt = gpio_get_level(APDS_9960_INT_GPIO);
// Status
error = apds_9960_read_byte(APDS_9960_REG_STATUS, &status_raw);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read status failed. %x", error);
} else {
sprintf(status_str, "%d :%d%d%d%d %d%d%d%d", interrupt,
(status_raw >> 7) & 1, (status_raw >> 6) & 1,
(status_raw >> 5) & 1, (status_raw >> 4) & 1,
(status_raw >> 3) & 1, (status_raw >> 2) & 1,
(status_raw >> 1) & 1, status_raw & 1);
display_print8_str(0, 0, status_str);
ESP_LOGD(APDS_9960_TAG, "Status: %2x", status_raw);
}
// // clear
// error = apds_9960_read_word(APDS_9960_REG_CDATAL, &clear_raw);
// if (error != ESP_OK) {
// ESP_LOGW(APDS_9960_TAG, "read failed. %d", error);
// } else {
// sprintf(clear_str, "C:% 5d", clear_raw);
// display_print8_str(64, 6, clear_str);
// ESP_LOGD(APDS_9960_TAG, "Clear: %d", clear_raw);
// }
// // red
// error = apds_9960_read_word(APDS_9960_REG_RDATAL, &red_raw);
// if (error != ESP_OK) {
// ESP_LOGW(APDS_9960_TAG, "read failed. %d", error);
// } else {
// sprintf(red_str, "R:% 5d", red_raw);
// display_print8_str(0, 4, red_str);
// ESP_LOGD(APDS_9960_TAG, "Red: %d", red_raw);
// }
// // green
// error = apds_9960_read_word(APDS_9960_REG_GDATAL, &green_raw);
// if (error != ESP_OK) {
// ESP_LOGW(APDS_9960_TAG, "read failed. %d", error);
// } else {
// sprintf(green_str, "G:% 5d", green_raw);
// display_print8_str(64, 4, green_str);
// ESP_LOGD(APDS_9960_TAG, "Green: %d", green_raw);
// }
// // blue
// error = apds_9960_read_word(APDS_9960_REG_BDATAL, &blue_raw);
// if (error != ESP_OK) {
// ESP_LOGW(APDS_9960_TAG, "read failed. %d", error);
// } else {
// sprintf(blue_str, "B:% 5d", blue_raw);
// display_print8_str(0, 6, blue_str);
// ESP_LOGD(APDS_9960_TAG, "Blue: %d", blue_raw);
// }
vTaskDelay(pdMS_TO_TICKS(10));
}
display_fill_rect(0, 2, 128, 8, 0x00);
}
void apds_9960_auto_fetch() {
if (is_apds_9960_online == 0) {
return;
}
xTaskCreate(apds_9960_fetch, "APDS-9960-fetch", 2048, NULL, 10, NULL);
}
void apds_9960_read_gesture() {
ESP_LOGI(APDS_9960_TAG, "apes_9960_gesture_fetch");
if (is_apds_9960_online == 0) {
return;
}
uint8_t byte_buffer;
esp_err_t error;
uint32_t gesture_values_raw_arr[32];
error = apds_9960_read_byte(APDS_9960_REG_GSTATUS, &byte_buffer);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GSTATUS failed. %d", error);
return;
}
ESP_LOGI(APDS_9960_TAG, "APDS-9960 interrupt. status: %x", byte_buffer);
if (!(byte_buffer & APDS_9960_GVALID)) {
ESP_LOGI(APDS_9960_TAG, "Gesture no valid");
return;
}
ESP_LOGI(APDS_9960_TAG, "Gesture interrupt");
error = apds_9960_read_byte(APDS_9960_REG_GFLVL, &byte_buffer);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GFLVL failed. %d", error);
return;
}
if (byte_buffer == 0) {
ESP_LOGI(APDS_9960_TAG, "Gesture FIFO empty");
return;
}
ESP_LOGI(APDS_9960_TAG, "Gesture FIFO Level: %d", byte_buffer);
error = apds_9960_read_bytes_len(
APDS_9960_REG_GFIFO_U, (uint8_t*)gesture_values_raw_arr, byte_buffer * 4);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GFIFO(len: %d) failed. % d",
byte_buffer * 4, error);
} else {
for (int i = 0; i < byte_buffer; i++) {
printf("Gesture %2d: %02x %02x %02x %02x\n", i,
gesture_values_raw_arr[i] & 0xff,
gesture_values_raw_arr[i] >> 8 & 0xff,
gesture_values_raw_arr[i] >> 16 & 0xff,
gesture_values_raw_arr[i] >> 24 & 0xff);
}
// display_print8_str(0, 0, gesture_str);
}
}
void apds_9960_read_proximity() {
uint8_t proximity_raw;
char proximity_str[10];
esp_err_t error;
// Proximity
error = apds_9960_read_byte(APDS_9960_REG_PDATA, &proximity_raw);
if (error != ESP_OK) {
@ -197,66 +378,94 @@ void apes_9960_fetch(void* arg) {
} else {
sprintf(proximity_str, "Prox: % 5d ", proximity_raw);
display_print8_str(8, 2, proximity_str);
ESP_LOGD(APDS_9960_TAG, "Prox: %d %x", proximity_raw);
}
// red
error = apds_9960_read_word(APDS_9960_REG_RDATAL, &red_raw);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read failed. %x", error);
} else {
sprintf(red_str, "R:% 5d", red_raw);
display_print8_str(0, 4, red_str);
ESP_LOGD(APDS_9960_TAG, "Red: %d", red_raw);
}
// green
error = apds_9960_read_word(APDS_9960_REG_GDATAL, &green_raw);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read failed. %x", error);
} else {
sprintf(green_str, "G:% 5d", green_raw);
display_print8_str(64, 4, green_str);
ESP_LOGD(APDS_9960_TAG, "Green: %d", green_raw);
}
// blue
error = apds_9960_read_word(APDS_9960_REG_BDATAL, &blue_raw);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read failed. %x", error);
} else {
sprintf(blue_str, "B:% 5d", blue_raw);
display_print8_str(0, 6, blue_str);
ESP_LOGD(APDS_9960_TAG, "Blue: %d", blue_raw);
}
// clear
error = apds_9960_read_word(APDS_9960_REG_CDATAL, &clear_raw);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read failed. %x", error);
} else {
sprintf(clear_str, "C:% 4d", clear_raw);
display_print8_str(64, 6, clear_str);
ESP_LOGD(APDS_9960_TAG, "Clear: %d", clear_raw);
ESP_LOGI(APDS_9960_TAG, "Prox: %d", proximity_raw);
}
vTaskDelay(pdMS_TO_TICKS(1000));
// Clear Proximity Interrupt
error = apds_9960_write_empty(APDS_9960_REG_PICLEAR);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "write clear failed. %x", error);
}
display_fill_rect(0, 2, 128, 8, 0x00);
}
void apes_9960_auto_fetch() {
if (is_apds_9960_online == 0) {
return;
}
xTaskCreate(apes_9960_fetch, "APDS-9960-fetch", 2048, NULL, 10, NULL);
void apds_9960_int_handler(void* arg) {
xQueueSendFromISR(apds_9960_int_evt_queue, &arg, NULL);
}
void apes_9960_init() {
void apds_9960_int_evt_handler() {
ESP_ERROR_CHECK(apds_9960_write_empty(APDS_9960_REG_AICLEAR));
esp_err_t error;
uint8_t status_raw;
while (xQueueReceive(apds_9960_int_evt_queue, NULL, portMAX_DELAY)) {
error = apds_9960_read_byte(APDS_9960_REG_STATUS, &status_raw);
ESP_ERROR_RETRY(apds_9960_write_empty(APDS_9960_REG_AICLEAR), 10);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG,
"[apds_9960_int_evt_handler] read status failed. %d", error);
continue;
}
ESP_LOGW(
APDS_9960_TAG, "[apds_9960_int_evt_handler] status %d%d%d%d %d%d%d%d",
(status_raw >> 7) & 1, (status_raw >> 6) & 1, (status_raw >> 5) & 1,
(status_raw >> 4) & 1, (status_raw >> 3) & 1, (status_raw >> 2) & 1,
(status_raw >> 1) & 1, status_raw & 1);
if (status_raw & APDS_9960_PINT) {
apds_9960_read_proximity();
} else {
ESP_LOGW(APDS_9960_TAG, "Proximity interrupt not set. %x",
status_raw & APDS_9960_PINT);
}
if (status_raw & APDS_9960_GINT) {
apds_9960_read_gesture();
}
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
void apds_9960_init() {
if (is_apds_9960_online == 0) {
ESP_LOGI(APDS_9960_TAG, "APDS-9960 is offline");
return;
}
ESP_LOGI(APDS_9960_TAG, "Initializing APDS-9960");
// esp_log_level_set(APDS_9960_TAG, ESP_LOG_DEBUG);
gpio_config_t io_conf = {};
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = 0;
io_conf.pull_down_en = 0;
io_conf.intr_type = GPIO_INTR_NEGEDGE;
io_conf.pin_bit_mask = 1ULL << APDS_9960_INT_GPIO;
gpio_config(&io_conf);
gpio_isr_handler_add(APDS_9960_INT_GPIO, apds_9960_int_handler, NULL);
// 环境光 ADC 积分时间
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_ATIME, 246)); // 27.8ms
// 增益
ESP_ERROR_CHECK(
apds_9960_write(APDS_9960_REG_CONTROL, APDS_9960_CONTROL_VALUE));
ESP_ERROR_CHECK(
apds_9960_write(APDS_9960_REG_ENABLE, APDS_9960_ENABLE_VALUE));
// enable gesture interrupt
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GCONF4, 0x03));
// set wait time
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_WTIME, 171));
// set interrupt persistence
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_PERS, 0x44));
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_PILT, 40));
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_PIHT, 200));
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_CONFIG2, 0x80));
// // enable sleep after interrupt
// ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_CONFIG3, 0x10));
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_ENABLE, 0b00101101));
apds_9960_int_evt_queue = xQueueCreate(10, NULL);
xTaskCreate(apds_9960_int_evt_handler, "apds_9960_gesture_fetch", 2048, NULL,
10, NULL);
}

20
main/common.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include "esp_log.h"
#define ESP_ERROR_RETRY(action, max_retries) \
{ \
uint8_t retry_count = 0; \
esp_err_t error; \
while (retry_count < max_retries) { \
error = action; \
if (error == ESP_OK) break; \
retry_count++; \
ESP_LOGI("RETRY", "Retrying... (%d/%d)\n", retry_count + 1, \
max_retries); \
} \
if (error != ESP_OK) { \
ESP_LOGE("RETRY", "retry failed. %d", error); \
ESP_ERROR_CHECK(error); \
} \
}

View File

@ -17,32 +17,35 @@ static const char *TAG = "DisplayAmbientLight";
void app_main(void) {
light_init_strip();
gpio_install_isr_service(0);
init_i2c();
i2c_check_slaves();
init_display();
display_print8_str(0, 0, "Ambient Light");
ci_03t_init();
apes_9960_init();
apes_9960_auto_fetch();
auto_fetch_temperature();
pca9555_init();
ui_input_init();
xTaskCreate(mqtt_publish_ui_input, "mqtt_publish_ui_input", 2048, NULL, 10,
NULL);
vTaskDelay(pdMS_TO_TICKS(10));
// ci_03t_init();
apds_9960_init();
apds_9960_auto_fetch();
// auto_fetch_temperature();
// pca9555_init();
// ui_input_init();
// xTaskCreate(mqtt_publish_ui_input, "mqtt_publish_ui_input", 2048, NULL, 10,
// NULL);
// vTaskDelay(pdMS_TO_TICKS(10));
light_play(light_mode_connection_wifi);
if (connect_wifi()) {
light_play(light_mode_idle);
}
vTaskDelay(pdMS_TO_TICKS(1000));
mqtt_app_start();
if (waiting_for_mqtt_connected()) {
light_play(light_mode_mqtt_connected);
}
if (waiting_for_desktop_online()) {
light_play(light_mode_desktop_online);
}
while (waiting_and_get_colors()) {
light_play_colors(NUMBER_OF_LEDS * 3, mqtt_colors_buffer);
}
// if (connect_wifi()) {
// light_play(light_mode_idle);
// }
// vTaskDelay(pdMS_TO_TICKS(1000));
// mqtt_app_start();
// if (waiting_for_mqtt_connected()) {
// light_play(light_mode_mqtt_connected);
// }
// if (waiting_for_desktop_online()) {
// light_play(light_mode_desktop_online);
// }
// while (waiting_and_get_colors()) {
// light_play_colors(NUMBER_OF_LEDS * 3, mqtt_colors_buffer);
// }
}

View File

@ -196,8 +196,6 @@ void ui_input_init(void) {
ui_input_event = xQueueCreate(10, sizeof(s_ui_input_t));
ui_input_raw_event = xQueueCreate(10, 0);
// install gpio isr service
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT);
// hook isr handler for specific gpio pin
gpio_isr_handler_add(ENCODER_INT_GPIO, gpio_isr_handler, NULL);