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,8 +2013,7 @@ 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];
@ -2037,7 +2022,6 @@ void APDS9960_show(boolean json)
uint16_t val; uint16_t val;
uint8_t val_prox; uint8_t val_prox;
readRedLight(val); readRedLight(val);
sprintf (red_chr, "%u", val); sprintf (red_chr, "%u", val);
readGreenLight(val); readGreenLight(val);
@ -2045,50 +2029,46 @@ void APDS9960_show(boolean json)
readBlueLight(val); readBlueLight(val);
sprintf (blue_chr, "%u", val ); sprintf (blue_chr, "%u", val );
readAmbientLight(val); readAmbientLight(val);
sprintf (ambient_chr, "%u", val ); sprintf (ambient_chr, "%u", val);
readProximity(val_prox); readProximity(val_prox);
sprintf (prox_chr, "%u", val_prox ); sprintf (prox_chr, "%u", val_prox );
if (json) { if (json) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"Red\":%s, \"Green\":%s, \"Blue\":%s, \"Ambient\":%s, \"Proximity\":%s}"), 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); mqtt_data, APDS9960stype, red_chr, green_chr, blue_chr, ambient_chr, prox_chr);
} #ifdef USE_WEBSERVER
} else {
#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{ else {
if (json && (currentGesture[0] != '\0' )) { if (json && (currentGesture[0] != '\0' )) {
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"%s\":1}"), mqtt_data, APDS9960stype, currentGesture); snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"%s\":1}"), mqtt_data, APDS9960stype, currentGesture);
currentGesture[0] = '\0'; currentGesture[0] = '\0';
} }
}
} }
}
/*********************************************************************************************\ /*********************************************************************************************\
* Commands * Command Sensor27
*
* Command | Payload | Description
* ---------|---------|--------------------------
* Sensor27 | | Show current gesture mode
* Sensor27 | 0 / Off | Disable gesture mode
* Sensor27 | 1 / On | Enable gesture mode
\*********************************************************************************************/ \*********************************************************************************************/
boolean apds9960_command() bool APDS9960CommandSensor()
{ {
char command [CMDSZ];
boolean serviced = true; 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) { switch (XdrvMailbox.payload) {
case 0: // Off case 0: // Off
disableGestureSensor(); disableGestureSensor();
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"Off\"}")); gesture_mode = 0;
gesture_mode = false;
enableLightSensor(false); enableLightSensor(false);
enableProximitySensor(false); enableProximitySensor(false);
break; break;
@ -2097,57 +2077,35 @@ boolean apds9960_command()
disableLightSensor(); disableLightSensor();
disableProximitySensor(); disableProximitySensor();
enableGestureSensor(false); enableGestureSensor(false);
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("{\"Gesture\":\"on\"}")); gesture_mode = 1;
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\"}"));
} }
} }
} snprintf_P(mqtt_data, sizeof(mqtt_data), S_JSON_SENSOR_INDEX_SVALUE, XSNS_27, GetStateText(gesture_mode));
else {
serviced = false; // Unknown command
}
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();
break; } else if (APDS9960type) {
case FUNC_COMMAND:
result = apds9960_command();
break;
}
}
return result;
}
#define XSNS_91
boolean Xsns91(byte function)
{
boolean result = false;
if (APDS9960type) {
switch (function) { switch (function) {
case FUNC_EVERY_50_MSECOND: case FUNC_EVERY_50_MSECOND:
APDS9960_loop(); APDS9960_loop();
break; break;
case FUNC_COMMAND:
if (XSNS_27 == XdrvMailbox.index) {
result = APDS9960CommandSensor();
}
break;
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
APDS9960_show(1); APDS9960_show(1);
break; break;
@ -2158,6 +2116,7 @@ boolean Xsns91(byte function)
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
} }
}
return result; return result;
} }
#endif // USE_APDS9960 #endif // USE_APDS9960