Quad RGB sensor with SPI output drive, I2C display interface & analogue inputs / UI for various LED types
Dependencies: MCP23S17 PCF8574 TextLCD eeprom mbed-dev
Diff: main.cpp
- Revision:
- 0:9d301ae51ecf
- Child:
- 1:d99dad8a1f50
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Wed Aug 10 09:19:45 2016 +0000 @@ -0,0 +1,468 @@ +#include "mbed.h" +#include "TextLCD.h" +#include "VEML6040.h" + +#define FACTOR 1 + +#define SENSOR_ADDR (41 << 1) +#define WHITE_REG 148 +#define SENSOR_RED_REG 148 + +//------------------------------------------------------------------------------- +//----------- LED's ------------------------------------------------------------- +//------------------------------------------------------------------------------- +// Detection LED's use the onboard MBED LED's +// These are all blue on the MBED but on DSE PCB they areas follows +// LED1 = GREEN +// LED2 = BLUE +// LED3 = RED +// LED4 = YELLOW +DigitalOut GreenLed(LED1); +DigitalOut BlueLed(LED2); +DigitalOut RedLed(LED3); +DigitalOut YellowLed(LED4); + +//DigitalOut myled(LED1); +//DigitalOut myGreen(p21); +//DigitalOut myYellow(p22); +//DigitalOut myled4(LED4); +//DigitalOut green(LED1); +//------------------------------------------------------------------------------- + +//------------------------------------------------------------------------------- +//----------- Analogue Inputs --------------------------------------------------- +//------------------------------------------------------------------------------- +//Analogue inputs are used to set thresholds for detector levels +AnalogIn SystemThreshold1(p15); +//AnalogIn SystemThreshold2(p16); +//AnalogIn SystemThreshold3(p17); +//AnalogIn SystemThreshold4(p18); +//------------------------------------------------------------------------------- + +//------------------------------------------------------------------------------- +//------------ Text display ----------------------------------------------------- +//------------------------------------------------------------------------------- +//debug display. Standard display driver set up in 4 bit mode +//final version uses I2C port +TextLCD lcd(p14, p16, p17, p18, p19, p20, TextLCD::LCD16x2); // rs, e, d4-d7 + +//set up I2C Communication to LCD +//I2C i2c_lcd(p9,p10); // SDA, SCL + +//I2C Portexpander PCF8574 for LCD +//TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD16x2); // I2C bus, PCF8574 Slaveaddress, LCD Type ok + +//------------------------------------------------------------------------------- + + +//------------------------------------------------------------------------------- +//------------ colour sensor Comms Pins ----------------------------------------- +//------------------------------------------------------------------------------- +//TMC Sensor: this is the raw I2C Port, the clock like is muxed +I2C i2c(p28, p27); //pins for I2C communication (SDA, SCL) + + +typedef struct{ + uint16_t White; + uint16_t Red; + uint16_t Green; + uint16_t Blue; +}Colour; + +Colour SensorData; + +//------------------------------------------------------------------------------- + +//------------TSC Stuff---------------- +int sensor_addr = 41 << 1; + +char TempReg[1] = {0}; +char TempData[2] = {0,0}; + + +//------------TSC Stuff---------------- + +uint16_t Red = 1; +uint16_t Green = 2; +uint16_t Blue = 3; +uint16_t White = 4; + +uint32_t AvgRed = 0; +uint32_t AvgGreen = 0; +uint32_t AvgBlue = 0; +uint32_t AvgWhite = 0; +uint32_t Temp = 0; +uint32_t TempR = 0; +uint32_t TempG = 0; +uint32_t TempB = 0; + + +uint16_t RedProp = 0; +uint16_t GreenProp = 0; +uint16_t BlueProp = 0; + +uint16_t myMix = 0; + +float MyCCT; +float MyCCTi; +uint8_t DisplayIndex = 0; + +uint8_t Config = 0x30; +// bit description setting +// 0 SD, 0 = enabled 0 +// 1 AF, 0 = Automode 0 +// 2 TRIG, 0 = no trigrer 0 +// 3 N/F, 0 0 +// 4 IT0 0 0 +// 5 IT1 1 = 160ms 1 +// 6 IT2 0 0 +// 7 N/F 0 0 + + +uint16_t Filter(uint32_t *Acc,uint16_t NewData, uint8_t factor) +{ + uint32_t Temp = 0; + Temp = *Acc >> factor ; + Temp = *Acc - Temp + NewData; + *Acc = Temp; + return *Acc >> factor; +} + + +void ConfigureTSC(uint8_t Address) +{ + I2C i2c(p28, p27); //pins for I2C communication (SDA, SCL) + i2c.frequency(200000); + + + + char id_regval[1] = {146}; + char data[1] = {0}; + i2c.write(sensor_addr,id_regval,1, true); + i2c.read(sensor_addr,data,1,false); + + if (data[0]==68) { + GreenLed = 0; + wait (2); + GreenLed = 1; + } else { + GreenLed = 1; + } + + // Initialize color sensor + + char enable_register[2] = {128,3}; //enable the sensor + i2c.write(sensor_addr,enable_register,2,false); + + char timing_register[2] = {0x81,0xC0}; //approx 100ms + i2c.write(sensor_addr,timing_register,2,false); + + char control_register[2] = {0x8F,2}; //gain = x16 + i2c.write(sensor_addr,control_register,2,false); + + + + // Read data from color sensor (Clear/Red/Green/Blue) + //led = 1; + +} + + + +int main() +{ +//----------------------Initialise My TCS3472-------------------- + //Initialise the I2C port + ConfigureTSC(0); + + + +//---------------------- My TCS3472 -------------------- + + //myled2 = 1; + //myled3 = 1; + //myled4 = 1; + while(1) { + + DisplayIndex++; + if(DisplayIndex >= 20) { + DisplayIndex = 0; + } + //myled = 1; + wait(0.1); + //myled = 0; + //wait(0.1); + + //get new data +//--------------VEML6040------------------- +// RGBW.getRData(&Red); +// RGBW.getGData(&Green); +// RGBW.getBData(&Blue); +// RGBW.getWData(&White); +//--------------VEML6040------------------- + +//----------------------My TCS3472-------------------- + + //TempReg[0] = 148; + //TempData[0] = 0; + //TempData[1] = 0; + char clear_reg[1] = {148}; + char clear_data[2] = {0,0}; + + + i2c.write(SENSOR_ADDR, WHITE_REG,1, true); + i2c.read(sensor_addr,clear_data,2, false); + + White = ((int)clear_data[1] << 8) | clear_data[0]; + + + + char red_reg[1] = {150}; + char red_data[2] = {0,0}; + i2c.write(sensor_addr,red_reg,1, true); + i2c.read(sensor_addr,red_data,2, false); + + Red = ((int)red_data[1] << 8) | red_data[0]; + + char green_reg[1] = {152}; + char green_data[2] = {0,0}; + i2c.write(sensor_addr,green_reg,1, true); + i2c.read(sensor_addr,green_data,2, false); + + Green = ((int)green_data[1] << 8) | green_data[0]; + + char blue_reg[1] = {154}; + char blue_data[2] = {0,0}; + i2c.write(sensor_addr,blue_reg,1, true); + i2c.read(sensor_addr,blue_data,2, false); + + Blue = ((int)blue_data[1] << 8) | blue_data[0]; +//----------------------My TCS3472-------------------- + + + + + //Red filter + //Red = Filter(&AvgRed, Red, FACTOR); + + //Green Filter + //Green = Filter(&AvgGreen, Green, FACTOR); + + //Blue Filter + //Blue = Filter(&AvgBlue, Blue, FACTOR); + + //White Filter + //White = Filter(&AvgWhite, White, FACTOR); + +#define USEWHITE //normalised % for each +#ifdef USEWHITE + Temp = (Red+Blue+Green)*10000; + Temp = Temp / White; + TempR = (Red* 10000) / Temp ; + TempG = (Green* 10000) / Temp ; + TempB = (Blue* 10000) / Temp ; + + RedProp = (TempR*100) / White; + GreenProp = (TempG*100) / White; + BlueProp = (TempB*100) / White; +#else + // Vn2 without using white.... + Temp = (Red+Blue+Green); + RedProp = (Red* 100) / Temp ; + GreenProp = (Green* 100) / Temp ; + BlueProp = (Blue* 100) / Temp ; + + //RedProp = (TempR*100) / White; + //GreenProp = (TempG*100) / White; + //BlueProp = (TempB*100) / White; +#endif + + + + //make 12 bit only (display) + Red >>=4; + Green >>=4; + Blue >>=4; + White >>=4; + + //lcd.locate(0, 0); + //lcd.printf("%03x %03x %03x %03x",Red,Green,Blue, White); + + myMix = GreenProp*1000/RedProp; + lcd.locate(0, 0); + lcd.printf("Mix = %i %i ",myMix, White); + lcd.locate(0, 1); + + //lcd.printf("B=%03x W=%03x",Blue,White); + //if(DisplayIndex <=10) + { + //lcd.printf("R %i G %i B %i ",RedProp, GreenProp, BlueProp); + lcd.printf("ADC = %05i ",SystemThreshold1.read_u16()>>4); + } + // else + // { + // MyCCTi = (((float)Red-(float)Blue)/(float)Green) +0.5; + // MyCCT = 4278.6 * pow((double)MyCCTi,(double)-1.2455); + // //lcd.locate(0, 1); + // lcd.printf("CCT = %.2f ",MyCCT); + // } + +#define Mix + + //guess the colour + + //if((Green > 70) && (Red >70)) //looking at intensity somthing is on! + if(White > 15) { //looking at intensity somthing is on! +#ifndef Mix + if((GreenProp <= 30) && (RedProp >=50)) { //pretty sure it's Yellow + myYellow = 1; + myGreen = 0; + } else { + myYellow = 0; + if((GreenProp > 40) && (RedProp <=40)) { //pretty sure it's green + myGreen = 1; + } else { + myGreen = 1; + myYellow = 1; + } + } +#else + //as a single ratio + if((RedProp - GreenProp) > 20) { + //pretty sure it's Yellow + YellowLed = 1; + GreenLed = 0; + } else { + YellowLed = 0; + if((GreenProp - RedProp) > 5) { + //pretty sure it's green + GreenLed = 1; + } else { + GreenLed = 1; + YellowLed = 1; + } + } + /* + if(myMix >1600) + { + myGreen = 1; + myYellow = 0; + } + else + { + if(myMix <1100) + { + myGreen = 0; + myYellow = 1; + } + else + { + myGreen = 1; + myYellow = 1; + } + } + */ +#endif + + } else { + //not enough intensity to determine + GreenLed = 0; + YellowLed = 0; + } + } +} + + +//--------------------------------------------------------------------------------------------- +//--------------------------------------------------------------------------------------------- +//Code for the TCS34725 Part +//--------------------------------------------------------------------------------------------- +//--------------------------------------------------------------------------------------------- + +//#include "mbed.h" + +//I2C i2c(p9, p10); //pins for I2C communication (SDA, SCL) +//Serial pc(USBTX, USBRX); //Used to view the colors that are read in + +//int sensor_addr = 41 << 1; + +//DigitalOut green(LED1); +//DigitalOut led(p11); + +int TCS3472main() +{ +// pc.baud(9600); + GreenLed = 1; // off + + // Connect to the Color sensor and verify + + i2c.frequency(200000); + + char id_regval[1] = {146}; + char data[1] = {0}; + i2c.write(sensor_addr,id_regval,1, true); + i2c.read(sensor_addr,data,1,false); + + if (data[0]==68) { + GreenLed = 0; + wait (2); + GreenLed = 1; + } else { + GreenLed = 1; + } + + // Initialize color sensor + + char timing_register[2] = {129,0}; + i2c.write(sensor_addr,timing_register,2,false); + + char control_register[2] = {143,0}; + i2c.write(sensor_addr,control_register,2,false); + + char enable_register[2] = {128,3}; + i2c.write(sensor_addr,enable_register,2,false); + + // Read data from color sensor (Clear/Red/Green/Blue) +// led = 1; + while (true) { + char clear_reg[1] = {148}; + char clear_data[2] = {0,0}; + i2c.write(sensor_addr,clear_reg,1, true); + i2c.read(sensor_addr,clear_data,2, false); + + int clear_value = ((int)clear_data[1] << 8) | clear_data[0]; + + char red_reg[1] = {150}; + char red_data[2] = {0,0}; + i2c.write(sensor_addr,red_reg,1, true); + i2c.read(sensor_addr,red_data,2, false); + + int red_value = ((int)red_data[1] << 8) | red_data[0]; + + char green_reg[1] = {152}; + char green_data[2] = {0,0}; + i2c.write(sensor_addr,green_reg,1, true); + i2c.read(sensor_addr,green_data,2, false); + + int green_value = ((int)green_data[1] << 8) | green_data[0]; + + char blue_reg[1] = {154}; + char blue_data[2] = {0,0}; + i2c.write(sensor_addr,blue_reg,1, true); + i2c.read(sensor_addr,blue_data,2, false); + + int blue_value = ((int)blue_data[1] << 8) | blue_data[0]; + + // print sensor readings + +// pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n", clear_value, red_value, green_value, blue_value); + //The above code displays the red, green, and blue values read in by the color sensor. + wait(0.5); + } + +} + + + +//--------------------------------------------------------------------------------------------- +//--------------------------------------------------------------------------------------------- \ No newline at end of file