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:
10:efe8585f7fa4
Parent:
9:077d1ad96bca
Child:
11:729eb224dce6
--- a/main.cpp	Mon Oct 10 15:55:52 2016 +0000
+++ b/main.cpp	Thu Oct 13 15:56:00 2016 +0000
@@ -74,7 +74,12 @@
 //AnalogIn GreenTrimmer(p16);
 //AnalogIn BlueTrimmer(p17);
 //AnalogIn HysTrimmer(p18);
-//-------------------------------------------------------------------------------
+
+AnalogIn   Trimmer1(p15);
+AnalogIn   Trimmer2(p16); 
+AnalogIn   Trimmer3(p17);
+
+//-----------------------------------------------------
 
 //-------------------------------------------------------------------------------
 //------------ Text display -----------------------------------------------------
@@ -121,6 +126,14 @@
 
 Colour SensorData;
 
+
+//-------------------------------------------------------------------------------
+//------------ Serial Port for RS485 -----------------------------------------
+//-------------------------------------------------------------------------------
+Serial RS485(p26,p25);
+//DigitalOut RS485DIR(p21);
+char SendMessage = 0;
+char CommsData = 0;
 //-------------------------------------------------------------------------------
 //The system looks up the type of LED that is being detected for Each sensor and will fill in the result table & update the associated Output list
 //outputs start at 0 torough to 15
@@ -232,6 +245,11 @@
 uint16_t    BlueThreshold   = 0;
 uint16_t    Hysteresis      = 0;
 
+    
+uint16_t   SingleThreshold = 0;
+uint16_t   MixRThreshold   = 0;
+uint16_t   MixGThreshold   = 0;
+
 //------------------------- filter routine if required -------------------------------
 uint16_t Filter(uint32_t *Acc,uint16_t NewData, uint8_t factor)
 {
@@ -390,16 +408,16 @@
 void GetThresholds(void)
 {
     //Load the pot values ant make 0 to 100%  value 
-    /*
-    RedThreshold    = RedTrimmer.read_u16()   * 100 >> 11;
-    GreenThreshold  = GreenTrimmer.read_u16() * 100 >> 11;
-    BlueThreshold   = BlueTrimmer.read_u16()  * 100 >> 11;
-    Hysteresis      = HysTrimmer.read_u16()   * 100 >> 11;      
-    */
     
-    RedThreshold    = 45;
-    GreenThreshold  = 35;
-    BlueThreshold   = 10;
+    SingleThreshold = 50 - ( Trimmer1.read_u16()/1310 );
+    MixRThreshold   = 50 - ( Trimmer2.read_u16()  /1310 );
+    MixGThreshold   = 50 - ( Trimmer3.read_u16()  /1310 );
+    //Hysteresis      = HysTrimmer.read_u16()   * 100 >> 11;      
+
+    
+    RedThreshold    = 45; //default for yellow detect (red)
+    GreenThreshold  = 35;//default for yellow detect (green)
+    BlueThreshold   = 10;//not used
     Hysteresis      = 5;
 }
 
@@ -462,7 +480,7 @@
     lcd.printf("R%02i G%02i B%02i W%03i",RedProp,GreenProp,BlueProp,White); 
     lcd.locate(0, 1);       
     //lcd.printf("R%02i G%02i B%02i H%02i ",RedThreshold, GreenThreshold, BlueThreshold, Hysteresis); 
-    lcd.printf("R%02i G%02i B%02i S%02i ",RedThreshold, GreenThreshold, BlueThreshold, GetModeSwitch()  );
+    lcd.printf("S%02i R%02i G%02i D%01i ",SingleThreshold, MixRThreshold, MixGThreshold, GetModeSwitch()  );
 }
 
 
