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:
13:ef2f1c11e33b
Parent:
12:eab1bc88fe68
Child:
14:02de92b0206a
--- a/main.cpp	Mon Nov 07 11:19:24 2016 +0000
+++ b/main.cpp	Thu Nov 10 16:57:19 2016 +0000
@@ -1,7 +1,8 @@
 #include "mbed.h"
 #include "TextLCD.h"
 #include "MCP23S17.h"
-//#include "24LCxx_I2C.h"
+#include "eeprom.h"
+#include "PCF8574.h"
 
 
 #define FACTOR 1
@@ -37,15 +38,18 @@
 
 //--------------------------------------
 #define NEWBUTTON   0x80
-#define UPBUTTON    0x04
-#define DOWNBUTTON  0x01
-#define LEFTBUTTON  0x03
-#define RIGHTBUTTON 0x02
-#define TICKBUTTON  0x05
+#define UPBUTTON    0x01
+#define DOWNBUTTON  0x05
+#define LEFTBUTTON  0x02
+#define RIGHTBUTTON 0x04
+#define TICKBUTTON  0x03
+
+
 
 void SelectSensorPort(uint8_t SensorID);
 void CreateOutputTable(void);
-void SaveSensorData(void);
+void LoadSensorData(void);
+void SaveSensorData(uint8_t Mode);
 
 //-------------------------------------------------------------------------------
 //----------- LED's -------------------------------------------------------------
@@ -117,9 +121,19 @@
 //C24LCXX_I2C ConfigEEPROM(p9, p10, 0x01, p11, 400000); // Create an instance of the class C2424LCXX_I2C, p28: SDA, p29:SDL, p5: wired to WP input of 24LCxx, address: A3=0,A2=0,A=1, on 3.3V I2C bus
 //C24LCXX_I2C mem(p13, p14, 00000100, NC , 400000); 
 
+//EEPROM ConfigMemory(p9, p10, 0x41, EEPROM::24C01);
+  EEPROM ConfigMemory(p9,p10,0,EEPROM::T24C01);
 
 
 //-------------------------------------------------------------------------------
+//------------ I2C Buttons & LED Interface Chip   -------------------------------
+//-------------------------------------------------------------------------------
+
+ JPCF8574 Interface(p9,p10,0x42);
+
+uint8_t IfButtons;
+
+//-------------------------------------------------------------------------------
 //------------ Create SPI bus -----------------------------------------------------
 //-------------------------------------------------------------------------------
 //set up the SPI port for use as output driver
@@ -403,6 +417,41 @@
 uint8_t GetButtonState(void)
 {
     uint8_t ButtonStateNew = 0;
+    uint8_t IfButtons;
+    
+    IfButtons = Interface.read(); //returns 0 if fail. check this is ok
+
+    IfButtons = ~IfButtons;       
+    IfButtons &= 0x1F;  
+  
+        
+    if (IfButtons == 1)
+    {
+        ButtonStateNew = 1;
+    }
+    
+    if (IfButtons == 2)
+    {
+        ButtonStateNew = 2;
+    }
+    
+    if (IfButtons == 4)
+    {
+        ButtonStateNew = 3;
+    }
+    if (IfButtons == 8)
+    {
+        ButtonStateNew = 4;
+    }
+        
+    if (IfButtons == 16)
+    {
+        ButtonStateNew = 5;
+    }      
+
+    
+  /*  
+    
     if (Button1 == 1)
     {
         ButtonStateNew = 1;
@@ -422,6 +471,7 @@
         ButtonStateNew = 4;
     }
     
+   */ 
     if(ButtonStateNew != 0)
     {
         if(ButtonState == 0)
@@ -574,7 +624,8 @@
     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("S%02i R%02i G%02i D%01i ",SingleThreshold, MixRThreshold, MixGThreshold, GetModeSwitch()  );
+    lcd.printf("S%02i R%02i G%02i D%02i ",SingleThreshold, MixRThreshold, MixGThreshold, GetModeSwitch()  );
+    //lcd.printf("Button Read %03i ",IfButtons);  
 }
 
 
@@ -808,9 +859,51 @@
 }
 
 
-void SaveSensorData(void)
+void SaveSensorData(uint8_t Mode)
+{
+    uint8_t MemSave;
+    for (MemSave = 0;MemSave <32;MemSave++) 
+    {  
+        if (Mode == 1)
+        {
+            ConfigMemory.write(MemSave,(int8_t)Config[MemSave]); 
+        }
+        else
+        {
+            ConfigMemory.write(MemSave,0); 
+         
+        }    
+    }  
+    LoadSensorData();  //re-load data from EEProm to be sure  
+}
+
+void LoadSensorData(void)
 {
-  //nothing here right now  
+    uint8_t MemSave;
+ //   uint8_t FailFlag = 0;
+    uint8_t MemData;    
+  
+    for (MemSave = 0;MemSave <32;MemSave++) 
+    {   
+        ConfigMemory.read(MemSave,(int8_t&)MemData); 
+        Config[MemSave]= MemData;
+/*
+        if (MemSave == 0)
+        {
+            if (Config[MemSave] > 6)    //case of no config (a bit rough!)
+            {
+                FailFlag = 1;
+            }
+        }
+*/
+    }
+ /*      
+    if(FailFlag == 1)
+    {
+
+        SaveSensorData(0);  //force default
+    }  
+    */  
 }
 
 
