Merge pull request #8277 from Theosakamg/improve_gesture

APDS-9960 - Improve gesture
This commit is contained in:
Theo Arends 2020-04-26 09:00:09 +02:00 committed by GitHub
commit 5f2dec57ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -61,6 +61,8 @@
#define APDS9930_CHIPID_1 0x12 // we will check, if someone got an incorrect sensor #define APDS9930_CHIPID_1 0x12 // we will check, if someone got an incorrect sensor
#define APDS9930_CHIPID_2 0x39 // there are case reports about "accidentially bought" 9930's #define APDS9930_CHIPID_2 0x39 // there are case reports about "accidentially bought" 9930's
#define APDS9960_GESTURE // Enable Gesture feature(2k on flash)
/* Gesture parameters */ /* Gesture parameters */
#define GESTURE_THRESHOLD_OUT 10 #define GESTURE_THRESHOLD_OUT 10
#define GESTURE_SENSITIVITY_1 50 #define GESTURE_SENSITIVITY_1 50
@ -72,13 +74,26 @@
const char APDS9960_TAG[] = "APDS9960"; const char APDS9960_TAG[] = "APDS9960";
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
const char HTTP_APDS9960_SNS[] PROGMEM = #ifdef APDS9960_GESTURE
"{s}" "Red" "{m}%s{e}"
"{s}" "Green" "{m}%s{e}" #define D_GESTURE "Gesture"
"{s}" "Blue" "{m}%s{e}"
"{s}" "Ambient" "{m}%s " D_UNIT_LUX "{e}" const char HTTP_SNS_GESTURE[] PROGMEM = "{s}%s " D_GESTURE "{m}%s{e}";
"{s}" "CCT" "{m}%s " "K" "{e}" // calculated color temperature in Kelvin
"{s}" "Proximity" "{m}%s{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr> #endif // APDS9960_GESTURE
#define D_COLOR_RED "Red"
#define D_COLOR_GREEN "Green"
#define D_COLOR_BLUE "Blue"
#define D_CCT "CCT"
#define D_PROXIMITY "Proximity"
const char HTTP_SNS_COLOR_RED[] PROGMEM = "{s}%s " D_COLOR_RED "{m}%u{e}";
const char HTTP_SNS_COLOR_GREEN[] PROGMEM = "{s}%s " D_COLOR_GREEN "{m}%u{e}";
const char HTTP_SNS_COLOR_BLUE[] PROGMEM = "{s}%s " D_COLOR_BLUE "{m}%u{e}";
const char HTTP_SNS_CCT[] PROGMEM = "{s}%s " D_CCT "{m}%u " "K" "{e}";
const char HTTP_SNS_PROXIMITY[] PROGMEM = "{s}%s " D_PROXIMITY "{m}%u{e}";
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
/*********************************************************************************************\ /*********************************************************************************************\
@ -236,7 +251,15 @@ const char HTTP_APDS9960_SNS[] PROGMEM =
#define APDS9960_ERROR 0xFF #define APDS9960_ERROR 0xFF
#ifdef APDS9960_GESTURE
/* Direction definitions */ /* Direction definitions */
const char GESTURE_UP[] PROGMEM = "Up";
const char GESTURE_DOWN[] PROGMEM = "Down";
const char GESTURE_LEFT[] PROGMEM = "Left";
const char GESTURE_RIGHT[] PROGMEM = "Right";
const char GESTURE_LONG[] PROGMEM = "Long";
const char GESTURE_NONE[] PROGMEM = "None";
enum { enum {
DIR_NONE, DIR_NONE,
DIR_LEFT, DIR_LEFT,
@ -249,12 +272,14 @@ enum {
}; };
/* State definitions*/ /* State definitions*/
/*
enum { enum {
APDS9960_NA_STATE, APDS9960_NA_STATE,
APDS9960_NEAR_STATE, APDS9960_NEAR_STATE,
APDS9960_FAR_STATE, APDS9960_FAR_STATE,
APDS9960_ALL_STATE APDS9960_ALL_STATE
}; };
*/
/* Container for gesture data */ /* Container for gesture data */
typedef struct gesture_data_type { typedef struct gesture_data_type {
@ -268,8 +293,7 @@ typedef struct gesture_data_type {
uint8_t out_threshold; uint8_t out_threshold;
} gesture_data_t; } gesture_data_t;
typedef struct gesture_type typedef struct gesture_type {
{
int16_t ud_delta_ = 0; int16_t ud_delta_ = 0;
int16_t lr_delta_ = 0; int16_t lr_delta_ = 0;
int16_t ud_count_ = 0; int16_t ud_count_ = 0;
@ -278,6 +302,8 @@ typedef struct gesture_type
int16_t motion_ = DIR_NONE; int16_t motion_ = DIR_NONE;
} gesture_t; } gesture_t;
#endif // APDS9960_GESTURE
typedef struct color_data_type { typedef struct color_data_type {
uint16_t a; // measured ambient uint16_t a; // measured ambient
uint16_t r; // Red uint16_t r; // Red
@ -289,8 +315,11 @@ typedef struct color_data_type {
} color_data_t; } color_data_t;
/*Members*/ /*Members*/
#ifdef APDS9960_GESTURE
gesture_data_t gesture_data; gesture_data_t gesture_data;
gesture_t gesture; gesture_t gesture;
#endif // APDS9960_GESTURE
color_data_t color_data; color_data_t color_data;
volatile uint8_t recovery_loop_counter = 0; // count number of stateloops to switch the sensor off, if needed volatile uint8_t recovery_loop_counter = 0; // count number of stateloops to switch the sensor off, if needed
@ -721,6 +750,8 @@ void setProxPhotoMask(uint8_t mask) {
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val); I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG3, val);
} }
#ifdef APDS9960_GESTURE
/** /**
* @brief Gets the entry proximity threshold for gesture sensing * @brief Gets the entry proximity threshold for gesture sensing
* *
@ -924,6 +955,8 @@ void setGestureWaitTime(uint8_t time) {
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF2, val); I2cWrite8(APDS9960_I2C_ADDR, APDS9960_GCONF2, val);
} }
#endif // APDS9960_GESTURE
/** /**
* @brief Gets the low threshold for ambient light interrupts * @brief Gets the low threshold for ambient light interrupts
* *
@ -1238,6 +1271,8 @@ bool APDS9960_init(void) {
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG3, DEFAULT_CONFIG3); I2cWrite8(APDS9960_I2C_ADDR, APDS9960_CONFIG3, DEFAULT_CONFIG3);
/* Set default values for gesture sense registers */ /* Set default values for gesture sense registers */
#ifdef APDS9960_GESTURE
setGestureEnterThresh(DEFAULT_GPENTH); setGestureEnterThresh(DEFAULT_GPENTH);
setGestureExitThresh(DEFAULT_GEXTH); setGestureExitThresh(DEFAULT_GEXTH);
@ -1256,6 +1291,8 @@ bool APDS9960_init(void) {
setGestureIntEnable(DEFAULT_GIEN); setGestureIntEnable(DEFAULT_GIEN);
#endif // APDS9960_GESTURE
disablePower(); // go to sleep disablePower(); // go to sleep
return true; return true;
@ -1357,6 +1394,7 @@ void disableProximitySensor(void) {
setMode(PROXIMITY, OFF); setMode(PROXIMITY, OFF);
} }
#ifdef APDS9960_GESTURE
/** /**
* @brief Starts the gesture recognition engine on the APDS-9960 * @brief Starts the gesture recognition engine on the APDS-9960
* *
@ -1451,7 +1489,7 @@ int16_t readGesture(void) {
fifo_level = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GFLVL); fifo_level = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GFLVL);
#ifdef USE_DEBUG_DRIVER #ifdef USE_DEBUG_DRIVER
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DRV: FIFO Level : %d"), fifo_level); AddLog_P2(LOG_LEVEL_DEBUG, PSTR("DRV: FIFO Level : %d"), fifo_level);
#endif // USE_DEBUG_DRIVER #endif // USE_DEBUG_DRIVER
/* If there's stuff in the FIFO, read it into our data block */ /* If there's stuff in the FIFO, read it into our data block */
@ -1504,6 +1542,8 @@ int16_t readGesture(void) {
} }
} }
#endif // APDS9960_GESTURE
/** /**
* Turn the APDS-9960 on * Turn the APDS-9960 on
* *
@ -1540,6 +1580,8 @@ void readAllColorAndProximityData(void) {
* High-level gesture controls * High-level gesture controls
\******************************************************************************/ \******************************************************************************/
#ifdef APDS9960_GESTURE
/** /**
* @brief Resets all the parameters in the gesture data member * @brief Resets all the parameters in the gesture data member
*/ */
@ -1705,28 +1747,28 @@ void handleGesture(void) {
if (isGestureAvailable()) { if (isGestureAvailable()) {
switch (readGesture()) { switch (readGesture()) {
case DIR_UP: case DIR_UP:
AddLog_P(LOG_LEVEL_DEBUG, PSTR("UP")); AddLog_P(LOG_LEVEL_DEBUG, GESTURE_UP);
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Up")); snprintf_P(currentGesture, sizeof(currentGesture), GESTURE_UP);
break; break;
case DIR_DOWN: case DIR_DOWN:
AddLog_P(LOG_LEVEL_DEBUG, PSTR("DOWN")); AddLog_P(LOG_LEVEL_DEBUG, GESTURE_DOWN);
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Down")); snprintf_P(currentGesture, sizeof(currentGesture), GESTURE_DOWN);
break; break;
case DIR_LEFT: case DIR_LEFT:
AddLog_P(LOG_LEVEL_DEBUG, PSTR("LEFT")); AddLog_P(LOG_LEVEL_DEBUG, GESTURE_LEFT);
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Left")); snprintf_P(currentGesture, sizeof(currentGesture), GESTURE_LEFT);
break; break;
case DIR_RIGHT: case DIR_RIGHT:
AddLog_P(LOG_LEVEL_DEBUG, PSTR("RIGHT")); AddLog_P(LOG_LEVEL_DEBUG, GESTURE_RIGHT);
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Right")); snprintf_P(currentGesture, sizeof(currentGesture), GESTURE_RIGHT);
break; break;
default: default:
if (APDS9960_overload) { if (APDS9960_overload) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("LONG")); AddLog_P(LOG_LEVEL_DEBUG, GESTURE_LONG);
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Long")); snprintf_P(currentGesture, sizeof(currentGesture), GESTURE_LONG);
} else { } else {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("NONE")); AddLog_P(LOG_LEVEL_DEBUG, GESTURE_NONE);
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("None")); snprintf_P(currentGesture, sizeof(currentGesture), GESTURE_NONE);
} }
break; break;
} }
@ -1734,6 +1776,8 @@ void handleGesture(void) {
} }
} }
#endif // APDS9960_GESTURE
void APDS9960_adjustATime(void) { // not really used atm void APDS9960_adjustATime(void) { // not really used atm
// readAllColorAndProximityData(); // readAllColorAndProximityData();
I2cValidRead16LE(&color_data.a, APDS9960_I2C_ADDR, APDS9960_CDATAL); I2cValidRead16LE(&color_data.a, APDS9960_I2C_ADDR, APDS9960_CDATAL);
@ -1766,6 +1810,8 @@ void APDS9960_adjustATime(void) { // not really used atm
delay(20); delay(20);
} }
#ifdef APDS9960_GESTURE
void APDS9960_loop(void) { void APDS9960_loop(void) {
if (recovery_loop_counter > 0) { if (recovery_loop_counter > 0) {
recovery_loop_counter -= 1; recovery_loop_counter -= 1;
@ -1794,6 +1840,8 @@ void APDS9960_loop(void) {
} }
} }
#endif // APDS9960_GESTURE
void APDS9960_detect(void) { void APDS9960_detect(void) {
if (APDS9960type || I2cActive(APDS9960_I2C_ADDR)) { return; } if (APDS9960type || I2cActive(APDS9960_I2C_ADDR)) { return; }
@ -1809,7 +1857,9 @@ void APDS9960_detect(void) {
I2cSetActiveFound(APDS9960_I2C_ADDR, APDS9960_TAG); I2cSetActiveFound(APDS9960_I2C_ADDR, APDS9960_TAG);
enableProximitySensor(); enableProximitySensor();
#ifdef APDS9960_GESTURE
enableGestureSensor(); enableGestureSensor();
#endif // APDS9960_GESTURE
} else { } else {
APDS9960type = 0; APDS9960type = 0;
} }
@ -1828,41 +1878,48 @@ void APDS9960_show(bool json) {
if (!APDS9960type) { return; } if (!APDS9960type) { return; }
if (!gesture_mode && !APDS9960_overload) { if (!gesture_mode && !APDS9960_overload) {
char red_chr[10]; uint16_t ambient;
char green_chr[10];
char blue_chr[10];
char ambient_chr[10];
char cct_chr[10];
char prox_chr[10];
readAllColorAndProximityData(); readAllColorAndProximityData();
ambient = color_data.a/4;
sprintf(ambient_chr, "%u", color_data.a/4);
sprintf(red_chr, "%u", color_data.r);
sprintf(green_chr, "%u", color_data.g);
sprintf(blue_chr, "%u", color_data.b);
sprintf(prox_chr, "%u", color_data.p);
/* disableLightSensor(); /* disableLightSensor();
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ATIME, DEFAULT_ATIME); // reset to default I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ATIME, DEFAULT_ATIME); // reset to default
enableLightSensor();*/ enableLightSensor();*/
calculateColorTemperature(); // and calculate Lux calculateColorTemperature(); // and calculate Lux
sprintf(cct_chr, "%u", color_data.cct);
if (json) { if (json) {
ResponseAppend_P(PSTR(",\"%s\":{\"Red\":%s,\"Green\":%s,\"Blue\":%s,\"Ambient\":%s,\"CCT\":%s,\"Proximity\":%s}"), ResponseAppend_P(PSTR(",\"%s\":{\"Red\":%u,\"Green\":%u,\"Blue\":%u,\"Ambient\":%u,\"CCT\":%u,\"Proximity\":%u}"),
APDS9960_TAG, red_chr, green_chr, blue_chr, ambient_chr, cct_chr, prox_chr); APDS9960_TAG,
color_data.r,
color_data.g,
color_data.b,
ambient,
color_data.cct,
color_data.p);
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
} else { } else {
WSContentSend_PD(HTTP_APDS9960_SNS, red_chr, green_chr, blue_chr, ambient_chr, cct_chr, prox_chr); WSContentSend_PD(HTTP_SNS_COLOR_RED, APDS9960_TAG, color_data.r);
WSContentSend_PD(HTTP_SNS_COLOR_GREEN, APDS9960_TAG, color_data.g);
WSContentSend_PD(HTTP_SNS_COLOR_BLUE, APDS9960_TAG, color_data.b);
WSContentSend_PD(HTTP_SNS_ILLUMINANCE, APDS9960_TAG, ambient);
WSContentSend_PD(HTTP_SNS_CCT, APDS9960_TAG, color_data.cct);
WSContentSend_PD(HTTP_SNS_PROXIMITY, APDS9960_TAG, color_data.p);
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
#ifdef APDS9960_GESTURE
} else { } else {
if (json && (currentGesture[0] != '\0' )) { if (currentGesture[0] != '\0') {
ResponseAppend_P(PSTR(",\"%s\":{\"%s\":1}"), APDS9960_TAG, currentGesture); if (json) {
currentGesture[0] = '\0'; ResponseAppend_P(PSTR(",\"%s\":{\"%s\":1}"), APDS9960_TAG, currentGesture);
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_GESTURE, APDS9960_TAG, currentGesture);
#endif // USE_WEBSERVER
currentGesture[0] = '\0';
}
} }
#endif // APDS9960_GESTURE
} }
} }
@ -1882,11 +1939,14 @@ bool APDS9960CommandSensor(void) {
switch (XdrvMailbox.payload) { switch (XdrvMailbox.payload) {
case 0: // Off case 0: // Off
#ifdef APDS9960_GESTURE
disableGestureSensor(); disableGestureSensor();
#endif // APDS9960_GESTURE
gesture_mode = 0; gesture_mode = 0;
enableLightSensor(); enableLightSensor();
APDS9960_overload = false; // prevent unwanted re-enabling APDS9960_overload = false; // prevent unwanted re-enabling
break; break;
#ifdef APDS9960_GESTURE
case 1: // On with default gain of 4x case 1: // On with default gain of 4x
if (APDS9960type) { if (APDS9960type) {
setGestureGain(DEFAULT_GGAIN); setGestureGain(DEFAULT_GGAIN);
@ -1905,6 +1965,7 @@ bool APDS9960CommandSensor(void) {
gesture_mode = 1; gesture_mode = 1;
} }
break; break;
#endif // APDS9960_GESTURE
default: default:
int temp_aTime = (uint8_t)XdrvMailbox.payload; int temp_aTime = (uint8_t)XdrvMailbox.payload;
if (temp_aTime > 2 && temp_aTime < 256) { if (temp_aTime > 2 && temp_aTime < 256) {
@ -1933,9 +1994,11 @@ bool Xsns27(uint8_t function) {
APDS9960_detect(); APDS9960_detect();
} else if (APDS9960type) { } else if (APDS9960type) {
switch (function) { switch (function) {
#ifdef APDS9960_GESTURE
case FUNC_EVERY_50_MSECOND: case FUNC_EVERY_50_MSECOND:
APDS9960_loop(); APDS9960_loop();
break; break;
#endif // APDS9960_GESTURE
case FUNC_COMMAND_SENSOR: case FUNC_COMMAND_SENSOR:
if (XSNS_27 == XdrvMailbox.index) { if (XSNS_27 == XdrvMailbox.index) {
result = APDS9960CommandSensor(); result = APDS9960CommandSensor();