Add web server gesture display

Signed-off-by: Mickael Gaillard <mick.gaillard@gmail.com>
This commit is contained in:
Mickael Gaillard 2020-04-25 19:09:53 +02:00
parent 76f9ebfb72
commit 2ca2fc7cac
1 changed files with 73 additions and 42 deletions

View File

@ -61,6 +61,8 @@
#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 APDS9960_GESTURE
/* Gesture parameters */
#define GESTURE_THRESHOLD_OUT 10
#define GESTURE_SENSITIVITY_1 50
@ -72,13 +74,26 @@
const char APDS9960_TAG[] = "APDS9960";
#ifdef USE_WEBSERVER
const char HTTP_APDS9960_SNS[] PROGMEM =
"{s}" "Red" "{m}%s{e}"
"{s}" "Green" "{m}%s{e}"
"{s}" "Blue" "{m}%s{e}"
"{s}" "Ambient" "{m}%s " D_UNIT_LUX "{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>
#ifdef APDS9960_GESTURE
#define D_GESTURE "Gesture"
const char HTTP_SNS_GESTURE[] PROGMEM = "{s}%s " D_GESTURE "{m}%s{e}";
#endif // APDS9960_GESTURE
#define D_COLOR_RED "Red"
#define D_COLOR_GREEN "Green"
#define D_COLOR_BLUE "Blue"
#define D_PROXIMITY "Proximity"
#define D_CCT "CCT"
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
/*********************************************************************************************\
@ -236,7 +251,15 @@ const char HTTP_APDS9960_SNS[] PROGMEM =
#define APDS9960_ERROR 0xFF
#ifdef APDS9960_GESTURE
/* 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 {
DIR_NONE,
DIR_LEFT,
@ -249,12 +272,14 @@ enum {
};
/* State definitions*/
/*
enum {
APDS9960_NA_STATE,
APDS9960_NEAR_STATE,
APDS9960_FAR_STATE,
APDS9960_ALL_STATE
};
*/
/* Container for gesture data */
typedef struct gesture_data_type {
@ -268,8 +293,7 @@ typedef struct gesture_data_type {
uint8_t out_threshold;
} gesture_data_t;
typedef struct gesture_type
{
typedef struct gesture_type {
int16_t ud_delta_ = 0;
int16_t lr_delta_ = 0;
int16_t ud_count_ = 0;
@ -278,6 +302,8 @@ typedef struct gesture_type
int16_t motion_ = DIR_NONE;
} gesture_t;
#endif // APDS9960_GESTURE
typedef struct color_data_type {
uint16_t a; // measured ambient
uint16_t r; // Red
@ -1451,7 +1477,7 @@ int16_t readGesture(void) {
fifo_level = I2cRead8(APDS9960_I2C_ADDR, APDS9960_GFLVL);
#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
/* If there's stuff in the FIFO, read it into our data block */
@ -1705,28 +1731,28 @@ void handleGesture(void) {
if (isGestureAvailable()) {
switch (readGesture()) {
case DIR_UP:
AddLog_P(LOG_LEVEL_DEBUG, PSTR("UP"));
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Up"));
AddLog_P(LOG_LEVEL_DEBUG, GESTURE_UP);
snprintf_P(currentGesture, sizeof(currentGesture), GESTURE_UP);
break;
case DIR_DOWN:
AddLog_P(LOG_LEVEL_DEBUG, PSTR("DOWN"));
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Down"));
AddLog_P(LOG_LEVEL_DEBUG, GESTURE_DOWN);
snprintf_P(currentGesture, sizeof(currentGesture), GESTURE_DOWN);
break;
case DIR_LEFT:
AddLog_P(LOG_LEVEL_DEBUG, PSTR("LEFT"));
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Left"));
AddLog_P(LOG_LEVEL_DEBUG, GESTURE_LEFT);
snprintf_P(currentGesture, sizeof(currentGesture), GESTURE_LEFT);
break;
case DIR_RIGHT:
AddLog_P(LOG_LEVEL_DEBUG, PSTR("RIGHT"));
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Right"));
AddLog_P(LOG_LEVEL_DEBUG, GESTURE_RIGHT);
snprintf_P(currentGesture, sizeof(currentGesture), GESTURE_RIGHT);
break;
default:
if (APDS9960_overload) {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("LONG"));
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("Long"));
AddLog_P(LOG_LEVEL_DEBUG, GESTURE_LONG);
snprintf_P(currentGesture, sizeof(currentGesture), GESTURE_LONG);
} else {
AddLog_P(LOG_LEVEL_DEBUG, PSTR("NONE"));
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("None"));
AddLog_P(LOG_LEVEL_DEBUG, GESTURE_NONE);
snprintf_P(currentGesture, sizeof(currentGesture), GESTURE_NONE);
}
break;
}
@ -1828,40 +1854,45 @@ void APDS9960_show(bool json) {
if (!APDS9960type) { return; }
if (!gesture_mode && !APDS9960_overload) {
char red_chr[10];
char green_chr[10];
char blue_chr[10];
char ambient_chr[10];
char cct_chr[10];
char prox_chr[10];
uint16_t ambient;
readAllColorAndProximityData();
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);
ambient = color_data.a/4;
/* disableLightSensor();
I2cWrite8(APDS9960_I2C_ADDR, APDS9960_ATIME, DEFAULT_ATIME); // reset to default
enableLightSensor();*/
calculateColorTemperature(); // and calculate Lux
sprintf(cct_chr, "%u", color_data.cct);
if (json) {
ResponseAppend_P(PSTR(",\"%s\":{\"Red\":%s,\"Green\":%s,\"Blue\":%s,\"Ambient\":%s,\"CCT\":%s,\"Proximity\":%s}"),
APDS9960_TAG, red_chr, green_chr, blue_chr, ambient_chr, cct_chr, prox_chr);
ResponseAppend_P(PSTR(",\"%s\":{\"Red\":%u,\"Green\":%u,\"Blue\":%u,\"Ambient\":%u,\"CCT\":%s,\"Proximity\":%u}"),
APDS9960_TAG,
color_data.r,
color_data.g,
color_data.b,
ambient,
color_data.cct,
color_data.p);
#ifdef USE_WEBSERVER
} 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
}
} else {
if (json && (currentGesture[0] != '\0' )) {
ResponseAppend_P(PSTR(",\"%s\":{\"%s\":1}"), APDS9960_TAG, currentGesture);
currentGesture[0] = '\0';
if (currentGesture[0] != '\0') {
if (json) {
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';
}
}
}
}