feat(apds9960): 手势解析。

This commit is contained in:
Ivan Li 2023-03-11 10:46:24 +08:00
parent 33e08edeed
commit 895aa3058d

View File

@ -241,40 +241,40 @@ 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 (;;) {
// 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);
@ -330,43 +330,79 @@ 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 (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");
} }
// display_print8_str(0, 0, gesture_str); // display_print8_str(0, 0, gesture_str);
} }
@ -374,7 +410,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 +418,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);
} }
} }
@ -412,7 +442,7 @@ void apds_9960_int_evt_handler() {
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 +453,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(100 / portTICK_PERIOD_MS); vTaskDelay(10 / portTICK_PERIOD_MS);
} }
} }
@ -457,14 +487,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));