Ok

Dependencies:   mbed_rtos_types Mutex mbed_rtos_storage mbed Semaphore

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ThreadI2c.cpp Source File

ThreadI2c.cpp

00001 #include "mbed.h"
00002 #include "Si7021.h"
00003 #include "MMA8451Q.h"
00004 #include "TCS34725.h"
00005 
00006 extern Serial pc;
00007 Si7021 sensor(PB_9,PB_8);// Temp sensor 
00008 MMA8451Q acc(PB_9, PB_8, MMA8451_I2C_ADDRESS);// Accelerometer
00009 I2C RGB(PB_9, PB_8); // RGB sensor
00010 DigitalOut ledColour(PB_7); // TCS34725 led
00011 extern int I2c_timer;
00012 extern bool normal;
00013 int clear_value, green_value, red_value, blue_value;
00014 float temp_value, hum_value;
00015 float x,y,z;
00016 extern uint8_t dominant_color[3];//Red, green, blue, and the dominant color over the hour.
00017 char clear_data[2] = {0,0};
00018 char red_data[2] = {0,0};
00019 char green_data[2] = {0,0};
00020 char blue_data[2] = {0,0};
00021 char clear_reg[1] = {0x94}; // {‭1001 0100‬} -> 0x14 and we set 1st bit to 1
00022 char red_reg[1] = {0x96}; // {‭1001 0110‬} -> 0x16 and we set 1st bit to 1
00023 char green_reg[1] = {0x98}; // {‭1001 1000‬} -> 0x18 and we set 1st bit to 1
00024 char blue_reg[1] = {0x9A}; // {‭1001 1010‬} -> 0x1A and we set 1st bit to 1
00025 
00026 
00027     
00028     
00029   void I2cThread ()
00030 {
00031         while (true){
00032         
00033         pc.printf(" ");
00034         //Temp&Hum sensor
00035         sensor.measure();
00036         hum_value=sensor.get_humidity()/1000;
00037         temp_value=sensor.get_temperature()/1000;
00038       
00039         
00040       
00041      //Accelerometer 
00042      x = acc.getAccX();
00043      y = acc.getAccY();
00044      z = acc.getAccZ();
00045     // pc.printf("x = %f\ty = %f\tz = %f\n\r", x, y, z);
00046      
00047      //RGB
00048      
00049     // Turn on the led in the sensor
00050     ledColour = 1;
00051 
00052             //Reads clear value
00053             RGB.write(RGB_ADDR,clear_reg,1, true);
00054             RGB.read(RGB_ADDR,clear_data,2, false);
00055 
00056             //We store in clear_value the concatenation of clear_data[1] and clear_data[0], switching the bytes.
00057             clear_value = ((int)clear_data[1] << 8) | clear_data[0];
00058              
00059             //Reads red value
00060             RGB.write(RGB_ADDR,red_reg,1, true);
00061             RGB.read(RGB_ADDR,red_data,2, false);
00062 
00063             //We store in red_value the concatenation of red_data[1] and red_data[0]
00064             red_value = ((int)red_data[1] << 8) | red_data[0];
00065 
00066             //Reads green value
00067             RGB.write(RGB_ADDR,green_reg,1, true);
00068             RGB.read(RGB_ADDR,green_data,2, false);
00069 
00070             //We store in green_value the concatenation of green_data[1] and green_data[0]
00071             green_value = ((int)green_data[1] << 8) | green_data[0];
00072 
00073             //Reads blue value
00074             RGB.write(RGB_ADDR,blue_reg,1, true);
00075             RGB.read(RGB_ADDR,blue_data,2, false);
00076 
00077             //We store in blue_value the concatenation of blue_data[1] and blue_data[0]
00078              blue_value = ((int)blue_data[1] << 8) | blue_data[0];
00079             
00080             ledColour = 0;
00081             Thread::wait(I2c_timer);
00082                         
00083             }
00084             }
00085