Sensorian  1.0
C API Reference Guide Library
APDS9300.c
Go to the documentation of this file.
1 /****************************************************************************
2  * Copyright (C) 2015 Sensorian
3  * *
4  * This file is part of Sensorian. *
5  * *
6  * Sensorian is free software: you can redistribute it and/or modify it *
7  * under the terms of the GNU Lesser General Public License as published *
8  * by the Free Software Foundation, either version 3 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * Sensorian is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with Sensorian. *
18  * If not, see <http://www.gnu.org/licenses/>. *
19  ****************************************************************************/
20 
28 #include "APDS9300.h"
29 #include "i2c.h"
30 #include "math.h"
31 
32 #pragma GCC diagnostic ignored "-pedantic"
33 
37 
42 unsigned char AL_Initialize(void)
43 {
45  AL_PowerState(POWER_ON); //Power on sensor
46  return control;
47 }
48 
54 void AL_PowerState(powerState state)
55 {
56  unsigned char cmd = (unsigned char) state;
57  APDS9300_WriteByte(COMMAND|CMD_CLEAR_INT|CONTROL); //Clear interrupts
58  APDS9300_WriteByte(cmd); //Power on/off the sensors by writing to control reg
59 }
60 
65 unsigned char AL_ChipID(void)
66 {
67  unsigned char partID = 0x00;
68  partID = APDS9300_ReadByte(COMMAND|ID);
69  return partID;
70 }
71 
77 unsigned int AL_ReadChannel(channel chan)
78 {
79  unsigned int channelValue = 0;
80  switch(chan)
81  {
82  case CH0:
84  channelValue = APDS9300_ReadWord();
85  break;
86  case CH1:
88  channelValue = APDS9300_ReadWord();
89  break;
90  default:
91  channelValue = 0;
92  break;
93  }
94  return channelValue;
95 }
96 
103 float AL_Lux(unsigned int ch0, unsigned int ch1)
104 {
105  float k = ch1/ch0;
106  float Lux=0;
107 
108  if((k>=0)&& (k<=0.52))
109  Lux=(0.0315*ch0)-(0.0593*ch0*pow(k,1.4));
110  else if((k>0.52)&& (k<=0.65))
111  Lux=(0.0229*ch0)-(0.0291*ch1);
112  else if((k>0.65)&& (k<=0.80))
113  Lux=(0.0157*ch0)-(0.0180*ch1);
114  else if((k>0.80)&& (k<=1.30))
115  Lux=(0.00338*ch0)-(0.00260*ch1);
116  else
117  Lux=0;
118  return Lux;
119 }
120 
126 unsigned char AL_SetGain(gain val)
127 {
128  unsigned char value = 0;
129  switch(val)
130  {
131  case GAIN_1:
132  value = APDS9300_ReadByte(COMMAND|TIMING); //Write to TIMING register
133  value &= 0b11101111;
134  APDS9300_WriteByte(COMMAND|TIMING); //Write to TIMING register
135  APDS9300_WriteByte(value);
136  break;
137  case GAIN_16:
138  APDS9300_WriteByte(COMMAND|TIMING); //Write to TIMING register
140  break;
141  default:
144  }
145  return val;
146 }
147 
153 void AL_SetSamplingTime(sampTime_t sampling_time)
154 {
156  APDS9300_WriteByte(sampling_time);
157 }
158 
164 void AL_SetIntLowThreshold(unsigned int lowthreshvalue)
165 {
166  APDS9300_WriteWord(COMMAND | THRESHLOWLOW| CMD_WORD,lowthreshvalue);
167 }
168 
174 void AL_SetIntHighThreshold(unsigned int highthreshvalue)
175 {
177 }
178 
183 void AL_Clear_Interrupt(void)
184 {
185  APDS9300_WriteByte(COMMAND|CMD_CLEAR_INT); //Set clear bit to 1
186 }
187 
196 void AL_ConfigureInterrupt(unsigned char enable, unsigned char persistence)
197 {
198  if(enable)
199  {
201  APDS9300_WriteByte(persistence);
202  }
203  else
204  {
207  }
208 }
209 
211 
212 
219 void APDS9300_WriteByte(unsigned char data)
220 {
221  I2C_WriteByte(data); //Sets a specific register to a certain value
222 }
223 
230 void APDS9300_WriteRegister(unsigned char reg, unsigned char data)
231 {
232  I2C_WriteByteRegister(reg,data); //Writes data on the specific register
233 }
234 
241 void APDS9300_WriteWord(unsigned char reg, unsigned int data)
242 {
243  unsigned char buffer[2];
244  buffer[1] = (unsigned char) (data >> 8); //MSB
245  buffer[0] = (unsigned char) (data && 0xff); //LSB
246  I2C_WriteWordRegister(reg,buffer);
247 }
248 
254 char APDS9300_ReadByte(char reg)
255 {
256  return I2C_ReadByteRegister(reg);
257 }
258 
264 unsigned int APDS9300_ReadWordReg(char reg)
265 { unsigned int val = I2C_ReadWordRegisterRS(reg);
266  return val;
267 }
268 
273 unsigned int APDS9300_ReadWord(void)
274 {
275  char buff[2] = {0x00,0x00};
276  bcm2835_i2c_read(buff,2);
277 
278  return (buff[1] << 8)|buff[0];
279 }
#define TIMING
Definition: APDS9300.h:41
#define COMMAND
Definition: APDS9300.h:57
void APDS9300_WriteWord(unsigned char reg, unsigned int data)
Writes a word to the specific register.
Definition: APDS9300.c:241
#define ID
Definition: APDS9300.h:48
unsigned int APDS9300_ReadWord(void)
Read a single word from the sensor. Assumes register pointer is preset.
Definition: APDS9300.c:273
void AL_SetSamplingTime(sampTime_t sampling_time)
Sets the sampling time for the sensor,can be one of three predetermined values.
Definition: APDS9300.c:153
unsigned char buffer[256]
Definition: main.c:74
void AL_SetIntHighThreshold(unsigned int highthreshvalue)
Sets the high threshold value for the interrupt.
Definition: APDS9300.c:174
void I2C_WriteByte(char bdata)
Writes a byte value to the I2C bus. This assumes the register pointer is preset.
Definition: i2c.c:55
#define DATA1LOW
Definition: APDS9300.h:51
unsigned char AL_ChipID(void)
Returns the CHIP id.
Definition: APDS9300.c:65
Definition: APDS9300.h:90
unsigned char AL_Initialize(void)
Powers on the sensor , sets sensor gain to 1x and clears any existing interrupts. ...
Definition: APDS9300.c:42
void I2C_WriteByteRegister(unsigned char reg, unsigned char data)
Writes a byte value to a register address.
Definition: i2c.c:66
#define GAIN
Definition: APDS9300.h:67
void AL_SetIntLowThreshold(unsigned int lowthreshvalue)
Sets the low threshold value for the interrupt.
Definition: APDS9300.c:164
unsigned char AL_SetGain(gain val)
Set sensor gain. Default gain value is GAIN_1 or 1x.
Definition: APDS9300.c:126
void AL_PowerState(powerState state)
Set the sensor power state to either POWER_ON or POWER_OFF.
Definition: APDS9300.c:54
void AL_ConfigureInterrupt(unsigned char enable, unsigned char persistence)
Enables or disables the interrupt and sets the persistence of interrupt occurences The interrupt will...
Definition: APDS9300.c:196
#define INTERRUPT
Definition: APDS9300.h:46
Definition: APDS9300.h:91
#define INTERR_ENA
Definition: APDS9300.h:78
unsigned int APDS9300_ReadWordReg(char reg)
Read word from register.
Definition: APDS9300.c:264
#define THRESHHIGHLOW
Definition: APDS9300.h:44
#define THRESHLOWLOW
Definition: APDS9300.h:42
void APDS9300_WriteByte(unsigned char data)
Writes a single byte of data to the current sensor register. Function assumes that I2C peripheral is ...
Definition: APDS9300.c:219
channel
Definition: APDS9300.h:90
void AL_Clear_Interrupt(void)
Clear any existing interrupts.
Definition: APDS9300.c:183
#define CMD_WORD
Definition: APDS9300.h:59
#define CMD_CLEAR_INT
Definition: APDS9300.h:58
I2C driver.
#define CONTROL
Definition: APDS9300.h:40
void I2C_WriteWordRegister(unsigned char reg, unsigned char *data)
Writes a word value (16 bit) to a register address.
Definition: i2c.c:82
unsigned char I2C_ReadByteRegister(char reg)
Reads a byte from a register.
Definition: i2c.c:122
gain
Definition: APDS9300.h:94
unsigned int AL_ReadChannel(channel chan)
Reads the value from one of the two photodiode channels.
Definition: APDS9300.c:77
char APDS9300_ReadByte(char reg)
Reads a single byte from the given sensor register.
Definition: APDS9300.c:254
#define DATA0LOW
Definition: APDS9300.h:49
#define INTERR_DISMASK
Definition: APDS9300.h:77
float AL_Lux(unsigned int ch0, unsigned int ch1)
Computes LUX ambient light value from sensor data.
Definition: APDS9300.c:103
sampTime_t
Definition: APDS9300.h:101
unsigned int I2C_ReadWordRegisterRS(char reg)
Readm result from a word length register.
Definition: i2c.c:148
powerState
Definition: APDS9300.h:98
void APDS9300_WriteRegister(unsigned char reg, unsigned char data)
Writes a byte to the specific register.
Definition: APDS9300.c:230