diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 98f729833..f31845bee 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -790,6 +790,14 @@ bool MqttShowSensor(void) return json_data_available; } +void MqttPublishSensor(void) +{ + mqtt_data[0] = '\0'; + if (MqttShowSensor()) { + MqttPublishTeleSensor(); + } +} + /********************************************************************************************/ void PerformEverySecond(void) @@ -843,14 +851,8 @@ void PerformEverySecond(void) tele_period = 0; 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); } } diff --git a/tasmota/xdrv_02_mqtt.ino b/tasmota/xdrv_02_mqtt.ino index bee7b2475..ca5f639ab 100644 --- a/tasmota/xdrv_02_mqtt.ino +++ b/tasmota/xdrv_02_mqtt.ino @@ -429,6 +429,14 @@ void MqttPublishPrefixTopic_P(uint32_t prefix, const char* subtopic) 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) { char stopic[TOPSZ]; diff --git a/tasmota/xsns_27_apds9960.ino b/tasmota/xsns_27_apds9960.ino index 1d0cd623a..87405a019 100644 --- a/tasmota/xsns_27_apds9960.ino +++ b/tasmota/xsns_27_apds9960.ino @@ -1810,14 +1810,7 @@ void handleGesture(void) { snprintf_P(currentGesture, sizeof(currentGesture), PSTR("None")); } } - - 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 - } + MqttPublishSensor(); } } diff --git a/tasmota/xsns_31_ccs811.ino b/tasmota/xsns_31_ccs811.ino index f310f01cc..ef553cb13 100644 --- a/tasmota/xsns_31_ccs811.ino +++ b/tasmota/xsns_31_ccs811.ino @@ -33,8 +33,8 @@ #include "Adafruit_CCS811.h" Adafruit_CCS811 ccs; -uint8_t CCS811_ready; -uint8_t CCS811_type; +uint8_t CCS811_ready = 0; +uint8_t CCS811_type = 0;; uint16_t eCO2; uint16_t TVOC; uint8_t tcnt = 0; @@ -45,6 +45,8 @@ uint8_t ecnt = 0; void CCS811Update(void) // Perform every n second { + if (I2cActive(CCS811_ADDRESS)) { return; } + tcnt++; if (tcnt >= EVERYNSECONDS) { tcnt = 0; @@ -53,7 +55,7 @@ void CCS811Update(void) // Perform every n second sint8_t res = ccs.begin(CCS811_ADDRESS); if (!res) { CCS811_type = 1; - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "CCS811", 0x5A); + I2cSetActiveFound(CCS811_ADDRESS, "CCS811"); } else { //AddLog_P2(LOG_LEVEL_DEBUG, "CCS811 init failed: %d",res); } diff --git a/tasmota/xsns_32_mpu6050.ino b/tasmota/xsns_32_mpu6050.ino index cb3782167..61395d5d6 100644 --- a/tasmota/xsns_32_mpu6050.ino +++ b/tasmota/xsns_32_mpu6050.ino @@ -67,6 +67,8 @@ MPU6050 mpu6050; void MPU_6050PerformReading(void) { + if (!MPU_6050_found) { return; } + #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_dmp.fifoCount = mpu6050.getFIFOCount(); @@ -140,12 +142,11 @@ void MPU_6050Detect(void) MPU_6050_found = mpu6050.testConnection(); #endif //USE_MPU6050_DMP Settings.flag2.axis_resolution = 2; // Need to be services by command Sensor32 - } 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,49 +169,49 @@ const char HTTP_SNS_AXIS[] PROGMEM = void MPU_6050Show(bool json) { - if (MPU_6050_found) { - MPU_6050PerformReading(); + if (!MPU_6050_found) { return; } - double tempConv = (MPU_6050_temperature / 340.0 + 35.53); - char temperature[33]; - dtostrfd(tempConv, Settings.flag2.temperature_resolution, temperature); - char axis_ax[33]; - dtostrfd(MPU_6050_ax, Settings.flag2.axis_resolution, axis_ax); - char axis_ay[33]; - dtostrfd(MPU_6050_ay, Settings.flag2.axis_resolution, axis_ay); - char axis_az[33]; - dtostrfd(MPU_6050_az, Settings.flag2.axis_resolution, axis_az); - char axis_gx[33]; - dtostrfd(MPU_6050_gx, Settings.flag2.axis_resolution, axis_gx); - char axis_gy[33]; - dtostrfd(MPU_6050_gy, Settings.flag2.axis_resolution, axis_gy); - char axis_gz[33]; - dtostrfd(MPU_6050_gz, Settings.flag2.axis_resolution, axis_gz); + MPU_6050PerformReading(); - if (json) { - char json_axis_ax[25]; - snprintf_P(json_axis_ax, sizeof(json_axis_ax), PSTR(",\"" D_JSON_AXIS_AX "\":%s"), axis_ax); - char json_axis_ay[25]; - snprintf_P(json_axis_ay, sizeof(json_axis_ay), PSTR(",\"" D_JSON_AXIS_AY "\":%s"), axis_ay); - char json_axis_az[25]; - snprintf_P(json_axis_az, sizeof(json_axis_az), PSTR(",\"" D_JSON_AXIS_AZ "\":%s"), axis_az); - char json_axis_gx[25]; - snprintf_P(json_axis_gx, sizeof(json_axis_gx), PSTR(",\"" D_JSON_AXIS_GX "\":%s"), axis_gx); - char json_axis_gy[25]; - snprintf_P(json_axis_gy, sizeof(json_axis_gy), PSTR(",\"" D_JSON_AXIS_GY "\":%s"), axis_gy); - char json_axis_gz[25]; - snprintf_P(json_axis_gz, sizeof(json_axis_gz), PSTR(",\"" D_JSON_AXIS_GZ "\":%s"), axis_gz); - ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%s%s%s%s%s%s%s}"), - D_SENSOR_MPU6050, temperature, json_axis_ax, json_axis_ay, json_axis_az, json_axis_gx, json_axis_gy, json_axis_gz); + double tempConv = (MPU_6050_temperature / 340.0 + 35.53); + char temperature[33]; + dtostrfd(tempConv, Settings.flag2.temperature_resolution, temperature); + char axis_ax[33]; + dtostrfd(MPU_6050_ax, Settings.flag2.axis_resolution, axis_ax); + char axis_ay[33]; + dtostrfd(MPU_6050_ay, Settings.flag2.axis_resolution, axis_ay); + char axis_az[33]; + dtostrfd(MPU_6050_az, Settings.flag2.axis_resolution, axis_az); + char axis_gx[33]; + dtostrfd(MPU_6050_gx, Settings.flag2.axis_resolution, axis_gx); + char axis_gy[33]; + dtostrfd(MPU_6050_gy, Settings.flag2.axis_resolution, axis_gy); + char axis_gz[33]; + dtostrfd(MPU_6050_gz, Settings.flag2.axis_resolution, axis_gz); + + if (json) { + char json_axis_ax[25]; + snprintf_P(json_axis_ax, sizeof(json_axis_ax), PSTR(",\"" D_JSON_AXIS_AX "\":%s"), axis_ax); + char json_axis_ay[25]; + snprintf_P(json_axis_ay, sizeof(json_axis_ay), PSTR(",\"" D_JSON_AXIS_AY "\":%s"), axis_ay); + char json_axis_az[25]; + snprintf_P(json_axis_az, sizeof(json_axis_az), PSTR(",\"" D_JSON_AXIS_AZ "\":%s"), axis_az); + char json_axis_gx[25]; + snprintf_P(json_axis_gx, sizeof(json_axis_gx), PSTR(",\"" D_JSON_AXIS_GX "\":%s"), axis_gx); + char json_axis_gy[25]; + snprintf_P(json_axis_gy, sizeof(json_axis_gy), PSTR(",\"" D_JSON_AXIS_GY "\":%s"), axis_gy); + char json_axis_gz[25]; + snprintf_P(json_axis_gz, sizeof(json_axis_gz), PSTR(",\"" D_JSON_AXIS_GZ "\":%s"), axis_gz); + ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_TEMPERATURE "\":%s%s%s%s%s%s%s}"), + D_SENSOR_MPU6050, temperature, json_axis_ax, json_axis_ay, json_axis_az, json_axis_gx, json_axis_gy, json_axis_gz); #ifdef USE_DOMOTICZ - DomoticzSensor(DZ_TEMP, temperature); + DomoticzSensor(DZ_TEMP, temperature); #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER - } else { - WSContentSend_PD(HTTP_SNS_TEMP, D_SENSOR_MPU6050, temperature, TempUnit()); - WSContentSend_PD(HTTP_SNS_AXIS, axis_ax, axis_ay, axis_az, axis_gx, axis_gy, axis_gz); + } else { + WSContentSend_PD(HTTP_SNS_TEMP, D_SENSOR_MPU6050, temperature, TempUnit()); + WSContentSend_PD(HTTP_SNS_AXIS, axis_ax, axis_ay, axis_az, axis_gx, axis_gy, axis_gz); #endif // USE_WEBSERVER - } } } diff --git a/tasmota/xsns_33_ds3231.ino b/tasmota/xsns_33_ds3231.ino index ca268df65..9e5062acf 100644 --- a/tasmota/xsns_33_ds3231.ino +++ b/tasmota/xsns_33_ds3231.ino @@ -64,7 +64,7 @@ #define CENTURY 7 //Century bit in Month register #define DYDT 6 //Day/Date flag bit in alarm Day/Date registers 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; /*----------------------------------------------------------------------* @@ -72,12 +72,11 @@ bool DS3231chipDetected = false; ----------------------------------------------------------------------*/ void DS3231Detect(void) { - DS3231chipDetected = false; + if (DS3231chipDetected || I2cActive(USE_RTC_ADDR)) { return; } + 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; - } else { - AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_I2C "DS3231 NOT " D_FOUND_AT " 0x%x"), USE_RTC_ADDR); } } @@ -128,6 +127,39 @@ 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 } +void DS3231EverySecond(void) +{ + if (!DS3231chipDetected) { return; } + + TIME_T tmpTime; + 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 + 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 + // We need it to set rules etc. + BreakTime(Rtc.utc_time, tmpTime); + if (Rtc.utc_time < START_VALID_TIME ) { + ds3231ReadStatus = true; //if time in DS3231 is valid, do not update again + } + RtcTime.year = tmpTime.year + 1970; + Rtc.daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year); + Rtc.standard_time = RuleToTime(Settings.tflag[0], RtcTime.year); + AddLog_P2(LOG_LEVEL_INFO, PSTR("Set time from DS3231 to RTC (" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"), + GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str()); + if (Rtc.local_time < START_VALID_TIME) { // 2016-01-01 + rules_flag.time_init = 1; + } else { + rules_flag.time_set = 1; + } + } + 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"), + GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str()); + SetDS3231Time (Rtc.utc_time); //update the DS3231 time + ds3231WriteStatus = true; + } +} + /*********************************************************************************************\ Interface \*********************************************************************************************/ @@ -139,38 +171,12 @@ bool Xsns33(uint8_t function) bool result = false; switch (function) { + case FUNC_EVERY_SECOND: + DS3231EverySecond(); + break; case FUNC_INIT: DS3231Detect(); break; - case FUNC_EVERY_SECOND: - 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 - ntp_force_sync = true; //force to sync with ntp - 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 - // We need it to set rules etc. - BreakTime(Rtc.utc_time, tmpTime); - if (Rtc.utc_time < START_VALID_TIME ) { - ds3231ReadStatus = true; //if time in DS3231 is valid, do not update again - } - RtcTime.year = tmpTime.year + 1970; - Rtc.daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year); - Rtc.standard_time = RuleToTime(Settings.tflag[0], RtcTime.year); - AddLog_P2(LOG_LEVEL_INFO, PSTR("Set time from DS3231 to RTC (" D_UTC_TIME ") %s, (" D_DST_TIME ") %s, (" D_STD_TIME ") %s"), - GetTime(0).c_str(), GetTime(2).c_str(), GetTime(3).c_str()); - if (Rtc.local_time < START_VALID_TIME) { // 2016-01-01 - rules_flag.time_init = 1; - } else { - 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 - 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()); - SetDS3231Time (Rtc.utc_time); //update the DS3231 time - ds3231WriteStatus = true; - } - break; } return result; } diff --git a/tasmota/xsns_36_mgc3130.ino b/tasmota/xsns_36_mgc3130.ino index c3915d437..51dc070d6 100644 --- a/tasmota/xsns_36_mgc3130.ino +++ b/tasmota/xsns_36_mgc3130.ino @@ -44,7 +44,7 @@ bool MGC3130_type = false; -char MGC3130stype[8]; +char MGC3130stype[] = "MGC3130"; #define MGC3130_SYSTEM_STATUS 0x15 @@ -198,40 +198,30 @@ 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 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(){ if ( MGC_data.out.outputConfigMask.touchInfo && MGC3130_touchTimeout == 0){ if (MGC3130_handleTouch()){ MGC3130_triggeredByTouch = true; - MGC3130_triggerTele(); + MqttPublishSensor(); } } if(MGC3130_mode == 1){ if( MGC_data.out.outputConfigMask.gestureInfo && MGC_data.out.gestureInfo.gestureCode > 0){ MGC3130_handleGesture(); - MGC3130_triggerTele(); + MqttPublishSensor(); } } if(MGC3130_mode == 2){ if(MGC_data.out.outputConfigMask.airWheelInfo && MGC_data.out.systemInfo.airWheelValid){ MGC3130_handleAirWheel(); - MGC3130_triggerTele(); + MqttPublishSensor(); } } if(MGC3130_mode == 3){ 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(); } - -bool MGC3130_detect(void) +void MGC3130_detect(void) { - if (MGC3130_type){ - return true; - } + if (MGC3130_type || I2cActive(MGC3130_I2C_ADDR)) { return; } pinMode(MGC3130_xfer, INPUT_PULLUP); pinMode(MGC3130_reset, OUTPUT); @@ -498,17 +485,11 @@ bool MGC3130_detect(void) digitalWrite(MGC3130_reset, HIGH); delay(50); - bool success = false; - success = MGC3130_receiveMessage(); // This should read the firmware info - if (success) { - strcpy_P(MGC3130stype, PSTR("MGC3130")); - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, MGC3130stype, MGC3130_I2C_ADDR); + if (MGC3130_receiveMessage()) { // This should read the firmware info + I2cSetActiveFound(MGC3130_I2C_ADDR, MGC3130stype); MGC3130_currentGesture[0] = '\0'; MGC3130_type = true; - } else { - AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MGC3130 did not respond at address 0x%x"), MGC3130_I2C_ADDR); } - return success; } /*********************************************************************************************\ diff --git a/tasmota/xsns_41_max44009.ino b/tasmota/xsns_41_max44009.ino index c59e4fc09..7de6cf4d3 100644 --- a/tasmota/xsns_41_max44009.ino +++ b/tasmota/xsns_41_max44009.ino @@ -66,18 +66,14 @@ bool Max4409Read_lum(void) void Max4409Detect(void) { - uint8_t reg[8]; - bool failed = false; - - if (max44009_found) { - return; - } + if (max44009_found) { return; } uint8_t buffer1; uint8_t buffer2; for (uint32_t i = 0; 0 != max44009_addresses[i]; i++) { max44009_address = max44009_addresses[i]; + if (I2cActive(max44009_address)) { continue; } if ((I2cValidRead8(&buffer1, max44009_address, REG_LOWER_THRESHOLD)) && (I2cValidRead8(&buffer2, max44009_address, REG_THRESHOLD_TIMER))) { @@ -94,8 +90,8 @@ void Max4409Detect(void) Wire.write(REG_CONFIG); Wire.write(MAX44009_CONTINUOUS_AUTO_MODE); if (0 == Wire.endTransmission()) { + I2cSetActiveFound(max44009_address, max44009_types); max44009_found = 1; - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, max44009_types, max44009_address); break; } } @@ -155,9 +151,6 @@ bool Xsns41(uint8_t function) bool result = false; switch (function) { - case FUNC_INIT: - Max4409Detect(); - break; case FUNC_EVERY_SECOND: Max4409EverySecond(); break; @@ -169,6 +162,9 @@ bool Xsns41(uint8_t function) Max4409Show(0); break; #endif // USE_WEBSERVER + case FUNC_INIT: + Max4409Detect(); + break; } return result; } diff --git a/tasmota/xsns_45_vl53l0x.ino b/tasmota/xsns_45_vl53l0x.ino index b97df790b..b2e00f3c8 100644 --- a/tasmota/xsns_45_vl53l0x.ino +++ b/tasmota/xsns_45_vl53l0x.ino @@ -42,7 +42,7 @@ void Vl53l0Detect(void) if (!sensor.init()) { return; } - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "VL53L0X", sensor.getAddress()); + I2cSetActiveFound(sensor.getAddress(), "VL53L0X"); sensor.setTimeout(500); diff --git a/tasmota/xsns_46_MLX90614.ino b/tasmota/xsns_46_MLX90614.ino index f4be8afb1..9a17ac9c0 100644 --- a/tasmota/xsns_46_MLX90614.ino +++ b/tasmota/xsns_46_MLX90614.ino @@ -25,7 +25,13 @@ #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 amb_temp; @@ -33,55 +39,51 @@ void MLX90614_Init(void) { if (!I2cSetDevice(I2_ADR_IRT)) { return; } - mlx_ready=1; - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "MLX90614", I2_ADR_IRT); + I2cSetActiveFound(I2_ADR_IRT, "MLX90614"); + mlx_ready = true; // not needed on tasmota //Wire.begin(); //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 // 0 = chip, 1 = object temperature // * 0.02 - 273.15 -uint16_t read_irtmp(uint8_t flag) { - uint8_t hig,low; - uint16_t val; +uint16_t read_irtmp(uint8_t flag) +{ + Wire.beginTransmission(I2_ADR_IRT); + if (!flag) { + Wire.write(MLX90614_TA); + } else { + Wire.write(MLX90614_TOBJ1); + } + Wire.endTransmission(false); - Wire.beginTransmission(I2_ADR_IRT); - if (!flag) Wire.write(MLX90614_TA); - else Wire.write(MLX90614_TOBJ1); - Wire.endTransmission(false); + Wire.requestFrom(I2_ADR_IRT, 3); + uint8_t low = Wire.read(); + uint8_t hig = Wire.read(); + Wire.read(); - Wire.requestFrom(I2_ADR_IRT, (uint8_t)3); - low=Wire.read(); - hig=Wire.read(); - Wire.read(); - - val=((uint16_t)hig<<8)|low; - return val; + uint16_t val = ((uint16_t)hig << 8) | low; + 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); - if (uval&0x8000) { - obj_temp=-999; + uint16_t uval = read_irtmp(1); + if (uval & 0x8000) { + obj_temp = -999; } else { - obj_temp=((float)uval*0.02)-273.15; + obj_temp = ((float)uval * 0.02) - 273.15; } - uval=read_irtmp(0); - if (uval&0x8000) { - amb_temp=-999; + uval = read_irtmp(0); + if (uval & 0x8000) { + amb_temp = -999; } else { - amb_temp=((float)uval*0.02)-273.15; + amb_temp = ((float)uval * 0.02) - 273.15; } } @@ -89,10 +91,11 @@ void MLX90614_Every_Second(void) { const char HTTP_IRTMP[] PROGMEM = "{s}MXL90614 " "OBJ-" D_TEMPERATURE "{m}%s C" "{e}" "{s}MXL90614 " "AMB-" D_TEMPERATURE "{m}%s C" "{e}"; +#endif // USE_WEBSERVER -void MLX90614_Show(uint8_t json) { - - if (!mlx_ready) return; +void MLX90614_Show(uint8_t json) +{ + if (!mlx_ready) { return; } char obj_tstr[16]; dtostrfd(obj_temp, Settings.flag2.temperature_resolution, obj_tstr); @@ -100,15 +103,13 @@ void MLX90614_Show(uint8_t json) { dtostrfd(amb_temp, Settings.flag2.temperature_resolution, amb_tstr); if (json) { - ResponseAppend_P(PSTR(",\"MLX90614\":{\"OBJTMP\":%s,\"AMBTMP\":%s}"), obj_tstr,amb_tstr); + ResponseAppend_P(PSTR(",\"MLX90614\":{\"OBJTMP\":%s,\"AMBTMP\":%s}"), obj_tstr, amb_tstr); #ifdef USE_WEBSERVER } else { - WSContentSend_PD(HTTP_IRTMP,obj_tstr,amb_tstr); + WSContentSend_PD(HTTP_IRTMP, obj_tstr, amb_tstr); #endif } - } -#endif // USE_WEBSERVER /*********************************************************************************************\ * Interface @@ -126,7 +127,7 @@ bool Xsns46(byte function) break; case FUNC_JSON_APPEND: MLX90614_Show(1); - break; + break; #ifdef USE_WEBSERVER case FUNC_WEB_SENSOR: MLX90614_Show(0); diff --git a/tasmota/xsns_48_chirp.ino b/tasmota/xsns_48_chirp.ino index dce2550ff..a7a88846b 100644 --- a/tasmota/xsns_48_chirp.ino +++ b/tasmota/xsns_48_chirp.ino @@ -224,36 +224,35 @@ bool ChirpSet(uint8_t addr) { /********************************************************************************************/ -bool ChirpScan() { - ChirpClockSet(); - chirp_found_sensors = 0; - for (uint8_t address = 1; address <= 127; address++) { - chirp_sensor[chirp_found_sensors].version = 0; - chirp_sensor[chirp_found_sensors].version = ChirpReadVersion(address); - delay(2); - chirp_sensor[chirp_found_sensors].version = ChirpReadVersion(address); - if(chirp_sensor[chirp_found_sensors].version > 0) { - AddLog_P2(LOG_LEVEL_INFO, S_LOG_I2C_FOUND_AT, "CHIRP", address); - if(chirp_found_sensors 0) { + I2cSetActiveFound(address, "CHIRP"); + if (chirp_found_sensors 0); } /********************************************************************************************/ void ChirpDetect(void) { - if (chirp_next_job > 0) { - return; - } + if (chirp_next_job > 0) { return; } + DEBUG_SENSOR_LOG(PSTR("CHIRP: scan will start ...")); if (ChirpScan()) { uint8_t chirp_model = 0; // TODO: ?? @@ -528,9 +527,6 @@ bool Xsns48(uint8_t function) bool result = false; switch (function) { - case FUNC_INIT: - ChirpDetect(); // We can call CHIRPSCAN later to re-detect - break; case FUNC_EVERY_100_MSECOND: if(chirp_found_sensors > 0){ ChirpEvery100MSecond(); @@ -548,6 +544,9 @@ bool Xsns48(uint8_t function) ChirpShow(0); break; #endif // USE_WEBSERVER + case FUNC_INIT: + ChirpDetect(); // We can call CHIRPSCAN later to re-detect + break; } return result; } diff --git a/tasmota/xsns_50_paj7620.ino b/tasmota/xsns_50_paj7620.ino index b147caf36..fdf5da48b 100644 --- a/tasmota/xsns_50_paj7620.ino +++ b/tasmota/xsns_50_paj7620.ino @@ -30,7 +30,6 @@ #ifdef USE_I2C #ifdef USE_PAJ7620 - /*********************************************************************************************\ * PAJ7620 - Gesture sensor * @@ -38,34 +37,34 @@ \*********************************************************************************************/ #define XSNS_50 50 -#define XI2C_34 34 // See I2CDEVICES.md +#define XI2C_34 34 // See I2CDEVICES.md -#define PAJ7620_ADDR 0x73 // standard address +#define PAJ7620_ADDR 0x73 // standard address -#define PAJ7620_BANK_SEL 0xEF // 8 bit, write -> 0 or 1 +#define PAJ7620_BANK_SEL 0xEF // 8 bit, write -> 0 or 1 // the registers are organized in 2 banks // bank: 0 -#define PAJ7620_GET_GESTURE 0x43 // 8 bit, read -#define PAJ7620_PROXIMITY_AVG_Y 0x6c // 8 bit, read -> 255: near , lower numbers: far +#define PAJ7620_GET_GESTURE 0x43 // 8 bit, read +#define PAJ7620_PROXIMITY_AVG_Y 0x6c // 8 bit, read -> 255: near , lower numbers: far -#define PAJ7620_OBJECT_CENTER_X 0xad // 5 bit, read -#define PAJ7620_OBJECT_CENTER_Y 0xaf // 5 bit, read +#define PAJ7620_OBJECT_CENTER_X 0xad // 5 bit, read +#define PAJ7620_OBJECT_CENTER_Y 0xaf // 5 bit, read -#define PAJ7620_DOWN 1 // readings from PAJ7620_GET_GESTURE -#define PAJ7620_UP 2 -#define PAJ7620_RIGHT 4 -#define PAJ7620_LEFT 8 -#define PAJ7620_NEAR 16 -#define PAJ7620_FAR 32 -#define PAJ7620_CW 64 -#define PAJ7620_CCW 128 +#define PAJ7620_DOWN 1 // readings from PAJ7620_GET_GESTURE +#define PAJ7620_UP 2 +#define PAJ7620_RIGHT 4 +#define PAJ7620_LEFT 8 +#define PAJ7620_NEAR 16 +#define PAJ7620_FAR 32 +#define PAJ7620_CW 64 +#define PAJ7620_CCW 128 // bank: 1 // nothing at the moment const uint8_t PAJ7620initRegisterArray[][2] PROGMEM = { // set all needed registers - {0xEF,0x00}, // bank 0 + {0xEF,0x00}, // bank 0 {0x32,0x29}, {0x33,0x01}, {0x34,0x00}, {0x35,0x01}, {0x36,0x00}, {0x37,0x07}, {0x38,0x17}, {0x39,0x06}, {0x3A,0x12}, {0x3F,0x00}, {0x40,0x02}, {0x41,0xFF}, {0x42,0x01}, {0x46,0x2D}, {0x47,0x0F}, {0x48,0x3C}, {0x49,0x00}, {0x4A,0x1E}, {0x4B,0x00}, {0x4C,0x20}, {0x4D,0x00}, {0x4E,0x1A}, {0x4F,0x14}, {0x50,0x00}, @@ -102,50 +101,15 @@ const uint8_t PAJ7620initRegisterArray[][2] PROGMEM = { // set all needed regist * constants \*********************************************************************************************/ -#define D_CMND_PAJ7620 "PAJ7620" - -const char S_JSON_PAJ7620_COMMAND_NVALUE[] PROGMEM = "{\"" D_CMND_PAJ7620 "%s\":%d}"; - -const char kPAJ7620Types[] PROGMEM = "PAJ7620"; +const char kPaj7620Directions[] PROGMEM = "Down|Up|Right|Left|Near|Far|CW|CCW"; 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 \*********************************************************************************************/ -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_next_job = 0; // 0 = detect, 1 = init, 2 = wait for gesture, 255 = sensor not found and do nothing @@ -176,46 +140,29 @@ struct{ } PIN; } PAJ7620_state; -/********************************************************************************************/ +/*********************************************************************************************\ + * helper function +\*********************************************************************************************/ + +void PAJ7620SelectBank(uint8_t bank) +{ + I2cWrite(PAJ7620_ADDR, PAJ7620_BANK_SEL, bank &1, 1); +} /********************************************************************************************/ + 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 - case PAJ7620_DOWN: - 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; + uint32_t index = 0; + 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 case PAJ7620_LEFT: - DEBUG_SENSOR_LOG(PSTR("LEFT")); - snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("Left")); - if(PAJ7620_gesture.unfinished){ + index++; // 3 + case PAJ7620_RIGHT: + index++; // 2 + case PAJ7620_UP: + index++; // 1 + case PAJ7620_DOWN: + if (PAJ7620_gesture.unfinished) { PAJ7620_finished_gesture = true; break; } @@ -223,143 +170,138 @@ void PAJ7620DecodeGesture(void) PAJ7620_timeout_counter = 5; break; case PAJ7620_NEAR: - DEBUG_SENSOR_LOG(PSTR("NEAR")); - snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("Near")); + index = 4; PAJ7620_finished_gesture = true; PAJ7620_timeout_counter = 25; // more time to "escape" from gesture (will be 2.8 second) break; case PAJ7620_FAR: - DEBUG_SENSOR_LOG(PSTR("FAR")); - snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("Far")); + index = 5; PAJ7620_finished_gesture = true; PAJ7620_timeout_counter = 25; break; case PAJ7620_CW: - DEBUG_SENSOR_LOG(PSTR("ClockWise")); - snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("CW")); + index = 6; PAJ7620_finished_gesture = true; break; case PAJ7620_CCW: - DEBUG_SENSOR_LOG(PSTR("CounterClockWise")); - snprintf_P(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), PSTR("CCW")); + index = 7; PAJ7620_finished_gesture = true; break; default: - if(PAJ7620_gesture.unfinished){ + index = 8; + if (PAJ7620_gesture.unfinished) { PAJ7620_finished_gesture = true; // this will finish up, down, left, right - break; } break; } -if(PAJ7620_finished_gesture){ - if (PAJ7620_gesture.unfinished){ - 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 - } + if (index < 8) { + GetTextIndexed(PAJ7620_currentGestureName, sizeof(PAJ7620_currentGestureName), index, kPaj7620Directions); } - if (PAJ7620_gesture.current == PAJ7620_gesture.last){ - PAJ7620_gesture.same++; + + if (PAJ7620_finished_gesture) { + if (PAJ7620_gesture.unfinished) { + 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 + } } - else{ - PAJ7620_gesture.same = 1; + if (PAJ7620_gesture.current == PAJ7620_gesture.last) { + PAJ7620_gesture.same++; + } else { + PAJ7620_gesture.same = 1; } - PAJ7620_gesture.last = PAJ7620_gesture.current; - PAJ7620_finished_gesture = false; - PAJ7620_gesture.unfinished = 0; - PAJ7620_timeout_counter += 3; // add delay 0.3 seconds for every kind of gesture - PAJ7620TriggerTele(); + PAJ7620_gesture.last = PAJ7620_gesture.current; + PAJ7620_finished_gesture = false; + PAJ7620_gesture.unfinished = 0; + PAJ7620_timeout_counter += 3; // add delay 0.3 seconds for every kind of gesture + MqttPublishSensor(); } } /********************************************************************************************/ -void PAJ7620ReadGesture(void){ - switch(PAJ7620_mode){ + +void PAJ7620ReadGesture(void) +{ + switch (PAJ7620_mode) { case 1: - PAJ7620_gesture.current = I2cRead8(PAJ7620_ADDR,PAJ7620_GET_GESTURE); - if(PAJ7620_gesture.current > 0 || PAJ7620_gesture.unfinished){ - DEBUG_SENSOR_LOG(PSTR("PAJ7620: gesture: %u"),PAJ7620_gesture.current ); - PAJ7620DecodeGesture(); + PAJ7620_gesture.current = I2cRead8(PAJ7620_ADDR,PAJ7620_GET_GESTURE); + if ((PAJ7620_gesture.current > 0) || PAJ7620_gesture.unfinished) { + DEBUG_SENSOR_LOG(PSTR("PAJ: gesture: %u"), PAJ7620_gesture.current); + PAJ7620DecodeGesture(); } - - break; + break; case 2: - PAJ7620_state.proximity = I2cRead8(PAJ7620_ADDR, PAJ7620_PROXIMITY_AVG_Y); - if((PAJ7620_state.proximity>0)||(PAJ7620_state.last_proximity>0)) - { - if(PAJ7620_state.proximity!=PAJ7620_state.last_proximity){ - PAJ7620_state.last_proximity = PAJ7620_state.proximity; - DEBUG_SENSOR_LOG(PSTR("PAJ7620: Proximity: %u"),PAJ7620_state.proximity ); - PAJ7620TriggerTele(); + PAJ7620_state.proximity = I2cRead8(PAJ7620_ADDR, PAJ7620_PROXIMITY_AVG_Y); + if ((PAJ7620_state.proximity > 0) || (PAJ7620_state.last_proximity > 0)) { + if (PAJ7620_state.proximity != PAJ7620_state.last_proximity) { + PAJ7620_state.last_proximity = PAJ7620_state.proximity; + DEBUG_SENSOR_LOG(PSTR("PAJ: Proximity: %u"), PAJ7620_state.proximity); + MqttPublishSensor(); + } } - } - break; - case 3: case 4: case 5: - PAJ7620_state.x = I2cRead8(PAJ7620_ADDR, PAJ7620_OBJECT_CENTER_X); - PAJ7620_state.y = I2cRead8(PAJ7620_ADDR, PAJ7620_OBJECT_CENTER_Y); - if(PAJ7620_state.y>0 && PAJ7620_state.x>0){ - 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_x = PAJ7620_state.x; - DEBUG_SENSOR_LOG(PSTR("PAJ7620: x: %u y: %u"), PAJ7620_state.x, PAJ7620_state.y); + break; + case 3: + case 4: + case 5: + PAJ7620_state.x = I2cRead8(PAJ7620_ADDR, PAJ7620_OBJECT_CENTER_X); + PAJ7620_state.y = I2cRead8(PAJ7620_ADDR, PAJ7620_OBJECT_CENTER_Y); + if ((PAJ7620_state.y > 0) && (PAJ7620_state.x > 0)) { + 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_x = PAJ7620_state.x; + DEBUG_SENSOR_LOG(PSTR("PAJ: x: %u y: %u"), PAJ7620_state.x, PAJ7620_state.y); - PAJ7620_state.corner = 0; - // 1|2 - // --- - // 3|4 - switch(PAJ7620_state.y){ - case 0: case 1: case 2: case 3: case 4: case 5:// case 0..5: would be nicer - PAJ7620_state.corner = 3; - break; - case 9: case 10: case 11: case 12: case 13: case 14: - PAJ7620_state.corner = 1; - break; - default: - break; - } - if(PAJ7620_state.corner!=0){ - switch(PAJ7620_state.x){ - case 0: case 1: case 2: case 3: case 4: case 5: - break; - case 9: case 10: case 11: case 12: case 13: case 14: - PAJ7620_state.corner++; - break; - default: PAJ7620_state.corner = 0; - break; + // 1|2 + // --- + // 3|4 + switch (PAJ7620_state.y) { + case 0: case 1: case 2: case 3: case 4: case 5:// case 0..5: would be nicer + PAJ7620_state.corner = 3; + break; + case 9: case 10: case 11: case 12: case 13: case 14: + PAJ7620_state.corner = 1; + break; } - } - DEBUG_SENSOR_LOG(PSTR("PAJ7620: corner: %u"), PAJ7620_state.corner); - // PIN-part: - if(PAJ7620_state.PIN.countdown == 0){ - PAJ7620_state.PIN.step=0; - PAJ7620_state.PIN.valid=0; - } - if(!PAJ7620_state.PIN.step){ - if(PAJ7620_state.corner == PAJ7620_PIN[PAJ7620_state.PIN.step]){ - PAJ7620_state.PIN.step=1; - PAJ7620_state.PIN.countdown=7; + if (PAJ7620_state.corner != 0) { + switch (PAJ7620_state.x) { + case 0: case 1: case 2: case 3: case 4: case 5: + break; + case 9: case 10: case 11: case 12: case 13: case 14: + PAJ7620_state.corner++; + break; + default: + PAJ7620_state.corner = 0; + break; + } } - } - else{ - if(PAJ7620_state.corner == PAJ7620_PIN[PAJ7620_state.PIN.step]){ - PAJ7620_state.PIN.step+=1; - PAJ7620_state.PIN.countdown=7; + DEBUG_SENSOR_LOG(PSTR("PAJ: corner: %u"), PAJ7620_state.corner); + // PIN-part: + if (PAJ7620_state.PIN.countdown == 0) { + PAJ7620_state.PIN.step = 0; + PAJ7620_state.PIN.valid = 0; } - else{ - PAJ7620_state.PIN.countdown-=1; + if (!PAJ7620_state.PIN.step) { + if (PAJ7620_state.corner == PAJ7620_PIN[PAJ7620_state.PIN.step]) { + PAJ7620_state.PIN.step = 1; + PAJ7620_state.PIN.countdown = 7; + } + } else { + if (PAJ7620_state.corner == PAJ7620_PIN[PAJ7620_state.PIN.step]) { + PAJ7620_state.PIN.step += 1; + PAJ7620_state.PIN.countdown = 7; + } else { + PAJ7620_state.PIN.countdown -= 1; + } } + if (PAJ7620_state.PIN.step == 4) { + PAJ7620_state.PIN.valid = 1; + DEBUG_SENSOR_LOG(PSTR("PAJ: PIN valid!!")); + PAJ7620_state.PIN.countdown = 0; // will restart in the next loop + } + MqttPublishSensor(); } - if(PAJ7620_state.PIN.step == 4){ - PAJ7620_state.PIN.valid = 1; - DEBUG_SENSOR_LOG(PSTR("PAJ7620: PIN valid!!")); - PAJ7620_state.PIN.countdown = 0; // will restart in the next loop - } - PAJ7620TriggerTele(); - } - } - break; - default: - break; + } + break; } } @@ -367,168 +309,121 @@ void PAJ7620ReadGesture(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); // do it twice uint16_t PAJ7620_id = I2cRead16LE(PAJ7620_ADDR,0); // read ID from reg 1 and 0 uint8_t PAJ7620_ver = I2cRead8(PAJ7620_ADDR,2); - if (PAJ7620_id == 0x7620) { // 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); - uint8_t PAJ7620_model = 0; - GetTextIndexed(PAJ7620_name, sizeof(PAJ7620_name), PAJ7620_model, kPAJ7620Types); + if (0x7620 == PAJ7620_id) { // this device ID makes sense ;) + I2cSetActiveFound(PAJ7620_ADDR, PAJ7620_name); + AddLog_P2(LOG_LEVEL_DEBUG, PSTR("PAJ: ID: 0x%x and VER: %u"), PAJ7620_id, PAJ7620_ver); PAJ7620_next_job = 1; // now init } else { - DEBUG_SENSOR_LOG(PSTR("PAJ7620: sensor not found, false ID 0x%x"), PAJ7620_id); - PAJ7620_next_job = 255; // do not loop + DEBUG_SENSOR_LOG(PSTR("PAJ: sensor not found, false ID 0x%x"), PAJ7620_id); } } /********************************************************************************************/ + void PAJ7620Init(void) { - DEBUG_SENSOR_LOG(PSTR("PAJ7620: init sensor start %u"),millis()); + DEBUG_SENSOR_LOG(PSTR("PAJ: init sensor start %u"),millis()); union{ uint32_t raw; uint8_t reg_val[4]; } 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); - DEBUG_SENSOR_LOG("%x %x %x %x",buf.reg_val[0],buf.reg_val[1],buf.reg_val[2],buf.reg_val[3]); + buf.raw = pgm_read_dword(PAJ7620initRegisterArray + i); + 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[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 } /********************************************************************************************/ -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) { - if(PAJ7620_timeout_counter == 0){ - switch(PAJ7620_next_job){ + if (255 == PAJ7620_next_job) { return; } + + if (0 == PAJ7620_timeout_counter) { + switch (PAJ7620_next_job) { case 0: - PAJ7620Detect(); - break; + PAJ7620Detect(); + break; case 1: - PAJ7620Init(); - break; + PAJ7620Init(); + break; case 2: - if(PAJ7620_mode != 0){ - PAJ7620ReadGesture(); - } - break; - default: - break; + if (PAJ7620_mode != 0) { + PAJ7620ReadGesture(); + } + break; } - } - else { + } else { PAJ7620_timeout_counter--; } } -/********************************************************************************************/ -// normaly in i18n.h - -#define D_JSON_PAJ7620 "PAJ7620" - -#ifdef USE_WEBSERVER - // {s} = , {m} = , {e} = - - 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) { + if (255 == PAJ7620_next_job) { return; } + 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); PAJ7620_currentGestureName[0] = '\0'; return; } - switch(PAJ7620_mode){ + switch (PAJ7620_mode) { case 2: - if(PAJ7620_mode>1){ ResponseAppend_P(PSTR(",\"%s\":{\"Proximity\":%u}"), PAJ7620_name, PAJ7620_state.proximity); - } - break; + break; case 3: - if(PAJ7620_mode>1 && PAJ7620_state.corner>0){ - ResponseAppend_P(PSTR(",\"%s\":{\"Corner\":%u}"), PAJ7620_name, PAJ7620_state.corner); - } - break; + if (PAJ7620_state.corner > 0) { + ResponseAppend_P(PSTR(",\"%s\":{\"Corner\":%u}"), PAJ7620_name, PAJ7620_state.corner); + } + break; case 4: - if(PAJ7620_mode>1 && PAJ7620_state.PIN.valid){ - ResponseAppend_P(PSTR(",\"%s\":{\"PIN\":%u}"), PAJ7620_name, 1); //TODO: more than one PIN - PAJ7620_state.PIN.valid = 0; - } - break; + if (PAJ7620_state.PIN.valid) { + ResponseAppend_P(PSTR(",\"%s\":{\"PIN\":%u}"), PAJ7620_name, 1); //TODO: more than one PIN + PAJ7620_state.PIN.valid = 0; + } + break; case 5: - if(PAJ7620_mode>1){ 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 serviced = true; - if (XdrvMailbox.data_len > 0) { - DEBUG_SENSOR_LOG(PSTR("PAJ7620: got argument for mode")); - PAJ7620SelectMode(XdrvMailbox.payload); //select mode - Response_P(S_JSON_PAJ7620_COMMAND_NVALUE, XdrvMailbox.command, XdrvMailbox.payload); - } - else { - DEBUG_SENSOR_LOG(PSTR("PAJ7620: show mode")); - Response_P(S_JSON_PAJ7620_COMMAND_NVALUE, XdrvMailbox.command, PAJ7620_mode); - } - return serviced; +bool PAJ7620CommandSensor(void) +{ + if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 5)) { + PAJ7620_mode = XdrvMailbox.payload; + } + Response_P(S_JSON_SENSOR_INDEX_NVALUE, XSNS_50, PAJ7620_mode); + + return true; } /*********************************************************************************************\ @@ -542,27 +437,17 @@ bool Xsns50(uint8_t function) bool result = false; switch (function) { - case FUNC_INIT: - DEBUG_SENSOR_LOG(PSTR("PAJ7620: 1 second until init")); - break; case FUNC_COMMAND_SENSOR: if (XSNS_50 == XdrvMailbox.index){ - result = PAJ7620Cmd(); + result = PAJ7620CommandSensor(); } break; case FUNC_EVERY_100_MSECOND: - if(PAJ7620_next_job <255) { - PAJ7620Loop(); - } + PAJ7620Loop(); break; case FUNC_JSON_APPEND: PAJ7620Show(1); break; -#ifdef USE_WEBSERVER - case FUNC_WEB_SENSOR: - PAJ7620Show(0); - break; -#endif // USE_WEBSERVER } return result; }