Compare commits
2 Commits
33e08edeed
...
eaedd765ed
Author | SHA1 | Date | |
---|---|---|---|
eaedd765ed | |||
895aa3058d |
161
main/apds_9960.c
161
main/apds_9960.c
@ -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;
|
||||||
@ -241,40 +249,48 @@ void apds_9960_fetch(void* arg) {
|
|||||||
uint8_t byte_buffer;
|
uint8_t byte_buffer;
|
||||||
uint8_t gesture_status_raw;
|
uint8_t gesture_status_raw;
|
||||||
uint8_t status_raw;
|
uint8_t status_raw;
|
||||||
char red_str[10];
|
char red_str[20];
|
||||||
char green_str[10];
|
char green_str[20];
|
||||||
char blue_str[10];
|
char blue_str[20];
|
||||||
char clear_str[10];
|
char clear_str[20];
|
||||||
char status_str[10];
|
char status_str[20];
|
||||||
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);
|
||||||
|
|
||||||
// Status
|
// // Status
|
||||||
error = apds_9960_read_byte(APDS_9960_REG_STATUS, &status_raw);
|
// error = apds_9960_read_byte(APDS_9960_REG_STATUS, &status_raw);
|
||||||
if (error != ESP_OK) {
|
// if (error != ESP_OK) {
|
||||||
ESP_LOGW(APDS_9960_TAG, "read status failed. %x", error);
|
// ESP_LOGW(APDS_9960_TAG, "read status failed. %x", error);
|
||||||
} else {
|
// } else {
|
||||||
sprintf(status_str, "%d :%d%d%d%d %d%d%d%d", interrupt,
|
// sprintf(status_str, "%d :%d%d%d%d %d%d%d%d", interrupt,
|
||||||
(status_raw >> 7) & 1, (status_raw >> 6) & 1,
|
// (status_raw >> 7) & 1, (status_raw >> 6) & 1,
|
||||||
(status_raw >> 5) & 1, (status_raw >> 4) & 1,
|
// (status_raw >> 5) & 1, (status_raw >> 4) & 1,
|
||||||
(status_raw >> 3) & 1, (status_raw >> 2) & 1,
|
// (status_raw >> 3) & 1, (status_raw >> 2) & 1,
|
||||||
(status_raw >> 1) & 1, status_raw & 1);
|
// (status_raw >> 1) & 1, status_raw & 1);
|
||||||
display_print8_str(0, 0, status_str);
|
// display_print8_str(0, 0, status_str);
|
||||||
ESP_LOGD(APDS_9960_TAG, "Status: %2x", status_raw);
|
// ESP_LOGD(APDS_9960_TAG, "Status: %2x", status_raw);
|
||||||
}
|
// }
|
||||||
|
|
||||||
error = apds_9960_read_byte(APDS_9960_REG_GFLVL, &byte_buffer);
|
// error = apds_9960_read_byte(APDS_9960_REG_GFLVL, &byte_buffer);
|
||||||
if (error != ESP_OK) {
|
// if (error != ESP_OK) {
|
||||||
ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GFLVL failed. %d", error);
|
// ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GFLVL failed. %d", error);
|
||||||
}
|
// }
|
||||||
if (byte_buffer == 0) {
|
// if (byte_buffer == 0) {
|
||||||
sprintf(status_str, "LVL: %4d", byte_buffer);
|
// sprintf(status_str, "LVL: %4d", byte_buffer);
|
||||||
display_print8_str(0, 4, status_str);
|
// display_print8_str(0, 4, status_str);
|
||||||
}
|
// }
|
||||||
|
|
||||||
// // clear
|
// // clear
|
||||||
// error = apds_9960_read_word(APDS_9960_REG_CDATAL, &clear_raw);
|
// error = apds_9960_read_word(APDS_9960_REG_CDATAL, &clear_raw);
|
||||||
@ -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);
|
||||||
}
|
}
|
||||||
@ -330,43 +346,82 @@ void apds_9960_read_gesture() {
|
|||||||
uint8_t byte_buffer;
|
uint8_t byte_buffer;
|
||||||
esp_err_t error;
|
esp_err_t error;
|
||||||
uint32_t gesture_values_raw_arr[32];
|
uint32_t gesture_values_raw_arr[32];
|
||||||
|
char gesture_values_str_arr[20];
|
||||||
|
|
||||||
error = apds_9960_read_byte(APDS_9960_REG_GSTATUS, &byte_buffer);
|
error = apds_9960_read_byte(APDS_9960_REG_GSTATUS, &byte_buffer);
|
||||||
if (error != ESP_OK) {
|
if (error != ESP_OK) {
|
||||||
ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GSTATUS failed. %d", error);
|
ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GSTATUS failed. %d", error);
|
||||||
return;
|
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)) {
|
if (!(byte_buffer & APDS_9960_GVALID)) {
|
||||||
ESP_LOGI(APDS_9960_TAG, "Gesture no valid");
|
ESP_LOGI(APDS_9960_TAG, "Gesture no valid");
|
||||||
return;
|
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);
|
error = apds_9960_read_byte(APDS_9960_REG_GFLVL, &byte_buffer);
|
||||||
if (error != ESP_OK) {
|
if (error != ESP_OK) {
|
||||||
ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GFLVL failed. %d", error);
|
ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GFLVL failed. %d", error);
|
||||||
|
apds_9960_write(APDS_9960_REG_GCONF4, 0x06);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (byte_buffer == 0) {
|
if (byte_buffer < 4) {
|
||||||
ESP_LOGI(APDS_9960_TAG, "Gesture FIFO empty");
|
ESP_LOGD(APDS_9960_TAG, "Gesture FIFO level too low: %d", byte_buffer);
|
||||||
|
apds_9960_write(APDS_9960_REG_GCONF4, 0x06);
|
||||||
return;
|
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(
|
error = apds_9960_read_bytes_len(
|
||||||
APDS_9960_REG_GFIFO_U, (uint8_t*)gesture_values_raw_arr, byte_buffer * 4);
|
APDS_9960_REG_GFIFO_U, (uint8_t*)gesture_values_raw_arr, byte_buffer * 4);
|
||||||
apds_9960_write(APDS_9960_REG_GCONF4, 0x06);
|
apds_9960_write(APDS_9960_REG_GCONF4, 0x06);
|
||||||
|
|
||||||
if (error != ESP_OK) {
|
if (error != ESP_OK) {
|
||||||
ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GFIFO(len: %d) failed. % d",
|
ESP_LOGW(APDS_9960_TAG, "read APDS_9960_REG_GFIFO(len: %d) failed. % d",
|
||||||
byte_buffer * 4, error);
|
byte_buffer * 4, error);
|
||||||
} else {
|
} 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;
|
||||||
|
|
||||||
for (int i = 0; i < byte_buffer; i++) {
|
for (int i = 0; i < byte_buffer; i++) {
|
||||||
printf("Gesture %2d: %02x %02x %02x %02x\n", i,
|
if (i < 2) {
|
||||||
gesture_values_raw_arr[i] & 0xff,
|
u = gesture_values_raw_arr[i] & 0xff;
|
||||||
gesture_values_raw_arr[i] >> 8 & 0xff,
|
d = gesture_values_raw_arr[i] >> 8 & 0xff;
|
||||||
gesture_values_raw_arr[i] >> 16 & 0xff,
|
l = gesture_values_raw_arr[i] >> 16 & 0xff;
|
||||||
gesture_values_raw_arr[i] >> 24 & 0xff);
|
r = gesture_values_raw_arr[i] >> 24 & 0xff;
|
||||||
|
before_ud += (u - d) * 100 / u + d;
|
||||||
|
before_lr += (l - r) * 100 / l + r;
|
||||||
|
printf("Gesture %08x %03d %03d %03d %03d\n", gesture_values_raw_arr[i],
|
||||||
|
u, d, 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("Gesture %08x %03d %03d %03d %03d\n", gesture_values_raw_arr[i],
|
||||||
|
u, d, l, r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("Δud: %d, Δlr: %d \n", after_ud - before_ud, after_lr - before_lr);
|
||||||
|
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) {
|
||||||
|
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);
|
// display_print8_str(0, 0, gesture_str);
|
||||||
}
|
}
|
||||||
@ -374,7 +429,7 @@ void apds_9960_read_gesture() {
|
|||||||
|
|
||||||
void apds_9960_read_proximity() {
|
void apds_9960_read_proximity() {
|
||||||
uint8_t proximity_raw;
|
uint8_t proximity_raw;
|
||||||
char proximity_str[10];
|
char proximity_str[20];
|
||||||
esp_err_t error;
|
esp_err_t error;
|
||||||
|
|
||||||
// Proximity
|
// Proximity
|
||||||
@ -382,15 +437,9 @@ void apds_9960_read_proximity() {
|
|||||||
if (error != ESP_OK) {
|
if (error != ESP_OK) {
|
||||||
ESP_LOGW(APDS_9960_TAG, "read proximity failed. %x", error);
|
ESP_LOGW(APDS_9960_TAG, "read proximity failed. %x", error);
|
||||||
} else {
|
} else {
|
||||||
|
ESP_LOGD(APDS_9960_TAG, "Prox: % 5d ", proximity_raw);
|
||||||
sprintf(proximity_str, "Prox: % 5d ", proximity_raw);
|
sprintf(proximity_str, "Prox: % 5d ", proximity_raw);
|
||||||
display_print8_str(8, 2, proximity_str);
|
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 +452,15 @@ 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);
|
||||||
|
|
||||||
ESP_LOGI(
|
ESP_LOGD(
|
||||||
APDS_9960_TAG, "[apds_9960_int_evt_handler] status %d%d%d%d %d%d%d%d",
|
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 >> 7) & 1, (status_raw >> 6) & 1, (status_raw >> 5) & 1,
|
||||||
(status_raw >> 4) & 1, (status_raw >> 3) & 1, (status_raw >> 2) & 1,
|
(status_raw >> 4) & 1, (status_raw >> 3) & 1, (status_raw >> 2) & 1,
|
||||||
@ -423,7 +471,8 @@ 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(100 / portTICK_PERIOD_MS);
|
|
||||||
|
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -457,14 +506,14 @@ void apds_9960_init() {
|
|||||||
// 累积的手势数据达到 4 组时触发中断
|
// 累积的手势数据达到 4 组时触发中断
|
||||||
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GCONF1, 0x40));
|
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GCONF1, 0x40));
|
||||||
// Gesture Enter Threshold
|
// 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
|
// 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
|
// 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
|
// // set wait time
|
||||||
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_WTIME, 171));
|
// ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_WTIME, 171));
|
||||||
|
|
||||||
// set interrupt persistence
|
// set interrupt persistence
|
||||||
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_PERS, 0x44));
|
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_PERS, 0x44));
|
||||||
|
Loading…
Reference in New Issue
Block a user