From b23fdea7c8ee58fa15498d33bd9dca93d96588ac Mon Sep 17 00:00:00 2001 From: Tyeth Gundry Date: Tue, 21 May 2024 14:31:45 +0100 Subject: [PATCH] NAU7802: refactor data flushing --- .../WipperSnapper_I2C_Driver_NAU7802.h | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_NAU7802.h b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_NAU7802.h index f953a341c..f1741da08 100644 --- a/src/components/i2c/drivers/WipperSnapper_I2C_Driver_NAU7802.h +++ b/src/components/i2c/drivers/WipperSnapper_I2C_Driver_NAU7802.h @@ -83,11 +83,7 @@ class WipperSnapper_I2C_Driver_NAU7802 : public WipperSnapper_I2C_Driver { } // Take 10 readings to flush out old readings (10 samples per second) - for (uint8_t skipCounter = 0; skipCounter < 10; skipCounter++) { - while (!_nau7802->available()) - delay(1); - _nau7802->read(); - } + flushNAU7802(10); for (int retries = 0; retries < 3; retries++) { if (_nau7802->calibrate(NAU7802_CALMOD_INTERNAL)) { @@ -101,6 +97,19 @@ class WipperSnapper_I2C_Driver_NAU7802 : public WipperSnapper_I2C_Driver { return false; } + /*******************************************************************************/ + /*! + @brief Gets datapoints from sensor and discards (flushes old data). + */ + /*******************************************************************************/ + void flushNAU7802(uint8_t count) { + for (uint8_t skipCounter = 0; skipCounter < count; skipCounter++) { + while (!_nau7802->available()) + delay(1); + _nau7802->read(); + } + } + /*******************************************************************************/ /*! @brief Gets the sensor's raw "force" value.