move vl53l1x to class

This commit is contained in:
interfisch 2021-11-07 22:31:24 +01:00
parent c5bad1cc44
commit 275c641ec8
4 changed files with 150 additions and 103 deletions

View File

@ -0,0 +1,97 @@
//Connect SCL to D1, SDA to D2, GND and 3v3
#include "sensor_vl53l1x.h"
Sensor_VL53L1X::Sensor_VL53L1X()
{
vl53l1x = new VL53L1X();
}
void Sensor_VL53L1X::init() //Things to be done during setup()
{
Serial.println("initializing vl53l1x");
vl53l1x->setTimeout(500);
if (!vl53l1x->init()) {
Serial.println("No vl53l1x found!");
}else{
init_ok=true;
vl53l1x->setDistanceMode(VL53L1X::Long);
vl53l1x->setMeasurementTimingBudget(50000);
vl53l1x->startContinuous(1000); //This period should be at least as long as the timing budget.
}
}
//Also called during setup()
void Sensor_VL53L1X::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay)
{
data.minchange=minchange;
data.senddelaymax=senddelaymax;
data.readdelay=readdelay;
}
//Called during setup
void Sensor_VL53L1X::advertise(HomieNode& p_sensorNode)
{
sensorNode = &p_sensorNode;
sensorNode->advertise("tofstatus");
sensorNode->advertise("tofrange");
sensorNode->advertise("tofpeaksignal");
sensorNode->advertise("tofambient");
}
void Sensor_VL53L1X::sensorloop()
{
if (init_ok) {
sensordata &d=data;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis() >= (lastread_vl53l1x+d.readdelay)) { //avoid reading sensor twice in a short time
//tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c);
vl53l1x->read();
lastread_vl53l1x=millis();
}
d.value=vl53l1x->ranging_data.range_mm;
/* for debugging
Serial.print("range: ");
Serial.print(vl53l1x.ranging_data.range_mm);
Serial.print("\tstatus: ");
Serial.print(VL53L1X::rangeStatusToString(vl53l1x.ranging_data.range_status));
Serial.print("\tstatus=");
Serial.print(vl53l1x.ranging_data.range_status);
Serial.print("\tpeak signal: ");
Serial.print(vl53l1x.ranging_data.peak_signal_count_rate_MCPS);
Serial.print("\tambient: ");
Serial.print(vl53l1x.ranging_data.ambient_count_rate_MCPS);
Serial.println();
*/
if (abs((int)d.lastsentvalue-d.value)>=d.minchange){ //int abs
_changed=true;
}
if (lastsentvalue_vl53l1x_status!=vl53l1x->ranging_data.range_status) { //sensor status changed
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending VL53L1X range. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
Homie.getLogger() << "range vl53l1x " << ": " << d.value << endl;
sensorNode->setProperty("tofstatus").send(VL53L1X::rangeStatusToString(vl53l1x->ranging_data.range_status));
sensorNode->setProperty("tofrange").send(String(d.value));
sensorNode->setProperty("tofpeaksignal").send(String(vl53l1x->ranging_data.peak_signal_count_rate_MCPS));
sensorNode->setProperty("tofambient").send(String(vl53l1x->ranging_data.ambient_count_rate_MCPS));
d.lastsentvalue=d.value;
lastsentvalue_vl53l1x_status=vl53l1x->ranging_data.range_status;
d.lastsent=millis();
}
}
}

36
include/sensor_vl53l1x.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef SENSOR_VL53L1X_H
#define SENSOR_VL53L1X_H
#include "sensordata.h"
#include <Homie.h>
#include <Wire.h>
#include <VL53L1X.h>
class Sensor_VL53L1X
{
private:
VL53L1X *vl53l1x;
HomieNode *sensorNode; //reference to HomieNode
struct sensordata data; //struct values are changed in setup()
bool init_ok;
unsigned long lastread_vl53l1x=0;
VL53L1X::RangeStatus lastsentvalue_vl53l1x_status;
public:
Sensor_VL53L1X();
void init();
void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay);
void advertise(HomieNode& p_sensorNode);
void sensorloop();
};
#endif

View File

@ -258,9 +258,9 @@ build_flags =
-D dataBH1750_senddelaymax=1000*60*2
-D SENSOR_VL53L1X
-D dataVL53L1X_minchange=100
-D dataVL53L1X_senddelaymax=1000*30
-D dataVL53L1X_readdelay=1000
-D SENSOR_VL53L1X_minchange=100
-D SENSOR_VL53L1X_senddelaymax=1000*30
-D SENSOR_VL53L1X_readdelay=1000

View File

