Skip to content

Commit

Permalink
Merge pull request #75 from Lyr3x/fix-error-handling
Browse files Browse the repository at this point in the history
Fix error handling
  • Loading branch information
Lyr3x committed Dec 26, 2021
2 parents 2c2c69b + a10126f commit d40b76d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
27 changes: 16 additions & 11 deletions components/roode/roode.cpp
Expand Up @@ -82,31 +82,33 @@ namespace esphome
getZoneDistance();
zone++;
zone = zone % 2;
// App.feed_wdt();
App.feed_wdt();
// unsigned long end = micros();
// unsigned long delta = end - start;
// ESP_LOGI("Roode loop", "loop took %lu microseconds", delta);
}

void Roode::handleSensorStatus()
bool Roode::handleSensorStatus()
{
statusString = VL53L1X::rangeStatusToString(sensor_status); // This function call will manipulate the range_status variable
if (last_sensor_status == sensor_status && sensor_status == VL53L1X::RangeStatus::RangeValid)
const char *statusString = VL53L1X::rangeStatusToString(sensor_status); // This function call will manipulate the range_status variable
ESP_LOGD(TAG, "Sensor status: %d, Last sensor status: %d", sensor_status, last_sensor_status);

if (last_sensor_status == sensor_status && sensor_status == 0)
{
if (status_sensor != nullptr)
{
status_sensor->publish_state(statusString);
}
return true;
}
if (sensor_status != VL53L1X::RangeStatus::RangeValid && sensor_status != VL53L1X::RangeStatus::SignalFail && sensor_status != VL53L1X::RangeStatus::WrapTargetFail)
if (sensor_status != 0 && sensor_status != 2 && sensor_status != 7)
{
ESP_LOGE(TAG, "Ranging failed with an error. status: %d, error: %s", sensor_status, statusString);
if (status_sensor != nullptr)
{
status_sensor->publish_state(statusString);
}
return;
return false;
}
return true;
}

void Roode::getZoneDistance()
{
static int PathTrack[] = {0, 0, 0, 0};
Expand All @@ -123,7 +125,10 @@ namespace esphome
distance = distanceSensor.read();
distanceSensor.writeReg(distanceSensor.SYSTEM__MODE_START, 0x80); // stop reading
sensor_status = distanceSensor.ranging_data.range_status;
handleSensorStatus();
if (!handleSensorStatus())
{
return;
}

if (use_sampling_)
{
Expand Down
3 changes: 1 addition & 2 deletions components/roode/roode.h
Expand Up @@ -86,12 +86,11 @@ namespace esphome
void getZoneDistance();
void sendCounter(uint16_t counter);
void recalibration();
void handleSensorStatus();
bool handleSensorStatus();

uint16_t distance = 0;
VL53L1X::RangeStatus last_sensor_status = VL53L1X::RangeStatus::None;
VL53L1X::RangeStatus sensor_status = VL53L1X::RangeStatus::None;
const char *statusString = "";
int DIST_THRESHOLD_MAX[2] = {0, 0}; // max treshold of the two zones
int DIST_THRESHOLD_MIN[2] = {0, 0}; // min treshold of the two zones
int roi_width_{6}; // width of the ROI
Expand Down

0 comments on commit d40b76d

Please sign in to comment.