43 #include <avr/pgmspace.h>
44 #include "sensor_defs.h"
48 #define TSL2550_ADDR (0x39)
50 #define TSL2550_PWR_DOWN (0x00)
52 #define TSL2550_RD_CMD (0x03)
54 #define TSL2550_EXT_RANGE (0x1D)
56 #define TSL2550_STD_RANGE (0x18)
58 #define TSL2550_RD_ADC0 (0x43)
60 #define TSL2550_RD_ADC1 (0x83)
62 #define ADC_VALID_MASK (0x80)
64 #define TSL2550_ADC_VALID(a) (a & ADC_VALID_MASK)
65 #define TSL2550_MAX_LUX (1846)
78 static inline uint8_t tsl2550_init(
void)
85 static inline void tsl2550_set_command(uint8_t cmd)
109 static const unsigned char ratioLut[129] = {
110 100,100,100,100,100,100,100,100,
111 100,100,100,100,100,100,99,99,
112 99,99,99,99,99,99,99,99,
113 99,99,99,98,98,98,98,98,
114 98,98,97,97,97,97,97,96,
115 96,96,96,95,95,95,94,94,
116 93,93,93,92,92,91,91,90,
117 89,89,88,87,87,86,85,84,
118 83,82,81,80,79,78,77,75,
119 74,73,71,69,68,66,64,62,
120 60,58,56,54,52,49,47,44,
121 42,41,40,40,39,39,38,38,
122 37,37,37,36,36,36,35,35,
123 35,35,34,34,34,34,33,33,
124 33,33,32,32,32,32,32,31,
125 31,31,31,31,30,30,30,30,
129 static const unsigned short countLut[128] = {
130 0, 1, 2, 3, 4, 5, 6, 7,
131 8, 9, 10, 11, 12, 13, 14, 15,
132 16, 18, 20, 22, 24, 26, 28, 30,
133 32, 34, 36, 38, 40, 42, 44, 46,
134 49, 53, 57, 61, 65, 69, 73, 77,
135 81, 85, 89, 93, 97, 101, 105, 109,
136 115, 123, 131, 139, 147, 155, 163, 171,
137 179, 187, 195, 203, 211, 219, 227, 235,
138 247, 263, 279, 295, 311, 327, 343, 359,
139 375, 391, 407, 423, 439, 455, 471, 487,
140 511, 543, 575, 607, 639, 671, 703, 735,
141 767, 799, 831, 863, 895, 927, 959, 991,
142 1039,1103,1167,1231,1295,1359,1423,1487,
143 1551,1615,1679,1743,1807,1871,1935,1999,
144 2095,2223,2351,2479,2607,2735,2863,2991,
145 3119,3247,3375,3503,3631,3759,3887,4015
149 uint16_t count0 = countLut[adc0 & ~ADC_VALID_MASK];
150 uint16_t count1 = countLut[adc1 & ~ADC_VALID_MASK];
159 if ((count0) && (count1 <= count0))
161 ratio = (count1 * 128 / count0);
166 unsigned long lux = ((count0 - count1) * ratioLut[ratio]) / 256;
168 if (lux > TSL2550_MAX_LUX)
170 lux = TSL2550_MAX_LUX;
177 static inline void tsl2550_trigger(
void *pctx,
bool one_shot)
182 static inline uint8_t tsl2550_get_val(
void *pctx, uint8_t *pdata)
194 if ( TSL2550_ADC_VALID(adc0) && TSL2550_ADC_VALID(adc1))
202 p->type = SENSOR_DATA_LIGHT;
203 p->sensor = SENSOR_LEDPS;
209 static inline uint8_t tsl2550_get_raw(
void *pctx, uint8_t *pdata)
215 ((
sensor_raw_t*)pdata)->type = SENSOR_DATA_RAW | 2*
sizeof(uint8_t);
222 static inline uint8_t sensor_create_tsl2550(
void *pdata,
bool raw)
224 uint8_t rv =
sizeof(tsl2550_ctx_t);
231 pcfg = (tsl2550_ctx_t *)pdata;
232 pcfg->g.id = SENSOR_TSL2550;
233 pcfg->g.f_trigger = tsl2550_trigger;
234 pcfg->g.f_get_val = raw ? tsl2550_get_raw : tsl2550_get_val;
235 pcfg->g.f_sleep = NULL;
239 pcfg->g.last_error = tst ? SENSOR_ERR_OK : SENSOR_ERR_INIT;
#define DELAY_US(x)
Macro for delays with us resolution.
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)
static uint16_t tsl2550_scale(uint8_t adc0, uint8_t adc1)
static uint8_t tsl2550_get(uint8_t adc)