1
+ /* !
2
+ * @file WipperSnapper_I2C_Driver_MCP3421.h
3
+ *
4
+ * Device driver for the MCP3421 18-bit ADC sensor.
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 2024 for Adafruit Industries.
11
+ *
12
+ * MIT license, all text here must be included in any redistribution.
13
+ *
14
+ */
15
+ #ifndef WipperSnapper_I2C_Driver_MCP3421_H
16
+ #define WipperSnapper_I2C_Driver_MCP3421_H
17
+
18
+ #include " WipperSnapper_I2C_Driver.h"
19
+ #include " Wippersnapper.h"
20
+ #include < Adafruit_MCP3421.h>
21
+
22
+ /* *************************************************************************/
23
+ /* !
24
+ @brief Class that provides a driver interface for a MCP3421 sensor.
25
+ */
26
+ /* *************************************************************************/
27
+ class WipperSnapper_I2C_Driver_MCP3421 : public WipperSnapper_I2C_Driver {
28
+ public:
29
+ /* ******************************************************************************/
30
+ /* !
31
+ @brief Constructor for the MCP3421 sensor.
32
+ @param i2c
33
+ The I2C interface.
34
+ @param sensorAddress
35
+ 7-bit device address.
36
+ */
37
+ /* ******************************************************************************/
38
+ WipperSnapper_I2C_Driver_MCP3421 (TwoWire *i2c, uint16_t sensorAddress)
39
+ : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
40
+ _i2c = i2c;
41
+ _sensorAddress = sensorAddress;
42
+ }
43
+
44
+ /* ******************************************************************************/
45
+ /* !
46
+ @brief Destructor for an MCP3421 sensor.
47
+ */
48
+ /* ******************************************************************************/
49
+ ~WipperSnapper_I2C_Driver_MCP3421 () {
50
+ // Called when a MCP3421 component is deleted.
51
+ delete _mcp3421;
52
+ }
53
+
54
+ /* ******************************************************************************/
55
+ /* !
56
+ @brief Initializes the MCP3421 sensor and begins I2C.
57
+ @returns True if initialized successfully, False otherwise.
58
+ */
59
+ /* ******************************************************************************/
60
+ bool begin () {
61
+ _mcp3421 = new Adafruit_MCP3421 ();
62
+ if (_mcp3421->begin ((uint8_t )_sensorAddress, _i2c)) {
63
+ WS_DEBUG_PRINTLN (" Failed to find MCP3421 chip" );
64
+ return false ;
65
+ }
66
+
67
+ // NOTE: We should allow the gain to be set in future, like resolution
68
+ // 12_BIT (240 SPS), 14_BIT (60 SPS), 16_BIT (15 SPS), 18_BIT (3.75 SPS)
69
+ _mcp3421->setResolution (RESOLUTION_18_BIT);
70
+ if (_mcp3421->getResolution () != RESOLUTION_18_BIT) {
71
+ WS_DEBUG_PRINTLN (" Failed to set resolution" );
72
+ return false ;
73
+ }
74
+
75
+ _mcp3421->setMode (MODE_ONE_SHOT);
76
+ if (_mcp3421->getMode () != MODE_ONE_SHOT) {
77
+ WS_DEBUG_PRINTLN (" Failed to set mode" );
78
+ return false ;
79
+ }
80
+ return true ;
81
+ }
82
+
83
+ /* ******************************************************************************/
84
+ /* !
85
+ @brief Reads a voltage sensor and converts the
86
+ reading into the expected SI unit.
87
+ @param voltageEvent
88
+ voltage sensor reading, in volts.
89
+ @returns True if the sensor event was obtained successfully, False
90
+ otherwise.
91
+ */
92
+ /* ******************************************************************************/
93
+ bool getEventVoltage (sensors_event_t *voltageEvent) {
94
+ ulong start = millis ();
95
+ if (!_mcp3421->startOneShotConversion ()) {
96
+ WS_DEBUG_PRINTLN (" Failed to start one-shot conversion" );
97
+ return false ;
98
+ }
99
+ while (!_mcp3421->isReady ()) {
100
+ if (millis () - start > 1000 ) {
101
+ WS_DEBUG_PRINTLN (" Timeout waiting for conversion to complete" );
102
+ return false ;
103
+ }
104
+ }
105
+ voltageEvent->voltage = (float )_mcp3421->readADC ();
106
+ return true ;
107
+ }
108
+
109
+ protected:
110
+ Adafruit_MCP3421 *_mcp3421; // /< Pointer to MCP3421 temperature sensor object
111
+ };
112
+
113
+ #endif // WipperSnapper_I2C_Driver_MCP3421
0 commit comments