3 Commits

Author SHA1 Message Date
a54c554342 feat(apds9960): 功能恢复。 2023-04-02 19:29:12 +08:00
eaedd765ed feat(apds9960): 防中断导致假死。 2023-03-11 12:05:25 +08:00
895aa3058d feat(apds9960): 手势解析。 2023-03-11 10:46:24 +08:00
4 changed files with 183 additions and 113 deletions

View File

@@ -1,2 +1,2 @@
idf_component_register(SRCS "apds_9960.c" "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"
idf_component_register(SRCS "hw-ms03.c" "apds_9960.c" "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 ".")

View File

@@ -2,6 +2,7 @@
#include <stdlib.h>
#include "common.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "embedded_display.c"
#include "esp_log.h"
@@ -122,12 +123,15 @@
// 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
// X, GEN, PIEN, AIEN, WEN, PEN, AEN, PON
#define APDS_9960_ENABLE_VALUE 0b01100111
#define APDS_9960_TAG "APDS-9960"
static xQueueHandle apds_9960_int_evt_queue = NULL;
static int64_t last_apds_9960_int_time = 0;
esp_err_t apds_9960_write_empty(uint8_t command) {
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
@@ -231,6 +235,12 @@ esp_err_t apds_9960_read_bytes_len(uint8_t command, uint8_t* data,
return error;
}
void apds_9960_clear_all_int(void) {
ESP_LOGI(APDS_9960_TAG, "apds_9960_clear_all_int");
ESP_ERROR_CHECK_WITHOUT_ABORT(apds_9960_write_empty(APDS_9960_REG_AICLEAR));
ESP_ERROR_CHECK_WITHOUT_ABORT(apds_9960_write(APDS_9960_REG_GCONF4, 0x06));
}
void apds_9960_fetch(void* arg) {
ESP_LOGI(APDS_9960_TAG, "apds_9960_fetch");
esp_err_t error;
@@ -241,79 +251,60 @@ void apds_9960_fetch(void* arg) {
uint8_t byte_buffer;
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 status_str[10];
char red_str[20];
char green_str[20];
char blue_str[20];
char clear_str[20];
char status_str[20];
uint8_t interrupt = 0;
display_fill_rect(0, 2, 128, 8, 0x00);
for (;;) {
// display_fill_rect(0, 0, 128, 8, 0x00);
if (last_apds_9960_int_time + 1000000 < esp_timer_get_time()) {
interrupt = gpio_get_level(APDS_9960_INT_GPIO);
if (interrupt == 1 ||
last_apds_9960_int_time + 10000000 < esp_timer_get_time()) {
apds_9960_clear_all_int();
}
}
interrupt = gpio_get_level(APDS_9960_INT_GPIO);
// Status
error = apds_9960_read_byte(APDS_9960_REG_STATUS, &status_raw);
// clear
error = apds_9960_read_word(APDS_9960_REG_CDATAL, &clear_raw);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read status failed. %x", error);
ESP_LOGW(APDS_9960_TAG, "read failed. %d", 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);
sprintf(clear_str, "C:% 5d", clear_raw);
display_print8_str(64, 6, clear_str);
ESP_LOGD(APDS_9960_TAG, "Clear: %d", clear_raw);
}
error = apds_9960_read_byte(APDS_9960_REG_GFLVL, &byte_buffer);
// red
error = apds_9960_read_word(APDS_9960_REG_RDATAL, &red_raw);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GFLVL failed. %d", error);
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);
}
if (byte_buffer == 0) {
sprintf(status_str, "LVL: %4d", byte_buffer);
display_print8_str(0, 4, status_str);
// 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);
}
// // 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));
vTaskDelay(pdMS_TO_TICKS(1000));
}
display_fill_rect(0, 2, 128, 8, 0x00);
}
@@ -330,43 +321,98 @@ void apds_9960_read_gesture() {
uint8_t byte_buffer;
esp_err_t error;
uint32_t gesture_values_raw_arr[32];
char gesture_values_str_arr[20];
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);
ESP_LOGD(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");
ESP_LOGD(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);
apds_9960_write(APDS_9960_REG_GCONF4, 0x06);
return;
}
if (byte_buffer == 0) {
ESP_LOGI(APDS_9960_TAG, "Gesture FIFO empty");
if (byte_buffer < 4) {
ESP_LOGD(APDS_9960_TAG, "Gesture FIFO level too low: %d", byte_buffer);
apds_9960_write(APDS_9960_REG_GCONF4, 0x06);
return;
}
ESP_LOGI(APDS_9960_TAG, "Gesture FIFO Level: %d", byte_buffer);
ESP_LOGD(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);
apds_9960_write(APDS_9960_REG_GCONF4, 0x06);
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GFIFO(len: %d) failed. % d",
byte_buffer * 4, error);
} else {
int16_t before_ud = 0, before_lr = 0, after_ud = 0, after_lr = 0;
int16_t u = 0, d = 0, l = 0, r = 0;
uint8_t last_2_index = byte_buffer - 2;
// head 2
for (int i = 0; i < 2; i++) {
u = gesture_values_raw_arr[i] & 0xff;
d = gesture_values_raw_arr[i] >> 8 & 0xff;
l = gesture_values_raw_arr[i] >> 16 & 0xff;
r = gesture_values_raw_arr[i] >> 24 & 0xff;
before_ud += (u - d) * 100 / u + d;
before_lr += (l - r) * 100 / l + r;
}
// last 2
for (int i = last_2_index; i < byte_buffer; i++) {
u = gesture_values_raw_arr[i] & 0xff;
d = gesture_values_raw_arr[i] >> 8 & 0xff;
l = gesture_values_raw_arr[i] >> 16 & 0xff;
r = gesture_values_raw_arr[i] >> 24 & 0xff;
after_ud += (u - d) * 100 / u + d;
after_lr += (l - r) * 100 / l + r;
}
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);
if (i < 2) {
u = gesture_values_raw_arr[i] & 0xff;
d = gesture_values_raw_arr[i] >> 8 & 0xff;
l = gesture_values_raw_arr[i] >> 16 & 0xff;
r = gesture_values_raw_arr[i] >> 24 & 0xff;
before_ud += (u - d) * 100 / u + d;
before_lr += (l - r) * 100 / l + r;
} else if (i >= last_2_index) {
u = gesture_values_raw_arr[i] & 0xff;
d = gesture_values_raw_arr[i] >> 8 & 0xff;
l = gesture_values_raw_arr[i] >> 16 & 0xff;
r = gesture_values_raw_arr[i] >> 24 & 0xff;
after_ud += (u - d) * 100 / u + d;
after_lr += (l - r) * 100 / l + r;
}
}
printf("Δud: %d, Δlr: %d \n", after_ud - before_ud, after_lr - before_lr);
display_fill_rect(0, 0, 128, 2, 0x00);
if (abs(after_ud - before_ud) * 2 > abs(after_lr - before_lr)) {
if (after_ud - before_ud < -80) {
display_print8_str(0, 0, "Gesture: up");
} else if (after_ud - before_ud > 80) {
display_print8_str(0, 0, "Gesture: down");
}
} else {
if (after_lr - before_lr < -120) {
display_print8_str(0, 0, "Gesture: left");
} else if (after_lr - before_lr > 120) {
display_print8_str(0, 0, "Gesture: right");
}
}
// display_print8_str(0, 0, gesture_str);
}
@@ -374,7 +420,7 @@ void apds_9960_read_gesture() {
void apds_9960_read_proximity() {
uint8_t proximity_raw;
char proximity_str[10];
char proximity_str[20];
esp_err_t error;
// Proximity
@@ -382,15 +428,9 @@ void apds_9960_read_proximity() {
if (error != ESP_OK) {
ESP_LOGW(APDS_9960_TAG, "read proximity failed. %x", error);
} else {
ESP_LOGD(APDS_9960_TAG, "Prox: % 5d ", proximity_raw);
sprintf(proximity_str, "Prox: % 5d ", proximity_raw);
display_print8_str(8, 2, proximity_str);
ESP_LOGI(APDS_9960_TAG, "Prox: %d", proximity_raw);
}
// 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);
}
}
@@ -403,16 +443,15 @@ void apds_9960_int_evt_handler() {
return;
}
ESP_ERROR_CHECK(apds_9960_write_empty(APDS_9960_REG_AICLEAR));
// enable gesture interrupt
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GCONF4, 0x06));
apds_9960_clear_all_int();
esp_err_t error;
uint8_t status_raw;
while (xQueueReceive(apds_9960_int_evt_queue, NULL, portMAX_DELAY)) {
last_apds_9960_int_time = esp_timer_get_time();
ESP_ERROR_RETRY(apds_9960_read_byte(APDS_9960_REG_STATUS, &status_raw), 10);
ESP_ERROR_RETRY(apds_9960_write_empty(APDS_9960_REG_AICLEAR), 10);
ESP_LOGI(
ESP_LOGD(
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,
@@ -423,7 +462,8 @@ void apds_9960_int_evt_handler() {
if (status_raw & APDS_9960_GINT) {
apds_9960_read_gesture();
}
vTaskDelay(100 / portTICK_PERIOD_MS);
vTaskDelay(10 / portTICK_PERIOD_MS);
}
}
@@ -457,14 +497,14 @@ void apds_9960_init() {
// 累积的手势数据达到 4 组时触发中断
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GCONF1, 0x40));
// Gesture Enter Threshold
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GPENTH, 20));
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GPENTH, 3));
// Gesture Exit Threshold
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GEXTH, 10));
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GEXTH, 3));
// Gesture Drive Strength
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GCONF2, 0x40));
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GCONF2, 0b0110000));
// set wait time
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_WTIME, 171));
// // 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));
@@ -475,7 +515,8 @@ void apds_9960_init() {
// // 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, 0b01101101));
ESP_ERROR_CHECK(
apds_9960_write(APDS_9960_REG_ENABLE, APDS_9960_ENABLE_VALUE));
apds_9960_int_evt_queue = xQueueCreate(10, NULL);
xTaskCreate(apds_9960_int_evt_handler, "apds_9960_gesture_fetch", 2048, NULL,

27
main/hw-ms03.c Normal file
View File

@@ -0,0 +1,27 @@
#pragma once
#include "driver/gpio.h"
#include "freeRTOS/FreeRTOS.h"
#define HW_MS03_INT_GPIO 6
#define BEEP_GPIO 7
void hw_ms03_int_handler(void *arg) {
gpio_set_level(BEEP_GPIO, gpio_get_level(HW_MS03_INT_GPIO));
}
void hw_ms03_init() {
gpio_config_t io_conf = {};
io_conf.mode = GPIO_MODE_INPUT;
io_conf.pull_up_en = 0;
io_conf.pull_down_en = 1;
io_conf.intr_type = GPIO_INTR_ANYEDGE;
io_conf.pin_bit_mask = 1ULL << HW_MS03_INT_GPIO;
gpio_config(&io_conf);
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = 1ULL << BEEP_GPIO;
io_conf.pull_down_en = 0;
gpio_config(&io_conf);
gpio_isr_handler_add(HW_MS03_INT_GPIO, hw_ms03_int_handler, NULL);
}

View File

@@ -4,6 +4,7 @@
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "hw-ms03.c"
#include "i2c.c"
#include "light.c"
#include "mqtt.c"
@@ -24,28 +25,29 @@ void app_main(void) {
init_display();
display_print8_str(0, 0, "Ambient Light");
// hw_ms03_init();
// 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));
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);
}
}