Refactor I2C driver detection phase 5

This commit is contained in:
Theo Arends 2019-11-10 17:02:02 +01:00
parent 69af996ca1
commit b2e6a2fe75
12 changed files with 378 additions and 504 deletions

View File

@ -790,6 +790,14 @@ bool MqttShowSensor(void)
return json_data_available; return json_data_available;
} }
void MqttPublishSensor(void)
{
mqtt_data[0] = '\0';
if (MqttShowSensor()) {
MqttPublishTeleSensor();
}
}
/********************************************************************************************/ /********************************************************************************************/
void PerformEverySecond(void) void PerformEverySecond(void)
@ -843,14 +851,8 @@ void PerformEverySecond(void)
tele_period = 0; tele_period = 0;
MqttPublishTeleState(); MqttPublishTeleState();
MqttPublishSensor();
mqtt_data[0] = '\0';
if (MqttShowSensor()) {
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); // CMND_SENSORRETAIN
#if defined(USE_RULES) || defined(USE_SCRIPT)
RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES
}
XdrvCall(FUNC_AFTER_TELEPERIOD); XdrvCall(FUNC_AFTER_TELEPERIOD);
} }
} }

View File

@ -429,6 +429,14 @@ void MqttPublishPrefixTopic_P(uint32_t prefix, const char* subtopic)
MqttPublishPrefixTopic_P(prefix, subtopic, false); MqttPublishPrefixTopic_P(prefix, subtopic, false);
} }
void MqttPublishTeleSensor(void)
{
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); // CMND_SENSORRETAIN
#if defined(USE_RULES) || defined(USE_SCRIPT)
RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES
}
void MqttPublishPowerState(uint32_t device) void MqttPublishPowerState(uint32_t device)
{ {
char stopic[TOPSZ]; char stopic[TOPSZ];

View File

@ -1810,14 +1810,7 @@ void handleGesture(void) {
snprintf_P(currentGesture, sizeof(currentGesture), PSTR("None")); snprintf_P(currentGesture, sizeof(currentGesture), PSTR("None"));
} }
} }
MqttPublishSensor();
mqtt_data[0] = '\0';
if (MqttShowSensor()) {
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); // CMND_SENSORRETAIN
#ifdef USE_RULES
RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES
}
} }
} }

View File

@ -33,8 +33,8 @@
#include "Adafruit_CCS811.h" #include "Adafruit_CCS811.h"
Adafruit_CCS811 ccs; Adafruit_CCS811 ccs;
uint8_t CCS811_ready; uint8_t CCS811_ready = 0;
uint8_t CCS811_type; uint8_t CCS811_type = 0;;
uint16_t eCO2; uint16_t eCO2;
uint16_t TVOC; uint16_t TVOC;
uint8_t tcnt = 0; uint8_t tcnt = 0;
@ -45,6 +45,8 @@ uint8_t ecnt = 0;
void CCS811Update(void) // Perform every n second void CCS811Update(void) // Perform every n second
{ {
if (I2cActive(CCS811_ADDRESS)) { return; }
tcnt++; tcnt++;
if (tcnt >= EVERYNSECONDS) { if (tcnt >= EVERYNSECONDS) {
tcnt = 0; tcnt = 0;
@ -53,7 +55,7 @@ void CCS811Update(void) // Perform every n second
sint8_t res = ccs.begin(CCS811_ADDRESS); sint8_t res = ccs.begin(CCS811_ADDRESS);
if (!res) { if (!res) {
CCS811_type = 1; CCS811_type = 1;
AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "CCS811", 0x5A); I2cSetActiveFound(CCS811_ADDRESS, "CCS811");
} else { } else {
//AddLog_P2(LOG_LEVEL_DEBUG, "CCS811 init failed: %d",res); //AddLog_P2(LOG_LEVEL_DEBUG, "CCS811 init failed: %d",res);
} }

View File

@ -67,6 +67,8 @@ MPU6050 mpu6050;
void MPU_6050PerformReading(void) void MPU_6050PerformReading(void)
{ {
if (!MPU_6050_found) { return; }
#ifdef USE_MPU6050_DMP #ifdef USE_MPU6050_DMP
mpu6050.resetFIFO(); // with a default dampling rate of 200Hz, we create a delay of approx. 5ms with a complete read cycle mpu6050.resetFIFO(); // with a default dampling rate of 200Hz, we create a delay of approx. 5ms with a complete read cycle
MPU6050_dmp.fifoCount = mpu6050.getFIFOCount(); MPU6050_dmp.fifoCount = mpu6050.getFIFOCount();
@ -140,12 +142,11 @@ void MPU_6050Detect(void)
MPU_6050_found = mpu6050.testConnection(); MPU_6050_found = mpu6050.testConnection();
#endif //USE_MPU6050_DMP #endif //USE_MPU6050_DMP
Settings.flag2.axis_resolution = 2; // Need to be services by command Sensor32 Settings.flag2.axis_resolution = 2; // Need to be services by command Sensor32
} }
if (MPU_6050_found) if (MPU_6050_found)
{ {
AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, D_SENSOR_MPU6050, MPU_6050_address); I2cSetActiveFound(MPU_6050_address, D_SENSOR_MPU6050);
} }
} }
@ -168,7 +169,8 @@ const char HTTP_SNS_AXIS[] PROGMEM =
void MPU_6050Show(bool json) void MPU_6050Show(bool json)
{ {
if (MPU_6050_found) { if (!MPU_6050_found) { return; }
MPU_6050PerformReading(); MPU_6050PerformReading();
double tempConv = (MPU_6050_temperature / 340.0 + 35.53); double tempConv = (MPU_6050_temperature / 340.0 + 35.53);
@ -212,7 +214,6 @@ void MPU_6050Show(bool json)
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
} }
}
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface

View File

