upm  1.7.1
Sensor/Actuator repository for libmraa (v2.0.0)
adc121c021.hpp
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
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 
198  void enableAlertFlag(bool enable);
199 
206  void enableAlertPin(bool enable);
207 
218  void enableAlertHold(bool enable);
219 
229  void enableAlertPinPolarityHigh(bool enable);
230 
240 
249  mraa_result_t setAlertLowLimit(uint16_t limit);
250 
259  mraa_result_t setAlertHighLimit(uint16_t limit);
260 
271  mraa_result_t setHysteresis(uint16_t limit);
272 
280  uint16_t getHighestConversion();
281 
289  uint16_t getLowestConversion();
290 
296  mraa_result_t clearHighestConversion();
297 
303  mraa_result_t clearLowestConversion();
304 
305  private:
306  mraa_i2c_context m_i2c;
307  uint8_t m_addr;
308  float m_vref;
309  bool m_alertLow;
310  bool m_alertHigh;
311  };
312 }
313 
314 
void setAutomaticConversion(ADC121C021_CYCLE_TIME_T cycleTime)
Definition: adc121c021.cxx:211
mraa_result_t writeWord(uint8_t reg, uint16_t word)
Definition: adc121c021.cxx:70
mraa_result_t clearHighestConversion()
Definition: adc121c021.cxx:261
bool getAlertStatus()
Definition: adc121c021.cxx:122
bool alertHighTriggered()
Definition: adc121c021.hpp:181
ADC121C021(int bus, uint8_t address=ADC121C021_DEFAULT_I2C_ADDR, float vref=ADC121C021_DEFAULT_VREF)
Definition: adc121c021.cxx:35
mraa_result_t writeByte(uint8_t reg, uint8_t byte)
Definition: adc121c021.cxx:65
ADC121C021_CYCLE_TIME_T
Definition: adc121c021.hpp:62
float valueToVolts(uint16_t val)
Definition: adc121c021.cxx:112
mraa_result_t setHysteresis(uint16_t limit)
Definition: adc121c021.cxx:242
C++ API wrapper for the bh1749 driver.
Definition: a110x.hpp:29
~ADC121C021()
Definition: adc121c021.cxx:60
void enableAlertHold(bool enable)
Definition: adc121c021.cxx:183
uint16_t getLowestConversion()
Definition: adc121c021.cxx:256
bool alertLowTriggered()
Definition: adc121c021.hpp:173
mraa_result_t clearLowestConversion()
Definition: adc121c021.cxx:266
void clearAlertStatus()
Definition: adc121c021.cxx:146
void enableAlertPinPolarityHigh(bool enable)
Definition: adc121c021.cxx:197
void enableAlertFlag(bool enable)
Definition: adc121c021.cxx:155
mraa_result_t setAlertHighLimit(uint16_t limit)
Definition: adc121c021.cxx:233
API for the ADC121C021 I2C Analog-to-Digital Converter.
Definition: adc121c021.hpp:91
uint16_t value()
Definition: adc121c021.cxx:106
uint16_t getHighestConversion()
Definition: adc121c021.cxx:251
uint16_t readWord(uint8_t reg)
Definition: adc121c021.cxx:89
uint8_t readByte(uint8_t reg)
Definition: adc121c021.cxx:80
mraa_result_t setAlertLowLimit(uint16_t limit)
Definition: adc121c021.cxx:224
void enableAlertPin(bool enable)
Definition: adc121c021.cxx:169