AS7265x functions for setting integration time, LEDs, collecting/reading data

Dependents:   IntegratingSphereController LaunchDay

Committer:
nthompson22
Date:
Thu Jan 24 01:27:45 2019 +0000
Revision:
0:3699cacb5a93
Child:
1:4d4e07dcc694
AS7265x functions for setting integration time, LEDs, collecting/reading data

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nthompson22 0:3699cacb5a93 1 #include "AS7265xfunctions.h"
nthompson22 0:3699cacb5a93 2 #define REG_ADDR_STATUS 0x00
nthompson22 0:3699cacb5a93 3 #define REG_ADDR_WRITE 0x01
nthompson22 0:3699cacb5a93 4 #define REG_ADDR_READ 0x02
nthompson22 0:3699cacb5a93 5 #define STATUS_TX_VALID 0x02
nthompson22 0:3699cacb5a93 6 #define STATUS_RX_VALID 0x01
nthompson22 0:3699cacb5a93 7 #define REG_DEV_SEL 0x4F
nthompson22 0:3699cacb5a93 8 #define REG_LED_CONFIG 0x07
nthompson22 0:3699cacb5a93 9 #define REG_INTEG_TIME 0x05
nthompson22 0:3699cacb5a93 10
nthompson22 0:3699cacb5a93 11 AS7265x::AS7265x(I2C i2c, int addr) : _i2c(i2c) {
nthompson22 0:3699cacb5a93 12 _addr = addr;
nthompson22 0:3699cacb5a93 13 }
nthompson22 0:3699cacb5a93 14
nthompson22 0:3699cacb5a93 15 void AS7265x::collectData(void){
nthompson22 0:3699cacb5a93 16 int k;
nthompson22 0:3699cacb5a93 17 for (int i = 0; i < 3; i++) { //goes through all 3 sensors
nthompson22 0:3699cacb5a93 18 selectDevice(i);
nthompson22 0:3699cacb5a93 19 for (int j = 0; j < 6; j++) { //goes through all 6 channels on each sensor
nthompson22 0:3699cacb5a93 20 uint16_t data = getData(j);
nthompson22 0:3699cacb5a93 21 k = 6*i + j; //gives the channel currently being read for data out of all 18 channels
nthompson22 0:3699cacb5a93 22 switch (k) { //assigns values to the array of data for all 18 channels
nthompson22 0:3699cacb5a93 23 case 0: _channels[8] = data; break;
nthompson22 0:3699cacb5a93 24 case 1: _channels[10] = data; break;
nthompson22 0:3699cacb5a93 25 case 2: _channels[12] = data; break;
nthompson22 0:3699cacb5a93 26 case 3: _channels[13] = data; break;
nthompson22 0:3699cacb5a93 27 case 4: _channels[14] = data; break;
nthompson22 0:3699cacb5a93 28 case 5: _channels [15] = data; break;
nthompson22 0:3699cacb5a93 29 case 6: _channels [6] = data; break;
nthompson22 0:3699cacb5a93 30 case 7: _channels [7] = data; break;
nthompson22 0:3699cacb5a93 31 case 8: _channels [9] = data; break;
nthompson22 0:3699cacb5a93 32 case 9: _channels [11] = data; break;
nthompson22 0:3699cacb5a93 33 case 10: _channels [16] = data; break;
nthompson22 0:3699cacb5a93 34 case 11: _channels [17] = data; break;
nthompson22 0:3699cacb5a93 35 case 12: _channels [0] = data; break;
nthompson22 0:3699cacb5a93 36 case 13: _channels [1] = data; break;
nthompson22 0:3699cacb5a93 37 case 14: _channels [2] = data; break;
nthompson22 0:3699cacb5a93 38 case 15: _channels [3] = data; break;
nthompson22 0:3699cacb5a93 39 case 16: _channels [4] = data; break;
nthompson22 0:3699cacb5a93 40 case 17: _channels [5] = data; break;
nthompson22 0:3699cacb5a93 41 default: _channels [0] = 0;
nthompson22 0:3699cacb5a93 42 }
nthompson22 0:3699cacb5a93 43 }
nthompson22 0:3699cacb5a93 44 }
nthompson22 0:3699cacb5a93 45 }
nthompson22 0:3699cacb5a93 46
nthompson22 0:3699cacb5a93 47 uint16_t AS7265x::readData(int i) {
nthompson22 0:3699cacb5a93 48 i--; //so that the user can put in a number 1 to 18 for which channel they want, but the readData function reads from channels 0 to 17
nthompson22 0:3699cacb5a93 49 return _channels[i];
nthompson22 0:3699cacb5a93 50 }
nthompson22 0:3699cacb5a93 51
nthompson22 0:3699cacb5a93 52 void AS7265x::regWrite(char reg, char data) {
nthompson22 0:3699cacb5a93 53 char status;
nthompson22 0:3699cacb5a93 54 char buff[3];
nthompson22 0:3699cacb5a93 55 bool writeBuffReady = false;
nthompson22 0:3699cacb5a93 56 while (!writeBuffReady) {
nthompson22 0:3699cacb5a93 57 // Read slave I²C status to see if the write buffer is ready.
nthompson22 0:3699cacb5a93 58 buff[0] = REG_ADDR_STATUS;
nthompson22 0:3699cacb5a93 59 _i2c.write(_addr, buff, 1, true); //true at the end to not send a stop signal before the next start signal because our sensor doesn't need it
nthompson22 0:3699cacb5a93 60 _i2c.read(_addr, buff, 1);
nthompson22 0:3699cacb5a93 61 status = buff[0];
nthompson22 0:3699cacb5a93 62 if ((status & STATUS_TX_VALID) == 0)
nthompson22 0:3699cacb5a93 63 // No inbound TX pending at slave. Okay to write now.
nthompson22 0:3699cacb5a93 64 writeBuffReady = true;
nthompson22 0:3699cacb5a93 65
nthompson22 0:3699cacb5a93 66 }
nthompson22 0:3699cacb5a93 67 // Send the virtual register address (enabling bit 7 to indicate a write).
nthompson22 0:3699cacb5a93 68 buff[0] = REG_ADDR_WRITE;
nthompson22 0:3699cacb5a93 69 buff[1] = reg | 0x80;
nthompson22 0:3699cacb5a93 70 _i2c.write(_addr, buff, 2);
nthompson22 0:3699cacb5a93 71 writeBuffReady = false;
nthompson22 0:3699cacb5a93 72 while (!writeBuffReady) {
nthompson22 0:3699cacb5a93 73 // Read the slave I²C status to see if the write buffer is ready.
nthompson22 0:3699cacb5a93 74 buff [0] = REG_ADDR_STATUS;
nthompson22 0:3699cacb5a93 75 _i2c.write(_addr, buff, 1, true);
nthompson22 0:3699cacb5a93 76 _i2c.read(_addr, buff, 1);
nthompson22 0:3699cacb5a93 77 status = buff[0];
nthompson22 0:3699cacb5a93 78 if ((status & STATUS_TX_VALID) == 0)
nthompson22 0:3699cacb5a93 79 // No inbound TX pending at slave. Okay to write data now.
nthompson22 0:3699cacb5a93 80 writeBuffReady = true;
nthompson22 0:3699cacb5a93 81 }
nthompson22 0:3699cacb5a93 82 // Send the data to complete the operation.
nthompson22 0:3699cacb5a93 83 buff [0] = REG_ADDR_WRITE;
nthompson22 0:3699cacb5a93 84 buff [1] = data;
nthompson22 0:3699cacb5a93 85 _i2c.write(_addr, buff, 2);
nthompson22 0:3699cacb5a93 86 }
nthompson22 0:3699cacb5a93 87
nthompson22 0:3699cacb5a93 88 char AS7265x::regRead(char reg) {
nthompson22 0:3699cacb5a93 89 char buff [3];
nthompson22 0:3699cacb5a93 90 char status;
nthompson22 0:3699cacb5a93 91 bool writeBuffReady = false;
nthompson22 0:3699cacb5a93 92 while (!writeBuffReady) {
nthompson22 0:3699cacb5a93 93 // Read slave I²C status to see if the read buffer is ready.
nthompson22 0:3699cacb5a93 94 buff [0] = REG_ADDR_STATUS;
nthompson22 0:3699cacb5a93 95 _i2c.write(_addr, buff, 1, true);
nthompson22 0:3699cacb5a93 96 _i2c.read(_addr, buff, 1);
nthompson22 0:3699cacb5a93 97 status = buff[0];
nthompson22 0:3699cacb5a93 98 if ((status & STATUS_TX_VALID) == 0)
nthompson22 0:3699cacb5a93 99 // No inbound TX pending at slave. Okay to write now.
nthompson22 0:3699cacb5a93 100 writeBuffReady = true;
nthompson22 0:3699cacb5a93 101 }
nthompson22 0:3699cacb5a93 102 // Send the virtual register address (disabling bit 7 to indicate a read).
nthompson22 0:3699cacb5a93 103 buff [0] = REG_ADDR_WRITE;
nthompson22 0:3699cacb5a93 104 buff [1] = reg;
nthompson22 0:3699cacb5a93 105 _i2c.write(_addr, buff, 2);
nthompson22 0:3699cacb5a93 106 bool readBuffReady = false;
nthompson22 0:3699cacb5a93 107 while (!readBuffReady) {
nthompson22 0:3699cacb5a93 108 // Read the slave I²C status to see if our read data is available.
nthompson22 0:3699cacb5a93 109 buff [0] = REG_ADDR_STATUS;
nthompson22 0:3699cacb5a93 110 _i2c.write(_addr, buff, 1, true);
nthompson22 0:3699cacb5a93 111 _i2c.read(_addr, buff, 1);
nthompson22 0:3699cacb5a93 112 status = buff[0];
nthompson22 0:3699cacb5a93 113 if ((status & STATUS_RX_VALID) != 0)
nthompson22 0:3699cacb5a93 114 // Read data is ready.
nthompson22 0:3699cacb5a93 115 readBuffReady = true;
nthompson22 0:3699cacb5a93 116 }
nthompson22 0:3699cacb5a93 117 // Read the data to complete the operation.
nthompson22 0:3699cacb5a93 118 buff [0] = REG_ADDR_READ;
nthompson22 0:3699cacb5a93 119 _i2c.write(_addr, buff, 1, true);
nthompson22 0:3699cacb5a93 120 _i2c.read(_addr, buff, 1) ;
nthompson22 0:3699cacb5a93 121 return buff[0];
nthompson22 0:3699cacb5a93 122 }
nthompson22 0:3699cacb5a93 123
nthompson22 0:3699cacb5a93 124 uint16_t AS7265x::getData(int channel){
nthompson22 0:3699cacb5a93 125 int channel_addr;
nthompson22 0:3699cacb5a93 126 uint16_t high, low;
nthompson22 0:3699cacb5a93 127 switch(channel) { //based on the channel number put into the function, converts to the address of that channel
nthompson22 0:3699cacb5a93 128 case 0: channel_addr = 0x08; break;
nthompson22 0:3699cacb5a93 129 case 1: channel_addr = 0x0A; break;
nthompson22 0:3699cacb5a93 130 case 2: channel_addr = 0x0C; break;
nthompson22 0:3699cacb5a93 131 case 3: channel_addr = 0x0E; break;
nthompson22 0:3699cacb5a93 132 case 4: channel_addr = 0x10; break;
nthompson22 0:3699cacb5a93 133 case 5: channel_addr = 0x12; break;
nthompson22 0:3699cacb5a93 134 default: channel_addr = 0x08;
nthompson22 0:3699cacb5a93 135 }
nthompson22 0:3699cacb5a93 136 high = regRead(channel_addr)<<8; //reads the highest 8 bits of data, and shifts them over by 8, leaving 8 zeroes
nthompson22 0:3699cacb5a93 137 low = regRead(channel_addr + 1); //reads the lowest 8 bits of data
nthompson22 0:3699cacb5a93 138 return high | low; //lowest 8 bits of data fill in the 8 zeroes left by the highest 8 bits being shifted, created a 16 bit number
nthompson22 0:3699cacb5a93 139
nthompson22 0:3699cacb5a93 140 }
nthompson22 0:3699cacb5a93 141
nthompson22 0:3699cacb5a93 142 void AS7265x::selectDevice(int dev) {
nthompson22 0:3699cacb5a93 143 switch(dev) { //based on the value put into the function, converts to the address of the corresponding sensor and selects it
nthompson22 0:3699cacb5a93 144 case 0: regWrite(REG_DEV_SEL, 0x00); break;
nthompson22 0:3699cacb5a93 145 case 1: regWrite(REG_DEV_SEL, 0x01); break;
nthompson22 0:3699cacb5a93 146 case 2: regWrite(REG_DEV_SEL, 0x02); break;
nthompson22 0:3699cacb5a93 147 default: regWrite(REG_DEV_SEL, 0x00);
nthompson22 0:3699cacb5a93 148 }
nthompson22 0:3699cacb5a93 149 }
nthompson22 0:3699cacb5a93 150
nthompson22 0:3699cacb5a93 151 void AS7265x::ledSwitch(int dev, int set) {
nthompson22 0:3699cacb5a93 152 char setting;
nthompson22 0:3699cacb5a93 153 selectDevice(dev);
nthompson22 0:3699cacb5a93 154 setting = regRead(REG_LED_CONFIG); //configures the bits necessary to control the LED, now bit 3 will turn it on or off
nthompson22 0:3699cacb5a93 155 if(set) {
nthompson22 0:3699cacb5a93 156 setting = setting | 0x08; //turns bit 3 on
nthompson22 0:3699cacb5a93 157 }
nthompson22 0:3699cacb5a93 158 else {
nthompson22 0:3699cacb5a93 159 setting = setting & (0xFF - 0x08); //turns bit 3 off
nthompson22 0:3699cacb5a93 160 }
nthompson22 0:3699cacb5a93 161 regWrite(REG_LED_CONFIG, setting);
nthompson22 0:3699cacb5a93 162 }
nthompson22 0:3699cacb5a93 163
nthompson22 0:3699cacb5a93 164 void AS7265x::setAllLeds(int set) {
nthompson22 0:3699cacb5a93 165 for (int i = 0; i < 3; i++) {
nthompson22 0:3699cacb5a93 166 ledSwitch(i, set); //goes through every sensor, turning the corresponding LED on or off one at a time
nthompson22 0:3699cacb5a93 167 }
nthompson22 0:3699cacb5a93 168 }
nthompson22 0:3699cacb5a93 169
nthompson22 0:3699cacb5a93 170 char AS7265x::getDeviceType() {
nthompson22 0:3699cacb5a93 171 return regRead(0x00);
nthompson22 0:3699cacb5a93 172 }
nthompson22 0:3699cacb5a93 173
nthompson22 0:3699cacb5a93 174 char AS7265x::getHardwareVersion() {
nthompson22 0:3699cacb5a93 175 return regRead(0x01);
nthompson22 0:3699cacb5a93 176 }
nthompson22 0:3699cacb5a93 177
nthompson22 0:3699cacb5a93 178 void AS7265x::setIntegTime(int integTime) {
nthompson22 0:3699cacb5a93 179 char setting;
nthompson22 0:3699cacb5a93 180 setting = integTime/2.8;
nthompson22 0:3699cacb5a93 181 /* divides by 2.8 because the sensor takes the value put into the function and multiplies it by 2.8 to get the integration
nthompson22 0:3699cacb5a93 182 time in ms. This way, the user can input how many ms they want the integration time to be */
nthompson22 0:3699cacb5a93 183 regWrite(REG_INTEG_TIME, setting);
nthompson22 0:3699cacb5a93 184 }