2019-05-22 12:21:41 +01:00
|
|
|
/*
|
2020-06-08 14:09:51 +01:00
|
|
|
xsns_45_vl53l0x.ino - VL53L0X time of flight sensor support for Tasmota
|
2019-05-22 12:21:41 +01:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Theo Arends and Gerhard Mutz
|
2019-05-22 12:21:41 +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_VL53L0X
|
2020-06-08 14:09:51 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* VL53L0x time of flight sensor
|
|
|
|
*
|
|
|
|
* I2C Addres: 0x29
|
|
|
|
\*********************************************************************************************/
|
2019-05-22 12:21:41 +01:00
|
|
|
|
2021-01-22 15:48:41 +00:00
|
|
|
#define XSNS_45 45
|
|
|
|
#define XI2C_31 31 // See I2CDEVICES.md
|
|
|
|
|
|
|
|
#define USE_VL_MEDIAN
|
|
|
|
#define USE_VL_MEDIAN_SIZE 5 // Odd number of samples median detection
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-05-22 12:21:41 +01:00
|
|
|
#include <Wire.h>
|
|
|
|
#include "VL53L0X.h"
|
|
|
|
VL53L0X sensor;
|
|
|
|
|
2020-06-08 14:09:51 +01:00
|
|
|
struct {
|
|
|
|
uint16_t distance;
|
|
|
|
uint16_t distance_prev;
|
|
|
|
uint16_t buffer[5];
|
|
|
|
uint8_t ready = 0;
|
|
|
|
uint8_t index;
|
|
|
|
} Vl53l0x;
|
2019-05-22 12:21:41 +01:00
|
|
|
|
|
|
|
/********************************************************************************************/
|
|
|
|
|
2020-06-08 14:09:51 +01:00
|
|
|
void Vl53l0Detect(void) {
|
2019-11-07 15:56:05 +00:00
|
|
|
if (!I2cSetDevice(0x29)) { return; }
|
|
|
|
if (!sensor.init()) { return; }
|
2019-05-22 12:21:41 +01:00
|
|
|
|
2019-11-10 16:02:02 +00:00
|
|
|
I2cSetActiveFound(sensor.getAddress(), "VL53L0X");
|
2019-05-22 12:21:41 +01:00
|
|
|
|
|
|
|
sensor.setTimeout(500);
|
|
|
|
|
|
|
|
// Start continuous back-to-back mode (take readings as
|
|
|
|
// fast as possible). To use continuous timed mode
|
|
|
|
// instead, provide a desired inter-measurement period in
|
|
|
|
// ms (e.g. sensor.startContinuous(100)).
|
|
|
|
sensor.startContinuous();
|
2020-06-08 14:09:51 +01:00
|
|
|
Vl53l0x.ready = 1;
|
2019-05-22 12:21:41 +01:00
|
|
|
|
2020-06-08 14:09:51 +01:00
|
|
|
Vl53l0x.index = 0;
|
2019-05-22 12:21:41 +01:00
|
|
|
}
|
|
|
|
|
2020-06-08 14:09:51 +01:00
|
|
|
void Vl53l0Every_250MSecond(void) {
|
2019-05-22 12:21:41 +01:00
|
|
|
uint16_t dist = sensor.readRangeContinuousMillimeters();
|
2020-06-08 14:09:51 +01:00
|
|
|
if ((0 == dist) || (dist > 2000)) {
|
|
|
|
dist = 9999;
|
2019-05-22 12:21:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_VL_MEDIAN
|
|
|
|
// store in ring buffer
|
2020-06-08 14:09:51 +01:00
|
|
|
Vl53l0x.buffer[Vl53l0x.index] = dist;
|
|
|
|
Vl53l0x.index++;
|
2021-01-22 15:48:41 +00:00
|
|
|
if (Vl53l0x.index >= USE_VL_MEDIAN_SIZE) {
|
2020-06-08 14:09:51 +01:00
|
|
|
Vl53l0x.index = 0;
|
|
|
|
}
|
2019-05-22 12:21:41 +01:00
|
|
|
|
|
|
|
// sort list and take median
|
2021-01-22 15:48:41 +00:00
|
|
|
uint16_t tbuff[USE_VL_MEDIAN_SIZE];
|
2020-06-08 14:09:51 +01:00
|
|
|
memmove(tbuff, Vl53l0x.buffer, sizeof(tbuff));
|
|
|
|
uint16_t tmp;
|
|
|
|
uint8_t flag;
|
2021-01-22 15:48:41 +00:00
|
|
|
for (uint32_t ocnt = 0; ocnt < USE_VL_MEDIAN_SIZE; ocnt++) {
|
2020-06-08 14:09:51 +01:00
|
|
|
flag = 0;
|
2021-01-22 15:48:41 +00:00
|
|
|
for (uint32_t count = 0; count < USE_VL_MEDIAN_SIZE -1; count++) {
|
2020-06-08 14:09:51 +01:00
|
|
|
if (tbuff[count] > tbuff[count +1]) {
|
|
|
|
tmp = tbuff[count];
|
|
|
|
tbuff[count] = tbuff[count +1];
|
|
|
|
tbuff[count +1] = tmp;
|
|
|
|
flag = 1;
|
2019-05-22 12:21:41 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-08 14:09:51 +01:00
|
|
|
if (!flag) { break; }
|
2019-05-22 12:21:41 +01:00
|
|
|
}
|
2021-01-22 15:48:41 +00:00
|
|
|
Vl53l0x.distance = tbuff[(USE_VL_MEDIAN_SIZE -1) / 2];
|
2019-05-22 12:21:41 +01:00
|
|
|
#else
|
2020-06-08 14:09:51 +01:00
|
|
|
Vl53l0x.distance = dist;
|
2019-05-22 12:21:41 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-06-08 14:09:51 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
void Vl53l0Every_Second(void) {
|
|
|
|
if (abs(Vl53l0x.distance - Vl53l0x.distance_prev) > 8) {
|
|
|
|
Vl53l0x.distance_prev = Vl53l0x.distance;
|
|
|
|
DomoticzSensor(DZ_ILLUMINANCE, Vl53l0x.distance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
|
|
|
|
void Vl53l0Show(boolean json) {
|
2019-05-22 12:21:41 +01:00
|
|
|
if (json) {
|
2020-06-08 14:09:51 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"VL53L0X\":{\"" D_JSON_DISTANCE "\":%d}"), Vl53l0x.distance);
|
|
|
|
#ifdef USE_DOMOTICZ
|
2020-10-29 12:37:09 +00:00
|
|
|
if (0 == TasmotaGlobal.tele_period) {
|
2020-06-08 14:09:51 +01:00
|
|
|
DomoticzSensor(DZ_ILLUMINANCE, Vl53l0x.distance);
|
|
|
|
}
|
|
|
|
#endif // USE_DOMOTICZ
|
2019-05-22 12:21:41 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2021-01-22 15:48:41 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_DISTANCE, PSTR("VL53L0X"), Vl53l0x.distance);
|
2019-05-22 12:21:41 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2021-01-22 15:48:41 +00:00
|
|
|
bool Xsns45(byte function) {
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_31)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-05-22 12:21:41 +01:00
|
|
|
bool result = false;
|
|
|
|
|
2019-11-11 17:36:13 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
Vl53l0Detect();
|
|
|
|
}
|
2020-06-08 14:09:51 +01:00
|
|
|
else if (Vl53l0x.ready) {
|
2019-11-11 17:36:13 +00:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_250_MSECOND:
|
|
|
|
Vl53l0Every_250MSecond();
|
|
|
|
break;
|
2020-06-08 14:09:51 +01:00
|
|
|
#ifdef USE_DOMOTICZ
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
Vl53l0Every_Second();
|
|
|
|
break;
|
|
|
|
#endif // USE_DOMOTICZ
|
2019-11-11 17:36:13 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Vl53l0Show(1);
|
|
|
|
break;
|
2019-05-22 12:21:41 +01:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-11-11 17:36:13 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
Vl53l0Show(0);
|
|
|
|
break;
|
2019-05-22 12:21:41 +01:00
|
|
|
#endif // USE_WEBSERVER
|
2019-11-11 17:36:13 +00:00
|
|
|
}
|
2019-05-22 12:21:41 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_VL53L0X
|
|
|
|
#endif // USE_I2C
|