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:
- 8:e6e4eece90f7
- Parent:
- 7:9c5bca0ac9d7
- Child:
- 9:077d1ad96bca
--- a/main.cpp Wed Sep 28 14:34:47 2016 +0000 +++ b/main.cpp Mon Oct 10 15:08:44 2016 +0000 @@ -58,6 +58,12 @@ DigitalOut I2C_ID_Bit2(p12); DigitalOut I2C_ID_Bit3(p11); +//mode switch +DigitalIn ModeSwitch1(p19); +DigitalIn ModeSwitch2(p29); +DigitalIn ModeSwitch3(p30); + + //------------------------------------------------------------------------------- //------------------------------------------------------------------------------- @@ -75,13 +81,13 @@ //------------------------------------------------------------------------------- //debug display. Standard display driver set up in 4 bit mode //final version uses I2C port -TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x2); // rs, e, d4-d7 +//TextLCD lcd(p15, 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 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 +TextLCD_I2C lcd(&i2c_lcd, 0x40, TextLCD::LCD16x2); // I2C bus, PCF8574 Slaveaddress, LCD Type ok //------------------------------------------------------------------------------- @@ -118,25 +124,46 @@ //------------------------------------------------------------------------------- //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 - +/* char Config[32]={ //RGB, 0, //sensor 0 , uses 3 outputs - 0,1,2 GREEN_YELLOW, 0, //sensor 0 , uses 2 outputs - 0,1 - NOTUSED, 2, //sensor 1 - NOTUSED, 3, //sensor 2 - NOTUSED, 4, //sensor 3 - NOTUSED, 5, //sensor 4 - NOTUSED, 6, //sensor 5 - NOTUSED, 7, //sensor 6 - NOTUSED, 8, //sensor 7 - NOTUSED, 9, //sensor 8 - NOTUSED, 10, //sensor 9 - NOTUSED, 11, //sensor 10 - NOTUSED, 12, //sensor 11 - NOTUSED, 13, //sensor 12 - NOTUSED, 14, //sensor 13 + NOTUSED, 2, //sensor 1 , uses 2 outputs - 2,3 + NOTUSED, 4, //sensor 2 , uses 1 outputs - 4 + NOTUSED, 5, //sensor 3 , uses 1 outputs - 5 + NOTUSED, 6, //sensor 4 + NOTUSED, 7, //sensor 5 + NOTUSED, 8, //sensor 6 + NOTUSED, 9, //sensor 7 + NOTUSED, 10, //sensor 8 + NOTUSED, 11, //sensor 9 + NOTUSED, 12, //sensor 10 + NOTUSED, 13, //sensor 11 + NOTUSED, 14, //sensor 12 + NOTUSED, 15, //sensor 13 NOTUSED, 15, //sensor 14 NOTUSED, 15, //sensor 15 }; +*/ + +char Config[32]={ //RGB, 0, //sensor 0 , uses 3 outputs - 0,1,2 + GREEN_YELLOW, 0, //sensor 0 , uses 2 outputs - 0,1 + GREEN_YELLOW, 2, //sensor 1 , uses 2 outputs - 2,3 + STD_RED, 4, //sensor 2 , uses 1 outputs - 4 + STD_RED, 5, //sensor 3 , uses 1 outputs - 5 + NOTUSED, 6, //sensor 4 + NOTUSED, 7, //sensor 5 + NOTUSED, 8, //sensor 6 + NOTUSED, 9, //sensor 7 + NOTUSED, 10, //sensor 8 + NOTUSED, 11, //sensor 9 + NOTUSED, 12, //sensor 10 + NOTUSED, 13, //sensor 11 + NOTUSED, 14, //sensor 12 + NOTUSED, 15, //sensor 13 + NOTUSED, 15, //sensor 14 + NOTUSED, 15, //sensor 15 + }; + /* char Config[32]={ //RGB, 0, //sensor 0 , uses 3 outputs - 0,1,2 STD_RED, 0, //sensor 0 , uses 3 outputs - 0,1,2 @@ -164,6 +191,7 @@ char LEDState[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //current detected LED states uint16_t OutputData = 0; //word containing the current output states of the board +uint16_t OutputDataB = 0x8000;// test roll uint8_t SensorNumber = 0; //current sensor number char SensorResult = 0; //detected colour @@ -281,6 +309,28 @@ return NewData; } +uint8_t GetModeSwitch(void) +{ + uint8_t Result = 0; + + if (!ModeSwitch1) + { + Result += 1; + } + + if (!ModeSwitch2) + { + Result += 2; + } + + if (!ModeSwitch3) + { + Result += 4; + } + + return Result; +} + void InitOutputs(void) { // Set all 8 Port A bits to output direction @@ -411,7 +461,8 @@ lcd.locate(0, 0); 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 H%02i ",RedThreshold, GreenThreshold, BlueThreshold, Hysteresis); + lcd.printf("R%02i G%02i B%02i S%02i ",RedThreshold, GreenThreshold, BlueThreshold, GetModeSwitch() ); } @@ -601,33 +652,33 @@ void ShowLeds(char WhichColour) { - RedLed = 0; - GreenLed = 0; - BlueLed = 0; - YellowLed = 0; + RedLed = 1; + GreenLed = 1; + BlueLed = 1; + YellowLed = 1; switch (WhichColour) { case RED: { - RedLed = 1; + RedLed = 0; } break; case GREEN: { - GreenLed = 1; + GreenLed = 0; } break; case BLUE: { - BlueLed = 1; + BlueLed = 0; } break; case YELLOW: { - YellowLed = 1; + YellowLed = 0; } break; @@ -664,20 +715,41 @@ ScaleColours(); - UpdateDisplay(); - // work out % of colour mix Temp = (Red+Blue+Green); RedProp = (Red* 100) / Temp ; GreenProp = (Green* 100) / Temp ; BlueProp = (Blue* 100) / Temp ; - SensorResult = GetResults(SensorNumber); UpdateOutputs(); - ShowLeds(SensorResult); + + if (SensorNumber == GetModeSwitch()) + { + UpdateDisplay(); + ShowLeds(SensorResult); + } + } //end if + else + { + if (SensorNumber == GetModeSwitch()) + { + lcd.locate(0, 0); + lcd.printf("INVALID SENSOR "); + lcd.locate(0, 1); + lcd.printf("%02i ",GetModeSwitch() ); + } + } + + } //end for - WriteOutputPort(OutputData); + WriteOutputPort(OutputData); + //WriteOutputPort(OutputDataB); + //OutputDataB>>=1; + //if(OutputDataB == 0) + //{ + // OutputDataB = 0x8000; + //} } //while(1) }//main