3rd year group project. Electronic and Electrical Engineering. Heriot-Watt University. This is the code for the mbed for the Automatic Little Object Organiser (ALOO).

Dependencies:   MCP23017 TCS3472_I2C WattBob_TextLCD mbed

main.cpp

Committer:
dreamselec
Date:
2015-10-18
Revision:
0:fe5cb0a8fc5a
Child:
1:92a2a5ef65a8

File content as of revision 0:fe5cb0a8fc5a:

#include "mbed.h"
#include "WattBob_TextLCD.h"
#include "TCS3472_I2C.h"
#include "MCP23017.h"
#include <string>

#define BACKLIGHT_ON(INTERFACE) INTERFACE->write_bit(1, 4);
#define BACKLIGHT_OFF(INTERFACE) INTERFACE->write_bit(0, 4);

#define PRINT_LCD(TEXT) lcd->printf(TEXT);
#define CLEAR_LCD() lcd->cls();
#define LCDFL() lcd->locate(0,0);
#define LCDSL() lcd->locate(1,0);
#define CLEAR_LEDS() i2cport->write_bit(0, 12); i2cport->write_bit(0, 13); i2cport->write_bit(0, 14); i2cport->write_bit(0, 15);

DigitalOut myled(LED1);

MCP23017 *i2cport;
WattBob_TextLCD *lcd;

TCS3472_I2C rgb_sensor(p9, p10);

int main(){
        i2cport = new MCP23017(p9, p10, 0x40);
        lcd = new WattBob_TextLCD(i2cport);
        BACKLIGHT_ON(i2cport);
        int rgb_results[4];
        
        CLEAR_LCD();
        LCDFL();
        PRINT_LCD("Colour Sensor");
        LCDSL();
        PRINT_LCD("Stay in school!");
        
        wait(2.0);
        CLEAR_LCD();
        
        i2cport->config(0x0F00, 0x0F00, 0x0F00);
        
        rgb_sensor.enablePowerAndRGBC();
        rgb_sensor.setIntegrationTime(100);
        i2cport->write_bit(1, 15);
        
        myled = 1;
        
        Serial      pc(USBTX, USBRX);
        
        while (1){
            rgb_sensor.getAllColors(rgb_results);
            CLEAR_LCD();
            
            LCDFL();
            lcd->printf("R:%d, G:%d", rgb_results[0], rgb_results[1]);
            LCDSL();
            lcd->printf("B:%d, A:%d", rgb_results[2], rgb_results[3]);
            
            if (i2cport->read_bit(8)){
                rgb_sensor.setIntegrationTime(50);
                
                CLEAR_LCD();
                CLEAR_LEDS();
                LCDFL();
                
                PRINT_LCD("I-Time:50");
                pc.printf("Integration Time set to %dms\r\n", 50);
                pc.printf("R,G,B,A,IT\r\n");
                
                i2cport->write_bit(1, 12);
                wait(1.0);
            } else if (i2cport->read_bit(9)){
                rgb_sensor.setIntegrationTime(200);
                
                CLEAR_LCD();
                CLEAR_LEDS();
                LCDFL();
                
                PRINT_LCD("I-Time:200");
                pc.printf("Integration Time set to %dms\r\n", 200);
                pc.printf("R,G,B,A,IT\r\n");
                
                i2cport->write_bit(1, 13);
                wait(1);
            } else if (i2cport->read_bit(11)){
                rgb_sensor.setIntegrationTime(100);
                
                CLEAR_LCD();
                CLEAR_LEDS();
                LCDFL();
                
                PRINT_LCD("I-Time:100");
                pc.printf("Integration Time set to %dms\r\n", 100);
                pc.printf("R,G,B,A,IT\r\n");
                
                i2cport->write_bit(1, 15);
                wait(1.0);
            } else if (i2cport->read_bit(10)){
                char bufferIT[6];
                float fBufferIT;
                char *ITQ = "I";
                
                LCDFL();
                PRINT_LCD("Reading stdin");
                LCDSL();
                PRINT_LCD("\'I\' for I-Time");
                pc.printf("Reading stdin\r\n");
                pc.scanf("%5s", bufferIT);
                
                if (bufferIT[0] == *ITQ){
                    pc.printf("I-Time:%d\r\n", rgb_sensor.readIntegrationTime());
                }else{
                    CLEAR_LEDS();
                    
                    fBufferIT = atof(bufferIT);
                    rgb_sensor.setIntegrationTime(fBufferIT);
                    
                    pc.printf("Integration Time set to %fms\r\n", fBufferIT);
                    lcd->printf("I-Time:%d\r\n", fBufferIT);
                    pc.printf("R,G,B,A,IT\r\n");
                    
                    i2cport->write_bit(1, 14);
                }
                
            }
            //pc.printf("R:%d, G:%d, B:%d, A:%d. Integration Time:%d\r\n", rgb_results[0], rgb_results[1], rgb_results[2], rgb_results[3], rgb_sensor.readIntegrationTime());
            pc.printf("%d,%d,%d,%d,%d\r\n", rgb_results[0], rgb_results[1], rgb_results[2], rgb_results[3], rgb_sensor.readIntegrationTime());
            wait(2.0);
        }
}