diff --git a/main/apds_9960.c b/main/apds_9960.c index 9c0a458..2020b96 100644 --- a/main/apds_9960.c +++ b/main/apds_9960.c @@ -128,6 +128,8 @@ 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 +233,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; @@ -249,6 +257,14 @@ void apds_9960_fetch(void* arg) { uint8_t interrupt = 0; display_fill_rect(0, 2, 128, 8, 0x00); 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); // 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); // } - vTaskDelay(pdMS_TO_TICKS(10)); + vTaskDelay(pdMS_TO_TICKS(100)); } 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); display_fill_rect(0, 4, 128, 8, 0x00); - if (after_ud - before_ud < -80) { - display_print8_str(0, 4, "Gesture: up"); - } else if (after_ud - before_ud > 80) { - display_print8_str(0, 4, "Gesture: down"); - } - if (after_lr - before_lr < -80) { - display_print8_str(0, 6, "Gesture: left"); - } else if (after_lr - before_lr > 80) { - display_print8_str(0, 6, "Gesture: right"); + if (abs(after_ud - before_ud) * 2 > abs(after_lr - before_lr)) { + if (after_ud - before_ud < -80) { + display_print8_str(0, 4, "Gesture: up"); + } else if (after_ud - before_ud > 80) { + display_print8_str(0, 4, "Gesture: down"); + } + } else { + if (after_lr - before_lr < -120) { + display_print8_str(0, 4, "Gesture: left"); + } else if (after_lr - before_lr > 120) { + display_print8_str(0, 4, "Gesture: right"); + } } // display_print8_str(0, 0, gesture_str); } @@ -433,12 +452,11 @@ 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); @@ -453,6 +471,7 @@ void apds_9960_int_evt_handler() { if (status_raw & APDS_9960_GINT) { apds_9960_read_gesture(); } + vTaskDelay(10 / portTICK_PERIOD_MS); } }