Compare commits
No commits in common. "eaedd765ed3f219220660c9bc9f1d4525e3edf9d" and "33e08edeeda275652b054aff3b9859a6dfb8b07a" have entirely different histories.
eaedd765ed
...
33e08edeed
161
main/apds_9960.c
161
main/apds_9960.c
@ -128,8 +128,6 @@
|
||||
|
||||
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);
|
||||
@ -233,12 +231,6 @@ 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,48 +241,40 @@ void apds_9960_fetch(void* arg) {
|
||||
uint8_t byte_buffer;
|
||||
uint8_t gesture_status_raw;
|
||||
uint8_t status_raw;
|
||||
char red_str[20];
|
||||
char green_str[20];
|
||||
char blue_str[20];
|
||||
char clear_str[20];
|
||||
char status_str[20];
|
||||
char red_str[10];
|
||||
char green_str[10];
|
||||
char blue_str[10];
|
||||
char clear_str[10];
|
||||
char status_str[10];
|
||||
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);
|
||||
interrupt = gpio_get_level(APDS_9960_INT_GPIO);
|
||||
|
||||
// // Status
|
||||
// error = apds_9960_read_byte(APDS_9960_REG_STATUS, &status_raw);
|
||||
// if (error != ESP_OK) {
|
||||
// ESP_LOGW(APDS_9960_TAG, "read status failed. %x", 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);
|
||||
// }
|
||||
// Status
|
||||
error = apds_9960_read_byte(APDS_9960_REG_STATUS, &status_raw);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGW(APDS_9960_TAG, "read status failed. %x", 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);
|
||||
}
|
||||
|
||||
// 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);
|
||||
// }
|
||||
// if (byte_buffer == 0) {
|
||||
// sprintf(status_str, "LVL: %4d", byte_buffer);
|
||||
// display_print8_str(0, 4, status_str);
|
||||
// }
|
||||
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);
|
||||
}
|
||||
if (byte_buffer == 0) {
|
||||
sprintf(status_str, "LVL: %4d", byte_buffer);
|
||||
display_print8_str(0, 4, status_str);
|
||||
}
|
||||
|
||||
// // clear
|
||||
// error = apds_9960_read_word(APDS_9960_REG_CDATAL, &clear_raw);
|
||||
@ -329,7 +313,7 @@ void apds_9960_fetch(void* arg) {
|
||||
// ESP_LOGD(APDS_9960_TAG, "Blue: %d", blue_raw);
|
||||
// }
|
||||
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
vTaskDelay(pdMS_TO_TICKS(10));
|
||||
}
|
||||
display_fill_rect(0, 2, 128, 8, 0x00);
|
||||
}
|
||||
@ -346,82 +330,43 @@ 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_LOGD(APDS_9960_TAG, "APDS-9960 interrupt. status: %x", byte_buffer);
|
||||
ESP_LOGI(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_LOGD(APDS_9960_TAG, "Gesture interrupt");
|
||||
ESP_LOGI(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 < 4) {
|
||||
ESP_LOGD(APDS_9960_TAG, "Gesture FIFO level too low: %d", byte_buffer);
|
||||
apds_9960_write(APDS_9960_REG_GCONF4, 0x06);
|
||||
if (byte_buffer == 0) {
|
||||
ESP_LOGI(APDS_9960_TAG, "Gesture FIFO empty");
|
||||
return;
|
||||
}
|
||||
ESP_LOGD(APDS_9960_TAG, "Gesture FIFO Level: %d", byte_buffer);
|
||||
ESP_LOGI(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;
|
||||
|
||||
for (int i = 0; i < byte_buffer; i++) {
|
||||
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;
|
||||
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");
|
||||
}
|
||||
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);
|
||||
}
|
||||
// display_print8_str(0, 0, gesture_str);
|
||||
}
|
||||
@ -429,7 +374,7 @@ void apds_9960_read_gesture() {
|
||||
|
||||
void apds_9960_read_proximity() {
|
||||
uint8_t proximity_raw;
|
||||
char proximity_str[20];
|
||||
char proximity_str[10];
|
||||
esp_err_t error;
|
||||
|
||||
// Proximity
|
||||
@ -437,9 +382,15 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -452,15 +403,16 @@ void apds_9960_int_evt_handler() {
|
||||
return;
|
||||
}
|
||||
|
||||
apds_9960_clear_all_int();
|
||||
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));
|
||||
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_LOGD(
|
||||
ESP_LOGI(
|
||||
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,
|
||||
@ -471,8 +423,7 @@ void apds_9960_int_evt_handler() {
|
||||
if (status_raw & APDS_9960_GINT) {
|
||||
apds_9960_read_gesture();
|
||||
}
|
||||
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
|
||||
@ -506,14 +457,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, 3));
|
||||
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GPENTH, 20));
|
||||
// Gesture Exit Threshold
|
||||
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GEXTH, 3));
|
||||
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GEXTH, 10));
|
||||
// Gesture Drive Strength
|
||||
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GCONF2, 0b0110000));
|
||||
ESP_ERROR_CHECK(apds_9960_write(APDS_9960_REG_GCONF2, 0x40));
|
||||
|
||||
// // 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));
|
||||
|
Loading…
Reference in New Issue
Block a user