Renamed file and updated APDS9960 sensor

Renamed file and updated APDS9960 sensor
This commit is contained in:
Theo Arends 2018-06-26 22:40:13 +02:00
parent adc4e99887
commit a2728863c3

View File

@ -1,5 +1,5 @@
/* /*
xdrv_13_apds9960.ino - Support for I2C APDS9960 Proximity Sensor for Sonoff-Tasmota xsns_27_apds9960.ino - Support for I2C APDS9960 Proximity Sensor for Sonoff-Tasmota
Copyright (C) 2018 Shawn Hymel/Sparkfun and Theo Arends Copyright (C) 2018 Shawn Hymel/Sparkfun and Theo Arends
@ -27,6 +27,9 @@
#ifdef USE_I2C #ifdef USE_I2C
#ifdef USE_APDS9960 #ifdef USE_APDS9960
#define XSNS_27 27
/*********************************************************************************************\ /*********************************************************************************************\
* APDS9960 - Digital Proximity Ambient Light RGB and Gesture Sensor * APDS9960 - Digital Proximity Ambient Light RGB and Gesture Sensor
* *
@ -43,8 +46,7 @@
#endif #endif
#ifdef USE_VEML6070 #ifdef USE_VEML6070
#undef USE_VEML6070 // address conflict on the I2C-bus #undef USE_VEML6070 // address conflict on the I2C-bus
#endif #endif
#endif #endif
#define APDS9960_I2C_ADDR 0x39 #define APDS9960_I2C_ADDR 0x39
@ -57,17 +59,11 @@
#define GESTURE_SENSITIVITY_1 50 #define GESTURE_SENSITIVITY_1 50
#define GESTURE_SENSITIVITY_2 20 #define GESTURE_SENSITIVITY_2 20
enum GestureCommands {
CMND_GESTURE };
const char kGestureCommands[] PROGMEM =
"Gesture" ;
uint8_t APDS9960addr; uint8_t APDS9960addr;
uint8_t APDS9960type = 0; uint8_t APDS9960type = 0;
char APDS9960stype[7]; char APDS9960stype[7];
char currentGesture[6]; char currentGesture[6];
bool gesture_mode = true; uint8_t gesture_mode = 1;
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
#define APDS9960_LONG_RECOVERY 50 //long pause after sensor overload in loops #define APDS9960_LONG_RECOVERY 50 //long pause after sensor overload in loops
@ -1963,7 +1959,7 @@ void APDS9960_loop()
APDS9960_overload = false; APDS9960_overload = false;
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"On\"}")); snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"On\"}"));
MqttPublishPrefixTopic_P(RESULT_OR_TELE, mqtt_data); // only after the long break we report, that we are online again MqttPublishPrefixTopic_P(RESULT_OR_TELE, mqtt_data); // only after the long break we report, that we are online again
gesture_mode = true; gesture_mode = 1;
} }
if (gesture_mode) { if (gesture_mode) {
@ -1976,48 +1972,38 @@ void APDS9960_loop()
recovery_loop_counter = APDS9960_LONG_RECOVERY; // long pause after overload/long press - number of stateloops recovery_loop_counter = APDS9960_LONG_RECOVERY; // long pause after overload/long press - number of stateloops
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"Off\"}")); snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"Off\"}"));
MqttPublishPrefixTopic_P(RESULT_OR_TELE, mqtt_data); MqttPublishPrefixTopic_P(RESULT_OR_TELE, mqtt_data);
gesture_mode = false; gesture_mode = 0;
} }
} }
} }
} }
bool APDS9960_detect(void) bool APDS9960_detect(void)
{ {
if (APDS9960type) { if (APDS9960type) {
return true; return true;
} }
boolean success = false; boolean success = false;
char log[LOGSZ];
APDS9960type = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ID); APDS9960type = I2cRead8(APDS9960_I2C_ADDR, APDS9960_ID);
if (APDS9960type == APDS9960_CHIPID_1 || APDS9960type == APDS9960_CHIPID_2) if (APDS9960type == APDS9960_CHIPID_1 || APDS9960type == APDS9960_CHIPID_2) {
{
strcpy_P(APDS9960stype, PSTR("APDS9960")); strcpy_P(APDS9960stype, PSTR("APDS9960"));
snprintf_P(log, sizeof(log), PSTR("I2C: %s found at address 0x%x"), APDS9960stype, APDS9960_I2C_ADDR); snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, APDS9960stype, APDS9960_I2C_ADDR);
AddLog_P(LOG_LEVEL_DEBUG, log); AddLog(LOG_LEVEL_DEBUG);
if (APDS9960_init()){ if (APDS9960_init()) {
success = true; success = true;
snprintf_P(log, sizeof(log), PSTR("APDS9960 initialized")); AddLog_P(LOG_LEVEL_DEBUG, PSTR(D_LOG_DEBUG "APDS9960 initialized"));
AddLog_P(LOG_LEVEL_DEBUG, log);
enableGestureSensor(false); enableGestureSensor(false);
} }
} } else {
else snprintf_P(log_data, sizeof(log_data), PSTR("APDS9960 not found at address 0x%x"), APDS9960_I2C_ADDR);
{ AddLog(LOG_LEVEL_DEBUG);
snprintf_P(log, sizeof(log), PSTR("APDS9960 not found at address 0x%x"), APDS9960_I2C_ADDR);
AddLog_P(LOG_LEVEL_DEBUG, log);
} }
currentGesture[0] = '\0'; currentGesture[0] = '\0';
return success; return success;
} }
/*********************************************************************************************\ /*********************************************************************************************\
* Presentation * Presentation
\*********************************************************************************************/ \*********************************************************************************************/
@ -2027,135 +2013,108 @@ void APDS9960_show(boolean json)
if (!APDS9960type) { if (!APDS9960type) {
return; return;
} }
if (!gesture_mode) if (!gesture_mode) {
{ char red_chr[10];
char red_chr[10]; char green_chr[10];
char green_chr[10]; char blue_chr[10];
char blue_chr[10]; char ambient_chr[10];
char ambient_chr[10]; char prox_chr[10];
char prox_chr[10]; uint16_t val;
uint16_t val; uint8_t val_prox;
uint8_t val_prox;
readRedLight(val);
sprintf (red_chr, "%u", val);
readGreenLight(val);
sprintf (green_chr, "%u", val);
readBlueLight(val);
sprintf (blue_chr, "%u", val );
readAmbientLight(val);
sprintf (ambient_chr, "%u", val);
readRedLight(val); readProximity(val_prox);
sprintf (red_chr, "%u", val); sprintf (prox_chr, "%u", val_prox );
readGreenLight(val);
sprintf (green_chr, "%u", val);
readBlueLight(val);
sprintf (blue_chr, "%u", val );
readAmbientLight(val);
sprintf (ambient_chr, "%u", val );
readProximity(val_prox); if (json) {
sprintf (prox_chr, "%u", val_prox ); snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"Red\":%s,\"Green\":%s,\"Blue\":%s,\"Ambient\":%s,\"Proximity\":%s}"),
mqtt_data, APDS9960stype, red_chr, green_chr, blue_chr, ambient_chr, prox_chr);
if (json) { #ifdef USE_WEBSERVER
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"Red\":%s, \"Green\":%s, \"Blue\":%s, \"Ambient\":%s, \"Proximity\":%s}"), } else {
mqtt_data, APDS9960stype, red_chr, green_chr, blue_chr, ambient_chr, prox_chr);
}
#ifdef USE_WEBSERVER
else{
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_APDS_9960_SNS, mqtt_data, red_chr, green_chr, blue_chr, ambient_chr, prox_chr ); snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_APDS_9960_SNS, mqtt_data, red_chr, green_chr, blue_chr, ambient_chr, prox_chr );
} #endif // USE_WEBSERVER
#endif // USE_WEBSERVER
} }
else{
if (json && (currentGesture[0] != '\0' )) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"%s\":1}"), mqtt_data, APDS9960stype, currentGesture);
currentGesture[0] = '\0';
}
}
}
/*********************************************************************************************\
* Commands
\*********************************************************************************************/
boolean apds9960_command()
{
char command [CMDSZ];
boolean serviced = true;
int command_code = GetCommandCode(command, sizeof(command), XdrvMailbox.topic, kGestureCommands);
if (-1 == command_code) {
serviced = false; // Unknown command
return serviced;
}
if (CMND_GESTURE == command_code) {
switch (XdrvMailbox.payload) {
case 0: // Off
disableGestureSensor();
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"Off\"}"));
gesture_mode = false;
enableLightSensor(false);
enableProximitySensor(false);
break;
case 1: // On
if (APDS9960type) {
disableLightSensor();
disableProximitySensor();
enableGestureSensor(false);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"on\"}"));
gesture_mode = true;
}
default: // get status
if(gesture_mode){
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"on\"}"));
}
else{
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"Off\"}"));
}
}
} }
else { else {
serviced = false; // Unknown command if (json && (currentGesture[0] != '\0' )) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"%s\":1}"), mqtt_data, APDS9960stype, currentGesture);
currentGesture[0] = '\0';
}
} }
}
/*********************************************************************************************\
* Command Sensor27
*
* Command | Payload | Description
* ---------|---------|--------------------------
* Sensor27 | | Show current gesture mode
* Sensor27 | 0 / Off | Disable gesture mode
* Sensor27 | 1 / On | Enable gesture mode
\*********************************************************************************************/
bool APDS9960CommandSensor()
{
boolean serviced = true;
switch (XdrvMailbox.payload) {
case 0: // Off
disableGestureSensor();
gesture_mode = 0;
enableLightSensor(false);
enableProximitySensor(false);
break;
case 1: // On
if (APDS9960type) {
disableLightSensor();
disableProximitySensor();
enableGestureSensor(false);
gesture_mode = 1;
}
}
snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_SENSOR_INDEX_SVALUE, XSNS_27, GetStateText(gesture_mode));
return serviced; return serviced;
} }
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
#define XDRV_91 boolean Xsns27(byte function)
boolean Xdrv91(byte function)
{ {
boolean result = false; boolean result = false;
if (i2c_flg) { if (i2c_flg) {
switch (function) { if (FUNC_INIT == function) {
case FUNC_INIT: APDS9960_detect();
APDS9960_detect(); } else if (APDS9960type) {
break; switch (function) {
case FUNC_EVERY_50_MSECOND:
APDS9960_loop();
break;
case FUNC_COMMAND: case FUNC_COMMAND:
result = apds9960_command(); if (XSNS_27 == XdrvMailbox.index) {
break; result = APDS9960CommandSensor();
} }
} break;
return result; case FUNC_JSON_APPEND:
} APDS9960_show(1);
break;
#define XSNS_91
boolean Xsns91(byte function)
{
boolean result = false;
if (APDS9960type) {
switch (function) {
case FUNC_EVERY_50_MSECOND:
APDS9960_loop();
break;
case FUNC_JSON_APPEND:
APDS9960_show(1);
break;
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
case FUNC_WEB_APPEND: case FUNC_WEB_APPEND:
APDS9960_show(0); APDS9960_show(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
}
} }
} }
return result; return result;