diff --git a/src/107-Arduino-TMF8801.cpp b/src/107-Arduino-TMF8801.cpp index f748839..7b08774 100644 --- a/src/107-Arduino-TMF8801.cpp +++ b/src/107-Arduino-TMF8801.cpp @@ -42,6 +42,7 @@ ArduinoTMF8801::ArduinoTMF8801(TMF8801::I2cWriteFunc write, , _calib_data{calib_data} , _algo_state{algo_state} , _gpio_control{0} +, _old_tid{0} { } @@ -186,6 +187,32 @@ void ArduinoTMF8801::get(unit::Length & distance) distance = _distance; } +bool ArduinoTMF8801::isDataReady() +{ + uint8_t const tid = _io.read(TMF8801::Register::TID); + + if ((tid >_old_tid) || ((tid == 0) && (_old_tid == 255))) + { + if(_api.getRegisterContent() != TMF8801::RegisterContent::CommandResult) + return false; + + _old_tid = tid; + return true; + } + return false; +} + +unit::Length ArduinoTMF8801::getDistance() +{ + /* Clear the interrupt flag. */ + _api.clearInterrupt(TMF8801::InterruptSource::ObjectDectectionAvailable); + + /* Obtain distance data. */ + TMF8801::ObjectDetectionData data; + _api.application_readObjectDetectionResult(data); + return (data.field.distance_peak_0_mm / 1000.0) * unit::meter; +} + void ArduinoTMF8801::clearerr() { _error = TMF8801::Error::None; diff --git a/src/107-Arduino-TMF8801.h b/src/107-Arduino-TMF8801.h index 29c2685..b9c44f1 100644 --- a/src/107-Arduino-TMF8801.h +++ b/src/107-Arduino-TMF8801.h @@ -65,6 +65,8 @@ class ArduinoTMF8801 : public LengthSensorBase void onExternalEventHandler(); + bool isDataReady(); + unit::Length getDistance(); virtual void get(unit::Length & distance) override; @@ -81,6 +83,7 @@ class ArduinoTMF8801 : public LengthSensorBase TMF8801::AlgoState const & _algo_state; unit::Length _distance; uint8_t _gpio_control; + uint8_t _old_tid; bool update_available(); bool perform_update(uint8_t const * ram_firmware, size_t const ram_firmware_bytes);