40 #include "lib/sensors.h" 55 #define PRINTF(...) printf(__VA_ARGS__) 61 #define OPT3001_I2C_ADDRESS 0x45 64 #define REG_RESULT 0x00 65 #define REG_CONFIGURATION 0x01 66 #define REG_LOW_LIMIT 0x02 67 #define REG_HIGH_LIMIT 0x03 69 #define REG_MANUFACTURER_ID 0x7E 70 #define REG_DEVICE_ID 0x7F 77 #define CONFIG_RN 0x00F0 78 #define CONFIG_CT 0x0008 79 #define CONFIG_M 0x0006 80 #define CONFIG_OVF 0x0001 81 #define CONFIG_CRF 0x8000 82 #define CONFIG_FH 0x4000 83 #define CONFIG_FL 0x2000 84 #define CONFIG_L 0x1000 85 #define CONFIG_POL 0x0800 86 #define CONFIG_ME 0x0400 87 #define CONFIG_FC 0x0300 90 #define CONFIG_CT_100 0x0000 91 #define CONFIG_CT_800 CONFIG_CT 94 #define CONFIG_M_CONTI 0x0004 95 #define CONFIG_M_SINGLE 0x0002 96 #define CONFIG_M_SHUTDOWN 0x0000 99 #define CONFIG_RN_RESET 0x00C0 100 #define CONFIG_CT_RESET CONFIG_CT_800 101 #define CONFIG_L_RESET 0x1000 102 #define CONFIG_DEFAULTS (CONFIG_RN_RESET | CONFIG_CT_100 | CONFIG_L_RESET) 105 #define CONFIG_ENABLE_CONTINUOUS (CONFIG_M_CONTI | CONFIG_DEFAULTS) 106 #define CONFIG_ENABLE_SINGLE_SHOT (CONFIG_M_SINGLE | CONFIG_DEFAULTS) 107 #define CONFIG_DISABLE CONFIG_DEFAULTS 110 #define REGISTER_LENGTH 2 113 #define DATA_LENGTH 2 120 #define SENSOR_STATE_SLEEPING 0 121 #define SENSOR_STATE_ACTIVE 1 122 #define SENSOR_STATE_DATA_READY 2 124 static int state = SENSOR_STATE_SLEEPING;
127 #define SENSOR_STARTUP_DELAY (CLOCK_SECOND >> 3) 129 static struct ctimer startup_timer;
155 if(val & CONFIG_CRF) {
156 sensors_changed(&opt_3001_sensor);
157 state = SENSOR_STATE_DATA_READY;
171 uint16_t had_data_ready = state & SENSOR_STATE_DATA_READY;
176 val = CONFIG_ENABLE_SINGLE_SHOT;
179 state = SENSOR_STATE_ACTIVE;
181 val = CONFIG_DISABLE;
184 state = SENSOR_STATE_SLEEPING | had_data_ready;
201 if((state & SENSOR_STATE_DATA_READY) != SENSOR_STATE_DATA_READY) {
216 *raw_data = (val << 8) | (val >> 8 & 0xFF);
234 m = raw_data & 0x0FFF;
235 e = (raw_data & 0xF000) >> 12;
237 return m * (0.01 * exp2(e));
255 return CC26XX_SENSOR_READING_ERROR;
258 converted_val =
convert(raw_val);
259 PRINTF(
"OPT: %04X r=%d (centilux)\n", raw_val,
260 (
int)(converted_val * 100));
262 rv = (int)(converted_val * 100);
283 case SENSORS_HW_INIT:
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
static float convert(uint16_t raw_data)
Convert raw data to a value in lux.
Header file with macros which rename TI CC26xxware functions.
Header file for the Sensortag I2C Driver.
static void enable_sensor(bool enable)
Turn the sensor on/off.
void sensor_common_set_error_data(uint8_t *buf, uint8_t len)
Fill a result buffer with dummy error data.
bool sensor_common_read_reg(uint8_t addr, uint8_t *buf, uint8_t len)
Reads a sensor's register over I2C.
Header file for the Sensortag Opt3001 light sensor.
Header file for the callback timer
Header file for the Sensortag Common sensor utilities.
static bool read_data(uint16_t *raw_data)
Read the result register.
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
static int value(int type)
Returns a reading from the sensor.
static int status(int type)
Returns the status of the sensor.
void board_i2c_select(uint8_t new_interface, uint8_t address)
Select an I2C slave.
bool sensor_common_write_reg(uint8_t addr, uint8_t *buf, uint8_t len)
Write to a sensor's register over I2C.
static void select_on_bus(void)
Select the sensor on the I2C bus.
static void notify_ready(void *unused)
Callback when sensor is ready to read data from.
static int configure(int type, int enable)
Configuration function for the OPT3001 sensor.