@@ -476,7 +494,7 @@
         {
             case STD_RED:
             {
-                if((RedProp - GreenProp  > 30) && (RedProp - BlueProp  > 30))
+                if((RedProp - GreenProp  > SingleThreshold) && (RedProp - BlueProp  > SingleThreshold))
                 {
                     LEDColour = RED;
                 }          
@@ -485,7 +503,7 @@
             
             case STD_GREEN:
             {
-                if((GreenProp - RedProp  > 30) && (GreenProp - BlueProp  > 30))
+                if((GreenProp - RedProp  > SingleThreshold) && (GreenProp - BlueProp  > SingleThreshold))
                 {
                     LEDColour = GREEN;
                 }             
@@ -494,7 +512,7 @@
     
             case STD_BLUE:
             {
-                if((BlueProp - RedProp  > 30) && (BlueProp - GreenProp   > 30))
+                if((BlueProp - RedProp  > SingleThreshold) && (BlueProp - GreenProp   > SingleThreshold))
                 {
                     LEDColour = BLUE;
                 }             
@@ -503,9 +521,9 @@
             
             case STD_YELLOW:
             {
-                if ((RedProp - GreenProp) < 60) //check it is not red!
+                if ((RedProp - GreenProp) < (2* SingleThreshold)) //check it is not red!
                 { 
-                    if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) 
+                    if( (GreenProp <= (MixGThreshold-Hysteresis)) && (RedProp >=(MixRThreshold+Hysteresis)) ) 
                     {             
                         LEDColour = YELLOW;
                     }
@@ -516,7 +534,7 @@
             case GREEN_YELLOW:
             {
 
-                if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) 
+                if( (GreenProp <= (MixGThreshold-Hysteresis)) && (RedProp >=(MixRThreshold+Hysteresis)) ) 
                 { //pretty sure it's Yellow
                     if ((RedProp - GreenProp) < 60) //check it is not red!
                     {
@@ -525,7 +543,7 @@
                 } 
                 else 
                 {
-                    if( (GreenProp > (GreenThreshold+Hysteresis)) && (RedProp <=(RedThreshold-Hysteresis)) ) 
+                    if( (GreenProp > (MixGThreshold+Hysteresis)) && (RedProp <=(MixRThreshold-Hysteresis)) ) 
                     { //pretty sure it's green
                         LEDColour = GREEN;
                     } 
@@ -535,19 +553,19 @@
             
             case RGB:
             {
-                if((RedProp - GreenProp  > 30) && (RedProp - BlueProp  > 30))
+                if((RedProp - GreenProp  > SingleThreshold) && (RedProp - BlueProp  > SingleThreshold))
                 {
                     LEDColour = RED;
                 }
                 else
                 {       
-                    if((GreenProp - RedProp  > 30) && (GreenProp - BlueProp  > 30))
+                    if((GreenProp - RedProp  > SingleThreshold) && (GreenProp - BlueProp  > SingleThreshold))
                     {
                         LEDColour = GREEN;
                     }
                     else
                     {           
-                        if((BlueProp - RedProp  > 30) && (BlueProp - GreenProp   > 30))
+                        if((BlueProp - RedProp  > SingleThreshold) && (BlueProp - GreenProp   > SingleThreshold))
                         {
                             LEDColour = BLUE;
                         }
@@ -695,13 +713,45 @@
     }    
 }
 
-
+void InitCommsPort(void)
+{
+    Serial RS485(p26,p25); 
+    RS485.baud(19200);
+    RS485.format(8,RS485.None,1); //8 data, no Parity, 1 stop bit
+    //set up the other stuff!     
+    LPC_PINCON->PINSEL4 &= 0x0ffff;
+    LPC_PINCON->PINSEL4 |= 0x0AAAA;
+    
+    LPC_UART1->RS485CTRL = (1<<3)|(1<<4)|(1<<5);
+    LPC_UART1->RS485DLY = 1;
+    RS485.putc('*');
+}
+    
+void HandleRS485(void)
+{      
+    //check for character in buffer
+    if (RS485.readable())
+    {
+        CommsData = RS485.getc();
+        if (CommsData== '*') //check for correct request
+        {
+            RS485.putc('*');
+            CommsData = (char)(OutputData >> 8);
+            RS485.putc(CommsData);
+            CommsData = (char)(OutputData & 0xFF);
+            RS485.putc(CommsData);
+        }           
+    }    
+}
+    
 
 int main()
 {
     InitOutputs();          //set up output driver chip
     ConfigureTSC(0);        //Initialise all TCS34725's
+    InitCommsPort();        //Initialise the 485 port
 
+    
     while(1) 
     {
         GetThresholds();    //update thresholds   
@@ -756,6 +806,9 @@
         //if(OutputDataB == 0)
         //{
         //    OutputDataB = 0x8000;
-        //}     
+        //} 
+
+        //Handle Comms
+        HandleRS485();           
     } //while(1)
 }//main