mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-23 10:46:31 +00:00
Refactor and Domoticzify VL53L0X driver
Refactor and Domoticzify VL53L0X driver (#8637, #8640)
This commit is contained in:
parent
7e1c62e784
commit
61ea7535a8
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
xsns_45_vl53l0x.ino - VL53L0X support for Tasmota
|
xsns_45_vl53l0x.ino - VL53L0X time of flight sensor support for Tasmota
|
||||||
|
|
||||||
Copyright (C) 2020 Theo Arends and Gerhard Mutz
|
Copyright (C) 2020 Theo Arends and Gerhard Mutz
|
||||||
|
|
||||||
@ -19,23 +19,30 @@
|
|||||||
|
|
||||||
#ifdef USE_I2C
|
#ifdef USE_I2C
|
||||||
#ifdef USE_VL53L0X
|
#ifdef USE_VL53L0X
|
||||||
|
/*********************************************************************************************\
|
||||||
|
* VL53L0x time of flight sensor
|
||||||
|
*
|
||||||
|
* I2C Addres: 0x29
|
||||||
|
\*********************************************************************************************/
|
||||||
|
|
||||||
#define XSNS_45 45
|
#define XSNS_45 45
|
||||||
#define XI2C_31 31 // See I2CDEVICES.md
|
#define XI2C_31 31 // See I2CDEVICES.md
|
||||||
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include "VL53L0X.h"
|
#include "VL53L0X.h"
|
||||||
VL53L0X sensor;
|
VL53L0X sensor;
|
||||||
|
|
||||||
uint8_t vl53l0x_ready = 0;
|
struct {
|
||||||
uint16_t vl53l0x_distance;
|
uint16_t distance;
|
||||||
uint16_t Vl53l0_buffer[5];
|
uint16_t distance_prev;
|
||||||
uint8_t Vl53l0_index;
|
uint16_t buffer[5];
|
||||||
|
uint8_t ready = 0;
|
||||||
|
uint8_t index;
|
||||||
|
} Vl53l0x;
|
||||||
|
|
||||||
/********************************************************************************************/
|
/********************************************************************************************/
|
||||||
|
|
||||||
void Vl53l0Detect(void)
|
void Vl53l0Detect(void) {
|
||||||
{
|
|
||||||
if (!I2cSetDevice(0x29)) { return; }
|
if (!I2cSetDevice(0x29)) { return; }
|
||||||
|
|
||||||
if (!sensor.init()) { return; }
|
if (!sensor.init()) { return; }
|
||||||
@ -49,9 +56,9 @@ void Vl53l0Detect(void)
|
|||||||
// instead, provide a desired inter-measurement period in
|
// instead, provide a desired inter-measurement period in
|
||||||
// ms (e.g. sensor.startContinuous(100)).
|
// ms (e.g. sensor.startContinuous(100)).
|
||||||
sensor.startContinuous();
|
sensor.startContinuous();
|
||||||
vl53l0x_ready = 1;
|
Vl53l0x.ready = 1;
|
||||||
|
|
||||||
Vl53l0_index=0;
|
Vl53l0x.index = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_WEBSERVER
|
#ifdef USE_WEBSERVER
|
||||||
@ -61,50 +68,64 @@ const char HTTP_SNS_VL53L0X[] PROGMEM =
|
|||||||
|
|
||||||
#define USE_VL_MEDIAN
|
#define USE_VL_MEDIAN
|
||||||
|
|
||||||
void Vl53l0Every_250MSecond(void)
|
void Vl53l0Every_250MSecond(void) {
|
||||||
{
|
|
||||||
uint16_t tbuff[5],tmp;
|
|
||||||
uint8_t flag;
|
|
||||||
|
|
||||||
// every 200 ms
|
// every 200 ms
|
||||||
uint16_t dist = sensor.readRangeContinuousMillimeters();
|
uint16_t dist = sensor.readRangeContinuousMillimeters();
|
||||||
if (dist==0 || dist>2000) {
|
if ((0 == dist) || (dist > 2000)) {
|
||||||
dist=9999;
|
dist = 9999;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_VL_MEDIAN
|
#ifdef USE_VL_MEDIAN
|
||||||
// store in ring buffer
|
// store in ring buffer
|
||||||
Vl53l0_buffer[Vl53l0_index]=dist;
|
Vl53l0x.buffer[Vl53l0x.index] = dist;
|
||||||
Vl53l0_index++;
|
Vl53l0x.index++;
|
||||||
if (Vl53l0_index>=5) Vl53l0_index=0;
|
if (Vl53l0x.index >= 5) {
|
||||||
|
Vl53l0x.index = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// sort list and take median
|
// sort list and take median
|
||||||
memmove(tbuff,Vl53l0_buffer,sizeof(tbuff));
|
uint16_t tbuff[5];
|
||||||
for (byte ocnt=0; ocnt<5; ocnt++) {
|
memmove(tbuff, Vl53l0x.buffer, sizeof(tbuff));
|
||||||
flag=0;
|
uint16_t tmp;
|
||||||
for (byte count=0; count<4; count++) {
|
uint8_t flag;
|
||||||
if (tbuff[count]>tbuff[count+1]) {
|
for (uint32_t ocnt = 0; ocnt < 5; ocnt++) {
|
||||||
tmp=tbuff[count];
|
flag = 0;
|
||||||
tbuff[count]=tbuff[count+1];
|
for (uint32_t count = 0; count < 4; count++) {
|
||||||
tbuff[count+1]=tmp;
|
if (tbuff[count] > tbuff[count +1]) {
|
||||||
flag=1;
|
tmp = tbuff[count];
|
||||||
|
tbuff[count] = tbuff[count +1];
|
||||||
|
tbuff[count +1] = tmp;
|
||||||
|
flag = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!flag) break;
|
if (!flag) { break; }
|
||||||
}
|
}
|
||||||
vl53l0x_distance=tbuff[2];
|
Vl53l0x.distance = tbuff[2];
|
||||||
#else
|
#else
|
||||||
vl53l0x_distance=dist;
|
Vl53l0x.distance = dist;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void Vl53l0Show(boolean json)
|
#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) {
|
||||||
if (json) {
|
if (json) {
|
||||||
ResponseAppend_P(PSTR(",\"VL53L0X\":{\"" D_JSON_DISTANCE "\":%d}"), vl53l0x_distance);
|
ResponseAppend_P(PSTR(",\"VL53L0X\":{\"" D_JSON_DISTANCE "\":%d}"), Vl53l0x.distance);
|
||||||
|
#ifdef USE_DOMOTICZ
|
||||||
|
if (0 == tele_period) {
|
||||||
|
DomoticzSensor(DZ_ILLUMINANCE, Vl53l0x.distance);
|
||||||
|
}
|
||||||
|
#endif // USE_DOMOTICZ
|
||||||
#ifdef USE_WEBSERVER
|
#ifdef USE_WEBSERVER
|
||||||
} else {
|
} else {
|
||||||
WSContentSend_PD(HTTP_SNS_VL53L0X, vl53l0x_distance);
|
WSContentSend_PD(HTTP_SNS_VL53L0X, Vl53l0x.distance);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -122,11 +143,16 @@ bool Xsns45(byte function)
|
|||||||
if (FUNC_INIT == function) {
|
if (FUNC_INIT == function) {
|
||||||
Vl53l0Detect();
|
Vl53l0Detect();
|
||||||
}
|
}
|
||||||
else if (vl53l0x_ready) {
|
else if (Vl53l0x.ready) {
|
||||||
switch (function) {
|
switch (function) {
|
||||||
case FUNC_EVERY_250_MSECOND:
|
case FUNC_EVERY_250_MSECOND:
|
||||||
Vl53l0Every_250MSecond();
|
Vl53l0Every_250MSecond();
|
||||||
break;
|
break;
|
||||||
|
#ifdef USE_DOMOTICZ
|
||||||
|
case FUNC_EVERY_SECOND:
|
||||||
|
Vl53l0Every_Second();
|
||||||
|
break;
|
||||||
|
#endif // USE_DOMOTICZ
|
||||||
case FUNC_JSON_APPEND:
|
case FUNC_JSON_APPEND:
|
||||||
Vl53l0Show(1);
|
Vl53l0Show(1);
|
||||||
break;
|
break;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user