|
1 | 1 | #include "configuration.h" |
2 | | -#include <memory> |
3 | 2 |
|
4 | 3 | #if !MESHTASTIC_EXCLUDE_ENVIRONMENTAL_SENSOR && __has_include(<Adafruit_VL53L0X.h>) |
5 | 4 |
|
6 | 5 | #include "../mesh/generated/meshtastic/telemetry.pb.h" |
7 | | -#include "TelemetrySensor.h" |
| 6 | +#include <memory> |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +#include "../mesh/generated/meshtastic/telemetry.pb.h" |
| 11 | +#include "FSCommon.h" |
8 | 12 | #include "VL53L0XSensor.h" |
| 13 | +#include "SPILock.h" |
| 14 | +#include "SafeFile.h" |
| 15 | +#include "TelemetrySensor.h" |
9 | 16 |
|
10 | 17 | #include <Adafruit_VL53L0X.h> |
11 | 18 | #include <typeinfo> |
| 19 | +#include <pb_decode.h> |
| 20 | +#include <pb_encode.h> |
| 21 | + |
| 22 | + |
| 23 | +bool VL53L0XSensor::loadState() |
| 24 | +{ |
| 25 | +#ifdef FSCom |
| 26 | + spiLock->lock(); |
| 27 | + auto file = FSCom.open(VL53L0XStateFileName, FILE_O_READ); |
| 28 | + bool okay = false; |
| 29 | + if (file) { |
| 30 | + LOG_INFO("%s state read from %s", sensorName, VL53L0XStateFileName); |
| 31 | + pb_istream_t stream = {&readcb, &file, meshtastic_VL53L0XState_size}; |
| 32 | + |
| 33 | + if (!pb_decode(&stream, &meshtastic_VL53L0XState_msg, &vl53state)) { |
| 34 | + LOG_ERROR("Error: can't decode protobuf %s", PB_GET_ERROR(&stream)); |
| 35 | + } else { |
| 36 | + mode = (Adafruit_VL53L0X::VL53L0X_Sense_config_t)vl53state.mode; |
| 37 | + okay = true; |
| 38 | + } |
| 39 | + file.close(); |
| 40 | + } else { |
| 41 | + LOG_INFO("No %s state found (File: %s)", sensorName, VL53L0XStateFileName); |
| 42 | + } |
| 43 | + spiLock->unlock(); |
| 44 | + return okay; |
| 45 | +#else |
| 46 | + LOG_ERROR("%s: ERROR - Filesystem not implemented", sensorName); |
| 47 | +#endif |
| 48 | +} |
| 49 | + |
| 50 | +bool VL53L0XSensor::saveState() |
| 51 | +{ |
| 52 | +#ifdef FSCom |
| 53 | + auto file = SafeFile(VL53L0XStateFileName); |
| 54 | + |
| 55 | + vl53state.mode = (meshtastic_VL53L0XState_RangingMode)mode; |
| 56 | + |
| 57 | + bool okay = false; |
| 58 | + |
| 59 | + LOG_INFO("%s: state write to %s", sensorName, VL53L0XStateFileName); |
| 60 | + pb_ostream_t stream = {&writecb, static_cast<Print *>(&file), meshtastic_VL53L0XState_size}; |
| 61 | + |
| 62 | + if (!pb_encode(&stream, &meshtastic_VL53L0XState_msg, &vl53state)) { |
| 63 | + LOG_ERROR("Error: can't encode protobuf %s", PB_GET_ERROR(&stream)); |
| 64 | + } else { |
| 65 | + okay = true; |
| 66 | + } |
| 67 | + |
| 68 | + okay &= file.close(); |
| 69 | + |
| 70 | + if (okay) |
| 71 | + LOG_INFO("%s: state write to %s successful", sensorName, VL53L0XStateFileName); |
| 72 | + |
| 73 | + return okay; |
| 74 | +#else |
| 75 | + LOG_ERROR("%s: ERROR - Filesystem not implemented", sensorName); |
| 76 | +#endif |
| 77 | +} |
12 | 78 |
|
13 | 79 | VL53L0XSensor::VL53L0XSensor() : TelemetrySensor(meshtastic_TelemetrySensorType_VL53L0X, "VL53L0X") {} |
14 | 80 |
|
15 | 81 | bool VL53L0XSensor::initDevice(TwoWire *bus, ScanI2C::FoundDevice *dev) |
16 | 82 | { |
17 | 83 | LOG_INFO("Init sensor: %s", sensorName); |
18 | | - status = vl53l0x.begin(VL53L0X_I2C_ADDR, false, bus); |
| 84 | + |
| 85 | + loadState(); |
| 86 | + |
| 87 | + status = vl53l0x.begin(VL53L0X_I2C_ADDR, false, bus, mode); |
| 88 | + |
19 | 89 | if (!status) { |
20 | 90 | return status; |
21 | 91 | } |
|
0 commit comments