1+ /* !
2+ * @file WipperSnapper_I2C_Driver_NAU7802.h
3+ *
4+ * Device driver for the NAU7802 24bit ADC / load cell breakout
5+ *
6+ * Adafruit invests time and resources providing this open source code,
7+ * please support Adafruit and open-source hardware by purchasing
8+ * products from Adafruit!
9+ *
10+ * Copyright (c) Tyeth Gundry for Adafruit Industries 2024
11+ *
12+ * MIT license, all text here must be included in any redistribution.
13+ *
14+ */
15+
16+ #ifndef WipperSnapper_I2C_Driver_NAU7802_H
17+ #define WipperSnapper_I2C_Driver_NAU7802_H
18+
19+ #include " WipperSnapper_I2C_Driver.h"
20+ #include < Adafruit_NAU7802.h>
21+
22+ #define NAU7802_TIMEOUT_MS 250 // /< Timeout waiting for data from NAU7802
23+
24+ /* *************************************************************************/
25+ /* !
26+ @brief Class that provides a driver interface for the NAU7802.
27+ */
28+ /* *************************************************************************/
29+ class WipperSnapper_I2C_Driver_NAU7802 : public WipperSnapper_I2C_Driver {
30+ public:
31+ /* ******************************************************************************/
32+ /* !
33+ @brief Constructor for an NAU7802.
34+ @param i2c
35+ The I2C interface.
36+ @param sensorAddress
37+ 7-bit device address.
38+ */
39+ /* ******************************************************************************/
40+ WipperSnapper_I2C_Driver_NAU7802 (TwoWire *i2c, uint16_t sensorAddress)
41+ : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
42+ _i2c = i2c;
43+ _sensorAddress = sensorAddress;
44+ _nau7802 = new Adafruit_NAU7802 ();
45+ }
46+
47+ /* ******************************************************************************/
48+ /* !
49+ @brief Destructor for an NAU7802.
50+ */
51+ /* ******************************************************************************/
52+ ~WipperSnapper_I2C_Driver_NAU7802 () { _nau7802 = nullptr ; }
53+
54+ /* ******************************************************************************/
55+ /* !
56+ @brief Initializes the NAU7802 sensor and begins I2C.
57+ @returns True if initialized successfully, False otherwise.
58+ */
59+ /* ******************************************************************************/
60+ bool begin () { return _nau7802->begin (_i2c) && configure_nau7802 (); }
61+
62+ /* ******************************************************************************/
63+ /* !
64+ @brief Configures the NAU7802 sensor.
65+ @returns True if configured successfully, False otherwise.
66+ */
67+ /* ******************************************************************************/
68+ bool configure_nau7802 () {
69+ if (!_nau7802->setLDO (NAU7802_3V0)) {
70+ WS_DEBUG_PRINTLN (" Failed to set LDO to 3V0" );
71+ return false ;
72+ }
73+
74+ if (!_nau7802->setGain (NAU7802_GAIN_128)) {
75+ WS_DEBUG_PRINTLN (" Failed to set gain to 128" );
76+ return false ;
77+ }
78+
79+ if (!_nau7802->setRate (NAU7802_RATE_10SPS) &&
80+ !_nau7802->setRate (NAU7802_RATE_10SPS)) {
81+ WS_DEBUG_PRINTLN (" Failed to set sample rate to 10SPS" );
82+ return false ;
83+ }
84+
85+ // Take 10 readings to flush out old readings (10 samples per second)
86+ flushNAU7802 (10 );
87+
88+ for (int retries = 0 ; retries < 3 ; retries++) {
89+ if (_nau7802->calibrate (NAU7802_CALMOD_INTERNAL)) {
90+ WS_DEBUG_PRINTLN (" Calibrated internal offset" );
91+ return true ;
92+ }
93+ WS_DEBUG_PRINTLN (" Failed to calibrate internal offset, retrying!" );
94+ delay (1000 );
95+ }
96+ WS_DEBUG_PRINTLN (" ERROR: Failed to calibrate internal offset of NAU7802." );
97+ return false ;
98+ }
99+
100+ /* ******************************************************************************/
101+ /* !
102+ @brief Gets datapoints from sensor and discards (flushes old data).
103+ @param count
104+ Number of readings to discard.
105+ */
106+ /* ******************************************************************************/
107+ void flushNAU7802 (uint8_t count) {
108+ for (uint8_t skipCounter = 0 ; skipCounter < count; skipCounter++) {
109+ while (!_nau7802->available ())
110+ delay (1 );
111+ _nau7802->read ();
112+ }
113+ }
114+
115+ /* ******************************************************************************/
116+ /* !
117+ @brief Gets the sensor's raw "force" value.
118+ @param rawEvent
119+ Pointer to an Adafruit_Sensor event.
120+ @returns True if the reading was obtained successfully, False otherwise.
121+ */
122+ /* ******************************************************************************/
123+ bool getEventRaw (sensors_event_t *rawEvent) {
124+ unsigned long start = millis ();
125+
126+ // Wait for the sensor to be ready
127+ while (!_nau7802->available ()) {
128+ if (millis () - start > NAU7802_TIMEOUT_MS) {
129+ WS_DEBUG_PRINTLN (" NAU7802 data not available" );
130+ return false ;
131+ }
132+ }
133+ rawEvent->data [0 ] = (float )_nau7802->read ();
134+ return true ;
135+ }
136+
137+ protected:
138+ Adafruit_NAU7802 *_nau7802 = nullptr ; // /< NAU7802 object
139+ };
140+
141+ #endif // WipperSnapper_I2C_Driver_NAU7802_H
0 commit comments