My Project

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 //QUITAR TICKER e implementarlo fuera
00003 
00004 I2C i2c(PB_7, PB_6); //pins for I2C communication (SDA, SCL)
00005 Serial pc(USBTX, USBRX, 9600); //9600 baudios - used to print some values
00006 DigitalOut ledColour(PB_15); // TCS34725 led
00007 DigitalOut ledR(PA_4); //RGB led - red light
00008 DigitalOut ledG(PH_1);  //RGB led - green light
00009 DigitalOut ledB(PA_14);  //RGB led - blue light
00010 
00011 // We set the sensor address. For TCS34725 is 0x29 = ‭‭0010 1001‬ (bin) ->> ‭0101 0010‬ (bin) = 0x52
00012 // We shift 1 bit to the left because in I2C protocol slave address is 7-bit. So we discard the 8th bit
00013 int sensor_addr = 0x29 << 1;
00014 //Variable for ISR
00015 bool readColour =  false;
00016 
00017 Ticker t;
00018 
00019 //ISR code
00020 void read_colour (void)
00021 {
00022     readColour =  true;
00023 }
00024 
00025 //Get max value (r,g,b) function
00026 char getMax(int r, int g, int b)
00027 {
00028     char result;
00029     int max;
00030     if (r < g) {
00031         max = g;
00032         result = 'g';
00033     } else {
00034         max= r;
00035         result = 'r';
00036     }
00037     if (max < b) {
00038         result = 'b';
00039     }
00040     return result;
00041 }
00042 
00043 int main()
00044 {
00045 
00046     t.attach(read_colour, 1.0); // Every second the ticker triggers an interruption
00047     green = 1; // LED of B-L072Z-LRWAN1 board on
00048 
00049     // Connect to the Color sensor and verify whether we connected to the correct sensor.
00050 
00051     //  i2c.frequency(200000);
00052     /*******************************************************
00053       * id_regval contains command register value: ‭1001 0010 *
00054       * COMMAND REGISTER structure                           *
00055       * 7   |  6     5  |  4   3   2   1  0                  *
00056       * CMD      TYPE          ADDR/SF                       *
00057       *                                                      *
00058       * CMD=1                                                *
00059       * TYPE=00 -> repeated byte protocol transaction        *
00060       * ADDR/SF= 10010 -> ADDR 0x12 - Device ID              *
00061       ********************************************************/
00062 
00063 
00064 
00065 
00066     // Initialize color sensor
00067 
00068     // Timing register address 0x01 (0000 0001). We set 1st bit to 1 -> 1000 0001
00069     char timing_register[2] = {0x81,0x50}; //0x50 ~ 400ms
00070     i2c.write(sensor_addr,timing_register,2,false);
00071 
00072     // Control register address 0x0F (0000 1111). We set 1st bit to 1 -> 1000 1111
00073     char control_register[2] = {0x8F,0}; //{0x8F, 0x00}, {1000 1111, 0000 0000} -> 1x gain
00074     i2c.write(sensor_addr,control_register,2,false);
00075 
00076     // Enable register address 0x00 (0000 0000). We set 1st bit to 1 -> 1000 0000
00077     char enable_register[2] = {0x80,0x03}; //{0x80, 0x03}, {1000 0000, 0000 0011} -> AEN = PON = 1
00078     i2c.write(sensor_addr,enable_register,2,false);
00079 
00080     // Read data from color sensor (Clear/Red/Green/Blue)
00081     char clear_reg[1] = {0x94}; // {‭1001 0100‬} -> 0x14 and we set 1st bit to 1
00082     char clear_data[2] = {0,0};
00083     char red_reg[1] = {0x96}; // {‭1001 0110‬} -> 0x16 and we set 1st bit to 1
00084     char red_data[2] = {0,0};
00085     char green_reg[1] = {0x98}; // {‭1001 1000‬} -> 0x18 and we set 1st bit to 1
00086     char green_data[2] = {0,0};
00087     char blue_reg[1] = {0x9A}; // {‭1001 1010‬} -> 0x1A and we set 1st bit to 1
00088     char blue_data[2] = {0,0};
00089 
00090     // Turn on the led in the sensor
00091     ledColour = 1;
00092 
00093     while (true) {
00094         //If ISR has been executed, we read clear & RGB values
00095         if (readColour) {
00096             readColour = 0; //readColour = false
00097             //Reads clear value
00098             i2c.write(sensor_addr,clear_reg,1, true);
00099             i2c.read(sensor_addr,clear_data,2, false);
00100 
00101             //We store in clear_value the concatenation of clear_data[1] and clear_data[0]
00102             int clear_value = ((int)clear_data[1] << 8) | clear_data[0];
00103 
00104             //Reads red value
00105             i2c.write(sensor_addr,red_reg,1, true);
00106             i2c.read(sensor_addr,red_data,2, false);
00107 
00108             //We store in red_value the concatenation of red_data[1] and red_data[0]
00109             int red_value = ((int)red_data[1] << 8) | red_data[0];
00110 
00111             //Reads green value
00112             i2c.write(sensor_addr,green_reg,1, true);
00113             i2c.read(sensor_addr,green_data,2, false);
00114 
00115             //We store in green_value the concatenation of green_data[1] and green_data[0]
00116             int green_value = ((int)green_data[1] << 8) | green_data[0];
00117 
00118             //Reads blue value
00119             i2c.write(sensor_addr,blue_reg,1, true);
00120             i2c.read(sensor_addr,blue_data,2, false);
00121 
00122             //We store in blue_value the concatenation of blue_data[1] and blue_data[0]
00123             int blue_value = ((int)blue_data[1] << 8) | blue_data[0];
00124 
00125             // print sensor readings
00126 
00127             pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\n\r", clear_value, red_value, green_value, blue_value);
00128 
00129             //Obtains which one is the greatest - red, green or blue
00130             char max = getMax(red_value, green_value, blue_value);
00131 
00132             //Switchs the color of the greatest value. First, we switch off all of them
00133             ledR.write(1);
00134             ledG.write(1);
00135             ledB.write(1);
00136             if (max == 'r') {
00137                 ledR.write(0);
00138                 pc.printf("R\r\n");
00139             } else if(max == 'g') {
00140                 pc.printf("G\r\n");
00141                 ledG.write(0);
00142             } else {
00143                 pc.printf("B\r\n");
00144                 ledB.write(0);
00145             }
00146 
00147         }
00148     }
00149 }