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:
2:4a8eacbb3657
Parent:
1:d99dad8a1f50
Child:
3:b8218b61b0e2
--- a/main.cpp	Wed Aug 10 16:01:36 2016 +0000
+++ b/main.cpp	Fri Aug 12 11:01:07 2016 +0000
@@ -1,6 +1,6 @@
 #include "mbed.h"
 #include "TextLCD.h"
-#include "VEML6040.h"
+#include "MCP23S17.h"
 
 #define FACTOR 1
 
@@ -40,10 +40,10 @@
 //----------- Analogue Inputs ---------------------------------------------------
 //-------------------------------------------------------------------------------
 //Analogue inputs are used to set thresholds for detector levels
-AnalogIn SystemThreshold1(p15);
-//AnalogIn SystemThreshold2(p16);
-//AnalogIn SystemThreshold3(p17);
-//AnalogIn SystemThreshold4(p18);
+//AnalogIn RedTrimmer(p15);
+//AnalogIn GreenTrimmer(p16);
+//AnalogIn BlueTrimmer(p17);
+//AnalogIn HysTrimmer(p18);
 //-------------------------------------------------------------------------------
 
 //-------------------------------------------------------------------------------
@@ -61,6 +61,19 @@
 
 //-------------------------------------------------------------------------------
 
+//-------------------------------------------------------------------------------
+//------------ Create SPI bus -----------------------------------------------------
+//-------------------------------------------------------------------------------
+//set up the SPI port for use as output driver
+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);
+
+//-------------------------------------------------------------------------------
 
 //-------------------------------------------------------------------------------
 //------------ colour sensor Comms Pins -----------------------------------------
@@ -127,6 +140,15 @@
 //  7   N/F   0                       0
 
 
+uint16_t MyPort = 1;
+
+//working thresholds from pot inputs, scaled 0-100
+uint16_t    RedThreshold    = 0;
+uint16_t    GreenThreshold  = 0;
+uint16_t    BlueThreshold   = 0;
+uint16_t    Hysteresis      = 0;
+
+
 uint16_t Filter(uint32_t *Acc,uint16_t NewData, uint8_t factor)
 {
     uint32_t Temp = 0;
@@ -209,34 +231,103 @@
     return NewData;
 }
 
+void InitOutputs(void)
+{
+    //  Set all 8 Port A bits to output direction
+    chip.direction(PORT_A, 0x00);
+    //  Set all 8 Port B bits to output direction
+    chip.direction(PORT_B, 0x00); 
+}
 
+void SetOutputBit(char bit)
+{
+    uint16_t OutputWord = 1;
+    char BitA, BitB, PortA, PortB;
+    
+    OutputWord <<= bit;
+    BitA = OutputWord & 0xFF;
+    BitB = OutputWord >> 8;
+    
+    PortA = chip.read(PORT_A);
+    PortA |=BitA;
+    chip.write(PORT_A, PortA);
+    
+    PortB = chip.read(PORT_B);
+    PortB |=BitB;
+    chip.write(PORT_B, PortB);   
+}
+
+void ClearOutputBit(char bit)
+{
+    uint16_t OutputWord = 1;
+    char BitA, BitB, PortA, PortB;
+    
+    OutputWord <<= bit;
+    OutputWord = ~OutputWord;
+        
+    BitA = OutputWord & 0xFF;
+    BitB = OutputWord >> 8;
+    
+    PortA = chip.read(PORT_A);
+    PortA &=BitA;
+    chip.write(PORT_A, PortA);
+    
+    PortB = chip.read(PORT_B);
+    PortB &=BitB;
+    chip.write(PORT_B, PortB);   
+}
+
+void WriteOutputPort(uint16_t FullPort)
+{
+    chip.write(PORT_A, FullPort & 0xFF); 
+    chip.write(PORT_B, FullPort >> 8);     
+}
+
+
+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;
+    Hysteresis      = 5;
+}
 
 int main()
 {
+    InitOutputs();
     //----------------------Initialise My TCS3472--------------------
     ConfigureTSC(0);
     //----------------------    My TCS3472       --------------------
 
     while(1) 
     {
+        
+        //WriteOutputPort(MyPort);
+        ClearOutputBit(MyPort);
+        MyPort++;
+        if (MyPort == 16)
+        {
+            MyPort = 0;
+        }
+        SetOutputBit(MyPort);       
+        
+        GetThresholds();
 
         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--------------------
 
 
@@ -258,7 +349,7 @@
 
 
 
-#define USEWHITE       //normalised % for each
+//#define USEWHITE       //normalised % for each
 #ifdef USEWHITE
         Temp = (Red+Blue+Green)*10000;
         Temp = Temp / White;
@@ -290,87 +381,54 @@
         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);
-        }
-
-
-        lcd.locate(0, 1);
-        lcd.printf("%03i %03i %03i %04i",RedProp,GreenProp,BlueProp,myMix);    
-
-
-#define Mix
+        lcd.printf("R%02i G%02i B%02i W%04i",RedProp,GreenProp,BlueProp,White); 
+        lcd.locate(0, 1);       
+        lcd.printf("R%02i G%02i B%02i H%02i ",RedThreshold, GreenThreshold, BlueThreshold, Hysteresis);       
+ 
 
         //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
+        if(White > 15) //looking at intensity somthing is on!
+        { 
+        /*
+            if((GreenProp <= 30) && (RedProp >=50)) 
+            { //pretty sure it's Yellow
                 YellowLed = 1;
                 GreenLed = 0;
-            } else {
+            } 
+            else 
+            {
                 YellowLed = 0;
-                if((GreenProp - RedProp) > 5) {
-                    //pretty sure it's green
+                if((GreenProp > 40) && (RedProp <=40)) { //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 {
+            */
+            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;
@@ -379,96 +437,49 @@
 }
 
 
-//---------------------------------------------------------------------------------------------
-//---------------------------------------------------------------------------------------------
-//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);
+/*
+---------------------------------------------------------------------------------------------
+//SPI SampleCode
 
-    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);
+#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