upm  0.3.1
Sensor/Actuator repository for libmraa (v0.7.2)
adc121c021.h
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2014 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 #pragma once
25 
26 #include <string>
27 #include <mraa/i2c.h>
28 
29 #define ADC121C021_I2C_BUS 0
30 #define ADC121C021_DEFAULT_I2C_ADDR 0x55
31 
32 #define ADC121C021_RESOLUTION 4096 // 12 bits
33 
38 #define ADC121C021_REG_RESULT 0x00
39 #define ADC121C021_REG_ALERT_STATUS 0x01
40 #define ADC121C021_REG_CONFIG 0x02
41 #define ADC121C021_REG_ALERT_LIM_UNDER 0x03
42 #define ADC121C021_REG_ALERT_LIM_OVER 0x04
43 #define ADC121C021_REG_ALERT_HYS 0x05
44 #define ADC121C021_REG_LOWEST_CONV 0x06
45 #define ADC121C021_REG_HIGHEST_CONV 0x07
46 
47 // For the Grove I2C ADC
48 #define ADC121C021_DEFAULT_VREF 3.0
49 
50 namespace upm {
62  typedef enum { ADC121C021_CYCLE_NONE = 0, // disabled
63  ADC121C021_CYCLE_32 = 1, // 27 ksps
64  ADC121C021_CYCLE_64 = 2, // 13.5
65  ADC121C021_CYCLE_128 = 3, // 6.7
66  ADC121C021_CYCLE_256 = 4, // 3.4
67  ADC121C021_CYCLE_512 = 5, // 1.7
68  ADC121C021_CYCLE_1024 = 6, // 0.9
69  ADC121C021_CYCLE_2048 = 7 // 0.4
70  } ADC121C021_CYCLE_TIME_T;
71 
91  class ADC121C021 {
92  public:
100  ADC121C021(int bus, uint8_t address = ADC121C021_DEFAULT_I2C_ADDR,
101  float vref = ADC121C021_DEFAULT_VREF);
102 
106  ~ADC121C021();
107 
115  mraa_result_t writeByte(uint8_t reg, uint8_t byte);
116 
124  mraa_result_t writeWord(uint8_t reg, uint16_t word);
125 
132  uint8_t readByte(uint8_t reg);
133 
140  uint16_t readWord(uint8_t reg);
141 
147  uint16_t value();
148 
155  float valueToVolts(uint16_t val);
156 
165  bool getAlertStatus();
166 
173  bool alertLowTriggered() { return m_alertLow; };
174 
181  bool alertHighTriggered() { return m_alertHigh; };
182 
187  void clearAlertStatus();
188 
197  void enableAlertFlag(bool enable);
198 
204  void enableAlertPin(bool enable);
205 
215  void enableAlertHold(bool enable);
216 
225  void enableAlertPinPolarityHigh(bool enable);
226 
235  void setAutomaticConversion(ADC121C021_CYCLE_TIME_T cycleTime);
236 
245  mraa_result_t setAlertLowLimit(uint16_t limit);
246 
255  mraa_result_t setAlertHighLimit(uint16_t limit);
256 
267  mraa_result_t setHysteresis(uint16_t limit);
268 
276  uint16_t getHighestConversion();
277 
285  uint16_t getLowestConversion();
286 
292  mraa_result_t clearHighestConversion();
293 
299  mraa_result_t clearLowestConversion();
300 
301  private:
302  mraa_i2c_context m_i2c;
303  uint8_t m_addr;
304  float m_vref;
305  bool m_alertLow;
306  bool m_alertHigh;
307  };
308 }
309 
310 
void setAutomaticConversion(ADC121C021_CYCLE_TIME_T cycleTime)
Definition: adc121c021.cxx:192
mraa_result_t writeWord(uint8_t reg, uint16_t word)
Definition: adc121c021.cxx:59
mraa_result_t clearHighestConversion()
Definition: adc121c021.cxx:242
bool getAlertStatus()
Definition: adc121c021.cxx:103
bool alertHighTriggered()
Definition: adc121c021.h:181
ADC121C021(int bus, uint8_t address=ADC121C021_DEFAULT_I2C_ADDR, float vref=ADC121C021_DEFAULT_VREF)
Definition: adc121c021.cxx:34
mraa_result_t writeByte(uint8_t reg, uint8_t byte)
Definition: adc121c021.cxx:54
float valueToVolts(uint16_t val)
Definition: adc121c021.cxx:93
mraa_result_t setHysteresis(uint16_t limit)
Definition: adc121c021.cxx:223
Definition: a110x.h:29
~ADC121C021()
Definition: adc121c021.cxx:49
void enableAlertHold(bool enable)
Definition: adc121c021.cxx:164
uint16_t getLowestConversion()
Definition: adc121c021.cxx:237
bool alertLowTriggered()
Definition: adc121c021.h:173
mraa_result_t clearLowestConversion()
Definition: adc121c021.cxx:247
void clearAlertStatus()
Definition: adc121c021.cxx:127
void enableAlertPinPolarityHigh(bool enable)
Definition: adc121c021.cxx:178
void enableAlertFlag(bool enable)
Definition: adc121c021.cxx:136
mraa_result_t setAlertHighLimit(uint16_t limit)
Definition: adc121c021.cxx:214
API for the ADC121C021 I2C ADC.
Definition: adc121c021.h:91
uint16_t value()
Definition: adc121c021.cxx:87
uint16_t getHighestConversion()
Definition: adc121c021.cxx:232
uint16_t readWord(uint8_t reg)
Definition: adc121c021.cxx:74
uint8_t readByte(uint8_t reg)
Definition: adc121c021.cxx:69
mraa_result_t setAlertLowLimit(uint16_t limit)
Definition: adc121c021.cxx:205
void enableAlertPin(bool enable)
Definition: adc121c021.cxx:150