14#include "opt3001_drv.h"
19static const uint8_t REG_RESULT = 0x00;
20static const uint8_t REG_CONF = 0x01;
23static const uint8_t REG_MAN_ID = 0x7E;
24static const uint8_t REG_DEV_ID = 0x7F;
27static const uint16_t MASK_RESULT_MAT = 0x0FFF;
30opt3001_drv::opt3001_drv(
i2c_interface & i2c, uint8_t i2c_addr)
31: _i2c(i2c), _i2c_addr(i2c_addr) { }
33void opt3001_drv::start_measure(uint16_t CONF){
34 writeRegister(OPT3001::REG_CONF, CONF);
37bool opt3001_drv::measure_ready() {
38 uint16_t val = readRegister(OPT3001::REG_CONF);
42uint32_t opt3001_drv::get_light(){
43 uint32_t raw = readRegister(OPT3001::REG_RESULT);
44 uint16_t exp = raw >> 12;
45 raw &= OPT3001::MASK_RESULT_MAT;
46 return (raw << exp) / 100;
49bool opt3001_drv::detect_sensor() {
50 uint16_t man = readRegister(OPT3001::REG_MAN_ID);
51 uint16_t dev = readRegister(OPT3001::REG_DEV_ID);
52 return (man == 0x5449 && dev == 0x3001);
56void opt3001_drv::writeRegister(uint8_t reg, uint16_t value){
59 txbuf[1] = value >> 8;
60 txbuf[2] = value & 0x00FF;
61 _i2c.i2cWrite(_i2c_addr, txbuf, 3);
64uint16_t opt3001_drv::readRegister(uint8_t reg){
69 _i2c.i2cWrite(_i2c_addr, txbuf, 1);
70 _i2c.i2cRead (_i2c_addr, rxbuf, 2);
71 val = (rxbuf[0] << 8) | rxbuf[1];