@@ -877,7 +970,7 @@
             DisplayScreen--;
             if (DisplayScreen == 255)
             {
-                DisplayScreen = 16;
+                DisplayScreen = 17;
             }
         }    
     }
@@ -886,52 +979,82 @@
     if (DisplayScreen == 17)
     {
         lcd.locate(0, 0);
-        lcd.printf("Left to save"); 
+        lcd.printf("RIGHT - SAVE    "); 
+        lcd.locate(0, 1);
+        lcd.printf("LEFT - DEFAULT  "); 
            
         if (CurrentButton == (RIGHTBUTTON | NEWBUTTON))
         {
-            SaveSensorData();
-        }      
-    }
- 
- 
- 
-    //------------------- select the sensor type ---------------- 
-    DisplaySensorType = Config[(DisplayScreen-1)*2];  
-     
-    if (CurrentButton == (RIGHTBUTTON | NEWBUTTON))
-    {
-        if (DisplaySensorType < 6)
-        {
-            DisplaySensorType++;
-            Config[(DisplayScreen-1)*2] = DisplaySensorType;
-        }    
-    }
-    else
-    {
+            SaveSensorData(1);
+        } 
+        
         if (CurrentButton == (LEFTBUTTON | NEWBUTTON))
         {
-            if (DisplaySensorType > 0)
-            {
-                DisplaySensorType--;
-                Config[(DisplayScreen-1)*2] = DisplaySensorType;
-            }
-        }    
+            SaveSensorData(0);
+        }  
+             
     }
- 
-    //update output list
-    CreateOutputTable();
- 
-    //show current config setting
-    DisplaySensorType = Config[(DisplayScreen-1)*2];
-    DisplaySensorOutput = Config[(DisplayScreen-1)*2 +1];
-   
-    if (DisplayScreen !=0)
-    {
-        lcd.locate(0, 0);
-        lcd.printf("Sensor %02i %s", DisplayScreen, LEDTypes[DisplaySensorType]); 
-        lcd.locate(0, 1);       
-        lcd.printf("First Output %02i ",DisplaySensorOutput);
+    else
+    { 
+  
+        //------------------- select the sensor type ---------------- 
+        DisplaySensorType = Config[(DisplayScreen-1)*2];  
+         
+        if (CurrentButton == (RIGHTBUTTON | NEWBUTTON))
+        {
+            if (DisplaySensorType < 6)
+            {
+                DisplaySensorType++;
+                Config[(DisplayScreen-1)*2] = DisplaySensorType;
+            }    
+        }
+        else
+        {
+            if (CurrentButton == (LEFTBUTTON | NEWBUTTON))
+            {
+                if (DisplaySensorType > 0)
+                {
+                    DisplaySensorType--;
+                    Config[(DisplayScreen-1)*2] = DisplaySensorType;
+                }
+            }    
+        }
+     
+        //update output list
+        CreateOutputTable();
+     
+        //show current config setting
+        DisplaySensorType = Config[(DisplayScreen-1)*2];
+        DisplaySensorOutput = Config[(DisplayScreen-1)*2 +1];
+       
+        if (DisplayScreen !=0)
+        {
+            lcd.locate(0, 0);
+            lcd.printf("Sensor %02i %s", DisplayScreen, LEDTypes[DisplaySensorType]); 
+            lcd.locate(0, 1);       
+            
+            //ConfigMemory.read(0,(int8_t&)DisplaySensorOutput); 
+            //lcd.printf("First Output %02i ",DisplaySensorOutput); 
+            
+            if (DisplaySensorType > 4)
+            {
+                lcd.printf("First O/P CN3.%02i",DisplaySensorOutput+2);     //add 2 just for display purposes to match schematic
+            }
+            else
+            {
+                if (DisplaySensorType > 0)
+                {
+                  lcd.printf("Output    CN3.%02i",DisplaySensorOutput+2);     //add 2 just for display purposes to match schematic
+                }
+                else
+                {
+                  lcd.printf("No Output       ");     //add 2 just for display purposes to match schematic 
+                } 
+            } 
+            
+            
+            
+        }
     }
 }   
 
@@ -960,6 +1083,10 @@
     ConfigureTSC(0);        //Initialise all TCS34725's
     InitCommsPort();        //Initialise the 485 port
 
+    //load config
+    LoadSensorData();
+    
+    lcd.setCursor(TextLCD::CurOff_BlkOff);
     
     while(1) 
     {