Skip to content

Commit

Permalink
Merge pull request #577 from tyeth/add-NAU7802
Browse files Browse the repository at this point in the history
Add NAU7802
  • Loading branch information
tyeth authored May 23, 2024
2 parents a4e63dc + 0089c50 commit d102bc7
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 3 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"limits": "c",
"type_traits": "c"
},
"C_Cpp.dimInactiveRegions": false,
"dotnet.defaultSolution": "disable"
"C_Cpp.dimInactiveRegions": true,
"dotnet.defaultSolution": "disable",
"cmake.configureOnOpen": false
}
1 change: 1 addition & 0 deletions ci
Submodule ci added at 72918f
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ paragraph=Arduino application for Adafruit.io WipperSnapper
category=Communication
url=https://github.com/adafruit/Adafruit_Wippersnapper_Arduino
architectures=*
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork
depends=Adafruit NeoPixel, Adafruit SPIFlash, ArduinoJson, Adafruit DotStar, Adafruit INA219, Adafruit LTR329 and LTR303, Adafruit LTR390 Library, Adafruit MCP3421, Adafruit NAU7802 Library, Adafruit SleepyDog Library, Adafruit TMP117, Adafruit TinyUSB Library, Adafruit AHTX0, Adafruit BME280 Library, Adafruit BMP280 Library, Adafruit BMP3XX Library, Adafruit DPS310, Adafruit SCD30, Adafruit SGP30 Sensor, Adafruit SGP40 Sensor, Sensirion I2C SCD4x, Sensirion I2C SEN5X, arduino-sht, Adafruit Si7021 Library, Adafruit MQTT Library, Adafruit MS8607, Adafruit MCP9808 Library, Adafruit MCP9600 Library, Adafruit MPL115A2, Adafruit MPRLS Library, Adafruit TSL2591 Library, Adafruit_VL53L0X, Adafruit VL53L1X, STM32duino VL53L4CD, Adafruit_VL6180X, Adafruit PM25 AQI Sensor, Adafruit VCNL4020 Library, Adafruit VCNL4040, Adafruit VEML7700 Library, Adafruit LC709203F, Adafruit LPS2X, Adafruit LPS35HW, Adafruit seesaw Library, Adafruit BME680 Library, Adafruit MAX1704X, Adafruit ADT7410 Library, Adafruit HTS221, Adafruit HTU21DF Library, Adafruit HTU31D Library, Adafruit PCT2075, hp_BH1750, ENS160 - Adafruit Fork
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ lib_deps =
adafruit/Adafruit MPL115A2
adafruit/Adafruit MPRLS Library
adafruit/Adafruit MS8607
adafruit/Adafruit NAU7802 Library
adafruit/Adafruit TMP117
adafruit/Adafruit TSL2591 Library
adafruit/Adafruit_VL53L0X
Expand Down
11 changes: 11 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,17 @@ bool WipperSnapper_Component_I2C::initI2CDevice(
_ltr329->configureDriver(msgDeviceInitReq);
drivers.push_back(_ltr329);
WS_DEBUG_PRINTLN("LTR329/303 Initialized Successfully!");
} else if (strcmp("nau7802", msgDeviceInitReq->i2c_device_name) == 0) {
_nau7802 = new WipperSnapper_I2C_Driver_NAU7802(this->_i2c, i2cAddress);
if (!_nau7802->begin()) {
WS_DEBUG_PRINTLN("ERROR: Failed to initialize NAU7802");
_busStatusResponse =
wippersnapper_i2c_v1_BusResponse_BUS_RESPONSE_DEVICE_INIT_FAIL;
return false;
}
_nau7802->configureDriver(msgDeviceInitReq);
drivers.push_back(_nau7802);
WS_DEBUG_PRINTLN("NAU7802 Initialized Successfully!");
} else if (strcmp("sgp30", msgDeviceInitReq->i2c_device_name) == 0) {
_sgp30 = new WipperSnapper_I2C_Driver_SGP30(this->_i2c, i2cAddress);
if (!_sgp30->begin()) {
Expand Down
2 changes: 2 additions & 0 deletions src/components/i2c/WipperSnapper_I2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include "drivers/WipperSnapper_I2C_Driver_MPL115A2.h"
#include "drivers/WipperSnapper_I2C_Driver_MPRLS.h"
#include "drivers/WipperSnapper_I2C_Driver_MS8607.h"
#include "drivers/WipperSnapper_I2C_Driver_NAU7802.h"
#include "drivers/WipperSnapper_I2C_Driver_PCT2075.h"
#include "drivers/WipperSnapper_I2C_Driver_PM25.h"
#include "drivers/WipperSnapper_I2C_Driver_SCD30.h"
Expand Down Expand Up @@ -134,6 +135,7 @@ class WipperSnapper_Component_I2C {
WipperSnapper_I2C_Driver_MPL115A2 *_mpl115a2 = nullptr;
WipperSnapper_I2C_Driver_MPRLS *_mprls = nullptr;
WipperSnapper_I2C_Driver_MS8607 *_ms8607 = nullptr;
WipperSnapper_I2C_Driver_NAU7802 *_nau7802 = nullptr;
WipperSnapper_I2C_Driver_TMP117 *_tmp117 = nullptr;
WipperSnapper_I2C_Driver_TSL2591 *_tsl2591 = nullptr;
WipperSnapper_I2C_Driver_VCNL4020 *_vcnl4020 = nullptr;
Expand Down
141 changes: 141 additions & 0 deletions src/components/i2c/drivers/WipperSnapper_I2C_Driver_NAU7802.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*!
* @file WipperSnapper_I2C_Driver_NAU7802.h
*
* Device driver for the NAU7802 24bit ADC / load cell breakout
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* Copyright (c) Tyeth Gundry for Adafruit Industries 2024
*
* MIT license, all text here must be included in any redistribution.
*
*/

#ifndef WipperSnapper_I2C_Driver_NAU7802_H
#define WipperSnapper_I2C_Driver_NAU7802_H

#include "WipperSnapper_I2C_Driver.h"
#include <Adafruit_NAU7802.h>

#define NAU7802_TIMEOUT_MS 250 ///< Timeout waiting for data from NAU7802

/**************************************************************************/
/*!
@brief Class that provides a driver interface for the NAU7802.
*/
/**************************************************************************/
class WipperSnapper_I2C_Driver_NAU7802 : public WipperSnapper_I2C_Driver {
public:
/*******************************************************************************/
/*!
@brief Constructor for an NAU7802.
@param i2c
The I2C interface.
@param sensorAddress
7-bit device address.
*/
/*******************************************************************************/
WipperSnapper_I2C_Driver_NAU7802(TwoWire *i2c, uint16_t sensorAddress)
: WipperSnapper_I2C_Driver(i2c, sensorAddress) {
_i2c = i2c;
_sensorAddress = sensorAddress;
_nau7802 = new Adafruit_NAU7802();
}

/*******************************************************************************/
/*!
@brief Destructor for an NAU7802.
*/
/*******************************************************************************/
~WipperSnapper_I2C_Driver_NAU7802() { _nau7802 = nullptr; }

/*******************************************************************************/
/*!
@brief Initializes the NAU7802 sensor and begins I2C.
@returns True if initialized successfully, False otherwise.
*/
/*******************************************************************************/
bool begin() { return _nau7802->begin(_i2c) && configure_nau7802(); }

/*******************************************************************************/
/*!
@brief Configures the NAU7802 sensor.
@returns True if configured successfully, False otherwise.
*/
/*******************************************************************************/
bool configure_nau7802() {
if (!_nau7802->setLDO(NAU7802_3V0)) {
WS_DEBUG_PRINTLN("Failed to set LDO to 3V0");
return false;
}

if (!_nau7802->setGain(NAU7802_GAIN_128)) {
WS_DEBUG_PRINTLN("Failed to set gain to 128");
return false;
}

if (!_nau7802->setRate(NAU7802_RATE_10SPS) &&
!_nau7802->setRate(NAU7802_RATE_10SPS)) {
WS_DEBUG_PRINTLN("Failed to set sample rate to 10SPS");
return false;
}

// Take 10 readings to flush out old readings (10 samples per second)
flushNAU7802(10);

for (int retries = 0; retries < 3; retries++) {
if (_nau7802->calibrate(NAU7802_CALMOD_INTERNAL)) {
WS_DEBUG_PRINTLN("Calibrated internal offset");
return true;
}
WS_DEBUG_PRINTLN("Failed to calibrate internal offset, retrying!");
delay(1000);
}
WS_DEBUG_PRINTLN("ERROR: Failed to calibrate internal offset of NAU7802.");
return false;
}

/*******************************************************************************/
/*!
@brief Gets datapoints from sensor and discards (flushes old data).
@param count
Number of readings to discard.
*/
/*******************************************************************************/
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.
@param rawEvent
Pointer to an Adafruit_Sensor event.
@returns True if the reading was obtained successfully, False otherwise.
*/
/*******************************************************************************/
bool getEventRaw(sensors_event_t *rawEvent) {
unsigned long start = millis();

// Wait for the sensor to be ready
while (!_nau7802->available()) {
if (millis() - start > NAU7802_TIMEOUT_MS) {
WS_DEBUG_PRINTLN("NAU7802 data not available");
return false;
}
}
rawEvent->data[0] = (float)_nau7802->read();
return true;
}

protected:
Adafruit_NAU7802 *_nau7802 = nullptr; ///< NAU7802 object
};

#endif // WipperSnapper_I2C_Driver_NAU7802_H

0 comments on commit d102bc7

Please sign in to comment.