Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions drivers/include/opt3001.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* @{
* @file opt3001.h
* @brief driver for OPT3001 sensor
* @author Oleg Artamonov [info@unwds.com]
* @author Oleg Artamonov [info@unwds.com],Indrishenok Alexandr [https://github.com/sashaindrish]
*
*/
#ifndef OPT3001_H_
#define OPT3001_H_
Expand All @@ -40,8 +41,8 @@
#define OPT3001_CFG_FC_4 0x0200
#define OPT3001_CFG_FC_8 0x0300
#define OPT3001_CFG_MASK 0x0400
#define OPT3001_CFG_POLPOS 0x0800
#define OPT3001_CFG_LATCH 0x1000
#define OPT3001_CFG_POLPOS 0x0800 // set _/-\_
#define OPT3001_CFG_LATCH 0x1000 // on pin int
#define OPT3001_CFG_FLAGL 0x2000
#define OPT3001_CFG_FLAGH 0x4000
#define OPT3001_CFG_CRF 0x8000
Expand All @@ -53,7 +54,7 @@
#define OPT3001_CFG_800MS 0x0008
#define OPT3001_CFG_RNAUTO 0x00C0

#define OPT3001_CFG (OPT3001_CFG_FC_1 | OPT3001_CFG_SHOT | OPT3001_CFG_100MS | OPT3001_CFG_RNAUTO )
#define OPT3001_CFG (OPT3001_CFG_FC_1 | OPT3001_CFG_SHOT | OPT3001_CFG_100MS | OPT3001_CFG_RNAUTO)
#define OPT3001_CFG_DEFAULT 0x10C8

/**
Expand All @@ -65,10 +66,13 @@
#define OPT3001_CHIP_ID 0x4954
#define OPT3001_REG_CONFIG_MASK 0xFE1F

#define OPT3001_REG_LOW_LIM 0x02
#define OPT3001_REG_HIGH_LIM 0x03

/**
* @brief Structure that holds the OPT3001 driver internal state and parameters
*/
typedef struct {
typedef struct {
i2c_t i2c; /**< Holds driver parameters */

} opt3001_t;
Expand Down Expand Up @@ -108,4 +112,15 @@ int opt3001_init(opt3001_t *dev);
*/
uint32_t opt3001_measure(opt3001_t *dev, opt3001_measure_t *measure);

//uint32_t opt3001_measure_interrupt(opt3001_t *dev, opt3001_measure_t *measure);

int write_registr(opt3001_t *dev, uint8_t addres_reg, uint16_t reg, uint16_t size);

void write_sensor_lim(opt3001_t *dev, float lim_lum_high, float lim_lum_low);

uint16_t get_data_reg(float data);

int write_registr(opt3001_t *dev, uint8_t addres_reg,uint16_t reg, uint16_t size);


#endif /* OPT3001_H_ */
118 changes: 98 additions & 20 deletions drivers/opt3001/opt3001.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @{
* @file opt3001.c
* @brief basic driver for OPT3001 sensor
* @authoh Oleg Artamonov [info@unwds.com]
* @authoh Oleg Artamonov [info@unwds.com], Indrishenok Alexandr [https://github.com/sashaindrish]
*/


Expand Down Expand Up @@ -62,6 +62,8 @@ int opt3001_init(opt3001_t *dev)
i2c_release(dev->i2c);
return -1;
}


/*
i2c_read_regs(dev->i2c, OPT3001_ADDRESS, OPT3001_REG_CONFIG, (char *)&chipid, 2);

Expand All @@ -77,27 +79,12 @@ int opt3001_init(opt3001_t *dev)
}

static uint32_t read_sensor(opt3001_t *dev) {

uint16_t data;
data = OPT3001_CFG;
i2c_write_regs(dev->i2c, OPT3001_ADDRESS, OPT3001_REG_CONFIG, (char *)&data, 2);

/* wait till measurement is finished */
int i = 100;
do {
/* 10 ms delay */
rtctimers_millis_sleep(10);

i2c_read_regs(dev->i2c, OPT3001_ADDRESS, OPT3001_REG_CONFIG, (char *)&data, 2);
if (data & OPT3001_CFG_CRF) {
break;
}

i--;
} while (i);

if (i == 0) {
return 0;
}
/* write in reg dev , addres reg, data reg */

write_registr(dev, OPT3001_REG_CONFIG, data, 2);

/* read result */
i2c_read_regs(dev->i2c, OPT3001_ADDRESS, OPT3001_REG_RESULT, (char *)&data, 2);
Expand Down Expand Up @@ -145,6 +132,97 @@ uint32_t opt3001_measure(opt3001_t *dev, opt3001_measure_t *measure)
}


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

void write_sensor_lim(opt3001_t *dev, float lim_lum_high, float lim_lum_low) {
uint16_t data;
data = OPT3001_CFG;
write_registr(dev, OPT3001_REG_CONFIG, data, 2);



float lum = lim_lum_high;

lum = get_data_reg(lum);


write_registr(dev, OPT3001_REG_HIGH_LIM, lum, 2);

lum = lim_lum_low;

lum = get_data_reg(lum);

write_registr(dev, OPT3001_REG_LOW_LIM, lum, 2);




}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
int write_registr(opt3001_t *dev, uint8_t addres_reg,uint16_t reg, uint16_t size){

uint16_t data_reg = reg;

i2c_write_regs(dev->i2c, OPT3001_ADDRESS, addres_reg, (char *)&data_reg, size);

/* wait till measurement is finished */
int i = 100;
do {
/* 10 ms delay */
rtctimers_millis_sleep(10);

i2c_read_regs(dev->i2c, OPT3001_ADDRESS, addres_reg, (char *)&data_reg, size);
if (data_reg & reg) {
break;
}

i--;
} while (i);

if (i == 0) {
return 0;
}else return 1;


}

uint16_t get_data_reg(float data){
uint16_t res=0;

if(data<=81.90)
res=0;
else if(data<=163.80)
res=1;
else if(data<=327.60)
res=2;
else if(data<=655.2)
res=3;
else if(data<=1310.40)
res=4;
else if(data<=2620.80)
res=5;
else if(data<=5241.60)
res=6;
else if(data<=10483.20)
res=7;
else if(data<=20966.40)
res=8;
else if(data<=41932.80)
res=9;
else if(data<=83865.60)
res=10;


uint32_t lsb_size_x100 = (1 << res);
uint16_t reg_data = (uint16_t)(data/lsb_size_x100)*100;
reg_data = ((reg_data << 8) | (reg_data >> 8));

res = (res<<12);
return reg_data|res;
}


#ifdef __cplusplus
}
#endif