48 #define PRINTFDEBUG(...) printf(__VA_ARGS__)
50 #define PRINTFDEBUG(...)
54 enum TSL2563_STATUSTYPES
62 static enum TSL2563_STATUSTYPES _TSL2563_STATUS = 0x00;
65 calculateLux(uint16_t *buffer)
67 uint32_t ch0, ch1 = 0;
68 uint32_t aux = (1<<14);
69 uint32_t ratio, lratio, tmp = 0;
71 ch0 = (buffer[0]*aux) >> 10;
72 ch1 = (buffer[1]*aux) >> 10;
74 PRINTFDEBUG(
"B0 %u, B1 %u\n", buffer[0], buffer[1]);
75 PRINTFDEBUG(
"ch0 %lu, ch1 %lu\n", ch0, ch1);
79 lratio = (ratio+1) >> 1;
81 PRINTFDEBUG(
"ratio %lu, lratio %lu\n", ratio, lratio);
83 if ((lratio >= 0) && (lratio <= K1T))
84 tmp = (ch0*B1T) - (ch1*M1T);
85 else if (lratio <= K2T)
86 tmp = (ch0*B2T) - (ch1*M2T);
87 else if (lratio <= K3T)
88 tmp = (ch0*B3T) - (ch1*M3T);
89 else if (lratio <= K4T)
90 tmp = (ch0*B4T) - (ch1*M4T);
91 else if (lratio <= K5T)
92 tmp = (ch0*B5T) - (ch1*M5T);
93 else if (lratio <= K6T)
94 tmp = (ch0*B6T) - (ch1*M6T);
95 else if (lratio <= K7T)
96 tmp = (ch0*B7T) - (ch1*M7T);
97 else if (lratio > K8T)
98 tmp = (ch0*B8T) - (ch1*M8T);
100 if (tmp < 0) tmp = 0;
104 PRINTFDEBUG(
"tmp %lu\n", tmp);
114 light_ziglet_init (
void)
116 if (!(_TSL2563_STATUS & INITED))
118 PRINTFDEBUG (
"light ziglet init\n");
119 _TSL2563_STATUS |= INITED;
135 tsl2563_write_reg (uint8_t reg, uint16_t val)
137 uint8_t tx_buf[] = { reg, 0x00, 0x00 };
139 tx_buf[1] = (uint8_t) (val >> 8);
140 tx_buf[2] = (uint8_t) (val & 0x00FF);
142 i2c_transmitinit (TSL2563_ADDR);
144 PRINTFDEBUG (
"I2C Ready to TX\n");
146 i2c_transmit_n (3, tx_buf);
148 PRINTFDEBUG (
"WRITE_REG 0x%04X @ reg 0x%02X\n", val, reg);
159 tsl2563_read_reg (uint8_t reg)
161 uint16_t readBuf[] = {0x00, 0x00};
162 uint8_t buf[] = { 0x00, 0x00, 0x00, 0x00};
167 i2c_transmitinit (TSL2563_ADDR);
169 i2c_transmit_n (1, &rtx);
173 i2c_receiveinit (TSL2563_ADDR);
175 i2c_receive_n (4, &buf[0]);
178 PRINTFDEBUG(
"\nb0 %u, b1 %u, b2 %u, b3 %u\n", buf[0], buf[1], buf[2], buf[3]);
180 readBuf[0] = (buf[1] << 8 | (buf[0]));
181 readBuf[1] = (buf[3] << 8 | (buf[2]));
185 if(readBuf[0] == readBuf[1]){
186 tsl2563_read_reg(TSL2563_READ);
189 retVal = calculateLux(&readBuf);
195 light_ziglet_on(
void)
198 uint8_t regon[] = { 0x00, TSL2563_PWRN };
200 i2c_transmitinit (TSL2563_ADDR);
202 i2c_transmit_n (2, ®on);
204 data = (uint16_t) tsl2563_read_reg (TSL2563_READ);
209 light_ziglet_off(
void)
211 uint8_t regoff = 0x00;
213 i2c_transmitinit (TSL2563_ADDR);
215 i2c_transmit_n (1, ®off);
226 light_ziglet_read(
void)
229 lux = light_ziglet_on();