2020-09-01 16:24:36 +01:00
|
|
|
/*
|
2022-12-10 15:55:04 +00:00
|
|
|
xsns_77_vl53l1x.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
|
2022-03-11 22:12:57 +00:00
|
|
|
*********************************************************************************************
|
|
|
|
*
|
|
|
|
* Note: When using multiple VL53L0X, it is required to also wire the XSHUT pin of all those sensors
|
|
|
|
* in order to let Tasmota change by software the I2C address of those and give them an unique address
|
|
|
|
* for operation. The sensor don't save its address, so this procedure of changing its address is needed
|
|
|
|
* to be performed every restart. The Addresses used for this are 120 (0x78) to 127 (0x7F). In the I2c
|
|
|
|
* Standard (https://i2cdevices.org/addresses) those addresses are used by the PCA9685.
|
|
|
|
* The base address (0x78) can be changed as a compile option with #define VL53L1X_XSHUT_ADDRESS 0xNN in
|
|
|
|
* your user_config_override.h
|
|
|
|
*
|
|
|
|
* The default value of VL53LXX_MAX_SENSORS is set in the file tasmota.h
|
|
|
|
* Changing that is backwards incompatible - Max supported devices by this driver are 8
|
|
|
|
*********************************************************************************************
|
|
|
|
* The following settings can be overriden
|
|
|
|
*
|
|
|
|
*
|
2020-09-01 16:24:36 +01:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
#include "VL53L1X.h"
|
|
|
|
|
2022-03-11 22:12:57 +00:00
|
|
|
#define VL53L1X_ADDRESS 0x29
|
|
|
|
#ifndef VL53L1X_XSHUT_ADDRESS
|
|
|
|
#define VL53L1X_XSHUT_ADDRESS 0x78
|
2022-03-10 21:00:20 +00:00
|
|
|
#endif
|
2020-09-01 16:24:36 +01:00
|
|
|
|
2022-03-11 21:02:32 +00:00
|
|
|
#ifndef VL53L1X_DISTANCE_MODE
|
|
|
|
#define VL53L1X_DISTANCE_MODE Long
|
|
|
|
#endif
|
|
|
|
|
2022-03-11 22:12:57 +00:00
|
|
|
VL53L1X vl53l1x_device[VL53LXX_MAX_SENSORS];
|
2022-03-11 21:02:32 +00:00
|
|
|
|
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];
|
|
|
|
|
2022-03-10 21:46:08 +00:00
|
|
|
uint8_t VL53L1X_xshut = 0;
|
|
|
|
uint8_t VL53L1X_detected = 0;
|
2020-09-01 16:24:36 +01:00
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void Vl53l1Detect(void) {
|
2022-03-10 21:00:20 +00:00
|
|
|
|
2022-03-10 21:46:08 +00:00
|
|
|
uint32_t i, xshut;
|
|
|
|
for (i = 0, xshut = 1 ; i < VL53LXX_MAX_SENSORS ; i++, xshut <<= 1) {
|
2022-03-10 21:00:20 +00:00
|
|
|
if (PinUsed(GPIO_VL53LXX_XSHUT1, i)) {
|
|
|
|
pinMode(Pin(GPIO_VL53LXX_XSHUT1, i), OUTPUT);
|
2022-03-10 21:46:08 +00:00
|
|
|
digitalWrite(Pin(GPIO_VL53LXX_XSHUT1, i), 0);
|
|
|
|
VL53L1X_xshut |= xshut;
|
2022-03-10 21:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-10 23:30:25 +00:00
|
|
|
for (i = 0, xshut = 1 ; i < VL53LXX_MAX_SENSORS ; i++, xshut <<= 1) {
|
2022-03-10 21:46:08 +00:00
|
|
|
if (xshut & VL53L1X_xshut) {
|
|
|
|
digitalWrite(Pin(GPIO_VL53LXX_XSHUT1, i), 1);
|
|
|
|
delay(2);
|
|
|
|
}
|
2022-03-11 22:12:57 +00:00
|
|
|
if (!I2cSetDevice(VL53L1X_ADDRESS) && !I2cSetDevice((uint8_t)(VL53L1X_XSHUT_ADDRESS+i))) { continue; } // Detection for unconfigured OR configured sensor
|
2022-03-10 21:46:08 +00:00
|
|
|
if (vl53l1x_device[i].init()) {
|
2022-03-10 23:30:25 +00:00
|
|
|
if (VL53L1X_xshut) {
|
2022-03-11 22:12:57 +00:00
|
|
|
vl53l1x_device[i].setAddress((uint8_t)(VL53L1X_XSHUT_ADDRESS+i));
|
2022-03-10 23:30:25 +00:00
|
|
|
}
|
|
|
|
uint8_t addr = vl53l1x_device[i].getAddress();
|
2022-03-10 21:46:08 +00:00
|
|
|
vl53l1x_device[i].setTimeout(500);
|
2022-03-11 21:02:32 +00:00
|
|
|
vl53l1x_device[i].setDistanceMode(VL53L1X::VL53L1X_DISTANCE_MODE); // could be Short, Medium, Long
|
2022-03-10 21:46:08 +00:00
|
|
|
vl53l1x_device[i].setMeasurementTimingBudget(140000);
|
|
|
|
vl53l1x_device[i].startContinuous(50);
|
|
|
|
VL53L1X_detected |= xshut;
|
|
|
|
|
|
|
|
if (VL53L1X_xshut) {
|
|
|
|
I2cSetActive(addr);
|
|
|
|
AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_I2C D_SENSOR " VL53L1X-%d " D_SENSOR_DETECTED " - " D_NEW_ADDRESS " 0x%02X"), i+1, addr);
|
|
|
|
} else {
|
|
|
|
I2cSetActiveFound(addr, "VL53L1X");
|
|
|
|
}
|
2022-03-10 23:30:25 +00:00
|
|
|
} // if init
|
|
|
|
if (0 == VL53L1X_xshut) break;
|
|
|
|
} // for
|
2020-09-01 16:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Vl53l1Every_250MSecond(void) {
|
2022-03-10 21:46:08 +00:00
|
|
|
uint32_t i, xshut;
|
|
|
|
for (i = 0, xshut = 1; i < VL53LXX_MAX_SENSORS; i++, xshut <<= 1) {
|
|
|
|
if (xshut & VL53L1X_detected) {
|
|
|
|
uint16_t dist = vl53l1x_device[i].read();
|
|
|
|
if (!dist || dist > 4000) {
|
|
|
|
dist = 9999;
|
|
|
|
}
|
|
|
|
vl53l1x_data[i].distance = dist;
|
|
|
|
} // if detected
|
2022-03-10 23:30:25 +00:00
|
|
|
if (0 == VL53L1X_xshut) break;
|
2022-03-10 21:46:08 +00:00
|
|
|
} // for
|
2020-09-01 16:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
void Vl53l1Every_Second(void) {
|
2022-11-08 15:59:46 +00:00
|
|
|
float distance = (float)vl53l1x_data[0].distance / 10; // cm
|
|
|
|
DomoticzFloatSensor(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) {
|
2022-03-10 21:46:08 +00:00
|
|
|
uint32_t i, xshut;
|
|
|
|
for (i = 0, xshut = 1 ; i < VL53LXX_MAX_SENSORS ; i++, xshut <<= 1) {
|
2022-11-08 15:59:46 +00:00
|
|
|
char types[12] = "VL53L1X";
|
2022-11-08 17:08:45 +00:00
|
|
|
if (VL53L1X_xshut) {
|
2022-11-08 15:59:46 +00:00
|
|
|
snprintf_P(types, sizeof(types), PSTR("VL53L1X%c%d"), IndexSeparator(), i +1);
|
|
|
|
}
|
|
|
|
float distance = (float)vl53l1x_data[i].distance / 10; // cm
|
2022-03-10 21:46:08 +00:00
|
|
|
if (xshut & VL53L1X_detected) {
|
|
|
|
if (json) {
|
2022-11-08 15:59:46 +00:00
|
|
|
ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_DISTANCE "\":%1_f}"), types, &distance);
|
2020-09-01 16:24:36 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
2022-03-10 21:46:08 +00:00
|
|
|
if (0 == TasmotaGlobal.tele_period) {
|
|
|
|
Vl53l1Every_Second();
|
|
|
|
}
|
2020-09-01 16:24:36 +01:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
2022-11-08 15:59:46 +00:00
|
|
|
} else {
|
|
|
|
WSContentSend_PD(HTTP_SNS_F_DISTANCE_CM, types, &distance);
|
2020-09-01 16:24:36 +01:00
|
|
|
#endif
|
2022-03-10 21:46:08 +00:00
|
|
|
}
|
|
|
|
} // if detected
|
2022-03-10 23:30:25 +00:00
|
|
|
if (0 == VL53L1X_xshut) break;
|
2022-03-10 21:46:08 +00:00
|
|
|
} // for
|
2020-09-01 16:24:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xsns77(uint32_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
|