2020-09-01 16:24:36 +01:00
|
|
|
/*
|
2022-03-10 21:00:20 +00:00
|
|
|
xsns_77_vl53l1x_device[0].ino - VL53L1X sensor support for Tasmota
|
2020-09-01 16:24:36 +01:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Theo Arends, Rui Marinho and Johann Obermeier
|
2020-09-01 16:24:36 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_VL53L1X
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* VL53L1X
|
|
|
|
*
|
|
|
|
* Source:
|
|
|
|
*
|
|
|
|
* I2C Address: 0x29
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XSNS_77 77
|
2020-09-29 10:08:03 +01:00
|
|
|
#define XI2C_54 54 // See I2CDEVICES.md
|
2020-09-01 16:24:36 +01:00
|
|
|
|
2022-03-10 21:00:20 +00:00
|
|
|
#define VL53LXX_MAX_SENSORS VL53L0X_MAX_SENSORS
|
|
|
|
#define GPIO_VL53LXX_XSHUT1 GPIO_VL53L0X_XSHUT1
|
|
|
|
|
2020-09-01 16:24:36 +01:00
|
|
|
#include "VL53L1X.h"
|
2022-03-10 21:00:20 +00:00
|
|
|
VL53L1X vl53l1x_device[VL53LXX_MAX_SENSORS];
|
2020-09-01 16:24:36 +01:00
|
|
|
|
2022-03-10 21:00:20 +00:00
|
|
|
#define VL53LXX_ADDRESS 0x29
|
|
|
|
#ifndef VL53LXX_ADDRESS_MOVED
|
|
|
|
#define VL53LXX_ADDRESS_MOVED 0x78
|
|
|
|
#endif
|
2020-09-01 16:24:36 +01:00
|
|
|
|
|
|
|
struct {
|
|
|
|
uint16_t distance = 0;
|
2022-03-10 21:00:20 +00:00
|
|
|
} vl53l1x_data[VL53LXX_MAX_SENSORS];
|
|
|
|
|
|
|
|
bool VL53L1X_xshut = false;
|
|
|
|
bool VL53L1X_detected = false;
|
2020-09-01 16:24:36 +01:00
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void Vl53l1Detect(void) {
|
2022-03-10 21:00:20 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < VL53LXX_MAX_SENSORS; i++) {
|
|
|
|
if (PinUsed(GPIO_VL53LXX_XSHUT1, i)) {
|
|
|
|
pinMode(Pin(GPIO_VL53LXX_XSHUT1, i), OUTPUT);
|
|
|
|
digitalWrite(Pin(GPIO_VL53LXX_XSHUT1, i), i==0 ? 1 : 0);
|
|
|
|
VL53L1X_xshut = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
break; // XSHUT indexes must be continuous. We stop at the 1st not defined one
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!I2cSetDevice(VL53LXX_ADDRESS)) { return; }
|
|
|
|
if (!vl53l1x_device[0].init()) { return; }
|
|
|
|
|
|
|
|
I2cSetActiveFound(vl53l1x_device[0].getAddress(), "VL53L1X");
|
|
|
|
vl53l1x_device[0].setTimeout(500);
|
|
|
|
vl53l1x_device[0].setDistanceMode(VL53L1X::Long); // could be Short, Medium, Long
|
|
|
|
vl53l1x_device[0].setMeasurementTimingBudget(140000);
|
|
|
|
vl53l1x_device[0].startContinuous(50);
|
|
|
|
VL53L1X_detected = true;
|
2020-09-01 16:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Vl53l1Every_250MSecond(void) {
|
2022-03-10 21:00:20 +00:00
|
|
|
uint16_t dist = vl53l1x_device[0].read();
|
2020-09-01 16:24:36 +01:00
|
|
|
if (!dist || dist > 4000) {
|
|
|
|
dist = 9999;
|
|
|
|
}
|
2022-03-10 21:00:20 +00:00
|
|
|
vl53l1x_data[0].distance = dist;
|
2020-09-01 16:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
void Vl53l1Every_Second(void) {
|
2020-09-29 17:29:19 +01:00
|
|
|
char distance[FLOATSZ];
|
2022-03-10 21:00:20 +00:00
|
|
|
dtostrfd((float)vl53l1x_data[0].distance / 10, 1, distance);
|
2020-09-29 17:29:19 +01:00
|
|
|
DomoticzSensor(DZ_ILLUMINANCE, distance);
|
2020-09-01 16:24:36 +01:00
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
|
2020-09-29 10:27:53 +01:00
|
|
|
void Vl53l1Show(bool json) {
|
2020-09-01 16:24:36 +01:00
|
|
|
if (json) {
|
2022-03-10 21:00:20 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"VL53L1X\":{\"" D_JSON_DISTANCE "\":%d}"), vl53l1x_data[0].distance);
|
2020-09-01 16:24:36 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
2020-10-29 12:37:09 +00:00
|
|
|
if (0 == TasmotaGlobal.tele_period) {
|
2020-09-30 10:07:13 +01:00
|
|
|
Vl53l1Every_Second();
|
2020-09-01 16:24:36 +01:00
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2022-03-10 21:00:20 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_DISTANCE, PSTR("VL53L1X"), vl53l1x_data[0].distance);
|
2020-09-01 16:24:36 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2021-01-22 15:48:41 +00:00
|
|
|
bool Xsns77(uint8_t function) {
|
2020-09-29 10:08:03 +01:00
|
|
|
if (!I2cEnabled(XI2C_54)) { return false; }
|
2021-01-22 15:48:41 +00:00
|
|
|
|
2020-09-01 16:24:36 +01:00
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
Vl53l1Detect();
|
|
|
|
}
|
2022-03-10 21:00:20 +00:00
|
|
|
else if (VL53L1X_detected) {
|
2020-09-01 16:24:36 +01:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_250_MSECOND:
|
|
|
|
Vl53l1Every_250MSecond();
|
|
|
|
break;
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
Vl53l1Every_Second();
|
|
|
|
break;
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Vl53l1Show(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
Vl53l1Show(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_VL53L1X
|
|
|
|
#endif // USE_I2C
|