@ -64,7 +64,7 @@
#define CENTURY 7 //Century bit in Month register #define CENTURY 7 //Century bit in Month register
#define DYDT 6 //Day/Date flag bit in alarm Day/Date registers #define DYDT 6 //Day/Date flag bit in alarm Day/Date registers
bool ds3231ReadStatus = false; bool ds3231ReadStatus = false;
bool ds3231WriteStatus = false; //flag, we want to wriet/write to DS3231 onlu once bool ds3231WriteStatus = false; //flag, we want to read/write to DS3231 only once
bool DS3231chipDetected = false; bool DS3231chipDetected = false;
/*----------------------------------------------------------------------* /*----------------------------------------------------------------------*
@ -72,12 +72,11 @@ bool DS3231chipDetected = false;
----------------------------------------------------------------------*/ ----------------------------------------------------------------------*/
void DS3231Detect(void) void DS3231Detect(void)
{ {
DS3231chipDetected = false; if (DS3231chipDetected || I2cActive(USE_RTC_ADDR)) { return; }
if (I2cValidRead(USE_RTC_ADDR, RTC_STATUS, 1)) { if (I2cValidRead(USE_RTC_ADDR, RTC_STATUS, 1)) {
AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "DS3231", USE_RTC_ADDR); I2cSetActiveFound(USE_RTC_ADDR, "DS3231");
DS3231chipDetected = true; DS3231chipDetected = true;
} else {
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_I2C "DS3231 NOT " D_FOUND_AT " 0x%x"), USE_RTC_ADDR);
} }
} }
@ -128,23 +127,12 @@ void SetDS3231Time (uint32_t epoch_time) {
I2cWrite8(USE_RTC_ADDR, RTC_STATUS, I2cRead8(USE_RTC_ADDR, RTC_STATUS) & ~_BV(OSF)); //clear the Oscillator Stop Flag I2cWrite8(USE_RTC_ADDR, RTC_STATUS, I2cRead8(USE_RTC_ADDR, RTC_STATUS) & ~_BV(OSF)); //clear the Oscillator Stop Flag
} }
/*********************************************************************************************\ void DS3231EverySecond(void)
Interface
\*********************************************************************************************/
bool Xsns33(uint8_t function)
{ {
if (!I2cEnabled(XI2C_26)) { return false; } if (!DS3231chipDetected) { return; }
bool result = false;
switch (function) {
case FUNC_INIT:
DS3231Detect();
break;
case FUNC_EVERY_SECOND:
TIME_T tmpTime; TIME_T tmpTime;
if (!ds3231ReadStatus && DS3231chipDetected && Rtc.utc_time < START_VALID_TIME ) { // We still did not sync with NTP (time not valid) , so, read time from DS3231 if (!ds3231ReadStatus && Rtc.utc_time < START_VALID_TIME ) { // We still did not sync with NTP (time not valid) , so, read time from DS3231
ntp_force_sync = true; //force to sync with ntp ntp_force_sync = true; //force to sync with ntp
Rtc.utc_time = ReadFromDS3231(); //we read UTC TIME from DS3231 Rtc.utc_time = ReadFromDS3231(); //we read UTC TIME from DS3231
// from this line, we just copy the function from "void RtcSecond()" at the support.ino ,line 2143 and above // from this line, we just copy the function from "void RtcSecond()" at the support.ino ,line 2143 and above
@ -164,12 +152,30 @@ bool Xsns33(uint8_t function)
rules_flag.time_set = 1; rules_flag.time_set = 1;
} }
} }
else if (!ds3231WriteStatus && DS3231chipDetected && Rtc.utc_time > START_VALID_TIME && abs(Rtc.utc_time - ReadFromDS3231()) > 60) {//if time is valid and is drift from RTC in more that 60 second else if (!ds3231WriteStatus && Rtc.utc_time > START_VALID_TIME && abs(Rtc.utc_time - ReadFromDS3231()) > 60) {//if time is valid and is drift from RTC in more that 60 second
AddLog_P2(LOG_LEVEL_INFO, PSTR("Write Time TO DS3231 from NTP (" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"), AddLog_P2(LOG_LEVEL_INFO, PSTR("Write Time TO DS3231 from NTP (" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"),
GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str()); GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str());
SetDS3231Time (Rtc.utc_time); //update the DS3231 time SetDS3231Time (Rtc.utc_time); //update the DS3231 time
ds3231WriteStatus = true; ds3231WriteStatus = true;
} }
}
/*********************************************************************************************\
Interface
\*********************************************************************************************/
bool Xsns33(uint8_t function)
{
if (!I2cEnabled(XI2C_26)) { return false; }
bool result = false;
switch (function) {
case FUNC_EVERY_SECOND:
DS3231EverySecond();
break;
case FUNC_INIT:
DS3231Detect();
break; break;
} }
return result; return result;

View File

