upm  0.2.0
Sensor/Actuator repository for libmraa (v0.6.1)
si114x.h
1 /*
2  * Author: Jon Trulson <jtrulson@ics.com>
3  * Copyright (c) 2015 Intel Corporation.
4  *
5  * Thanks to Adafruit for some important clues
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining
8  * a copy of this software and associated documentation files (the
9  * "Software"), to deal in the Software without restriction, including
10  * without limitation the rights to use, copy, modify, merge, publish,
11  * distribute, sublicense, and/or sell copies of the Software, and to
12  * permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 #pragma once
27 
28 #include <string>
29 #include <mraa/i2c.h>
30 #include <mraa/gpio.h>
31 
32 #define SI114X_I2C_BUS 0
33 #define SI114X_DEFAULT_I2C_ADDR 0x60
34 #define SI114X_HW_KEY 0x17
35 
36 
37 namespace upm {
38 
72  class SI114X {
73  public:
74 
78  typedef enum { REG_PART_ID = 0x00,
79  REG_REV_ID = 0x01,
80  REG_SEQ_ID = 0x02,
81  REG_INT_CFG = 0x03,
82  REG_IRQ_ENABLE = 0x04,
83 
84  // these two are not documented in the datasheet,
85  // but are mentioned there, as well as in the
86  // Adafruit example
87  REG_IRQ_MODE1 = 0x05,
88  REG_IRQ_MODE2 = 0x06,
89 
90  REG_HW_KEY = 0x07,
91  REG_MEAS_RATE0 = 0x08,
92  REG_MEAS_RATE1 = 0x09,
93 
94  REG_PS_LED21 = 0x0f,
95  REG_PS_LED3 = 0x10,
96 
97  REG_UCOEF0 = 0x13,
98  REG_UCOEF1 = 0x14,
99  REG_UCOEF2 = 0x15,
100  REG_UCOEF3 = 0x16,
101  REG_PARAM_WR = 0x17,
102  REG_COMMAND = 0x18,
103 
104  REG_RESPONSE = 0x20,
105  REG_IRQ_STATUS = 0x21,
106  REG_ALS_VIS_DATA0 = 0x22,
107  REG_ALS_VIS_DATA1 = 0x23,
108  REG_ALS_IR_DATA0 = 0x24,
109  REG_ALS_IR_DATA1 = 0x25,
110  REG_PS1_DATA0 = 0x26,
111  REG_PS1_DATA1 = 0x27,
112  REG_PS2_DATA0 = 0x28,
113  REG_PS2_DATA1 = 0x29,
114  REG_PS3_DATA0 = 0x2a,
115  REG_PS3_DATA1 = 0x2b,
116  REG_AUX_UVINDEX0 = 0x2c,
117  REG_AUX_UVINDEX1 = 0x2d,
118  REG_PARAM_READ = 0x2e,
119 
120  REG_CHIP_STAT = 0x30,
121 
122  REG_ANA_IN_KEY0 = 0x3b,
123  REG_ANA_IN_KEY1 = 0x3c,
124  REG_ANA_IN_KEY2 = 0x3d,
125  REG_ANA_IN_KEY3 = 0x3e
126  } SI114X_REG_T;
127 
131  typedef enum { PARAM_I2C_ADDDR = 0x00,
132  PARAM_CHLIST = 0x01,
133  PARAM_PSLED12_SEL = 0x02,
134  PARAM_PSLED3_SEL = 0x03,
135 
136  PARAM_PS_ENCODING = 0x05,
137  PARAM_ALS_ENCODING = 0x06,
138  PARAM_PS1_ADCMUX = 0x07,
139  PARAM_PS2_ADCMUX = 0x08,
140  PARAM_PS3_ADCMUX = 0x09,
141  PARAM_PS_ADC_COUNT = 0x0a,
142  PARAM_PS_ADC_GAIN = 0x0b,
143  PARAM_PS_ADC_MISC = 0x0c,
144 
145  PARAM_ALS_IR_ADCMUX = 0x0e,
146  PARAM_AUX_ADCMUX = 0x0f,
147  PARAM_ALS_VIS_ADC_COUNT = 0x10,
148  PARAM_ALS_VIS_ADC_GAIN = 0x11,
149  PARAM_ALS_VIS_ADC_MISC = 0x12,
150 
151  PARAM_LED_REC = 0x1c,
152  PARAM_ALS_IR_ADC_COUNT = 0x1d,
153  PARAM_ALS_IR_ADX_GAIN = 0x1e,
154  PARAM_ALS_IR_ADC_MISC = 0x1f
155  } SI114X_PARAM_T;
156 
160  typedef enum { CMD_NOOP = 0x00, // clear RESPONSE reg
161  CMD_RESET = 0x01,
162  CMD_BUSADDR = 0x02,
163 
164  CMD_PS_FORCE = 0x05,
165  CMD_GET_CAL = 0x12,
166  CMD_ALS_FORCE = 0x06,
167  CMD_PSALS_FORCE = 0x07,
168 
169  CMD_PS_PAUSE = 0x09,
170  CMD_ALS_PAUSE = 0x0a,
171  CMD_PSALS_PAUSE = 0x0b,
172 
173  CMD_PS_AUTO = 0x0d,
174  CMD_ALS_AUTO = 0x0e,
175  CMD_PSALS_AUTO = 0x0f,
176 
177  CMD_PARAM_QUERY = 0x80, // or'd with PARAM_T value
178  CMD_PARAM_SET = 0xa0 // or'd with PARAM_T value
179  } SI114X_CMD_T;
180 
181 
185  typedef enum { CHLIST_EN_PS1 = 0x01, // proximity sense 1-3
186  CHLIST_EN_PS2 = 0x02,
187  CHLIST_EN_PS3 = 0x04,
188 
189  CHLIST_EN_ALS_VIS = 0x10, // ambient light sense
190  CHLIST_EN_ALS_IR = 0x20,
191  CHLIST_EN_AUX = 0x40, // AUX sense
192  CHLIST_EN_UV = 0x80 // UV sense
194 
198  typedef enum { ERR_NONE = 0x00, // no error if high nibble is 0
199  // lower nibble is a counter
200  ERR_INVALID_SET = 0x80, // invalid setting
201  ERR_PS1_ADC_OVER = 0x88, // overflows
202  ERR_PS2_ADC_OVER = 0x89,
203  ERR_PS3_ADC_OVER = 0x8a,
204  ERR_ALS_VIS_ADC_OVER = 0x8c,
205  ERR_ALS_IR_ADC_OVER = 0x8d,
206  ERR_AUX_ADC_OVER = 0x8e
207  } SI114X_ERR_T;
208 
209 
213  typedef enum { IRQEN_ALS_IE = 0x01,
214  IRQEN_PS1_IE = 0x04,
215  IRQEN_PS2_IE = 0x08,
216  IRQEN_PS3_IE = 0x10
218 
225  SI114X(int bus, uint8_t address = SI114X_DEFAULT_I2C_ADDR);
226 
230  ~SI114X();
231 
239  bool writeByte(uint8_t reg, uint8_t byte);
240 
247  uint8_t readByte(uint8_t reg);
248 
255  uint16_t readWord(uint8_t reg);
256 
261  void reset();
262 
275  void setUVCalibration(uint8_t uvcoeff0, uint8_t uvcoeff1, uint8_t uvcoeff2,
276  uint8_t uvcoeff3);
277 
284  void writeParam(SI114X_PARAM_T param, uint8_t value);
285 
292  uint8_t readParam(SI114X_PARAM_T param);
293 
297  void initialize();
298 
303  void update();
304 
310  float getUVIndex() { return m_uvIndex; };
311 
312  private:
313  mraa_i2c_context m_i2c;
314  uint8_t m_addr;
315  // UV calibration values
316  uint8_t m_uv_cal[4];
317  // updated by update()
318  float m_uvIndex;
319  };
320 }
321 
322 
SI114X_REG_T
Definition: si114x.h:78
bool writeByte(uint8_t reg, uint8_t byte)
Definition: si114x.cxx:76
float getUVIndex()
Definition: si114x.h:310
SI114X_CHLIST_BITS_T
Definition: si114x.h:185
uint16_t readWord(uint8_t reg)
Definition: si114x.cxx:95
void setUVCalibration(uint8_t uvcoeff0, uint8_t uvcoeff1, uint8_t uvcoeff2, uint8_t uvcoeff3)
Definition: si114x.cxx:100
void reset()
Definition: si114x.cxx:131
Definition: a110x.h:29
C++ API for the SI1145 UV light sensor.
Definition: si114x.h:72
void writeParam(SI114X_PARAM_T param, uint8_t value)
Definition: si114x.cxx:110
SI114X_IRQEN_BITS_T
Definition: si114x.h:213
~SI114X()
Definition: si114x.cxx:71
SI114X_PARAM_T
Definition: si114x.h:131
uint8_t readByte(uint8_t reg)
Definition: si114x.cxx:90
uint8_t readParam(SI114X_PARAM_T param)
Definition: si114x.cxx:123
SI114X(int bus, uint8_t address=SI114X_DEFAULT_I2C_ADDR)
Definition: si114x.cxx:38
SI114X_ERR_T
Definition: si114x.h:198
void initialize()
Definition: si114x.cxx:157
SI114X_CMD_T
Definition: si114x.h:160
void update()
Definition: si114x.cxx:180