@ -251,19 +251,17 @@
#endif
#ifdef SENSOR_VL53L1X
#ifndef WIRE_H
#include <Wire.h>
#define WIRE_H
#include "sensor_vl53l1x.cpp"
Sensor_VL53L1X sensor_vl53l1x;
#ifndef SENSOR_VL53L1X_minchange
#define SENSOR_VL53L1X_minchange 100
#endif
#ifndef SENSOR_VL53L1X_senddelaymax
#define SENSOR_VL53L1X_senddelaymax 1000*30
#endif
#ifndef SENSOR_VL53L1X_readdelay
#define SENSOR_VL53L1X_readdelay 1000
#endif
#include <VL53L1X.h>
VL53L1X vl53l1x;
bool vl53l1xinit_ok=false;
struct sensordata dataVL53L1X;
uint16_t value_vl53l1x_range;
unsigned long lastread_vl53l1x=0;
VL53L1X::RangeStatus lastsentvalue_vl53l1x_status;
#endif
#ifdef SENSOR_ANEMOMETER
@ -398,26 +396,8 @@ void setup() {
#endif
#ifdef SENSOR_VL53L1X
Serial.println("initializing vl53l1x");
vl53l1x.setTimeout(500);
if (!vl53l1x.init()) {
Serial.println("No vl53l1x found!");
}else{
vl53l1xinit_ok=true;
vl53l1x.setDistanceMode(VL53L1X::Long);
vl53l1x.setMeasurementTimingBudget(50000);
vl53l1x.startContinuous(1000); //This period should be at least as long as the timing budget.
}
#ifdef dataVL53L1X_minchange
dataVL53L1X.minchange=dataVL53L1X_minchange;
#endif
#ifdef dataVL53L1X_senddelaymax
dataVL53L1X.senddelaymax=dataVL53L1X_senddelaymax;
#endif
#ifdef dataVL53L1X_readdelay
dataVL53L1X.readdelay=dataVL53L1X_readdelay;
#endif
sensor_vl53l1x.init();
sensor_vl53l1x.setSettings(SENSOR_VL53L1X_minchange,SENSOR_VL53L1X_senddelaymax,SENSOR_VL53L1X_readdelay);
#endif
#ifdef SENSOR_ANEMOMETER
@ -509,10 +489,7 @@ void setup() {
#endif
#ifdef SENSOR_VL53L1X
sensorNode.advertise("tofstatus");
sensorNode.advertise("tofrange");
sensorNode.advertise("tofpeaksignal");
sensorNode.advertise("tofambient");
sensor_vl53l1x.advertise(sensorNode);
#endif
#ifdef SENSOR_ANEMOMETER
@ -535,67 +512,6 @@ void loop() {
}
#ifdef SENSOR_VL53L1X
void loop_VL53L1X()
{
sensordata &d=dataVL53L1X;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis() >= (lastread_vl53l1x+d.readdelay)) { //avoid reading sensor twice in a short time
//tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c);
vl53l1x.read();
lastread_vl53l1x=millis();
}
value_vl53l1x_range=vl53l1x.ranging_data.range_mm;
/* for debugging
Serial.print("range: ");
Serial.print(vl53l1x.ranging_data.range_mm);
Serial.print("\tstatus: ");
Serial.print(VL53L1X::rangeStatusToString(vl53l1x.ranging_data.range_status));
Serial.print("\tstatus=");
Serial.print(vl53l1x.ranging_data.range_status);
Serial.print("\tpeak signal: ");
Serial.print(vl53l1x.ranging_data.peak_signal_count_rate_MCPS);
Serial.print("\tambient: ");
Serial.print(vl53l1x.ranging_data.ambient_count_rate_MCPS);
Serial.println();
*/
if (abs((int)d.lastsentvalue-value_vl53l1x_range)>=d.minchange){ //int abs
_changed=true;
}
if (lastsentvalue_vl53l1x_status!=vl53l1x.ranging_data.range_status) { //sensor status changed
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending VL53L1X range. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "range vl53l1x " << ": " << value_vl53l1x_range << endl;
sensorNode.setProperty("tofstatus").send(VL53L1X::rangeStatusToString(vl53l1x.ranging_data.range_status));
sensorNode.setProperty("tofrange").send(String(value_vl53l1x_range));
sensorNode.setProperty("tofpeaksignal").send(String(vl53l1x.ranging_data.peak_signal_count_rate_MCPS));
sensorNode.setProperty("tofambient").send(String(vl53l1x.ranging_data.ambient_count_rate_MCPS));
d.lastsentvalue=value_vl53l1x_range;
lastsentvalue_vl53l1x_status=vl53l1x.ranging_data.range_status;
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_ANEMOMETER
void loop_anemometer()
{
@ -734,9 +650,7 @@ void loopHandler() {
#endif
#ifdef SENSOR_VL53L1X
if (vl53l1xinit_ok) {
loop_VL53L1X();
}
sensor_vl53l1x.sensorloop();
#endif
#ifdef SENSOR_ANEMOMETER