feat(apds9960): 防中断导致假死。

This commit is contained in:
Ivan Li 2023-03-11 12:05:25 +08:00
parent 895aa3058d
commit eaedd765ed

View File

@ -128,6 +128,8 @@
static xQueueHandle apds_9960_int_evt_queue = NULL; 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) { esp_err_t apds_9960_write_empty(uint8_t command) {
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);
@ -231,6 +233,12 @@ esp_err_t apds_9960_read_bytes_len(uint8_t command, uint8_t* data,
return error; 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) { void apds_9960_fetch(void* arg) {
ESP_LOGI(APDS_9960_TAG, "apds_9960_fetch"); ESP_LOGI(APDS_9960_TAG, "apds_9960_fetch");
esp_err_t error; esp_err_t error;
@ -249,6 +257,14 @@ void apds_9960_fetch(void* arg) {
uint8_t interrupt = 0; uint8_t interrupt = 0;
display_fill_rect(0, 2, 128, 8, 0x00); display_fill_rect(0, 2, 128, 8, 0x00);
for (;;) { for (;;) {
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();
}
}
// display_fill_rect(0, 0, 128, 8, 0x00); // display_fill_rect(0, 0, 128, 8, 0x00);
// interrupt = gpio_get_level(APDS_9960_INT_GPIO); // interrupt = gpio_get_level(APDS_9960_INT_GPIO);
@ -313,7 +329,7 @@ void apds_9960_fetch(void* arg) {
// ESP_LOGD(APDS_9960_TAG, "Blue: %d", blue_raw); // ESP_LOGD(APDS_9960_TAG, "Blue: %d", blue_raw);
// } // }
vTaskDelay(pdMS_TO_TICKS(10)); vTaskDelay(pdMS_TO_TICKS(100));
} }
display_fill_rect(0, 2, 128, 8, 0x00); display_fill_rect(0, 2, 128, 8, 0x00);
} }
@ -393,16 +409,19 @@ void apds_9960_read_gesture() {
} }
printf("Δud: %d, Δlr: %d \n", after_ud - before_ud, after_lr - before_lr); printf("Δud: %d, Δlr: %d \n", after_ud - before_ud, after_lr - before_lr);
display_fill_rect(0, 4, 128, 8, 0x00); display_fill_rect(0, 4, 128, 8, 0x00);
if (abs(after_ud - before_ud) * 2 > abs(after_lr - before_lr)) {
if (after_ud - before_ud < -80) { if (after_ud - before_ud < -80) {
display_print8_str(0, 4, "Gesture: up"); display_print8_str(0, 4, "Gesture: up");
} else if (after_ud - before_ud > 80) { } else if (after_ud - before_ud > 80) {
display_print8_str(0, 4, "Gesture: down"); display_print8_str(0, 4, "Gesture: down");
} }
} else {
if (after_lr - before_lr < -80) { if (after_lr - before_lr < -120) {
display_print8_str(0, 6, "Gesture: left"); display_print8_str(0, 4, "Gesture: left");
} else if (after_lr - before_lr > 80) { } else if (after_lr - before_lr > 120) {
display_print8_str(0, 6, "Gesture: right"); display_print8_str(0, 4, "Gesture: right");
}
} }
// display_print8_str(0, 0, gesture_str); // display_print8_str(0, 0, gesture_str);
} }
@ -433,12 +452,11 @@ void apds_9960_int_evt_handler() {
return; return;
} }
ESP_ERROR_CHECK(apds_9960_write_empty(APDS_9960_REG_AICLEAR)); apds_9960_clear_all_int();
// enable gesture interrupt
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GCONF4, 0x06));
esp_err_t error; esp_err_t error;
uint8_t status_raw; uint8_t status_raw;
while (xQueueReceive(apds_9960_int_evt_queue, NULL, portMAX_DELAY)) { 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_read_byte(APDS_9960_REG_STATUS, &status_raw), 10);
ESP_ERROR_RETRY(apds_9960_write_empty(APDS_9960_REG_AICLEAR), 10); ESP_ERROR_RETRY(apds_9960_write_empty(APDS_9960_REG_AICLEAR), 10);
@ -453,6 +471,7 @@ void apds_9960_int_evt_handler() {
if (status_raw & APDS_9960_GINT) { if (status_raw & APDS_9960_GINT) {
apds_9960_read_gesture(); apds_9960_read_gesture();
} }
vTaskDelay(10 / portTICK_PERIOD_MS); vTaskDelay(10 / portTICK_PERIOD_MS);
} }
} }