// this is a private function that is used to get data from the sensor (infrared + full spectrum including infrared) static inline int tsl2561_getdata(TSL2561 *sensor, uint16_t *full_spectrum, uint16_t *infrared) { TSL2561_ON(sensor); // wait for the internal ADC to complete conversion switch(sensor->integration_time) { case TSL2561_INTEGRATIONTIME_13MS: usleep(20000); break; case TSL2561_INTEGRATIONTIME_101MS: usleep(150000); break; case TSL2561_INTEGRATIONTIME_402MS: usleep(450000); break; } //usleep(450000); // reads two bytes from channel 0 (full spectrum + infrared) //*full_spectrum = wiringPiI2CReadReg16(_fd, TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW); *full_spectrum = tsl2561_read16(sensor, TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW); //fprintf(stdout, "got 0x%04X for full spectrum light\n", *full_spectrum); // reads two bytes from channel 1 (infrared) //*infrared = wiringPiI2CReadReg16(_fd, TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW); *infrared = tsl2561_read16(sensor, TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW); //fprintf(stdout, "got 0x%04X for ir light\n", *infrared); // turn the device off to save power TSL2561_OFF(sensor); return 0; }
int tsl2561_get_data(uint16_t *broadband, uint16_t *ir, struct tsl2561 *tsl2561) { int rc; int delay_ticks; /* Wait integration time ms before getting a data sample */ switch (tsl2561->cfg.integration_time) { case TSL2561_LIGHT_ITIME_13MS: delay_ticks = 14 * OS_TICKS_PER_SEC / 1000; break; case TSL2561_LIGHT_ITIME_101MS: delay_ticks = 102 * OS_TICKS_PER_SEC / 1000; break; case TSL2561_LIGHT_ITIME_402MS: default: delay_ticks = 403 * OS_TICKS_PER_SEC / 1000; break; } os_time_delay(delay_ticks); *broadband = *ir = 0; rc = tsl2561_read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW, broadband); if (rc) { goto err; } rc = tsl2561_read16(TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW, ir); if (rc) { goto err; } #if MYNEWT_VAL(TSL2561_STATS) switch (tsl2561->cfg.integration_time) { case TSL2561_LIGHT_ITIME_13MS: STATS_INC(g_tsl2561stats, samples_13ms); break; case TSL2561_LIGHT_ITIME_101MS: STATS_INC(g_tsl2561stats, samples_101ms); break; case TSL2561_LIGHT_ITIME_402MS: STATS_INC(g_tsl2561stats, samples_402ms); default: break; } #endif err: return rc; }
/** * Gets a new data sample from the light sensor. * * @param The sensor interface * @param broadband The full (visible + ir) sensor output * @param ir The ir sensor output * * @return 0 on success, non-zero on failure */ int tsl2561_get_data(struct sensor_itf *itf, uint16_t *broadband, uint16_t *ir) { int rc; *broadband = *ir = 0; rc = tsl2561_read16(itf, TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN0_LOW, broadband); if (rc) { goto err; } rc = tsl2561_read16(itf, TSL2561_COMMAND_BIT | TSL2561_WORD_BIT | TSL2561_REGISTER_CHAN1_LOW, ir); if (rc) { goto err; } return 0; err: return rc; }