yan xu / Mbed 2 deprecated high speed light Bracelet

Dependencies:   ADXL345_I2C PololuLedStrip USBDevice mbed

Fork of xadow_m0_accelerometer by Seeed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "ADXL345_I2C.h"
00002 #include "mbed.h"
00003 #include "PololuLedStrip.h"
00004 
00005 #define DEBUG
00006 
00007 #ifdef DEBUG
00008 #include "USBSerial.h"                       // To use USB virtual serial, a driver is needed, check http://mbed.org/handbook/USBSerial
00009 #define LOG(args...)    pc.printf(args)
00010 USBSerial pc;
00011 #else
00012 #define LOG(args...)
00013 #endif
00014 PololuLedStrip ledStrip(P0_8);
00015 ADXL345_I2C accelerometer(P0_5, P0_4);
00016 
00017 
00018 #define LED_COUNT 6
00019 rgb_color colors[LED_COUNT];
00020 Timer timer;
00021 rgb_color color;
00022 
00023 DigitalOut led_white(P0_23);
00024 DigitalOut led_blue(P0_20);
00025 DigitalOut led_strip(P0_8);
00026 int  x,y,z,val;
00027 
00028 rgb_color hsvToRgb(float h, float s, float v)    //claim object hsvToRbg  
00029 {
00030     int i = floor(h * 6);
00031     float f = h * 6 - i;
00032     float p = v * (1 - s);
00033     float q = v * (1 - f * s);
00034     float t = v * (1 - (1 - f) * s);
00035     float r = 0, g = 0, b = 0;
00036     switch(i % 6){
00037         case 0: r = v; g = t; b = p; break;
00038         case 1: r = q; g = v; b = p; break;
00039         case 2: r = p; g = v; b = t; break;
00040         case 3: r = p; g = q; b = v; break;
00041         case 4: r = t; g = p; b = v; break;
00042         case 5: r = v; g = p; b = q; break;
00043     }
00044     return (rgb_color){r * 255, g * 255, b * 255};
00045 }
00046 
00047 int main()
00048 {
00049     
00050     
00051     led_white=0;
00052     led_blue=0;
00053 
00054     int readings[3] = {0, 0, 0};
00055 
00056     accelerometer.setPowerControl(0x00);
00057     accelerometer.setDataFormatControl(0x0B);
00058     accelerometer.setDataRate(ADXL345_3200HZ);
00059     accelerometer.setPowerControl(0x08);
00060 
00061     while (1) {
00062         accelerometer.getOutput(readings);
00063 
00064         x=abs((int16_t)readings[2]);
00065         y=abs((int16_t)readings[2]);         
00066         z=abs((int16_t)readings[2]);   
00067         
00068         val=x;
00069         if (val<y)val=y;
00070         if (val<z)val=z;
00071         if (val>1100)val=1100;          
00072                 
00073                 if (val>300)
00074                     {   
00075                                                     
00076                      color.red =0;
00077                      color.green = char((val-300)*255/800); 
00078                      color.blue = 0;
00079 
00080                                                
00081                      for(uint16_t i = 0; i < LED_COUNT; i++)
00082                      {
00083                      colors[i] = color;
00084                      }
00085 
00086                         ledStrip.write(colors, LED_COUNT);  
00087                         wait_ms (100);
00088                      }
00089                                 
00090                 else{
00091                     
00092                      rgb_color  colors[LED_COUNT]={};
00093                      ledStrip.write(colors, LED_COUNT);
00094                      
00095                      }
00096        
00097 
00098     }
00099 
00100 }