upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
ads1x15.hpp
1 /*
2  * Author: Marc Graham <marc@m2ag.net>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining
6  * a copy of this software and associated documentation files (the
7  * "Software"), to deal in the Software without restriction, including
8  * without limitation the rights to use, copy, modify, merge, publish,
9  * distribute, sublicense, and/or sell copies of the Software, and to
10  * permit persons to whom the Software is furnished to do so, subject to
11  * the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 #pragma once
26 
27 #include <iostream>
28 #include <string>
29 
30 namespace mraa {class I2c;}
31 
32 /*=========================================================================
33  I2C ADDRESS/BITS
34  -----------------------------------------------------------------------*/
35 #define ADS1X15_ADDRESS (0x48) // 1001 000 (ADDR = GND)
36 /*=========================================================================*/
37 
38 
39 /*=========================================================================
40  POINTER REGISTER
41  -----------------------------------------------------------------------*/
42 #define ADS1X15_REG_POINTER_MASK (0x03)
43 #define ADS1X15_REG_POINTER_CONVERT (0x00)
44 #define ADS1X15_REG_POINTER_CONFIG (0x01)
45 #define ADS1X15_REG_POINTER_LOWTHRESH (0x02)
46 #define ADS1X15_REG_POINTER_HITHRESH (0x03)
47 /*=========================================================================*/
48 
49 /*=========================================================================
50  CONFIG REGISTER
51 
52  -----------------------------------------------------------------------*/
53 #define ADS1X15_OS_MASK (0x8000)
54 #define ADS1X15_OS_SINGLE (0x8000) // Write: Set to start a single-conversion
55 #define ADS1X15_OS_BUSY (0x0000) // Read: Bit = 0 when conversion is in progress
56 #define ADS1X15_OS_NOTBUSY (0x8000) // Read: Bit = 1 when device is not performing a conversion
57 
58 #define ADS1X15_MUX_MASK (0x7000)
59 #define ADS1X15_MUX_DIFF_0_1 (0x0000) // Differential P = AIN0, N = AIN1 (default)
60 #define ADS1X15_MUX_DIFF_0_3 (0x1000) // Differential P = AIN0, N = AIN3
61 #define ADS1X15_MUX_DIFF_1_3 (0x2000) // Differential P = AIN1, N = AIN3
62 #define ADS1X15_MUX_DIFF_2_3 (0x3000) // Differential P = AIN2, N = AIN3
63 #define ADS1X15_MUX_SINGLE_0 (0x4000) // Single-ended AIN0
64 #define ADS1X15_MUX_SINGLE_1 (0x5000) // Single-ended AIN1
65 #define ADS1X15_MUX_SINGLE_2 (0x6000) // Single-ended AIN2
66 #define ADS1X15_MUX_SINGLE_3 (0x7000) // Single-ended AIN3
67 
68 #define ADS1X15_PGA_MASK (0x0E00)
69 #define ADS1X15_PGA_6_144V (0x0000) // +/-6.144V range = Gain 2/3
70 #define ADS1X15_PGA_4_096V (0x0200) // +/-4.096V range = Gain 1
71 #define ADS1X15_PGA_2_048V (0x0400) // +/-2.048V range = Gain 2 (default)
72 #define ADS1X15_PGA_1_024V (0x0600) // +/-1.024V range = Gain 4
73 #define ADS1X15_PGA_0_512V (0x0800) // +/-0.512V range = Gain 8
74 #define ADS1X15_PGA_0_256V (0x0A00) // +/-0.256V range = Gain 16
75 
76 #define ADS1X15_MODE_MASK (0x0100)
77 #define ADS1X15_MODE_CONTIN (0x0000) // Continuous conversion mode
78 #define ADS1X15_MODE_SINGLE (0x0100) // Power-down single-shot mode (default)
79 
80 #define ADS1X15_DR_MASK (0x00E0)
81 
82 #define ADS1X15_CMODE_MASK (0x0010)
83 #define ADS1X15_CMODE_TRAD (0x0000) // Traditional comparator with hysteresis (default)
84 #define ADS1X15_CMODE_WINDOW (0x0010) // Window comparator
85 
86 #define ADS1X15_CPOL_MASK (0x0008)
87 #define ADS1X15_CPOL_ACTVLOW (0x0000) // ALERT/RDY pin is low when active (default)
88 #define ADS1X15_CPOL_ACTVHI (0x0008) // ALERT/RDY pin is high when active
89 
90 #define ADS1X15_CLAT_MASK (0x0400) // Determines if ALERT/RDY pin latches once asserted
91 #define ADS1X15_CLAT_NONLAT (0x0000) // Non-latching comparator (default)
92 #define ADS1X15_CLAT_LATCH (0x0400) // Latching comparator
93 
94 #define ADS1X15_CQUE_MASK (0x0003)
95 /* This wouldn't compile for the python wrapper. with these in for some reason.
96 #define ADS1X15_CQUE_1CONV (0x0000) // Assert ALERT/RDY after one conversions
97 #define ADS1X15_CQUE_2CONV (0x0001) // Assert ALERT/RDY after two conversions
98 #define ADS1X15_CQUE_4CONV (0x0002) // Assert ALERT/RDY after four conversions
99 #define ADS1X15_CQUE_NONE (0x0003) // Disable the comparator and put ALERT/RDY in high state (default)
100 */
101 /*=========================================================================*/
102 
103 namespace upm {
113  class ADS1X15 {
114 
115  public:
116 
129  typedef enum ADSGAIN {
130  GAIN_TWOTHIRDS = ADS1X15_PGA_6_144V,
131  GAIN_ONE = ADS1X15_PGA_4_096V,
132  GAIN_TWO = ADS1X15_PGA_2_048V,
133  GAIN_FOUR = ADS1X15_PGA_1_024V,
134  GAIN_EIGHT = ADS1X15_PGA_0_512V,
135  GAIN_SIXTEEN = ADS1X15_PGA_0_256V
136  } ADSGAIN ;
137 
152  typedef enum ADSMUXMODE {
153  DIFF_0_1 = ADS1X15_MUX_DIFF_0_1, // Differential P = AIN0, N = AIN1 (default)
154  DIFF_0_3 = ADS1X15_MUX_DIFF_0_3, // Differential P = AIN0, N = AIN3
155  DIFF_1_3 = ADS1X15_MUX_DIFF_1_3, // Differential P = AIN1, N = AIN3
156  DIFF_2_3 = ADS1X15_MUX_DIFF_2_3, // Differential P = AIN2, N = AIN3
157  SINGLE_0 = ADS1X15_MUX_SINGLE_0, // Single-ended AIN0
158  SINGLE_1 = ADS1X15_MUX_SINGLE_1, // Single-ended AIN1
159  SINGLE_2 = ADS1X15_MUX_SINGLE_2, // Single-ended AIN2
160  SINGLE_3 = ADS1X15_MUX_SINGLE_3 // Single-ended AIN3
161  } ADSMUXMODE;
162 
173  typedef enum ADSCOMP {
174  CQUE_1CONV = 0x0000, // Assert ALERT/RDY after one conversions
175  CQUE_2CONV = 0x0001, // Assert ALERT/RDY after two conversions
176  CQUE_4CONV = 0x0002, // Assert ALERT/RDY after four conversions
177  CQUE_NONE = 0x0003 // Disable the comparator and put ALERT/RDY in high state (default)
178  } ADSCOMP;
179 
191  typedef enum ADSTHRESH {
192  THRESH_LOW = ADS1X15_REG_POINTER_LOWTHRESH,
193  THRESH_HIGH = ADS1X15_REG_POINTER_HITHRESH,
194  CONVERSION_RDY = 0x04,
195  THRESH_DEFAULT = 0x05
196 
197  } ADSTHRESH;
198 
207  typedef enum ADSSAMPLERATE {
208  SPS_DEFAULT = 0x0080
209  } ADSSAMPLERATE;
210 
217  ADS1X15(int bus, uint8_t address);
218 
222  virtual ~ADS1X15 ();
223 
227  std::string name()
228  {
229  return m_name;
230  }
231 
241  float getLastSample(int reg = ADS1X15_REG_POINTER_CONVERT);
242 
250  float getSample(ADSMUXMODE mode = ADS1X15::DIFF_0_1);
251 
256  ADSGAIN getGain(){
257  return (ADSGAIN)(m_config_reg & ADS1X15_PGA_MASK);
258  }
259 
267  void setGain(ADSGAIN gain = ADS1X15::GAIN_TWO);
268 
274  return (ADSSAMPLERATE)(m_config_reg & ADS1X15_DR_MASK);
275  }
276 
285  virtual void setSPS(ADSSAMPLERATE rate);
286 
292  bool getCompMode(void){
293  return (bool)(m_config_reg & ADS1X15_CMODE_MASK);
294  }
295 
303  void setCompMode(bool mode = false);
304 
311  bool getCompPol(void){
312  return (bool)(m_config_reg & ADS1X15_CPOL_MASK);
313  }
314 
323  void setCompPol(bool mode = false);
324 
331  bool getCompLatch(void){
332  return (bool)(m_config_reg & ADS1X15_CLAT_MASK);
333  }
334 
342  void setCompLatch(bool mode = false);
343 
354  return (ADSCOMP)(m_config_reg & ADS1X15_CQUE_MASK);
355  }
356 
366  void setCompQue(ADSCOMP mode = ADS1X15::CQUE_NONE);
367 
374  bool getContinuous(void){
375  return !(bool)(m_config_reg & ADS1X15_MODE_MASK);
376  }
377 
385  void setContinuous(bool mode = false);
386 
393  float getThresh(ADSTHRESH reg = THRESH_DEFAULT);
394 
407  void setThresh(ADSTHRESH reg = THRESH_DEFAULT , float value = 0.0);
408 
409  protected:
410  std::string m_name;
411  float m_conversionDelay;
412  uint8_t m_bitShift;
413  uint16_t m_config_reg;
414  //Must be overridden in subclass for proper values.
415  virtual float getMultiplier(void) = 0;
416  //Must be overridden in subclass for proper values.
417  virtual void setDelay(void) = 0;
418  void getCurrentConfig();
419  void updateConfigRegister(uint16_t update, bool read = false);
420  uint16_t swapWord(uint16_t value);
421 
422  mraa::I2c* i2c;
423 
424  };}
ADSCOMP
uint16_t enum containing values for setting ADS1X15 comparator queue modes.
Definition: ads1x15.hpp:173
std::string name()
Definition: ads1x15.hpp:227
bool getCompLatch(void)
Definition: ads1x15.hpp:331
ADSTHRESH
uint8_t enum containing register addresses used for setting HI and LOW threshold values as well as se...
Definition: ads1x15.hpp:191
Definition: ads1x15.hpp:113
ADSCOMP getCompQue(void)
Definition: ads1x15.hpp:353
bool getCompPol(void)
Definition: ads1x15.hpp:311
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
ADSMUXMODE
uint16_t enum containing values used for selecting ADS1X15 read operations.
Definition: ads1x15.hpp:152
ADSSAMPLERATE getSPS(void)
Definition: ads1x15.hpp:273
ADSGAIN
uint16_t enum containing values for setting gain for ADS1X15 devices.
Definition: ads1x15.hpp:129
ADSSAMPLERATE
uint16_t enum containing values representing the sample rate of the device. Will be overridden in sub...
Definition: ads1x15.hpp:207
Definition: ads1x15.hpp:30
bool getCompMode(void)
Definition: ads1x15.hpp:292
bool getContinuous(void)
Definition: ads1x15.hpp:374
ADSGAIN getGain()
Definition: ads1x15.hpp:256