µracoli Manual  Version foo
isl29020.h
1 /* Copyright (c) 2013 Axel Wachtler
2  All rights reserved.
3 
4  Redistribution and use in source and binary forms, with or without
5  modification, are permitted provided that the following conditions
6  are met:
7 
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright
11  notice, this list of conditions and the following disclaimer in the
12  documentation and/or other materials provided with the distribution.
13  * Neither the name of the authors nor the names of its contributors
14  may be used to endorse or promote products derived from this software
15  without specific prior written permission.
16 
17  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  POSSIBILITY OF SUCH DAMAGE. */
28 
29 #ifndef ISL29020_H
30 #define ISL29020_H
31 
41 /* === includes ============================================================ */
42 
43 /* === macros ============================================================== */
48 #define ISL29020_ADDR_0 (0x44)
49 
50 #define ISL29020_ADDR_1 (0x45)
51 
52 #define _ISL29020_EN (0x80)
53 #define _ISL29020_MODE (0x40)
54 #define _ISL29020_LIGHT (0x20)
55 #define _ISL29020_RES (0x1c)
56 #define _ISL29020_RANGE (0x03)
57 
58 #define ISL29020_SET_ENABLE(c) do{ (c) |= _ISL29020_EN;}while(0)
59 #define ISL29020_SET_DISABLE(c) do{ (c) &= ~_ISL29020_EN;}while(0)
60 
61 #define ISL29020_SET_MODE_SINGLE(c) do{ (c) &= ~_ISL29020_MODE;}while(0)
62 #define ISL29020_SET_MODE_CONT(c) do{ (c) |= _ISL29020_MODE;}while(0)
63 
64 #define ISL29020_SET_IR(c) do{ (c) |= _ISL29020_LIGHT;}while(0)
65 #define ISL29020_SET_LIGHT(c) do{ (c) &= ~_ISL29020_LIGHT;}while(0)
66 
67 #define ISL29020_SET_RESOLUTION(c,r) do{ (c) &= ~_ISL29020_RES;\
68  (c) |= (r<<2)&_ISL29020_RES; }while(0)
69 #define ISL29020_SET_RANGE(c,r) do{ (c) &= ~_ISL29020_RANGE;\
70  (c) |= (r)&_ISL29020_RANGE; }while(0)
71 
72 
73 #define ISL29020_GET_ENABLE(c) (((c) & _ISL29020_EN)>>7)
74 #define ISL29020_GET_MODE_CONT(c) (((c) & _ISL29020_MODE)>>6)
75 #define ISL29020_GET_IR(c) (((c) & _ISL29020_LIGHT)>>5)
76 #define ISL29020_GET_RES(c) (((c) & _ISL29020_RES)>>2)
77 #define ISL29020_GET_RANGE(c) (((c) & _ISL29020_RANGE))
78 
79 
80 /* === types =============================================================== */
81 typedef struct {
82  uint8_t addr;
83  uint8_t cmd;
84 } isl29020_ctx_t;
85 
86 /* === prototypes ========================================================== */
87 #ifdef __cplusplus
88 extern "C" {
89 #endif
90 static inline uint8_t isl29020_init(isl29020_ctx_t *pctx, uint8_t addr)
91 {
92  uint8_t rv;
93 
94  rv = i2c_probe(addr);
95  if (rv)
96  {
97  pctx->addr = addr;
98  pctx->cmd = 0;
99 
100  }
101  return rv;
102 }
103 
104 static inline void isl29020_set_command(isl29020_ctx_t *pctx, uint8_t cmd)
105 {
106  uint8_t buf[3] = {0,};
107  buf[1] = cmd;
108  pctx->cmd = cmd;
109  i2c_master_writeread(pctx->addr, buf, 2, NULL, 0);
110  i2c_master_writeread(pctx->addr, buf, 1, buf, 3);
111 }
112 
113 static inline uint8_t isl29020_get_command(isl29020_ctx_t *pctx)
114 {
115  uint8_t buf[3] = {0,};
116  i2c_master_writeread(pctx->addr, buf, 1, buf, 1);
117  pctx->cmd = buf[0];
118  return buf[0] ;
119 }
120 
121 static inline uint16_t isl29020_get(isl29020_ctx_t *pctx)
122 {
123  uint8_t buf[3] = {1,};
124  i2c_master_writeread(pctx->addr, buf, 1, buf, 2);
125  return buf[0] | buf[1] * 256;
126 }
127 
128 static inline float isl29020_scale(isl29020_ctx_t *pctx, uint16_t val)
129 {
130  float rv = 0.0;
131  uint8_t res, range;
132  int32_t cnt_max;
133  res = ISL29020_GET_RES(pctx->cmd);
134  range = ISL29020_GET_RANGE(pctx->cmd);
135  cnt_max = (1L << (16 - (res * 4))) - 1;
136  switch (range)
137  {
138  case 0:
139  rv = (float)(1000.0 / cnt_max) * (float)val;
140  break;
141  case 1:
142  rv = (float)(4000.0 / cnt_max) * (float)val;
143  break;
144  case 2:
145  rv = (float)(16000.0 / cnt_max) * (float)val;
146  break;
147  case 3:
148  rv = (float)(64000.0 / cnt_max) * (float)val;
149  break;
150  default:
151  rv = -1.0;
152  }
153 
154  return rv;
155 
156 }
157 
158 
159 uint8_t sensor_isl29020_trigger(void)
160 {
161 }
162 
163 uint8_t sensor_isl29020_get_raw(void)
164 {
165 }
166 
167 uint8_t sensor_isl29020_get_float(void)
168 {
169 }
170 
171 #ifdef __cplusplus
172 } /* extern "C" */
173 #endif
174 
175 #endif /* #ifndef ISL29020_H */
uint8_t i2c_probe(uint8_t devaddr)
uint8_t i2c_master_writeread(uint8_t devaddr, uint8_t *writebuf, uint8_t bytestowrite, uint8_t *readbuf, uint8_t bytestoread)