Quad RGB sensor with SPI output drive, I2C display interface & analogue inputs / UI for various LED types
Dependencies: MCP23S17 PCF8574 TextLCD eeprom mbed-dev
main.cpp@3:b8218b61b0e2, 2016-08-16 (annotated)
- Committer:
- jolyon
- Date:
- Tue Aug 16 10:21:19 2016 +0000
- Revision:
- 3:b8218b61b0e2
- Parent:
- 2:4a8eacbb3657
- Child:
- 4:dce5fc58abe9
Starting to expand the system so it is configurable - untested but compiles!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jolyon | 0:9d301ae51ecf | 1 | #include "mbed.h" |
jolyon | 0:9d301ae51ecf | 2 | #include "TextLCD.h" |
jolyon | 2:4a8eacbb3657 | 3 | #include "MCP23S17.h" |
jolyon | 0:9d301ae51ecf | 4 | |
jolyon | 0:9d301ae51ecf | 5 | #define FACTOR 1 |
jolyon | 0:9d301ae51ecf | 6 | |
jolyon | 0:9d301ae51ecf | 7 | #define SENSOR_ADDR (41 << 1) |
jolyon | 1:d99dad8a1f50 | 8 | #define SENSOR_WHITE_REG 148 |
jolyon | 1:d99dad8a1f50 | 9 | #define SENSOR_RED_REG 150 |
jolyon | 1:d99dad8a1f50 | 10 | #define SENSOR_GREEN_REG 152 |
jolyon | 1:d99dad8a1f50 | 11 | #define SENSOR_BLUE_REG 154 |
jolyon | 1:d99dad8a1f50 | 12 | |
jolyon | 3:b8218b61b0e2 | 13 | #define WHITE_THRESHOLD 15 |
jolyon | 3:b8218b61b0e2 | 14 | |
jolyon | 3:b8218b61b0e2 | 15 | //-------------------------------------- |
jolyon | 3:b8218b61b0e2 | 16 | //each LED type is defined here |
jolyon | 3:b8218b61b0e2 | 17 | #define NOTUSED 0 |
jolyon | 3:b8218b61b0e2 | 18 | #define STD_RED 1 |
jolyon | 3:b8218b61b0e2 | 19 | #define STD_GREEN 2 |
jolyon | 3:b8218b61b0e2 | 20 | #define STD_BLUE 3 |
jolyon | 3:b8218b61b0e2 | 21 | #define STD_YELLOW 4 |
jolyon | 3:b8218b61b0e2 | 22 | #define GREEN_YELLOW 5 |
jolyon | 3:b8218b61b0e2 | 23 | #define RGB 6 |
jolyon | 3:b8218b61b0e2 | 24 | |
jolyon | 3:b8218b61b0e2 | 25 | |
jolyon | 3:b8218b61b0e2 | 26 | //-------------------------------------- |
jolyon | 3:b8218b61b0e2 | 27 | //Current LED Colour Definitions |
jolyon | 3:b8218b61b0e2 | 28 | #define ERROR 0 //off or wrong |
jolyon | 3:b8218b61b0e2 | 29 | #define RED 1 |
jolyon | 3:b8218b61b0e2 | 30 | #define GREEN 2 |
jolyon | 3:b8218b61b0e2 | 31 | #define BLUE 3 |
jolyon | 3:b8218b61b0e2 | 32 | #define YELLOW 4 |
jolyon | 3:b8218b61b0e2 | 33 | |
jolyon | 3:b8218b61b0e2 | 34 | |
jolyon | 3:b8218b61b0e2 | 35 | |
jolyon | 3:b8218b61b0e2 | 36 | //-------------------------------------- |
jolyon | 1:d99dad8a1f50 | 37 | |
jolyon | 1:d99dad8a1f50 | 38 | |
jolyon | 1:d99dad8a1f50 | 39 | |
jolyon | 0:9d301ae51ecf | 40 | |
jolyon | 0:9d301ae51ecf | 41 | //------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 42 | //----------- LED's ------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 43 | //------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 44 | // Detection LED's use the onboard MBED LED's |
jolyon | 0:9d301ae51ecf | 45 | // These are all blue on the MBED but on DSE PCB they areas follows |
jolyon | 0:9d301ae51ecf | 46 | // LED1 = GREEN |
jolyon | 0:9d301ae51ecf | 47 | // LED2 = BLUE |
jolyon | 0:9d301ae51ecf | 48 | // LED3 = RED |
jolyon | 0:9d301ae51ecf | 49 | // LED4 = YELLOW |
jolyon | 0:9d301ae51ecf | 50 | DigitalOut GreenLed(LED1); |
jolyon | 0:9d301ae51ecf | 51 | DigitalOut BlueLed(LED2); |
jolyon | 0:9d301ae51ecf | 52 | DigitalOut RedLed(LED3); |
jolyon | 0:9d301ae51ecf | 53 | DigitalOut YellowLed(LED4); |
jolyon | 0:9d301ae51ecf | 54 | |
jolyon | 0:9d301ae51ecf | 55 | //------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 56 | |
jolyon | 0:9d301ae51ecf | 57 | //------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 58 | //----------- Analogue Inputs --------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 59 | //------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 60 | //Analogue inputs are used to set thresholds for detector levels |
jolyon | 2:4a8eacbb3657 | 61 | //AnalogIn RedTrimmer(p15); |
jolyon | 2:4a8eacbb3657 | 62 | //AnalogIn GreenTrimmer(p16); |
jolyon | 2:4a8eacbb3657 | 63 | //AnalogIn BlueTrimmer(p17); |
jolyon | 2:4a8eacbb3657 | 64 | //AnalogIn HysTrimmer(p18); |
jolyon | 0:9d301ae51ecf | 65 | //------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 66 | |
jolyon | 0:9d301ae51ecf | 67 | //------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 68 | //------------ Text display ----------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 69 | //------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 70 | //debug display. Standard display driver set up in 4 bit mode |
jolyon | 0:9d301ae51ecf | 71 | //final version uses I2C port |
jolyon | 0:9d301ae51ecf | 72 | TextLCD lcd(p14, p16, p17, p18, p19, p20, TextLCD::LCD16x2); // rs, e, d4-d7 |
jolyon | 0:9d301ae51ecf | 73 | |
jolyon | 0:9d301ae51ecf | 74 | //set up I2C Communication to LCD |
jolyon | 0:9d301ae51ecf | 75 | //I2C i2c_lcd(p9,p10); // SDA, SCL |
jolyon | 0:9d301ae51ecf | 76 | |
jolyon | 0:9d301ae51ecf | 77 | //I2C Portexpander PCF8574 for LCD |
jolyon | 0:9d301ae51ecf | 78 | //TextLCD_I2C lcd(&i2c_lcd, 0x42, TextLCD::LCD16x2); // I2C bus, PCF8574 Slaveaddress, LCD Type ok |
jolyon | 0:9d301ae51ecf | 79 | |
jolyon | 0:9d301ae51ecf | 80 | //------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 81 | |
jolyon | 2:4a8eacbb3657 | 82 | //------------------------------------------------------------------------------- |
jolyon | 2:4a8eacbb3657 | 83 | //------------ Create SPI bus ----------------------------------------------------- |
jolyon | 2:4a8eacbb3657 | 84 | //------------------------------------------------------------------------------- |
jolyon | 2:4a8eacbb3657 | 85 | //set up the SPI port for use as output driver |
jolyon | 2:4a8eacbb3657 | 86 | SPI spi(p5, p6, p7); |
jolyon | 2:4a8eacbb3657 | 87 | |
jolyon | 2:4a8eacbb3657 | 88 | char Opcode = 0x40; |
jolyon | 2:4a8eacbb3657 | 89 | |
jolyon | 2:4a8eacbb3657 | 90 | // Next create a MCP23S17 |
jolyon | 2:4a8eacbb3657 | 91 | // mbed p20 is connected to ~chipSelect on the MCP23S17 |
jolyon | 2:4a8eacbb3657 | 92 | MCP23S17 chip = MCP23S17(spi, p20, Opcode); |
jolyon | 2:4a8eacbb3657 | 93 | |
jolyon | 2:4a8eacbb3657 | 94 | //------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 95 | |
jolyon | 0:9d301ae51ecf | 96 | //------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 97 | //------------ colour sensor Comms Pins ----------------------------------------- |
jolyon | 0:9d301ae51ecf | 98 | //------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 99 | //TMC Sensor: this is the raw I2C Port, the clock like is muxed |
jolyon | 0:9d301ae51ecf | 100 | I2C i2c(p28, p27); //pins for I2C communication (SDA, SCL) |
jolyon | 0:9d301ae51ecf | 101 | |
jolyon | 0:9d301ae51ecf | 102 | |
jolyon | 0:9d301ae51ecf | 103 | typedef struct{ |
jolyon | 0:9d301ae51ecf | 104 | uint16_t White; |
jolyon | 0:9d301ae51ecf | 105 | uint16_t Red; |
jolyon | 0:9d301ae51ecf | 106 | uint16_t Green; |
jolyon | 0:9d301ae51ecf | 107 | uint16_t Blue; |
jolyon | 0:9d301ae51ecf | 108 | }Colour; |
jolyon | 0:9d301ae51ecf | 109 | |
jolyon | 0:9d301ae51ecf | 110 | Colour SensorData; |
jolyon | 0:9d301ae51ecf | 111 | |
jolyon | 0:9d301ae51ecf | 112 | //------------------------------------------------------------------------------- |
jolyon | 3:b8218b61b0e2 | 113 | //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 |
jolyon | 3:b8218b61b0e2 | 114 | //outputs start at 0 torough to 15 |
jolyon | 3:b8218b61b0e2 | 115 | |
jolyon | 3:b8218b61b0e2 | 116 | char Config[32]={ RGB, 0, //sensor 0 , uses 3 outputs - 0,1,2 |
jolyon | 3:b8218b61b0e2 | 117 | NOTUSED,3, //sensor 1 |
jolyon | 3:b8218b61b0e2 | 118 | NOTUSED,4, //sensor 2 |
jolyon | 3:b8218b61b0e2 | 119 | NOTUSED,5, //sensor 3 |
jolyon | 3:b8218b61b0e2 | 120 | NOTUSED,6, //sensor 4 |
jolyon | 3:b8218b61b0e2 | 121 | NOTUSED,7, //sensor 5 |
jolyon | 3:b8218b61b0e2 | 122 | NOTUSED,8, //sensor 6 |
jolyon | 3:b8218b61b0e2 | 123 | NOTUSED,9, //sensor 7 |
jolyon | 3:b8218b61b0e2 | 124 | NOTUSED,10, //sensor 8 |
jolyon | 3:b8218b61b0e2 | 125 | NOTUSED,11, //sensor 9 |
jolyon | 3:b8218b61b0e2 | 126 | NOTUSED,12, //sensor 10 |
jolyon | 3:b8218b61b0e2 | 127 | NOTUSED,13, //sensor 11 |
jolyon | 3:b8218b61b0e2 | 128 | NOTUSED,14, //sensor 12 |
jolyon | 3:b8218b61b0e2 | 129 | NOTUSED,15, //sensor 13 |
jolyon | 3:b8218b61b0e2 | 130 | NOTUSED,15, //sensor 14 |
jolyon | 3:b8218b61b0e2 | 131 | NOTUSED,15, //sensor 15 |
jolyon | 3:b8218b61b0e2 | 132 | }; |
jolyon | 3:b8218b61b0e2 | 133 | |
jolyon | 3:b8218b61b0e2 | 134 | char LEDState[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //current detected LED states |
jolyon | 3:b8218b61b0e2 | 135 | |
jolyon | 3:b8218b61b0e2 | 136 | uint16_t OutputData = 0; //word containing the current output states of the board |
jolyon | 3:b8218b61b0e2 | 137 | |
jolyon | 3:b8218b61b0e2 | 138 | uint8_t SensorNumber = 0; //current sensor number |
jolyon | 3:b8218b61b0e2 | 139 | char SensorResult = 0; //detected colour |
jolyon | 3:b8218b61b0e2 | 140 | |
jolyon | 0:9d301ae51ecf | 141 | |
jolyon | 0:9d301ae51ecf | 142 | //------------TSC Stuff---------------- |
jolyon | 0:9d301ae51ecf | 143 | int sensor_addr = 41 << 1; |
jolyon | 0:9d301ae51ecf | 144 | |
jolyon | 0:9d301ae51ecf | 145 | char TempReg[1] = {0}; |
jolyon | 0:9d301ae51ecf | 146 | char TempData[2] = {0,0}; |
jolyon | 0:9d301ae51ecf | 147 | |
jolyon | 0:9d301ae51ecf | 148 | |
jolyon | 0:9d301ae51ecf | 149 | //------------TSC Stuff---------------- |
jolyon | 0:9d301ae51ecf | 150 | |
jolyon | 0:9d301ae51ecf | 151 | uint16_t Red = 1; |
jolyon | 0:9d301ae51ecf | 152 | uint16_t Green = 2; |
jolyon | 0:9d301ae51ecf | 153 | uint16_t Blue = 3; |
jolyon | 0:9d301ae51ecf | 154 | uint16_t White = 4; |
jolyon | 1:d99dad8a1f50 | 155 | uint16_t Yellow = 0; |
jolyon | 0:9d301ae51ecf | 156 | |
jolyon | 0:9d301ae51ecf | 157 | uint32_t AvgRed = 0; |
jolyon | 0:9d301ae51ecf | 158 | uint32_t AvgGreen = 0; |
jolyon | 0:9d301ae51ecf | 159 | uint32_t AvgBlue = 0; |
jolyon | 0:9d301ae51ecf | 160 | uint32_t AvgWhite = 0; |
jolyon | 0:9d301ae51ecf | 161 | uint32_t Temp = 0; |
jolyon | 0:9d301ae51ecf | 162 | uint32_t TempR = 0; |
jolyon | 0:9d301ae51ecf | 163 | uint32_t TempG = 0; |
jolyon | 0:9d301ae51ecf | 164 | uint32_t TempB = 0; |
jolyon | 0:9d301ae51ecf | 165 | |
jolyon | 0:9d301ae51ecf | 166 | |
jolyon | 0:9d301ae51ecf | 167 | uint16_t RedProp = 0; |
jolyon | 0:9d301ae51ecf | 168 | uint16_t GreenProp = 0; |
jolyon | 0:9d301ae51ecf | 169 | uint16_t BlueProp = 0; |
jolyon | 0:9d301ae51ecf | 170 | |
jolyon | 0:9d301ae51ecf | 171 | uint16_t myMix = 0; |
jolyon | 0:9d301ae51ecf | 172 | |
jolyon | 0:9d301ae51ecf | 173 | float MyCCT; |
jolyon | 0:9d301ae51ecf | 174 | float MyCCTi; |
jolyon | 0:9d301ae51ecf | 175 | uint8_t DisplayIndex = 0; |
jolyon | 0:9d301ae51ecf | 176 | |
jolyon | 3:b8218b61b0e2 | 177 | uint8_t SensorConfig = 0x30; |
jolyon | 0:9d301ae51ecf | 178 | // bit description setting |
jolyon | 0:9d301ae51ecf | 179 | // 0 SD, 0 = enabled 0 |
jolyon | 0:9d301ae51ecf | 180 | // 1 AF, 0 = Automode 0 |
jolyon | 0:9d301ae51ecf | 181 | // 2 TRIG, 0 = no trigrer 0 |
jolyon | 0:9d301ae51ecf | 182 | // 3 N/F, 0 0 |
jolyon | 0:9d301ae51ecf | 183 | // 4 IT0 0 0 |
jolyon | 0:9d301ae51ecf | 184 | // 5 IT1 1 = 160ms 1 |
jolyon | 0:9d301ae51ecf | 185 | // 6 IT2 0 0 |
jolyon | 0:9d301ae51ecf | 186 | // 7 N/F 0 0 |
jolyon | 0:9d301ae51ecf | 187 | |
jolyon | 0:9d301ae51ecf | 188 | |
jolyon | 2:4a8eacbb3657 | 189 | uint16_t MyPort = 1; |
jolyon | 2:4a8eacbb3657 | 190 | |
jolyon | 2:4a8eacbb3657 | 191 | //working thresholds from pot inputs, scaled 0-100 |
jolyon | 2:4a8eacbb3657 | 192 | uint16_t RedThreshold = 0; |
jolyon | 2:4a8eacbb3657 | 193 | uint16_t GreenThreshold = 0; |
jolyon | 2:4a8eacbb3657 | 194 | uint16_t BlueThreshold = 0; |
jolyon | 2:4a8eacbb3657 | 195 | uint16_t Hysteresis = 0; |
jolyon | 2:4a8eacbb3657 | 196 | |
jolyon | 2:4a8eacbb3657 | 197 | |
jolyon | 0:9d301ae51ecf | 198 | uint16_t Filter(uint32_t *Acc,uint16_t NewData, uint8_t factor) |
jolyon | 0:9d301ae51ecf | 199 | { |
jolyon | 0:9d301ae51ecf | 200 | uint32_t Temp = 0; |
jolyon | 0:9d301ae51ecf | 201 | Temp = *Acc >> factor ; |
jolyon | 0:9d301ae51ecf | 202 | Temp = *Acc - Temp + NewData; |
jolyon | 0:9d301ae51ecf | 203 | *Acc = Temp; |
jolyon | 0:9d301ae51ecf | 204 | return *Acc >> factor; |
jolyon | 0:9d301ae51ecf | 205 | } |
jolyon | 0:9d301ae51ecf | 206 | |
jolyon | 0:9d301ae51ecf | 207 | |
jolyon | 0:9d301ae51ecf | 208 | void ConfigureTSC(uint8_t Address) |
jolyon | 0:9d301ae51ecf | 209 | { |
jolyon | 0:9d301ae51ecf | 210 | I2C i2c(p28, p27); //pins for I2C communication (SDA, SCL) |
jolyon | 0:9d301ae51ecf | 211 | i2c.frequency(200000); |
jolyon | 0:9d301ae51ecf | 212 | |
jolyon | 0:9d301ae51ecf | 213 | |
jolyon | 0:9d301ae51ecf | 214 | |
jolyon | 0:9d301ae51ecf | 215 | char id_regval[1] = {146}; |
jolyon | 0:9d301ae51ecf | 216 | char data[1] = {0}; |
jolyon | 0:9d301ae51ecf | 217 | i2c.write(sensor_addr,id_regval,1, true); |
jolyon | 0:9d301ae51ecf | 218 | i2c.read(sensor_addr,data,1,false); |
jolyon | 0:9d301ae51ecf | 219 | |
jolyon | 0:9d301ae51ecf | 220 | if (data[0]==68) { |
jolyon | 0:9d301ae51ecf | 221 | GreenLed = 0; |
jolyon | 0:9d301ae51ecf | 222 | wait (2); |
jolyon | 0:9d301ae51ecf | 223 | GreenLed = 1; |
jolyon | 0:9d301ae51ecf | 224 | } else { |
jolyon | 0:9d301ae51ecf | 225 | GreenLed = 1; |
jolyon | 0:9d301ae51ecf | 226 | } |
jolyon | 0:9d301ae51ecf | 227 | |
jolyon | 0:9d301ae51ecf | 228 | // Initialize color sensor |
jolyon | 0:9d301ae51ecf | 229 | |
jolyon | 0:9d301ae51ecf | 230 | char enable_register[2] = {128,3}; //enable the sensor |
jolyon | 0:9d301ae51ecf | 231 | i2c.write(sensor_addr,enable_register,2,false); |
jolyon | 0:9d301ae51ecf | 232 | |
jolyon | 0:9d301ae51ecf | 233 | char timing_register[2] = {0x81,0xC0}; //approx 100ms |
jolyon | 0:9d301ae51ecf | 234 | i2c.write(sensor_addr,timing_register,2,false); |
jolyon | 0:9d301ae51ecf | 235 | |
jolyon | 0:9d301ae51ecf | 236 | char control_register[2] = {0x8F,2}; //gain = x16 |
jolyon | 0:9d301ae51ecf | 237 | i2c.write(sensor_addr,control_register,2,false); |
jolyon | 0:9d301ae51ecf | 238 | |
jolyon | 0:9d301ae51ecf | 239 | |
jolyon | 0:9d301ae51ecf | 240 | |
jolyon | 0:9d301ae51ecf | 241 | // Read data from color sensor (Clear/Red/Green/Blue) |
jolyon | 0:9d301ae51ecf | 242 | //led = 1; |
jolyon | 0:9d301ae51ecf | 243 | |
jolyon | 0:9d301ae51ecf | 244 | } |
jolyon | 0:9d301ae51ecf | 245 | |
jolyon | 1:d99dad8a1f50 | 246 | Colour GetSensorData(char SensorNumber) |
jolyon | 1:d99dad8a1f50 | 247 | { |
jolyon | 1:d99dad8a1f50 | 248 | Colour NewData; |
jolyon | 1:d99dad8a1f50 | 249 | char AddrReg[1] = {0}; |
jolyon | 1:d99dad8a1f50 | 250 | char DataReg[5] ={0,0,0,0,0}; |
jolyon | 1:d99dad8a1f50 | 251 | |
jolyon | 1:d99dad8a1f50 | 252 | NewData.White = 0; |
jolyon | 1:d99dad8a1f50 | 253 | NewData.Red = 0; |
jolyon | 1:d99dad8a1f50 | 254 | NewData.Green = 0; |
jolyon | 1:d99dad8a1f50 | 255 | NewData.Blue = 0; |
jolyon | 1:d99dad8a1f50 | 256 | |
jolyon | 1:d99dad8a1f50 | 257 | AddrReg[0] = SENSOR_WHITE_REG; |
jolyon | 1:d99dad8a1f50 | 258 | i2c.write(SENSOR_ADDR, AddrReg,1, true); |
jolyon | 1:d99dad8a1f50 | 259 | i2c.read(SENSOR_ADDR,DataReg,2, false); |
jolyon | 1:d99dad8a1f50 | 260 | NewData.White = ((int)DataReg[1] << 8) | DataReg[0]; |
jolyon | 1:d99dad8a1f50 | 261 | |
jolyon | 1:d99dad8a1f50 | 262 | AddrReg[0] = SENSOR_RED_REG; |
jolyon | 1:d99dad8a1f50 | 263 | i2c.write(SENSOR_ADDR, AddrReg,1, true); |
jolyon | 1:d99dad8a1f50 | 264 | i2c.read(SENSOR_ADDR,DataReg,2, false); |
jolyon | 1:d99dad8a1f50 | 265 | NewData.Red = ((int)DataReg[1] << 8) | DataReg[0]; |
jolyon | 1:d99dad8a1f50 | 266 | |
jolyon | 1:d99dad8a1f50 | 267 | AddrReg[0] = SENSOR_GREEN_REG; |
jolyon | 1:d99dad8a1f50 | 268 | i2c.write(SENSOR_ADDR, AddrReg,1, true); |
jolyon | 1:d99dad8a1f50 | 269 | i2c.read(SENSOR_ADDR,DataReg,2, false); |
jolyon | 1:d99dad8a1f50 | 270 | NewData.Green = ((int)DataReg[1] << 8) | DataReg[0]; |
jolyon | 1:d99dad8a1f50 | 271 | |
jolyon | 1:d99dad8a1f50 | 272 | AddrReg[0] = SENSOR_BLUE_REG; |
jolyon | 1:d99dad8a1f50 | 273 | i2c.write(SENSOR_ADDR, AddrReg,1, true); |
jolyon | 1:d99dad8a1f50 | 274 | i2c.read(SENSOR_ADDR,DataReg,2, false); |
jolyon | 1:d99dad8a1f50 | 275 | NewData.Blue = ((int)DataReg[1] << 8) | DataReg[0]; |
jolyon | 1:d99dad8a1f50 | 276 | |
jolyon | 1:d99dad8a1f50 | 277 | return NewData; |
jolyon | 1:d99dad8a1f50 | 278 | } |
jolyon | 1:d99dad8a1f50 | 279 | |
jolyon | 2:4a8eacbb3657 | 280 | void InitOutputs(void) |
jolyon | 2:4a8eacbb3657 | 281 | { |
jolyon | 2:4a8eacbb3657 | 282 | // Set all 8 Port A bits to output direction |
jolyon | 2:4a8eacbb3657 | 283 | chip.direction(PORT_A, 0x00); |
jolyon | 2:4a8eacbb3657 | 284 | // Set all 8 Port B bits to output direction |
jolyon | 2:4a8eacbb3657 | 285 | chip.direction(PORT_B, 0x00); |
jolyon | 2:4a8eacbb3657 | 286 | } |
jolyon | 0:9d301ae51ecf | 287 | |
jolyon | 2:4a8eacbb3657 | 288 | void SetOutputBit(char bit) |
jolyon | 2:4a8eacbb3657 | 289 | { |
jolyon | 2:4a8eacbb3657 | 290 | uint16_t OutputWord = 1; |
jolyon | 2:4a8eacbb3657 | 291 | char BitA, BitB, PortA, PortB; |
jolyon | 2:4a8eacbb3657 | 292 | |
jolyon | 2:4a8eacbb3657 | 293 | OutputWord <<= bit; |
jolyon | 2:4a8eacbb3657 | 294 | BitA = OutputWord & 0xFF; |
jolyon | 2:4a8eacbb3657 | 295 | BitB = OutputWord >> 8; |
jolyon | 2:4a8eacbb3657 | 296 | |
jolyon | 2:4a8eacbb3657 | 297 | PortA = chip.read(PORT_A); |
jolyon | 2:4a8eacbb3657 | 298 | PortA |=BitA; |
jolyon | 2:4a8eacbb3657 | 299 | chip.write(PORT_A, PortA); |
jolyon | 2:4a8eacbb3657 | 300 | |
jolyon | 2:4a8eacbb3657 | 301 | PortB = chip.read(PORT_B); |
jolyon | 2:4a8eacbb3657 | 302 | PortB |=BitB; |
jolyon | 2:4a8eacbb3657 | 303 | chip.write(PORT_B, PortB); |
jolyon | 2:4a8eacbb3657 | 304 | } |
jolyon | 2:4a8eacbb3657 | 305 | |
jolyon | 2:4a8eacbb3657 | 306 | void ClearOutputBit(char bit) |
jolyon | 2:4a8eacbb3657 | 307 | { |
jolyon | 2:4a8eacbb3657 | 308 | uint16_t OutputWord = 1; |
jolyon | 2:4a8eacbb3657 | 309 | char BitA, BitB, PortA, PortB; |
jolyon | 2:4a8eacbb3657 | 310 | |
jolyon | 2:4a8eacbb3657 | 311 | OutputWord <<= bit; |
jolyon | 2:4a8eacbb3657 | 312 | OutputWord = ~OutputWord; |
jolyon | 2:4a8eacbb3657 | 313 | |
jolyon | 2:4a8eacbb3657 | 314 | BitA = OutputWord & 0xFF; |
jolyon | 2:4a8eacbb3657 | 315 | BitB = OutputWord >> 8; |
jolyon | 2:4a8eacbb3657 | 316 | |
jolyon | 2:4a8eacbb3657 | 317 | PortA = chip.read(PORT_A); |
jolyon | 2:4a8eacbb3657 | 318 | PortA &=BitA; |
jolyon | 2:4a8eacbb3657 | 319 | chip.write(PORT_A, PortA); |
jolyon | 2:4a8eacbb3657 | 320 | |
jolyon | 2:4a8eacbb3657 | 321 | PortB = chip.read(PORT_B); |
jolyon | 2:4a8eacbb3657 | 322 | PortB &=BitB; |
jolyon | 2:4a8eacbb3657 | 323 | chip.write(PORT_B, PortB); |
jolyon | 2:4a8eacbb3657 | 324 | } |
jolyon | 2:4a8eacbb3657 | 325 | |
jolyon | 2:4a8eacbb3657 | 326 | void WriteOutputPort(uint16_t FullPort) |
jolyon | 2:4a8eacbb3657 | 327 | { |
jolyon | 2:4a8eacbb3657 | 328 | chip.write(PORT_A, FullPort & 0xFF); |
jolyon | 2:4a8eacbb3657 | 329 | chip.write(PORT_B, FullPort >> 8); |
jolyon | 2:4a8eacbb3657 | 330 | } |
jolyon | 2:4a8eacbb3657 | 331 | |
jolyon | 2:4a8eacbb3657 | 332 | |
jolyon | 2:4a8eacbb3657 | 333 | void GetThresholds(void) |
jolyon | 2:4a8eacbb3657 | 334 | { |
jolyon | 2:4a8eacbb3657 | 335 | //Load the pot values ant make 0 to 100% value |
jolyon | 2:4a8eacbb3657 | 336 | /* |
jolyon | 2:4a8eacbb3657 | 337 | RedThreshold = RedTrimmer.read_u16() * 100 >> 11; |
jolyon | 2:4a8eacbb3657 | 338 | GreenThreshold = GreenTrimmer.read_u16() * 100 >> 11; |
jolyon | 2:4a8eacbb3657 | 339 | BlueThreshold = BlueTrimmer.read_u16() * 100 >> 11; |
jolyon | 3:b8218b61b0e2 | 340 | Hysteresis = HysTrimmer.read_u16() * 100 >> 11; |
jolyon | 3:b8218b61b0e2 | 341 | */ |
jolyon | 2:4a8eacbb3657 | 342 | |
jolyon | 2:4a8eacbb3657 | 343 | RedThreshold = 45; |
jolyon | 2:4a8eacbb3657 | 344 | GreenThreshold = 35; |
jolyon | 2:4a8eacbb3657 | 345 | BlueThreshold = 10; |
jolyon | 2:4a8eacbb3657 | 346 | Hysteresis = 5; |
jolyon | 2:4a8eacbb3657 | 347 | } |
jolyon | 0:9d301ae51ecf | 348 | |
jolyon | 3:b8218b61b0e2 | 349 | |
jolyon | 3:b8218b61b0e2 | 350 | void UpdateDisplay(void) |
jolyon | 3:b8218b61b0e2 | 351 | { |
jolyon | 3:b8218b61b0e2 | 352 | //make 12 bit only (display) |
jolyon | 3:b8218b61b0e2 | 353 | Red >>=4; |
jolyon | 3:b8218b61b0e2 | 354 | Green >>=4; |
jolyon | 3:b8218b61b0e2 | 355 | Blue >>=4; |
jolyon | 3:b8218b61b0e2 | 356 | White >>=4; |
jolyon | 3:b8218b61b0e2 | 357 | |
jolyon | 3:b8218b61b0e2 | 358 | lcd.locate(0, 0); |
jolyon | 3:b8218b61b0e2 | 359 | lcd.printf("R%02i G%02i B%02i W%04i",RedProp,GreenProp,BlueProp,White); |
jolyon | 3:b8218b61b0e2 | 360 | lcd.locate(0, 1); |
jolyon | 3:b8218b61b0e2 | 361 | lcd.printf("R%02i G%02i B%02i H%02i ",RedThreshold, GreenThreshold, BlueThreshold, Hysteresis); |
jolyon | 3:b8218b61b0e2 | 362 | } |
jolyon | 3:b8218b61b0e2 | 363 | |
jolyon | 3:b8218b61b0e2 | 364 | |
jolyon | 3:b8218b61b0e2 | 365 | char GetResults(uint8_t ThisSensor) |
jolyon | 3:b8218b61b0e2 | 366 | { |
jolyon | 3:b8218b61b0e2 | 367 | |
jolyon | 3:b8218b61b0e2 | 368 | char LEDColour = ERROR; |
jolyon | 3:b8218b61b0e2 | 369 | |
jolyon | 3:b8218b61b0e2 | 370 | |
jolyon | 3:b8218b61b0e2 | 371 | if(White > WHITE_THRESHOLD) //looking at intensity somthing is on! |
jolyon | 3:b8218b61b0e2 | 372 | { |
jolyon | 3:b8218b61b0e2 | 373 | switch(Config[(ThisSensor << 1)]) |
jolyon | 3:b8218b61b0e2 | 374 | { |
jolyon | 3:b8218b61b0e2 | 375 | case STD_RED: |
jolyon | 3:b8218b61b0e2 | 376 | { |
jolyon | 3:b8218b61b0e2 | 377 | if((RedProp - GreenProp > 30) && (RedProp - BlueProp > 30)) |
jolyon | 3:b8218b61b0e2 | 378 | { |
jolyon | 3:b8218b61b0e2 | 379 | LEDColour = RED; |
jolyon | 3:b8218b61b0e2 | 380 | } |
jolyon | 3:b8218b61b0e2 | 381 | } |
jolyon | 3:b8218b61b0e2 | 382 | break; |
jolyon | 3:b8218b61b0e2 | 383 | |
jolyon | 3:b8218b61b0e2 | 384 | case STD_GREEN: |
jolyon | 3:b8218b61b0e2 | 385 | { |
jolyon | 3:b8218b61b0e2 | 386 | if((GreenProp - RedProp > 30) && (GreenProp - BlueProp > 30)) |
jolyon | 3:b8218b61b0e2 | 387 | { |
jolyon | 3:b8218b61b0e2 | 388 | LEDColour = GREEN; |
jolyon | 3:b8218b61b0e2 | 389 | } |
jolyon | 3:b8218b61b0e2 | 390 | } |
jolyon | 3:b8218b61b0e2 | 391 | break; |
jolyon | 3:b8218b61b0e2 | 392 | |
jolyon | 3:b8218b61b0e2 | 393 | case STD_BLUE: |
jolyon | 3:b8218b61b0e2 | 394 | { |
jolyon | 3:b8218b61b0e2 | 395 | if((BlueProp - RedProp > 30) && (BlueProp - GreenProp > 30)) |
jolyon | 3:b8218b61b0e2 | 396 | { |
jolyon | 3:b8218b61b0e2 | 397 | LEDColour = BLUE; |
jolyon | 3:b8218b61b0e2 | 398 | } |
jolyon | 3:b8218b61b0e2 | 399 | } |
jolyon | 3:b8218b61b0e2 | 400 | break; |
jolyon | 3:b8218b61b0e2 | 401 | |
jolyon | 3:b8218b61b0e2 | 402 | case STD_YELLOW: |
jolyon | 3:b8218b61b0e2 | 403 | { |
jolyon | 3:b8218b61b0e2 | 404 | if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) |
jolyon | 3:b8218b61b0e2 | 405 | { |
jolyon | 3:b8218b61b0e2 | 406 | LEDColour = YELLOW; |
jolyon | 3:b8218b61b0e2 | 407 | } |
jolyon | 3:b8218b61b0e2 | 408 | } |
jolyon | 3:b8218b61b0e2 | 409 | break; |
jolyon | 3:b8218b61b0e2 | 410 | |
jolyon | 3:b8218b61b0e2 | 411 | case GREEN_YELLOW: |
jolyon | 3:b8218b61b0e2 | 412 | { |
jolyon | 3:b8218b61b0e2 | 413 | if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) |
jolyon | 3:b8218b61b0e2 | 414 | { //pretty sure it's Yellow |
jolyon | 3:b8218b61b0e2 | 415 | |
jolyon | 3:b8218b61b0e2 | 416 | LEDColour = YELLOW; |
jolyon | 3:b8218b61b0e2 | 417 | } |
jolyon | 3:b8218b61b0e2 | 418 | else |
jolyon | 3:b8218b61b0e2 | 419 | { |
jolyon | 3:b8218b61b0e2 | 420 | if( (GreenProp > (GreenThreshold+Hysteresis)) && (RedProp <=(RedThreshold-Hysteresis)) ) |
jolyon | 3:b8218b61b0e2 | 421 | { //pretty sure it's green |
jolyon | 3:b8218b61b0e2 | 422 | LEDColour = GREEN; |
jolyon | 3:b8218b61b0e2 | 423 | } |
jolyon | 3:b8218b61b0e2 | 424 | } |
jolyon | 3:b8218b61b0e2 | 425 | } |
jolyon | 3:b8218b61b0e2 | 426 | break; |
jolyon | 3:b8218b61b0e2 | 427 | |
jolyon | 3:b8218b61b0e2 | 428 | case RGB: |
jolyon | 3:b8218b61b0e2 | 429 | { |
jolyon | 3:b8218b61b0e2 | 430 | if((RedProp - GreenProp > 30) && (RedProp - BlueProp > 30)) |
jolyon | 3:b8218b61b0e2 | 431 | { |
jolyon | 3:b8218b61b0e2 | 432 | LEDColour = RED; |
jolyon | 3:b8218b61b0e2 | 433 | } |
jolyon | 3:b8218b61b0e2 | 434 | else |
jolyon | 3:b8218b61b0e2 | 435 | { |
jolyon | 3:b8218b61b0e2 | 436 | if((GreenProp - RedProp > 30) && (GreenProp - BlueProp > 30)) |
jolyon | 3:b8218b61b0e2 | 437 | { |
jolyon | 3:b8218b61b0e2 | 438 | LEDColour = GREEN; |
jolyon | 3:b8218b61b0e2 | 439 | } |
jolyon | 3:b8218b61b0e2 | 440 | else |
jolyon | 3:b8218b61b0e2 | 441 | { |
jolyon | 3:b8218b61b0e2 | 442 | if((BlueProp - RedProp > 30) && (BlueProp - GreenProp > 30)) |
jolyon | 3:b8218b61b0e2 | 443 | { |
jolyon | 3:b8218b61b0e2 | 444 | LEDColour = BLUE; |
jolyon | 3:b8218b61b0e2 | 445 | } |
jolyon | 3:b8218b61b0e2 | 446 | } |
jolyon | 3:b8218b61b0e2 | 447 | } |
jolyon | 3:b8218b61b0e2 | 448 | } |
jolyon | 3:b8218b61b0e2 | 449 | break; |
jolyon | 3:b8218b61b0e2 | 450 | |
jolyon | 3:b8218b61b0e2 | 451 | case NOTUSED: |
jolyon | 3:b8218b61b0e2 | 452 | default: |
jolyon | 3:b8218b61b0e2 | 453 | { |
jolyon | 3:b8218b61b0e2 | 454 | LEDColour = ERROR; |
jolyon | 3:b8218b61b0e2 | 455 | } |
jolyon | 3:b8218b61b0e2 | 456 | break; |
jolyon | 3:b8218b61b0e2 | 457 | } |
jolyon | 3:b8218b61b0e2 | 458 | } |
jolyon | 3:b8218b61b0e2 | 459 | return LEDColour; |
jolyon | 3:b8218b61b0e2 | 460 | } |
jolyon | 3:b8218b61b0e2 | 461 | |
jolyon | 3:b8218b61b0e2 | 462 | |
jolyon | 3:b8218b61b0e2 | 463 | |
jolyon | 0:9d301ae51ecf | 464 | int main() |
jolyon | 0:9d301ae51ecf | 465 | { |
jolyon | 2:4a8eacbb3657 | 466 | InitOutputs(); |
jolyon | 1:d99dad8a1f50 | 467 | //----------------------Initialise My TCS3472-------------------- |
jolyon | 0:9d301ae51ecf | 468 | ConfigureTSC(0); |
jolyon | 1:d99dad8a1f50 | 469 | //---------------------- My TCS3472 -------------------- |
jolyon | 0:9d301ae51ecf | 470 | |
jolyon | 1:d99dad8a1f50 | 471 | while(1) |
jolyon | 1:d99dad8a1f50 | 472 | { |
jolyon | 2:4a8eacbb3657 | 473 | |
jolyon | 2:4a8eacbb3657 | 474 | //WriteOutputPort(MyPort); |
jolyon | 2:4a8eacbb3657 | 475 | ClearOutputBit(MyPort); |
jolyon | 2:4a8eacbb3657 | 476 | MyPort++; |
jolyon | 2:4a8eacbb3657 | 477 | if (MyPort == 16) |
jolyon | 2:4a8eacbb3657 | 478 | { |
jolyon | 2:4a8eacbb3657 | 479 | MyPort = 0; |
jolyon | 2:4a8eacbb3657 | 480 | } |
jolyon | 2:4a8eacbb3657 | 481 | SetOutputBit(MyPort); |
jolyon | 2:4a8eacbb3657 | 482 | |
jolyon | 2:4a8eacbb3657 | 483 | GetThresholds(); |
jolyon | 0:9d301ae51ecf | 484 | |
jolyon | 0:9d301ae51ecf | 485 | DisplayIndex++; |
jolyon | 0:9d301ae51ecf | 486 | if(DisplayIndex >= 20) { |
jolyon | 0:9d301ae51ecf | 487 | DisplayIndex = 0; |
jolyon | 0:9d301ae51ecf | 488 | } |
jolyon | 2:4a8eacbb3657 | 489 | |
jolyon | 0:9d301ae51ecf | 490 | wait(0.1); |
jolyon | 2:4a8eacbb3657 | 491 | |
jolyon | 0:9d301ae51ecf | 492 | //----------------------My TCS3472-------------------- |
jolyon | 0:9d301ae51ecf | 493 | |
jolyon | 0:9d301ae51ecf | 494 | |
jolyon | 3:b8218b61b0e2 | 495 | for(SensorNumber = 0; SensorNumber < 16; SensorNumber++) |
jolyon | 3:b8218b61b0e2 | 496 | { |
jolyon | 3:b8218b61b0e2 | 497 | if(Config[((SensorNumber << 1) + 1)] != NOTUSED) //check to see if the sensor is being used |
jolyon | 3:b8218b61b0e2 | 498 | { |
jolyon | 3:b8218b61b0e2 | 499 | SensorData = GetSensorData(SensorNumber); |
jolyon | 3:b8218b61b0e2 | 500 | |
jolyon | 3:b8218b61b0e2 | 501 | White = SensorData.White; |
jolyon | 3:b8218b61b0e2 | 502 | Red = SensorData.Red; |
jolyon | 3:b8218b61b0e2 | 503 | Green = SensorData.Green; |
jolyon | 3:b8218b61b0e2 | 504 | Blue = SensorData.Blue; |
jolyon | 0:9d301ae51ecf | 505 | |
jolyon | 3:b8218b61b0e2 | 506 | UpdateDisplay(); |
jolyon | 3:b8218b61b0e2 | 507 | |
jolyon | 3:b8218b61b0e2 | 508 | // work out % of colour mix |
jolyon | 3:b8218b61b0e2 | 509 | Temp = (Red+Blue+Green); |
jolyon | 3:b8218b61b0e2 | 510 | RedProp = (Red* 100) / Temp ; |
jolyon | 3:b8218b61b0e2 | 511 | GreenProp = (Green* 100) / Temp ; |
jolyon | 3:b8218b61b0e2 | 512 | BlueProp = (Blue* 100) / Temp ; |
jolyon | 3:b8218b61b0e2 | 513 | |
jolyon | 3:b8218b61b0e2 | 514 | |
jolyon | 3:b8218b61b0e2 | 515 | SensorResult = GetResults(SensorNumber); |
jolyon | 3:b8218b61b0e2 | 516 | |
jolyon | 3:b8218b61b0e2 | 517 | |
jolyon | 0:9d301ae51ecf | 518 | |
jolyon | 3:b8218b61b0e2 | 519 | //good LED so turn on Outputs |
jolyon | 3:b8218b61b0e2 | 520 | switch (Config[(SensorNumber << 1)]) |
jolyon | 3:b8218b61b0e2 | 521 | { |
jolyon | 3:b8218b61b0e2 | 522 | case STD_RED: |
jolyon | 3:b8218b61b0e2 | 523 | case STD_GREEN: |
jolyon | 3:b8218b61b0e2 | 524 | case STD_BLUE: |
jolyon | 3:b8218b61b0e2 | 525 | case STD_YELLOW: |
jolyon | 3:b8218b61b0e2 | 526 | { |
jolyon | 3:b8218b61b0e2 | 527 | if (SensorResult == Config[(SensorNumber << 1)]) |
jolyon | 3:b8218b61b0e2 | 528 | { |
jolyon | 3:b8218b61b0e2 | 529 | SetOutputBit(Config[((SensorNumber << 1)+1)]); |
jolyon | 3:b8218b61b0e2 | 530 | } |
jolyon | 3:b8218b61b0e2 | 531 | else |
jolyon | 3:b8218b61b0e2 | 532 | { |
jolyon | 3:b8218b61b0e2 | 533 | ClearOutputBit(Config[((SensorNumber << 1)+1)]); |
jolyon | 3:b8218b61b0e2 | 534 | } |
jolyon | 3:b8218b61b0e2 | 535 | } |
jolyon | 3:b8218b61b0e2 | 536 | break; |
jolyon | 3:b8218b61b0e2 | 537 | |
jolyon | 3:b8218b61b0e2 | 538 | case GREEN_YELLOW: |
jolyon | 3:b8218b61b0e2 | 539 | { |
jolyon | 3:b8218b61b0e2 | 540 | if (SensorResult == YELLOW) |
jolyon | 3:b8218b61b0e2 | 541 | { |
jolyon | 3:b8218b61b0e2 | 542 | SetOutputBit(Config[((SensorNumber << 1)+1)]); //base output |
jolyon | 3:b8218b61b0e2 | 543 | ClearOutputBit( Config[((SensorNumber << 1)+1)] +1);//next output |
jolyon | 3:b8218b61b0e2 | 544 | } |
jolyon | 3:b8218b61b0e2 | 545 | else |
jolyon | 3:b8218b61b0e2 | 546 | { |
jolyon | 3:b8218b61b0e2 | 547 | if (SensorResult == GREEN) |
jolyon | 3:b8218b61b0e2 | 548 | { |
jolyon | 3:b8218b61b0e2 | 549 | ClearOutputBit(Config[((SensorNumber << 1)+1)]); //base output |
jolyon | 3:b8218b61b0e2 | 550 | SetOutputBit( Config[((SensorNumber << 1)+1)] +1);//next output |
jolyon | 3:b8218b61b0e2 | 551 | } |
jolyon | 3:b8218b61b0e2 | 552 | else |
jolyon | 3:b8218b61b0e2 | 553 | { |
jolyon | 3:b8218b61b0e2 | 554 | ClearOutputBit(Config[((SensorNumber << 1)+1)]); //base output |
jolyon | 3:b8218b61b0e2 | 555 | ClearOutputBit( Config[((SensorNumber << 1)+1)] +1);//next output |
jolyon | 3:b8218b61b0e2 | 556 | } |
jolyon | 3:b8218b61b0e2 | 557 | } |
jolyon | 3:b8218b61b0e2 | 558 | } |
jolyon | 3:b8218b61b0e2 | 559 | break; |
jolyon | 3:b8218b61b0e2 | 560 | |
jolyon | 3:b8218b61b0e2 | 561 | case RGB: |
jolyon | 3:b8218b61b0e2 | 562 | { |
jolyon | 3:b8218b61b0e2 | 563 | if (SensorResult == RED) |
jolyon | 3:b8218b61b0e2 | 564 | { |
jolyon | 3:b8218b61b0e2 | 565 | SetOutputBit(Config[((SensorNumber << 1)+1)]); //base output |
jolyon | 3:b8218b61b0e2 | 566 | ClearOutputBit(Config[((SensorNumber << 1)+1)] +1); //next output |
jolyon | 3:b8218b61b0e2 | 567 | ClearOutputBit(Config[((SensorNumber << 1)+1)] +2); //next output |
jolyon | 3:b8218b61b0e2 | 568 | } |
jolyon | 3:b8218b61b0e2 | 569 | else |
jolyon | 3:b8218b61b0e2 | 570 | { |
jolyon | 3:b8218b61b0e2 | 571 | if (SensorResult == GREEN) |
jolyon | 3:b8218b61b0e2 | 572 | { |
jolyon | 3:b8218b61b0e2 | 573 | ClearOutputBit(Config[((SensorNumber << 1)+1)]); //base output |
jolyon | 3:b8218b61b0e2 | 574 | SetOutputBit(Config[((SensorNumber << 1)+1)] +1); //next output |
jolyon | 3:b8218b61b0e2 | 575 | ClearOutputBit(Config[((SensorNumber << 1)+1)] +2); //next output |
jolyon | 3:b8218b61b0e2 | 576 | } |
jolyon | 3:b8218b61b0e2 | 577 | else |
jolyon | 3:b8218b61b0e2 | 578 | { |
jolyon | 3:b8218b61b0e2 | 579 | if (SensorResult == BLUE) |
jolyon | 3:b8218b61b0e2 | 580 | { |
jolyon | 3:b8218b61b0e2 | 581 | ClearOutputBit(Config[((SensorNumber << 1)+1)]); //base output |
jolyon | 3:b8218b61b0e2 | 582 | ClearOutputBit(Config[((SensorNumber << 1)+1)] +1); //next output |
jolyon | 3:b8218b61b0e2 | 583 | SetOutputBit(Config[((SensorNumber << 1)+1)] +2); //next output |
jolyon | 3:b8218b61b0e2 | 584 | } |
jolyon | 3:b8218b61b0e2 | 585 | else |
jolyon | 3:b8218b61b0e2 | 586 | { |
jolyon | 3:b8218b61b0e2 | 587 | ClearOutputBit(Config[((SensorNumber << 1)+1)]); //base output |
jolyon | 3:b8218b61b0e2 | 588 | ClearOutputBit(Config[((SensorNumber << 1)+1)] +1); //next output |
jolyon | 3:b8218b61b0e2 | 589 | ClearOutputBit(Config[((SensorNumber << 1)+1)] +2); //next output |
jolyon | 3:b8218b61b0e2 | 590 | } |
jolyon | 3:b8218b61b0e2 | 591 | |
jolyon | 3:b8218b61b0e2 | 592 | } |
jolyon | 3:b8218b61b0e2 | 593 | } |
jolyon | 3:b8218b61b0e2 | 594 | } |
jolyon | 3:b8218b61b0e2 | 595 | break; |
jolyon | 3:b8218b61b0e2 | 596 | |
jolyon | 3:b8218b61b0e2 | 597 | default: |
jolyon | 3:b8218b61b0e2 | 598 | { |
jolyon | 3:b8218b61b0e2 | 599 | ClearOutputBit(Config[((SensorNumber << 1)+1)]); //base output |
jolyon | 3:b8218b61b0e2 | 600 | } |
jolyon | 3:b8218b61b0e2 | 601 | break; |
jolyon | 3:b8218b61b0e2 | 602 | } //end switch |
jolyon | 3:b8218b61b0e2 | 603 | } //end if |
jolyon | 3:b8218b61b0e2 | 604 | } //end for |
jolyon | 3:b8218b61b0e2 | 605 | |
jolyon | 3:b8218b61b0e2 | 606 | |
jolyon | 0:9d301ae51ecf | 607 | |
jolyon | 1:d99dad8a1f50 | 608 | |
jolyon | 1:d99dad8a1f50 | 609 | |
jolyon | 0:9d301ae51ecf | 610 | |
jolyon | 0:9d301ae51ecf | 611 | //guess the colour |
jolyon | 3:b8218b61b0e2 | 612 | if(White > WHITE_THRESHOLD) //looking at intensity somthing is on! |
jolyon | 2:4a8eacbb3657 | 613 | { |
jolyon | 2:4a8eacbb3657 | 614 | /* |
jolyon | 2:4a8eacbb3657 | 615 | if((GreenProp <= 30) && (RedProp >=50)) |
jolyon | 2:4a8eacbb3657 | 616 | { //pretty sure it's Yellow |
jolyon | 0:9d301ae51ecf | 617 | YellowLed = 1; |
jolyon | 0:9d301ae51ecf | 618 | GreenLed = 0; |
jolyon | 2:4a8eacbb3657 | 619 | } |
jolyon | 2:4a8eacbb3657 | 620 | else |
jolyon | 2:4a8eacbb3657 | 621 | { |
jolyon | 0:9d301ae51ecf | 622 | YellowLed = 0; |
jolyon | 2:4a8eacbb3657 | 623 | if((GreenProp > 40) && (RedProp <=40)) { //pretty sure it's green |
jolyon | 0:9d301ae51ecf | 624 | GreenLed = 1; |
jolyon | 0:9d301ae51ecf | 625 | } else { |
jolyon | 0:9d301ae51ecf | 626 | GreenLed = 1; |
jolyon | 0:9d301ae51ecf | 627 | YellowLed = 1; |
jolyon | 0:9d301ae51ecf | 628 | } |
jolyon | 0:9d301ae51ecf | 629 | } |
jolyon | 2:4a8eacbb3657 | 630 | */ |
jolyon | 3:b8218b61b0e2 | 631 | |
jolyon | 3:b8218b61b0e2 | 632 | //------------------------CHECK red----------------------------------------- |
jolyon | 3:b8218b61b0e2 | 633 | if((RedProp - GreenProp > 30) && (RedProp - BlueProp > 30)) |
jolyon | 3:b8218b61b0e2 | 634 | { |
jolyon | 3:b8218b61b0e2 | 635 | RedLed = 1; |
jolyon | 3:b8218b61b0e2 | 636 | GreenLed = 0; |
jolyon | 3:b8218b61b0e2 | 637 | BlueLed = 0; |
jolyon | 3:b8218b61b0e2 | 638 | YellowLed = 0; |
jolyon | 3:b8218b61b0e2 | 639 | } |
jolyon | 3:b8218b61b0e2 | 640 | else |
jolyon | 3:b8218b61b0e2 | 641 | { |
jolyon | 3:b8218b61b0e2 | 642 | |
jolyon | 3:b8218b61b0e2 | 643 | if((GreenProp - RedProp > 30) && (GreenProp - BlueProp > 30)) |
jolyon | 3:b8218b61b0e2 | 644 | { |
jolyon | 3:b8218b61b0e2 | 645 | RedLed = 0; |
jolyon | 3:b8218b61b0e2 | 646 | GreenLed = 1; |
jolyon | 3:b8218b61b0e2 | 647 | BlueLed = 0; |
jolyon | 3:b8218b61b0e2 | 648 | YellowLed = 0; |
jolyon | 3:b8218b61b0e2 | 649 | } |
jolyon | 3:b8218b61b0e2 | 650 | else |
jolyon | 3:b8218b61b0e2 | 651 | { |
jolyon | 3:b8218b61b0e2 | 652 | if((BlueProp - RedProp > 30) && (BlueProp - GreenProp > 30)) |
jolyon | 3:b8218b61b0e2 | 653 | { |
jolyon | 3:b8218b61b0e2 | 654 | RedLed = 0; |
jolyon | 3:b8218b61b0e2 | 655 | GreenLed = 0; |
jolyon | 3:b8218b61b0e2 | 656 | BlueLed = 1; |
jolyon | 3:b8218b61b0e2 | 657 | YellowLed = 0; |
jolyon | 3:b8218b61b0e2 | 658 | } |
jolyon | 3:b8218b61b0e2 | 659 | else |
jolyon | 3:b8218b61b0e2 | 660 | { |
jolyon | 3:b8218b61b0e2 | 661 | if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) |
jolyon | 3:b8218b61b0e2 | 662 | { //pretty sure it's Yellow |
jolyon | 3:b8218b61b0e2 | 663 | //YellowLed = 1; |
jolyon | 3:b8218b61b0e2 | 664 | //GreenLed = 0; |
jolyon | 3:b8218b61b0e2 | 665 | |
jolyon | 3:b8218b61b0e2 | 666 | RedLed = 0; |
jolyon | 3:b8218b61b0e2 | 667 | GreenLed = 0; |
jolyon | 3:b8218b61b0e2 | 668 | BlueLed = 0; |
jolyon | 3:b8218b61b0e2 | 669 | YellowLed = 1;; |
jolyon | 3:b8218b61b0e2 | 670 | } |
jolyon | 3:b8218b61b0e2 | 671 | else |
jolyon | 3:b8218b61b0e2 | 672 | { |
jolyon | 3:b8218b61b0e2 | 673 | RedLed = 0; |
jolyon | 3:b8218b61b0e2 | 674 | GreenLed = 0; |
jolyon | 3:b8218b61b0e2 | 675 | BlueLed = 0; |
jolyon | 3:b8218b61b0e2 | 676 | YellowLed = 0; |
jolyon | 3:b8218b61b0e2 | 677 | } |
jolyon | 3:b8218b61b0e2 | 678 | } |
jolyon | 3:b8218b61b0e2 | 679 | } |
jolyon | 3:b8218b61b0e2 | 680 | } |
jolyon | 3:b8218b61b0e2 | 681 | |
jolyon | 3:b8218b61b0e2 | 682 | } |
jolyon | 3:b8218b61b0e2 | 683 | else |
jolyon | 3:b8218b61b0e2 | 684 | { |
jolyon | 3:b8218b61b0e2 | 685 | RedLed = 0; |
jolyon | 3:b8218b61b0e2 | 686 | GreenLed = 0; |
jolyon | 3:b8218b61b0e2 | 687 | BlueLed = 0; |
jolyon | 3:b8218b61b0e2 | 688 | YellowLed = 0; |
jolyon | 3:b8218b61b0e2 | 689 | } |
jolyon | 3:b8218b61b0e2 | 690 | |
jolyon | 3:b8218b61b0e2 | 691 | |
jolyon | 3:b8218b61b0e2 | 692 | |
jolyon | 3:b8218b61b0e2 | 693 | |
jolyon | 3:b8218b61b0e2 | 694 | /* |
jolyon | 3:b8218b61b0e2 | 695 | //------------------------------------------------------------------------------------------------------------------- |
jolyon | 3:b8218b61b0e2 | 696 | |
jolyon | 2:4a8eacbb3657 | 697 | if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) |
jolyon | 2:4a8eacbb3657 | 698 | { //pretty sure it's Yellow |
jolyon | 2:4a8eacbb3657 | 699 | YellowLed = 1; |
jolyon | 2:4a8eacbb3657 | 700 | GreenLed = 0; |
jolyon | 2:4a8eacbb3657 | 701 | } |
jolyon | 2:4a8eacbb3657 | 702 | else |
jolyon | 2:4a8eacbb3657 | 703 | { |
jolyon | 2:4a8eacbb3657 | 704 | YellowLed = 0; |
jolyon | 2:4a8eacbb3657 | 705 | if( (GreenProp > (GreenThreshold+Hysteresis)) && (RedProp <=(RedThreshold-Hysteresis)) ) |
jolyon | 2:4a8eacbb3657 | 706 | { //pretty sure it's green |
jolyon | 2:4a8eacbb3657 | 707 | GreenLed = 1; |
jolyon | 2:4a8eacbb3657 | 708 | } |
jolyon | 2:4a8eacbb3657 | 709 | else |
jolyon | 2:4a8eacbb3657 | 710 | { |
jolyon | 2:4a8eacbb3657 | 711 | GreenLed = 1; |
jolyon | 2:4a8eacbb3657 | 712 | YellowLed = 1; |
jolyon | 2:4a8eacbb3657 | 713 | } |
jolyon | 2:4a8eacbb3657 | 714 | } |
jolyon | 2:4a8eacbb3657 | 715 | } |
jolyon | 2:4a8eacbb3657 | 716 | else |
jolyon | 2:4a8eacbb3657 | 717 | { |
jolyon | 0:9d301ae51ecf | 718 | //not enough intensity to determine |
jolyon | 0:9d301ae51ecf | 719 | GreenLed = 0; |
jolyon | 0:9d301ae51ecf | 720 | YellowLed = 0; |
jolyon | 0:9d301ae51ecf | 721 | } |
jolyon | 3:b8218b61b0e2 | 722 | |
jolyon | 3:b8218b61b0e2 | 723 | */ |
jolyon | 3:b8218b61b0e2 | 724 | //---------------------------------------------------------------------------------------------------------------- |
jolyon | 3:b8218b61b0e2 | 725 | } //while(1) |
jolyon | 3:b8218b61b0e2 | 726 | }//main |
jolyon | 0:9d301ae51ecf | 727 | |
jolyon | 0:9d301ae51ecf | 728 | |
jolyon | 0:9d301ae51ecf | 729 | |
jolyon | 2:4a8eacbb3657 | 730 | /* |
jolyon | 2:4a8eacbb3657 | 731 | --------------------------------------------------------------------------------------------- |
jolyon | 2:4a8eacbb3657 | 732 | //SPI SampleCode |
jolyon | 0:9d301ae51ecf | 733 | |
jolyon | 2:4a8eacbb3657 | 734 | #include "mbed.h" |
jolyon | 2:4a8eacbb3657 | 735 | #include "MCP23S17.h" |
jolyon | 2:4a8eacbb3657 | 736 | // Create SPI bus |
jolyon | 2:4a8eacbb3657 | 737 | SPI spi(p5, p6, p7); |
jolyon | 2:4a8eacbb3657 | 738 | // |
jolyon | 2:4a8eacbb3657 | 739 | |
jolyon | 2:4a8eacbb3657 | 740 | char Opcode = 0x40; |
jolyon | 2:4a8eacbb3657 | 741 | |
jolyon | 2:4a8eacbb3657 | 742 | // Next create a MCP23S17 |
jolyon | 2:4a8eacbb3657 | 743 | // mbed p20 is connected to ~chipSelect on the MCP23S17 |
jolyon | 2:4a8eacbb3657 | 744 | MCP23S17 chip = MCP23S17(spi, p20, Opcode); |
jolyon | 2:4a8eacbb3657 | 745 | |
jolyon | 2:4a8eacbb3657 | 746 | DigitalOut led1(LED1); // mbed LED1 is used for test status display |
jolyon | 2:4a8eacbb3657 | 747 | |
jolyon | 2:4a8eacbb3657 | 748 | int main() { |
jolyon | 2:4a8eacbb3657 | 749 | // |
jolyon | 2:4a8eacbb3657 | 750 | // Set all 8 Port A bits to output direction |
jolyon | 2:4a8eacbb3657 | 751 | chip.direction(PORT_A, 0x00); |
jolyon | 2:4a8eacbb3657 | 752 | // Set all 8 Port B bits to input direction |
jolyon | 2:4a8eacbb3657 | 753 | chip.direction(PORT_B, 0xFF); |
jolyon | 2:4a8eacbb3657 | 754 | led1=0; |
jolyon | 2:4a8eacbb3657 | 755 | // Start Loopback test sending out and reading back values |
jolyon | 2:4a8eacbb3657 | 756 | // loopback test uses A0 and B0 pins - so use a wire to jumper those two pins on MCP23S17 together |
jolyon | 2:4a8eacbb3657 | 757 | while (1) { |
jolyon | 2:4a8eacbb3657 | 758 | // write 0xAA to MCP23S17 Port A |
jolyon | 2:4a8eacbb3657 | 759 | chip.write(PORT_A, 0xAA); |
jolyon | 2:4a8eacbb3657 | 760 | wait(.5); |
jolyon | 2:4a8eacbb3657 | 761 | // read back value from MCP23S17 Port B and display B0 on mbed led1 |
jolyon | 2:4a8eacbb3657 | 762 | led1 = chip.read(PORT_B)& 0x01; |
jolyon | 2:4a8eacbb3657 | 763 | // write 0x55 to MCP23S17 Port A |
jolyon | 2:4a8eacbb3657 | 764 | chip.write(PORT_A, 0x55); |
jolyon | 2:4a8eacbb3657 | 765 | wait(.5); |
jolyon | 2:4a8eacbb3657 | 766 | // read back value from MCP23S17 Port B and display B0 on mbed led1 |
jolyon | 2:4a8eacbb3657 | 767 | led1 = chip.read(PORT_B)& 0x01; |
jolyon | 2:4a8eacbb3657 | 768 | // led1 should blink slowly when it is all working |
jolyon | 0:9d301ae51ecf | 769 | } |
jolyon | 0:9d301ae51ecf | 770 | } |
jolyon | 2:4a8eacbb3657 | 771 | */ |
jolyon | 0:9d301ae51ecf | 772 | |
jolyon | 0:9d301ae51ecf | 773 | //--------------------------------------------------------------------------------------------- |
jolyon | 0:9d301ae51ecf | 774 | //--------------------------------------------------------------------------------------------- |