@ -44,7 +44,7 @@
bool MGC3130_type = false; bool MGC3130_type = false;
char MGC3130stype[8]; char MGC3130stype[] = "MGC3130";
#define MGC3130_SYSTEM_STATUS 0x15 #define MGC3130_SYSTEM_STATUS 0x15
@ -198,39 +198,29 @@ uint8_t MGC3130autoCal[] = {0x10, 0x00, 0x00, 0xA2, 0x80, 0x00 , 0x00, 0x00, 0x0
uint8_t MGC3130disableAirwheel[] = {0x10, 0x00, 0x00, 0xA2, 0x90, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00}; uint8_t MGC3130disableAirwheel[] = {0x10, 0x00, 0x00, 0xA2, 0x90, 0x00 , 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00};
uint8_t MGC3130enableAirwheel[] = {0x10, 0x00, 0x00, 0xA2, 0x90, 0x00 , 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00}; uint8_t MGC3130enableAirwheel[] = {0x10, 0x00, 0x00, 0xA2, 0x90, 0x00 , 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00};
void MGC3130_triggerTele(){
mqtt_data[0] = '\0';
if (MqttShowSensor()) {
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); // CMND_SENSORRETAIN
#ifdef USE_RULES
RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES
}
}
void MGC3130_handleSensorData(){ void MGC3130_handleSensorData(){
if ( MGC_data.out.outputConfigMask.touchInfo && MGC3130_touchTimeout == 0){ if ( MGC_data.out.outputConfigMask.touchInfo && MGC3130_touchTimeout == 0){
if (MGC3130_handleTouch()){ if (MGC3130_handleTouch()){
MGC3130_triggeredByTouch = true; MGC3130_triggeredByTouch = true;
MGC3130_triggerTele(); MqttPublishSensor();
} }
} }
if(MGC3130_mode == 1){ if(MGC3130_mode == 1){
if( MGC_data.out.outputConfigMask.gestureInfo && MGC_data.out.gestureInfo.gestureCode > 0){ if( MGC_data.out.outputConfigMask.gestureInfo && MGC_data.out.gestureInfo.gestureCode > 0){
MGC3130_handleGesture(); MGC3130_handleGesture();
MGC3130_triggerTele(); MqttPublishSensor();
} }
} }
if(MGC3130_mode == 2){ if(MGC3130_mode == 2){
if(MGC_data.out.outputConfigMask.airWheelInfo && MGC_data.out.systemInfo.airWheelValid){ if(MGC_data.out.outputConfigMask.airWheelInfo && MGC_data.out.systemInfo.airWheelValid){
MGC3130_handleAirWheel(); MGC3130_handleAirWheel();
MGC3130_triggerTele(); MqttPublishSensor();
} }
} }
if(MGC3130_mode == 3){ if(MGC3130_mode == 3){
if(MGC_data.out.systemInfo.positionValid && (MGC_data.out.z > MGC3130_MIN_ZVALUE)){ if(MGC_data.out.systemInfo.positionValid && (MGC_data.out.z > MGC3130_MIN_ZVALUE)){
MGC3130_triggerTele(); MqttPublishSensor();
} }
} }
} }
@ -484,12 +474,9 @@ void MGC3130_loop()
MGC3130_receiveMessage(); MGC3130_receiveMessage();
} }
void MGC3130_detect(void)
bool MGC3130_detect(void)
{ {
if (MGC3130_type){ if (MGC3130_type || I2cActive(MGC3130_I2C_ADDR)) { return; }
return true;
}
pinMode(MGC3130_xfer, INPUT_PULLUP); pinMode(MGC3130_xfer, INPUT_PULLUP);
pinMode(MGC3130_reset, OUTPUT); pinMode(MGC3130_reset, OUTPUT);
@ -498,17 +485,11 @@ bool MGC3130_detect(void)
digitalWrite(MGC3130_reset, HIGH); digitalWrite(MGC3130_reset, HIGH);
delay(50); delay(50);
bool success = false; if (MGC3130_receiveMessage()) { // This should read the firmware info
success = MGC3130_receiveMessage(); // This should read the firmware info I2cSetActiveFound(MGC3130_I2C_ADDR, MGC3130stype);
if (success) {
strcpy_P(MGC3130stype, PSTR("MGC3130"));
AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, MGC3130stype, MGC3130_I2C_ADDR);
MGC3130_currentGesture[0] = '\0'; MGC3130_currentGesture[0] = '\0';
MGC3130_type = true; MGC3130_type = true;
} else {
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MGC3130 did not respond at address 0x%x"), MGC3130_I2C_ADDR);
} }
return success;
} }
/*********************************************************************************************\ /*********************************************************************************************\

View File

@ -66,18 +66,14 @@ bool Max4409Read_lum(void)
void Max4409Detect(void) void Max4409Detect(void)
{ {
uint8_t reg[8]; if (max44009_found) { return; }
bool failed = false;
if (max44009_found) {
return;
}
uint8_t buffer1; uint8_t buffer1;
uint8_t buffer2; uint8_t buffer2;
for (uint32_t i = 0; 0 != max44009_addresses[i]; i++) { for (uint32_t i = 0; 0 != max44009_addresses[i]; i++) {
max44009_address = max44009_addresses[i]; max44009_address = max44009_addresses[i];
if (I2cActive(max44009_address)) { continue; }
if ((I2cValidRead8(&buffer1, max44009_address, REG_LOWER_THRESHOLD)) && if ((I2cValidRead8(&buffer1, max44009_address, REG_LOWER_THRESHOLD)) &&
(I2cValidRead8(&buffer2, max44009_address, REG_THRESHOLD_TIMER))) { (I2cValidRead8(&buffer2, max44009_address, REG_THRESHOLD_TIMER))) {
@ -94,8 +90,8 @@ void Max4409Detect(void)
Wire.write(REG_CONFIG); Wire.write(REG_CONFIG);
Wire.write(MAX44009_CONTINUOUS_AUTO_MODE); Wire.write(MAX44009_CONTINUOUS_AUTO_MODE);
if (0 == Wire.endTransmission()) { if (0 == Wire.endTransmission()) {
I2cSetActiveFound(max44009_address, max44009_types);
max44009_found = 1; max44009_found = 1;
AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, max44009_types, max44009_address);
break; break;
} }
} }
@ -155,9 +151,6 @@ bool Xsns41(uint8_t function)
bool result = false; bool result = false;
switch (function) { switch (function) {
case FUNC_INIT:
Max4409Detect();
break;
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
Max4409EverySecond(); Max4409EverySecond();
break; break;
@ -169,6 +162,9 @@ bool Xsns41(uint8_t function)
Max4409Show(0); Max4409Show(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_INIT:
Max4409Detect();
break;
} }
return result; return result;
} }

View File

@ -42,7 +42,7 @@ void Vl53l0Detect(void)
if (!sensor.init()) { return; } if (!sensor.init()) { return; }
AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "VL53L0X", sensor.getAddress()); I2cSetActiveFound(sensor.getAddress(), "VL53L0X");
sensor.setTimeout(500); sensor.setTimeout(500);

View File

@ -25,7 +25,13 @@
#define I2_ADR_IRT 0x5a #define I2_ADR_IRT 0x5a
uint8_t mlx_ready; #define MLX90614_RAWIR1 0x04
#define MLX90614_RAWIR2 0x05
#define MLX90614_TA 0x06
#define MLX90614_TOBJ1 0x07
#define MLX90614_TOBJ2 0x08
bool mlx_ready = false;
float obj_temp; float obj_temp;
float amb_temp; float amb_temp;
@ -33,44 +39,40 @@ void MLX90614_Init(void)
{ {
if (!I2cSetDevice(I2_ADR_IRT)) { return; } if (!I2cSetDevice(I2_ADR_IRT)) { return; }
mlx_ready=1; I2cSetActiveFound(I2_ADR_IRT, "MLX90614");
AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "MLX90614", I2_ADR_IRT); mlx_ready = true;
// not needed on tasmota // not needed on tasmota
//Wire.begin(); //Wire.begin();
//delay(500); //delay(500);
} }
#define MLX90614_RAWIR1 0x04
#define MLX90614_RAWIR2 0x05
#define MLX90614_TA 0x06
#define MLX90614_TOBJ1 0x07
#define MLX90614_TOBJ2 0x08
// return ir temp // return ir temp
// 0 = chip, 1 = object temperature // 0 = chip, 1 = object temperature
// * 0.02 - 273.15 // * 0.02 - 273.15
uint16_t read_irtmp(uint8_t flag) { uint16_t read_irtmp(uint8_t flag)
uint8_t hig,low; {
uint16_t val;
Wire.beginTransmission(I2_ADR_IRT); Wire.beginTransmission(I2_ADR_IRT);
if (!flag) Wire.write(MLX90614_TA); if (!flag) {
else Wire.write(MLX90614_TOBJ1); Wire.write(MLX90614_TA);
} else {
Wire.write(MLX90614_TOBJ1);
}
Wire.endTransmission(false); Wire.endTransmission(false);
Wire.requestFrom(I2_ADR_IRT, (uint8_t)3); Wire.requestFrom(I2_ADR_IRT, 3);
low=Wire.read(); uint8_t low = Wire.read();
hig=Wire.read(); uint8_t hig = Wire.read();
Wire.read(); Wire.read();
val=((uint16_t)hig<<8)|low; uint16_t val = ((uint16_t)hig << 8) | low;
return val; return val;
} }
void MLX90614_Every_Second(void) { void MLX90614_Every_Second(void)
{
if (!mlx_ready) { return; }
if (!mlx_ready) return;
uint16_t uval = read_irtmp(1); uint16_t uval = read_irtmp(1);
if (uval & 0x8000) { if (uval & 0x8000) {
obj_temp = -999; obj_temp = -999;
@ -89,10 +91,11 @@ void MLX90614_Every_Second(void) {
const char HTTP_IRTMP[] PROGMEM = const char HTTP_IRTMP[] PROGMEM =
"{s}MXL90614 " "OBJ-" D_TEMPERATURE "{m}%s C" "{e}" "{s}MXL90614 " "OBJ-" D_TEMPERATURE "{m}%s C" "{e}"
"{s}MXL90614 " "AMB-" D_TEMPERATURE "{m}%s C" "{e}"; "{s}MXL90614 " "AMB-" D_TEMPERATURE "{m}%s C" "{e}";
#endif // USE_WEBSERVER
void MLX90614_Show(uint8_t json) { void MLX90614_Show(uint8_t json)
{
if (!mlx_ready) return; if (!mlx_ready) { return; }
char obj_tstr[16]; char obj_tstr[16];
dtostrfd(obj_temp, Settings.flag2.temperature_resolution, obj_tstr); dtostrfd(obj_temp, Settings.flag2.temperature_resolution, obj_tstr);
@ -106,9 +109,7 @@ void MLX90614_Show(uint8_t json) {
WSContentSend_PD(HTTP_IRTMP, obj_tstr, amb_tstr); WSContentSend_PD(HTTP_IRTMP, obj_tstr, amb_tstr);
#endif #endif
} }
} }
#endif // USE_WEBSERVER
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface

View File

@ -224,7 +224,8 @@ bool ChirpSet(uint8_t addr) {
/********************************************************************************************/ /********************************************************************************************/
bool ChirpScan() { bool ChirpScan()
{
ChirpClockSet(); ChirpClockSet();
chirp_found_sensors = 0; chirp_found_sensors = 0;
for (uint8_t address = 1; address <= 127; address++) { for (uint8_t address = 1; address <= 127; address++) {
@ -233,7 +234,7 @@ bool ChirpScan() {
delay(2); delay(2);
chirp_sensor[chirp_found_sensors].version = ChirpReadVersion(address); chirp_sensor[chirp_found_sensors].version = ChirpReadVersion(address);
if (chirp_sensor[chirp_found_sensors].version > 0) { if (chirp_sensor[chirp_found_sensors].version > 0) {
AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "CHIRP", address); I2cSetActiveFound(address, "CHIRP");
if (chirp_found_sensors<CHIRP_MAX_SENSOR_COUNT) { if (chirp_found_sensors<CHIRP_MAX_SENSOR_COUNT) {
chirp_sensor[chirp_found_sensors].address = address; // push next sensor, as long as there is space in the array chirp_sensor[chirp_found_sensors].address = address; // push next sensor, as long as there is space in the array
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("CHIRP: fw %x"), chirp_sensor[chirp_found_sensors].version); AddLog_P2(LOG_LEVEL_DEBUG, PSTR("CHIRP: fw %x"), chirp_sensor[chirp_found_sensors].version);
@ -243,17 +244,15 @@ bool ChirpScan() {
} }
// chirp_timeout_count = 11; // wait a second to read the real fw-version in the next step // chirp_timeout_count = 11; // wait a second to read the real fw-version in the next step
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Found %u CHIRP sensor(s)."), chirp_found_sensors); AddLog_P2(LOG_LEVEL_DEBUG, PSTR("Found %u CHIRP sensor(s)."), chirp_found_sensors);
if (chirp_found_sensors == 0) {return false;} return (chirp_found_sensors > 0);
else {return true;}
} }
/********************************************************************************************/ /********************************************************************************************/
void ChirpDetect(void) void ChirpDetect(void)
{ {
if (chirp_next_job > 0) { if (chirp_next_job > 0) { return; }
return;
}
DEBUG_SENSOR_LOG(PSTR("CHIRP: scan will start ...")); DEBUG_SENSOR_LOG(PSTR("CHIRP: scan will start ..."));
if (ChirpScan()) { if (ChirpScan()) {
uint8_t chirp_model = 0; // TODO: ?? uint8_t chirp_model = 0; // TODO: ??
@ -528,9 +527,6 @@ bool Xsns48(uint8_t function)
bool result = false; bool result = false;
switch (function) { switch (function) {
case FUNC_INIT:
ChirpDetect(); // We can call CHIRPSCAN later to re-detect
break;
case FUNC_EVERY_100_MSECOND: case FUNC_EVERY_100_MSECOND:
if(chirp_found_sensors > 0){ if(chirp_found_sensors > 0){
ChirpEvery100MSecond(); ChirpEvery100MSecond();
@ -548,6 +544,9 @@ bool Xsns48(uint8_t function)
ChirpShow(0); ChirpShow(0);
break; break;
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
case FUNC_INIT:
ChirpDetect(); // We can call CHIRPSCAN later to re-detect
break;
} }
return result; return result;
} }

View File

@ -30,7 +30,6 @@
#ifdef USE_I2C #ifdef USE_I2C
#ifdef USE_PAJ7620 #ifdef USE_PAJ7620
/*********************************************************************************************\ /*********************************************************************************************\
* PAJ7620 - Gesture sensor * PAJ7620 - Gesture sensor
* *
@ -102,50 +101,15 @@ const uint8_t PAJ7620initRegisterArray[][2] PROGMEM = { // set all needed regist
* constants * constants
\*********************************************************************************************/ \*********************************************************************************************/
#define D_CMND_PAJ7620 "PAJ7620" const char kPaj7620Directions[] PROGMEM = "Down|Up|Right|Left|Near|Far|CW|CCW";
const char S_JSON_PAJ7620_COMMAND_NVALUE[] PROGMEM = "{\"" D_CMND_PAJ7620 "%s\":%d}";
const char kPAJ7620Types[] PROGMEM = "PAJ7620";
const uint8_t PAJ7620_PIN[]= {1,2,3,4}; // TOP-SECRET!! ;) const uint8_t PAJ7620_PIN[]= {1,2,3,4}; // TOP-SECRET!! ;)
/*********************************************************************************************\
* helper function
\*********************************************************************************************/
void PAJ7620SelectBank(uint8_t bank)
{
switch(bank){
case 0:
I2cWrite(PAJ7620_ADDR, PAJ7620_BANK_SEL, 0, 1);
break;
case 1:
I2cWrite(PAJ7620_ADDR, PAJ7620_BANK_SEL, 1, 1);
break;
default:
break;
}
}
/********************************************************************************************/
void PAJ7620TriggerTele(){
mqtt_data[0] = '\0';
if (MqttShowSensor()) {
MqttPublishPrefixTopic_P(TELE, PSTR(D_RSLT_SENSOR), Settings.flag.mqtt_sensor_retain); // CMND_SENSORRETAIN
#ifdef USE_RULES
RulesTeleperiod(); // Allow rule based HA messages
#endif // USE_RULES
}
}
/********************************************************************************************\ /********************************************************************************************\
| *globals | *globals
\*********************************************************************************************/ \*********************************************************************************************/
char PAJ7620_name[9]; char PAJ7620_name[] = "PAJ7620";
uint32_t PAJ7620_timeout_counter = 10; // the time interval is 100 ms -> i.e. 10 is 1 second (= start up interval) uint32_t PAJ7620_timeout_counter = 10; // the time interval is 100 ms -> i.e. 10 is 1 second (= start up interval)
uint32_t PAJ7620_next_job = 0; // 0 = detect, 1 = init, 2 = wait for gesture, 255 = sensor not found and do nothing uint32_t PAJ7620_next_job = 0; // 0 = detect, 1 = init, 2 = wait for gesture, 255 = sensor not found and do nothing
@ -176,45 +140,28 @@ struct{
} PIN; } PIN;
} PAJ7620_state; } PAJ7620_state;
/********************************************************************************************/ /*********************************************************************************************\
* helper function
\*********************************************************************************************/
void PAJ7620SelectBank(uint8_t bank)
{
I2cWrite(PAJ7620_ADDR, PAJ7620_BANK_SEL, bank &1, 1);
}
/********************************************************************************************/ /********************************************************************************************/
void PAJ7620DecodeGesture(void) void PAJ7620DecodeGesture(void)
{ {
switch (PAJ7620_gesture.current) { // we will accept only "clean" recognized gestures, the sensor can report multiple gestures at once via bitfield, but these are discarded uint32_t index = 0;
case PAJ7620_DOWN: switch (PAJ7620_gesture.current) { // We will accept only "clean" recognized gestures, the sensor can report multiple gestures at once via bitfield, but these are discarded
DEBUG_SENSOR_LOG(PSTR("DOWN"));
snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("Down"));
if(PAJ7620_gesture.unfinished){ // for better recognition of NEAR and FAR
PAJ7620_finished_gesture = true; // consider the gesture finished only in the second try, this adds some delay for up,down,left,right
break;
}
PAJ7620_gesture.unfinished = PAJ7620_gesture.current; // save the gesture, maybe it will be the final one
PAJ7620_timeout_counter = 5; // 0.5 (plus 0.3) seconds time interval to go into the sensing area and change movement to NEAR or FAR
break;
case PAJ7620_UP:
DEBUG_SENSOR_LOG(PSTR("UP"));
snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("Up"));
if(PAJ7620_gesture.unfinished){
PAJ7620_finished_gesture = true;
break;
}
PAJ7620_gesture.unfinished = PAJ7620_gesture.current;
PAJ7620_timeout_counter = 5;
break;
case PAJ7620_RIGHT:
DEBUG_SENSOR_LOG(PSTR("RIGHT"));
snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("Right"));
if(PAJ7620_gesture.unfinished){
PAJ7620_finished_gesture = true;
break;
}
PAJ7620_gesture.unfinished = PAJ7620_gesture.current;
PAJ7620_timeout_counter = 5;
break;
case PAJ7620_LEFT: case PAJ7620_LEFT:
DEBUG_SENSOR_LOG(PSTR("LEFT")); index++; // 3
snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("Left")); case PAJ7620_RIGHT:
index++; // 2
case PAJ7620_UP:
index++; // 1
case PAJ7620_DOWN:
if (PAJ7620_gesture.unfinished) { if (PAJ7620_gesture.unfinished) {
PAJ7620_finished_gesture = true; PAJ7620_finished_gesture = true;
break; break;
@ -223,84 +170,85 @@ void PAJ7620DecodeGesture(void)
PAJ7620_timeout_counter = 5; PAJ7620_timeout_counter = 5;
break; break;
case PAJ7620_NEAR: case PAJ7620_NEAR:
DEBUG_SENSOR_LOG(PSTR("NEAR")); index = 4;
snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("Near"));
PAJ7620_finished_gesture = true; PAJ7620_finished_gesture = true;
PAJ7620_timeout_counter = 25; // more time to "escape" from gesture (will be 2.8 second) PAJ7620_timeout_counter = 25; // more time to "escape" from gesture (will be 2.8 second)
break; break;
case PAJ7620_FAR: case PAJ7620_FAR:
DEBUG_SENSOR_LOG(PSTR("FAR")); index = 5;
snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("Far"));
PAJ7620_finished_gesture = true; PAJ7620_finished_gesture = true;
PAJ7620_timeout_counter = 25; PAJ7620_timeout_counter = 25;
break; break;
case PAJ7620_CW: case PAJ7620_CW:
DEBUG_SENSOR_LOG(PSTR("ClockWise")); index = 6;
snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("CW"));
PAJ7620_finished_gesture = true; PAJ7620_finished_gesture = true;
break; break;
case PAJ7620_CCW: case PAJ7620_CCW:
DEBUG_SENSOR_LOG(PSTR("CounterClockWise")); index = 7;
snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("CCW"));
PAJ7620_finished_gesture = true; PAJ7620_finished_gesture = true;
break; break;
default: default:
index = 8;
if (PAJ7620_gesture.unfinished) { if (PAJ7620_gesture.unfinished) {
PAJ7620_finished_gesture = true; // this will finish up, down, left, right PAJ7620_finished_gesture = true; // this will finish up, down, left, right
break;
} }
break; break;
} }
if (index < 8) {
GetTextIndexed(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), index, kPaj7620Directions);
}
if (PAJ7620_finished_gesture) { if (PAJ7620_finished_gesture) {
if (PAJ7620_gesture.unfinished) { if (PAJ7620_gesture.unfinished) {
if(PAJ7620_gesture.current!=PAJ7620_NEAR && PAJ7620_gesture.current!=PAJ7620_FAR){ if ((PAJ7620_gesture.current != PAJ7620_NEAR) && (PAJ7620_gesture.current != PAJ7620_FAR)) {
PAJ7620_gesture.current = PAJ7620_gesture.unfinished; // to count correctly for up, down, right, left PAJ7620_gesture.current = PAJ7620_gesture.unfinished; // to count correctly for up, down, right, left
} }
} }
if (PAJ7620_gesture.current == PAJ7620_gesture.last) { if (PAJ7620_gesture.current == PAJ7620_gesture.last) {
PAJ7620_gesture.same++; PAJ7620_gesture.same++;
} } else {
else{
PAJ7620_gesture.same = 1; PAJ7620_gesture.same = 1;
} }
PAJ7620_gesture.last = PAJ7620_gesture.current; PAJ7620_gesture.last = PAJ7620_gesture.current;
PAJ7620_finished_gesture = false; PAJ7620_finished_gesture = false;
PAJ7620_gesture.unfinished = 0; PAJ7620_gesture.unfinished = 0;
PAJ7620_timeout_counter += 3; // add delay 0.3 seconds for every kind of gesture PAJ7620_timeout_counter += 3; // add delay 0.3 seconds for every kind of gesture
PAJ7620TriggerTele(); MqttPublishSensor();
} }
} }
/********************************************************************************************/ /********************************************************************************************/
void PAJ7620ReadGesture(void){
void PAJ7620ReadGesture(void)
{
switch (PAJ7620_mode) { switch (PAJ7620_mode) {
case 1: case 1:
PAJ7620_gesture.current = I2cRead8(PAJ7620_ADDR,PAJ7620_GET_GESTURE); PAJ7620_gesture.current = I2cRead8(PAJ7620_ADDR,PAJ7620_GET_GESTURE);
if(PAJ7620_gesture.current > 0 || PAJ7620_gesture.unfinished){ if ((PAJ7620_gesture.current > 0) || PAJ7620_gesture.unfinished) {
DEBUG_SENSOR_LOG(PSTR("PAJ7620: gesture: %u"),PAJ7620_gesture.current ); DEBUG_SENSOR_LOG(PSTR("PAJ: gesture: %u"), PAJ7620_gesture.current);
PAJ7620DecodeGesture(); PAJ7620DecodeGesture();
} }
break; break;
case 2: case 2:
PAJ7620_state.proximity = I2cRead8(PAJ7620_ADDR, PAJ7620_PROXIMITY_AVG_Y); PAJ7620_state.proximity = I2cRead8(PAJ7620_ADDR, PAJ7620_PROXIMITY_AVG_Y);
if((PAJ7620_state.proximity>0)||(PAJ7620_state.last_proximity>0)) if ((PAJ7620_state.proximity > 0) || (PAJ7620_state.last_proximity > 0)) {
{
if (PAJ7620_state.proximity != PAJ7620_state.last_proximity) { if (PAJ7620_state.proximity != PAJ7620_state.last_proximity) {
PAJ7620_state.last_proximity = PAJ7620_state.proximity; PAJ7620_state.last_proximity = PAJ7620_state.proximity;
DEBUG_SENSOR_LOG(PSTR("PAJ7620: Proximity: %u"),PAJ7620_state.proximity ); DEBUG_SENSOR_LOG(PSTR("PAJ: Proximity: %u"), PAJ7620_state.proximity);
PAJ7620TriggerTele(); MqttPublishSensor();
} }
} }
break; break;
case 3: case 4: case 5: case 3:
case 4:
case 5:
PAJ7620_state.x = I2cRead8(PAJ7620_ADDR, PAJ7620_OBJECT_CENTER_X); PAJ7620_state.x = I2cRead8(PAJ7620_ADDR, PAJ7620_OBJECT_CENTER_X);
PAJ7620_state.y = I2cRead8(PAJ7620_ADDR, PAJ7620_OBJECT_CENTER_Y); PAJ7620_state.y = I2cRead8(PAJ7620_ADDR, PAJ7620_OBJECT_CENTER_Y);
if(PAJ7620_state.y>0 && PAJ7620_state.x>0){ if ((PAJ7620_state.y > 0) && (PAJ7620_state.x > 0)) {
if(PAJ7620_state.y!=PAJ7620_state.last_y || PAJ7620_state.x!=PAJ7620_state.last_x){ if ((PAJ7620_state.y != PAJ7620_state.last_y) || (PAJ7620_state.x != PAJ7620_state.last_x)) {
PAJ7620_state.last_y = PAJ7620_state.y; PAJ7620_state.last_y = PAJ7620_state.y;
PAJ7620_state.last_x = PAJ7620_state.x; PAJ7620_state.last_x = PAJ7620_state.x;
DEBUG_SENSOR_LOG(PSTR("PAJ7620: x: %u y: %u"), PAJ7620_state.x, PAJ7620_state.y); DEBUG_SENSOR_LOG(PSTR("PAJ: x: %u y: %u"), PAJ7620_state.x, PAJ7620_state.y);
PAJ7620_state.corner = 0; PAJ7620_state.corner = 0;
// 1|2 // 1|2
@ -313,8 +261,6 @@ void PAJ7620ReadGesture(void){
case 9: case 10: case 11: case 12: case 13: case 14: case 9: case 10: case 11: case 12: case 13: case 14:
PAJ7620_state.corner = 1; PAJ7620_state.corner = 1;
break; break;
default:
break;
} }
if (PAJ7620_state.corner != 0) { if (PAJ7620_state.corner != 0) {
switch (PAJ7620_state.x) { switch (PAJ7620_state.x) {
@ -328,7 +274,7 @@ void PAJ7620ReadGesture(void){
break; break;
} }
} }
DEBUG_SENSOR_LOG(PSTR("PAJ7620: corner: %u"), PAJ7620_state.corner); DEBUG_SENSOR_LOG(PSTR("PAJ: corner: %u"), PAJ7620_state.corner);
// PIN-part: // PIN-part:
if (PAJ7620_state.PIN.countdown == 0) { if (PAJ7620_state.PIN.countdown == 0) {
PAJ7620_state.PIN.step = 0; PAJ7620_state.PIN.step = 0;
@ -339,27 +285,23 @@ void PAJ7620ReadGesture(void){
PAJ7620_state.PIN.step = 1; PAJ7620_state.PIN.step = 1;
PAJ7620_state.PIN.countdown = 7; PAJ7620_state.PIN.countdown = 7;
} }
} } else {
else{
if (PAJ7620_state.corner == PAJ7620_PIN[PAJ7620_state.PIN.step]) { if (PAJ7620_state.corner == PAJ7620_PIN[PAJ7620_state.PIN.step]) {
PAJ7620_state.PIN.step += 1; PAJ7620_state.PIN.step += 1;
PAJ7620_state.PIN.countdown = 7; PAJ7620_state.PIN.countdown = 7;
} } else {
else{
PAJ7620_state.PIN.countdown -= 1; PAJ7620_state.PIN.countdown -= 1;
} }
} }
if (PAJ7620_state.PIN.step == 4) { if (PAJ7620_state.PIN.step == 4) {
PAJ7620_state.PIN.valid = 1; PAJ7620_state.PIN.valid = 1;
DEBUG_SENSOR_LOG(PSTR("PAJ7620: PIN valid!!")); DEBUG_SENSOR_LOG(PSTR("PAJ: PIN valid!!"));
PAJ7620_state.PIN.countdown = 0; // will restart in the next loop PAJ7620_state.PIN.countdown = 0; // will restart in the next loop
} }
PAJ7620TriggerTele(); MqttPublishSensor();
} }
} }
break; break;
default:
break;
} }
} }
@ -367,74 +309,51 @@ void PAJ7620ReadGesture(void){
void PAJ7620Detect(void) void PAJ7620Detect(void)
{ {
DEBUG_SENSOR_LOG(PSTR("PAJ7620: scan will start ...")); PAJ7620_next_job = 255; // do not loop
if (I2cActive(PAJ7620_ADDR)) { return; }
PAJ7620SelectBank(0); PAJ7620SelectBank(0);
PAJ7620SelectBank(0); // do it twice PAJ7620SelectBank(0); // do it twice
uint16_t PAJ7620_id = I2cRead16LE(PAJ7620_ADDR,0); // read ID from reg 1 and 0 uint16_t PAJ7620_id = I2cRead16LE(PAJ7620_ADDR,0); // read ID from reg 1 and 0
uint8_t PAJ7620_ver = I2cRead8(PAJ7620_ADDR,2); uint8_t PAJ7620_ver = I2cRead8(PAJ7620_ADDR,2);
if (PAJ7620_id == 0x7620) { // this device ID makes sense ;) if (0x7620 == PAJ7620_id) { // this device ID makes sense ;)
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PAJ7620: sensor found with ID: 0x%x and VER: %u"), PAJ7620_id, PAJ7620_ver); I2cSetActiveFound(PAJ7620_ADDR, PAJ7620_name);
uint8_t PAJ7620_model = 0; AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PAJ: ID: 0x%x and VER: %u"), PAJ7620_id, PAJ7620_ver);
GetTextIndexed(PAJ7620_name, sizeof(PAJ7620_name), PAJ7620_model, kPAJ7620Types);
PAJ7620_next_job = 1; // now init PAJ7620_next_job = 1; // now init
} }
else { else {
DEBUG_SENSOR_LOG(PSTR("PAJ7620: sensor not found, false ID 0x%x"), PAJ7620_id); DEBUG_SENSOR_LOG(PSTR("PAJ: sensor not found, false ID 0x%x"), PAJ7620_id);
PAJ7620_next_job = 255; // do not loop
} }
} }
/********************************************************************************************/ /********************************************************************************************/
void PAJ7620Init(void) void PAJ7620Init(void)
{ {
DEBUG_SENSOR_LOG(PSTR("PAJ7620: init sensor start %u"),millis()); DEBUG_SENSOR_LOG(PSTR("PAJ: init sensor start %u"),millis());
union{ union{
uint32_t raw; uint32_t raw;
uint8_t reg_val[4]; uint8_t reg_val[4];
} buf; } buf;
for (uint32_t i = 0; i < (sizeof(PAJ7620initRegisterArray) / 2); i += 2) for (uint32_t i = 0; i < (sizeof(PAJ7620initRegisterArray) / 2); i += 2)
{ {
buf.raw = pgm_read_dword(PAJ7620initRegisterArray + i); buf.raw = pgm_read_dword(PAJ7620initRegisterArray + i);
DEBUG_SENSOR_LOG("%x %x %x %x",buf.reg_val[0],buf.reg_val[1],buf.reg_val[2],buf.reg_val[3]); DEBUG_SENSOR_LOG("PAJ: %x %x %x %x",buf.reg_val[0],buf.reg_val[1],buf.reg_val[2],buf.reg_val[3]);
I2cWrite(PAJ7620_ADDR, buf.reg_val[0], buf.reg_val[1], 1); I2cWrite(PAJ7620_ADDR, buf.reg_val[0], buf.reg_val[1], 1);
I2cWrite(PAJ7620_ADDR, buf.reg_val[2], buf.reg_val[3], 1); I2cWrite(PAJ7620_ADDR, buf.reg_val[2], buf.reg_val[3], 1);
} }
DEBUG_SENSOR_LOG(PSTR("PAJ7620: init sensor done %u"),millis()); DEBUG_SENSOR_LOG(PSTR("PAJ: init sensor done %u"),millis());
PAJ7620_next_job = 2; // now loop and wait for gestures PAJ7620_next_job = 2; // now loop and wait for gestures
} }
/********************************************************************************************/ /********************************************************************************************/
void PAJ7620SelectMode(uint16_t mode){
DEBUG_SENSOR_LOG(PSTR("PAJ7620: set mode to %u"),mode);
switch(mode){
case 0:
PAJ7620_mode = 0;
break;
case 1:
PAJ7620_mode = 1;
break;
case 2:
PAJ7620_mode = 2;
break;
case 3:
PAJ7620_mode = 3;
break;
case 4:
PAJ7620_mode = 4;
break;
case 5:
PAJ7620_mode = 5;
break;
default:
break;
}
}
/********************************************************************************************/
void PAJ7620Loop(void) void PAJ7620Loop(void)
{ {
if(PAJ7620_timeout_counter == 0){ if (255 == PAJ7620_next_job) { return; }
if (0 == PAJ7620_timeout_counter) {
switch (PAJ7620_next_job) { switch (PAJ7620_next_job) {
case 0: case 0:
PAJ7620Detect(); PAJ7620Detect();
@ -447,88 +366,64 @@ void PAJ7620Loop(void)
PAJ7620ReadGesture(); PAJ7620ReadGesture();
} }
break; break;
default:
break;
} }
} } else {
else {
PAJ7620_timeout_counter--; PAJ7620_timeout_counter--;
} }
} }
/********************************************************************************************/
// normaly in i18n.h
#define D_JSON_PAJ7620 "PAJ7620"
#ifdef USE_WEBSERVER
// {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
const char HTTP_SNS_PAJ7620[] PROGMEM = "{s} " D_JSON_PAJ7620 ": {m}%s {e}";
const char HTTP_SNS_PAJ7620VER[] PROGMEM = "{s} PAJ7620 at address: {m}0x73{e}"
"{s} version: {m}1 {e}"; // only hard-coded ATM ;
#endif // USE_WEBSERVER
/********************************************************************************************/ /********************************************************************************************/
void PAJ7620Show(bool json) void PAJ7620Show(bool json)
{ {
if (255 == PAJ7620_next_job) { return; }
if (json) { if (json) {
if((PAJ7620_currentGestureName[0] != '\0' )){ if (PAJ7620_currentGestureName[0] != '\0' ) {
ResponseAppend_P(PSTR(",\"%s\":{\"%s\":%u}"), PAJ7620_name, PAJ7620_currentGestureName, PAJ7620_gesture.same); ResponseAppend_P(PSTR(",\"%s\":{\"%s\":%u}"), PAJ7620_name, PAJ7620_currentGestureName, PAJ7620_gesture.same);
PAJ7620_currentGestureName[0] = '\0'; PAJ7620_currentGestureName[0] = '\0';
return; return;
} }
switch (PAJ7620_mode) { switch (PAJ7620_mode) {
case 2: case 2:
if(PAJ7620_mode>1){
ResponseAppend_P(PSTR(",\"%s\":{\"Proximity\":%u}"), PAJ7620_name, PAJ7620_state.proximity); ResponseAppend_P(PSTR(",\"%s\":{\"Proximity\":%u}"), PAJ7620_name, PAJ7620_state.proximity);
}
break; break;
case 3: case 3:
if(PAJ7620_mode>1 && PAJ7620_state.corner>0){ if (PAJ7620_state.corner > 0) {
ResponseAppend_P(PSTR(",\"%s\":{\"Corner\":%u}"), PAJ7620_name, PAJ7620_state.corner); ResponseAppend_P(PSTR(",\"%s\":{\"Corner\":%u}"), PAJ7620_name, PAJ7620_state.corner);
} }
break; break;
case 4: case 4:
if(PAJ7620_mode>1 && PAJ7620_state.PIN.valid){ if (PAJ7620_state.PIN.valid) {
ResponseAppend_P(PSTR(",\"%s\":{\"PIN\":%u}"), PAJ7620_name, 1); //TODO: more than one PIN ResponseAppend_P(PSTR(",\"%s\":{\"PIN\":%u}"), PAJ7620_name, 1); //TODO: more than one PIN
PAJ7620_state.PIN.valid = 0; PAJ7620_state.PIN.valid = 0;
} }
break; break;
case 5: case 5:
if(PAJ7620_mode>1){
ResponseAppend_P(PSTR(",\"%s\":{\"x\":%u,\"y\":%u}"), PAJ7620_name, PAJ7620_state.x, PAJ7620_state.y); ResponseAppend_P(PSTR(",\"%s\":{\"x\":%u,\"y\":%u}"), PAJ7620_name, PAJ7620_state.x, PAJ7620_state.y);
}
break;
default:
break; break;
} }
#ifdef USE_WEBSERVER
} else {
WSContentSend_PD(HTTP_SNS_PAJ7620VER);
#endif // USE_WEBSERVER
} }
} }
/*********************************************************************************************\ /*********************************************************************************************\
* check the PAJ7620 commands * Command Sensor50
*
* 1 - Gesture mode
* 2 - Proximity mode
* 3 - Corner mode
* 4 - PIN mode
* 5 - X/Y mode
\*********************************************************************************************/ \*********************************************************************************************/
bool PAJ7620Cmd(void) { bool PAJ7620CommandSensor(void)
bool serviced = true; {
if (XdrvMailbox.data_len > 0) { if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 5)) {
DEBUG_SENSOR_LOG(PSTR("PAJ7620: got argument for mode")); PAJ7620_mode = XdrvMailbox.payload;
PAJ7620SelectMode(XdrvMailbox.payload); //select mode
Response_P(S_JSON_PAJ7620_COMMAND_NVALUE, XdrvMailbox.command, XdrvMailbox.payload);
} }
else { Response_P(S_JSON_SENSOR_INDEX_NVALUE, XSNS_50, PAJ7620_mode);
DEBUG_SENSOR_LOG(PSTR("PAJ7620: show mode"));
Response_P(S_JSON_PAJ7620_COMMAND_NVALUE, XdrvMailbox.command, PAJ7620_mode); return true;
}
return serviced;
} }
/*********************************************************************************************\ /*********************************************************************************************\
@ -542,27 +437,17 @@ bool Xsns50(uint8_t function)
bool result = false; bool result = false;
switch (function) { switch (function) {
case FUNC_INIT:
DEBUG_SENSOR_LOG(PSTR("PAJ7620: 1 second until init"));
break;
case FUNC_COMMAND_SENSOR: case FUNC_COMMAND_SENSOR:
if (XSNS_50 == XdrvMailbox.index){ if (XSNS_50 == XdrvMailbox.index){
result = PAJ7620Cmd(); result = PAJ7620CommandSensor();
} }
break; break;
case FUNC_EVERY_100_MSECOND: case FUNC_EVERY_100_MSECOND:
if(PAJ7620_next_job <255) {
PAJ7620Loop(); PAJ7620Loop();
}
break; break;
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
PAJ7620Show(1); PAJ7620Show(1);
break; break;
#ifdef USE_WEBSERVER
case FUNC_WEB_SENSOR:
PAJ7620Show(0);
break;
#endif // USE_WEBSERVER
} }
return result; return result;
} }