Quad RGB sensor with SPI output drive, I2C display interface & analogue inputs / UI for various LED types

Dependencies:   MCP23S17 PCF8574 TextLCD eeprom mbed-dev

Revision:
6:6346ef2b4638
Parent:
5:dc77c5967412
Child:
7:9c5bca0ac9d7
--- a/main.cpp	Tue Aug 16 15:54:31 2016 +0000
+++ b/main.cpp	Thu Aug 18 07:41:33 2016 +0000
@@ -157,6 +157,9 @@
                     STD_GREEN,      15,  //sensor 15
                 };
 
+//new config trpe
+
+
 
 char LEDState[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //current detected LED states
 
@@ -165,42 +168,11 @@
 uint8_t SensorNumber = 0;   //current sensor number
 char SensorResult = 0;      //detected colour
 
+uint8_t ConfigureSensor;
 
 //------------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;
-uint16_t Yellow = 0;
-
-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 SensorConfig = 0x30;
 // bit  description              setting
 //  0   SD,   0 = enabled             0
@@ -211,9 +183,20 @@
 //  5   IT1   1 = 160ms               1
 //  6   IT2   0                       0
 //  7   N/F   0                       0
+//------------TSC Stuff----------------
+
+uint16_t Red = 1;
+uint16_t Green = 2;
+uint16_t Blue = 3;
+uint16_t White = 4;
+
+uint32_t Temp = 0;
+
+uint16_t RedProp = 0;
+uint16_t GreenProp = 0;
+uint16_t BlueProp = 0;
 
 
-uint16_t MyPort = 1;
 
 //working thresholds from pot inputs, scaled 0-100
 uint16_t    RedThreshold    = 0;
@@ -221,7 +204,7 @@
 uint16_t    BlueThreshold   = 0;
 uint16_t    Hysteresis      = 0;
 
-
+//------------------------- filter routine if required -------------------------------
 uint16_t Filter(uint32_t *Acc,uint16_t NewData, uint8_t factor)
 {
     uint32_t Temp = 0;
@@ -237,38 +220,29 @@
     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;
+    for (ConfigureSensor = 0; ConfigureSensor < 16; ConfigureSensor++)
+    {
+        if((Config[ConfigureSensor]<<1) != NOTUSED)
+        { 
+            SelectSensorPort(ConfigureSensor); //set up H/W mux for this sensor
+              
+            char id_regval[1] = {146};
+            char data[1] = {0};
+            i2c.write(sensor_addr,id_regval,1, true);
+            i2c.read(sensor_addr,data,1,false);         // 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);
+        }
     }
+}
 
-    // 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;
-
-}
 
 Colour GetSensorData(char SensorNumber)
 {
@@ -318,7 +292,7 @@
 void SetOutputBit(char bit)
 {
     uint16_t OutputWord = 1;
-    char BitA, BitB, PortA, PortB;
+//    char BitA, BitB, PortA, PortB;
     
     OutputWord <<= bit;
     OutputData |= OutputWord;
@@ -338,7 +312,7 @@
 void ClearOutputBit(char bit)
 {
     uint16_t OutputWord = 1;
-    char BitA, BitB, PortA, PortB;
+//    char BitA, BitB, PortA, PortB;
     
     OutputWord <<= bit;
     OutputWord = ~OutputWord;
@@ -422,15 +396,18 @@
     } 
 }
 
+void ScaleColours(void)
+{
+     //make 12 bit only
+    Red >>=4;
+    Green >>=4;
+    Blue >>=4;
+    White >>=4;   
+}
+
 
 void UpdateDisplay(void)
 {
-    //make 12 bit only (display)
-    Red >>=4;
-    Green >>=4;
-    Blue >>=4;
-    White >>=4;
-
     lcd.locate(0, 0);
     lcd.printf("R%02i G%02i B%02i W%04i",RedProp,GreenProp,BlueProp,White); 
     lcd.locate(0, 1);       
@@ -665,38 +642,15 @@
 
 int main()
 {
-    InitOutputs();
-    //----------------------Initialise My TCS3472--------------------
-    ConfigureTSC(0);
-    //----------------------    My TCS3472       --------------------
+    InitOutputs();          //set up output driver chip
+    ConfigureTSC(0);        //Initialise all TCS34725's
 
     while(1) 
     {
-        
-        //WriteOutputPort(MyPort);
-        /*
-        ClearOutputBit(MyPort);
-        MyPort++;
-        if (MyPort == 16)
-        {
-            MyPort = 0;
-        }
-        SetOutputBit(MyPort);       
-       */ 
-        GetThresholds();
-
-        DisplayIndex++;
-        if(DisplayIndex >= 20) 
-        {
-            DisplayIndex = 0;
-        }
-
-        wait(0.1);
+        GetThresholds();    //update thresholds   
+        wait(0.1);          //100ms Update Cycle
         OutputData = 0;
         
-//----------------------My TCS3472--------------------
-
-
         for(SensorNumber = 0; SensorNumber < 16; SensorNumber++)
         {
             if(Config[((SensorNumber << 1))] != NOTUSED) //check to see if the sensor is being used
@@ -708,6 +662,8 @@
                 Green = SensorData.Green;
                 Blue  = SensorData.Blue;
 
+                ScaleColours();
+                
                 UpdateDisplay();
                 
                 // work out % of colour mix
@@ -722,170 +678,6 @@
                 ShowLeds(SensorResult);    
             } //end if
         } //end for 
-        WriteOutputPort(OutputData);
-
-
-
-/*
-        //guess the colour
-        if(White > WHITE_THRESHOLD) //looking at intensity somthing is on!
-        { 
-            if((GreenProp <= 30) && (RedProp >=50)) 
-            { //pretty sure it's Yellow
-                YellowLed = 1;
-                GreenLed = 0;
-            } 
-            else 
-            {
-                YellowLed = 0;
-                if((GreenProp > 40) && (RedProp <=40)) { //pretty sure it's green
-                    GreenLed = 1;
-                } else {
-                    GreenLed = 1;
-                    YellowLed = 1;
-                }
-            }
-            
-            //------------------------CHECK red-----------------------------------------
-            if((RedProp - GreenProp  > 30) && (RedProp - BlueProp  > 30))
-            {
-                RedLed = 1;
-                GreenLed = 0; 
-                BlueLed = 0;           
-                YellowLed = 0;
-            }
-            else
-            {
-                 
-                if((GreenProp - RedProp  > 30) && (GreenProp - BlueProp  > 30))
-                {
-                    RedLed = 0;
-                    GreenLed = 1; 
-                    BlueLed = 0;           
-                    YellowLed = 0;
-                }
-                else
-                {           
-                    if((BlueProp - RedProp  > 30) && (BlueProp - GreenProp   > 30))
-                    {
-                        RedLed = 0;
-                        GreenLed = 0; 
-                        BlueLed = 1;           
-                        YellowLed = 0;
-                    }
-                    else
-                    {     
-                        if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) 
-                        { //pretty sure it's Yellow
-                            //YellowLed = 1;
-                            //GreenLed = 0;
-                            
-                            RedLed = 0;
-                            GreenLed = 0; 
-                            BlueLed = 0;           
-                            YellowLed = 1;;
-                        }
-                        else
-                        { 
-                            RedLed = 0;
-                            GreenLed = 0; 
-                            BlueLed = 0;           
-                            YellowLed = 0;
-                        }
-                    }
-                }
-            }
-            
-        }
-        else
-        {
-            RedLed = 0;
-            GreenLed = 0; 
-            BlueLed = 0;           
-            YellowLed = 0;
-        }    
-            
-                           
- */
- 
-  /*
-  //-------------------------------------------------------------------------------------------------------------------          
-            
-            if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) 
-            { //pretty sure it's Yellow
-                YellowLed = 1;
-                GreenLed = 0;
-            } 
-            else 
-            {
-                YellowLed = 0;
-                if( (GreenProp > (GreenThreshold+Hysteresis)) && (RedProp <=(RedThreshold-Hysteresis)) ) 
-                { //pretty sure it's green
-                    GreenLed = 1;
-                } 
-                else 
-                {
-                    GreenLed = 1;
-                    YellowLed = 1;
-                }
-            }
-        } 
-        else 
-        {
-            //not enough intensity to determine
-            GreenLed = 0;
-            YellowLed = 0;
-        }
-    
-*/ 
-//----------------------------------------------------------------------------------------------------------------       
+        WriteOutputPort(OutputData);   
     } //while(1)
 }//main
-
-
-
-/*
----------------------------------------------------------------------------------------------
-//SPI SampleCode
-
-#include "mbed.h"
-#include "MCP23S17.h"
-// Create SPI bus
-SPI spi(p5, p6, p7);
-// 
- 
-char Opcode = 0x40;
- 
-// Next create a MCP23S17
-// mbed p20 is connected to ~chipSelect on the MCP23S17
-MCP23S17 chip = MCP23S17(spi, p20, Opcode);
- 
-DigitalOut led1(LED1); // mbed LED1 is used for test status display
- 
-int main() {
-//
-//  Set all 8 Port A bits to output direction
-    chip.direction(PORT_A, 0x00);
-//  Set all 8 Port B bits to input direction
-    chip.direction(PORT_B, 0xFF);
-    led1=0;
-//  Start Loopback test sending out and reading back values
-//  loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together
-    while (1) {
-        // write 0xAA to MCP23S17 Port A
-        chip.write(PORT_A, 0xAA);
-        wait(.5);
-        // read back value from MCP23S17 Port B and display B0 on mbed led1
-        led1 = chip.read(PORT_B)& 0x01;
-        // write 0x55 to MCP23S17 Port A
-        chip.write(PORT_A, 0x55);
-        wait(.5);
-        // read back value from MCP23S17 Port B and display B0 on mbed led1
-        led1 = chip.read(PORT_B)& 0x01;
-        // led1 should blink slowly when it is all working
-    }
-}
-*/
-
-//---------------------------------------------------------------------------------------------
-//---------------------------------------------------------------------------------------------
\ No newline at end of file