blablabla

Dependencies:   MAG3110 MMA8451Q SLCD- TSI USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main_lab2.cpp Source File

main_lab2.cpp

00001 //USB Academy - Lab2 rev 00
00002 //_____________________________________________________________//
00003 //======== INCLUDES ===========================================//
00004 //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯//
00005 #include "mbed.h"
00006 #include "MMA8451Q.h"
00007 #include "MAG3110.h"
00008 #include "SLCD.h"
00009 #include "TSISensor.h"
00010 
00011 //#include "USBMouse.h" //Lab1-Hid
00012 //#include "USBSerial.h" //Lab2-cdc
00013 //#include "USBHostMSD.h" //Lab3-Msd
00014 
00015 
00016 
00017 
00018 
00019 //_____________________________________________________________//
00020 //======== DEFINES & VARIABLES ================================//
00021 //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯//
00022 
00023 
00024 
00025 
00026 #define LED_ON  0 //outON, pwmON
00027 #define LED_OFF 1 //outOFF,pwmOFF
00028 DigitalOut gLED(LED_GREEN); //PTD5
00029 
00030 #define rLEDperiod 150      //[ms]
00031 PwmOut rLED(LED_RED);       //PTE29
00032 
00033 #define PRESS_ON  0
00034 #define PRESS_OFF 1
00035 DigitalIn  sw1(PTC3);  //if(sw1) Release else Press
00036 DigitalIn  sw3(PTC12); //while(sw3); wait for Press
00037 
00038 #define MMA8451_I2C_ADDRESS (0x1d<<1)
00039 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
00040 
00041 SLCD slcd; //[88:88][8.8.8.8] SegmentLCD
00042 
00043 AnalogIn  light(PTE22); //analog-light input
00044 
00045 TSISensor slider; //Capacitive Touch Slider
00046 
00047 MAG3110 mag(PTE25, PTE24); //Magnetometer
00048 
00049 Serial usb_osda(USBTX, USBRX); //OpenSDA Terminal
00050 #define pf usb_osda //printf out -> osda (lab1,2,3)
00051 
00052 
00053 struct KL46_SENSOR_DATA {
00054     int   sw1State;
00055     int   sw3State;
00056     float   accValX;
00057     float   accValY;
00058     float   accValZ;
00059     
00060     float   slider;
00061     float   light;
00062     int     magValX;
00063     int     magValY;
00064     int     magValZ;
00065 
00066 
00067 } sensorData;
00068 #define sD sensorData
00069 
00070 void SLCD_blinking_msg_wait(char *slcd_msg1, char *slcd_msg2);
00071 
00072 
00073 //_____________________________________________________________//
00074 //======== MAIN() =============================================//
00075 //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯//
00076 int main(void)
00077 {
00078     //---- MAIN/Inits -----------------------------------------//
00079     
00080     sw1.mode(PullUp);
00081     sw3.mode(PullUp);
00082     
00083     gLED = LED_ON; //Green LED ON to indicate running/writing
00084     rLED = LED_OFF; //Red LED OFF
00085     rLED.period(rLEDperiod); //Red LED (rLED) tsi/accZ/mag
00086     
00087     //---- MAIN/Inits (Wait4SW1) -> Start! --------------------//  
00088      
00089     //wait for Press SW1 - e.g. for HID/CDC/MSD Windows install.
00090     SLCD_blinking_msg_wait("   o","PrES");//Lab1=Hid;2=cdc;3=Msd
00091 
00092     //---- MAIN/Inits Interface -------------------------------//
00093     
00094     usb_osda.baud(115200);
00095     usb_osda.printf("\n___________________________________\r\n");
00096     usb_osda.printf("\nFRDM-KL46Z_Lab\r\n \r\n I am a CDC serial port @OpenSDA/mUSB. Baud=115200 \r\n");
00097 
00098     
00099     //---- MAIN/Inits Labs ------------------------------------//
00100     
00101     //---- MAIN/Inits (Wait4SW1) -> Calib. eCompass -----------//
00102     
00103     //---- MAIN/Inits Done! (Wait4SW1) -> MANI/Loop -----------//
00104     
00105     gLED = LED_OFF; //Inits are done
00106 
00107     //---- MAIN/Loop  -----------------------------------------//
00108     while (1) {
00109         
00110         //disable all SLCD DPs
00111         slcd.DP(0, false); slcd.DP(1, false); slcd.DP(2, false);
00112 
00113         // MAIN/Loop/Sensing and Storing data -----------------//
00114         sD.sw1State = sw1; sD.sw3State = sw3;
00115         sD.accValX = acc.getAccX(); //accX[-1..1]->mouse (Lab1)
00116         sD.accValY = acc.getAccY(); //accY[-1..1]->mouse (Lab1)
00117         sD.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED
00118         
00119         sD.slider = slider.readPercentage() * 100;
00120         sD.light = light;
00121         sD.magValX = mag.readVal(MAG_OUT_X_MSB);
00122         sD.magValY = mag.readVal(MAG_OUT_Y_MSB);
00123         sD.magValZ = mag.readVal(MAG_OUT_Z_MSB);
00124 
00125         // MAIN/Loop/Processing and Actions -------------------//
00126         //sensor -> terminal
00127         if (sD.sw1State != PRESS_OFF) { gLED = !gLED; //blinking 
00128             pf.printf(" \r\n");
00129             pf.printf(" Switches. Light . Slider . Accelerometer       . Magnetometer      . Compass\r\n");
00130             pf.printf("  SW1:SW2|  LUX  |  TSI   |  accX : accY : accZ |  magX: magY: maxZ | Heading\r\n");
00131             pf.printf("  %d : %d  | %1.3f |  %2.0f %%  | %+1.3f:%+1.3f:%+1.3f| %5d:%5d:%5d |  Lab3 \r\n", 
00132                 sD.sw1State, sD.sw3State,    sD.light, sD.slider,    sD.accValX, sD.accValY, sD.accValZ,    
00133                 (short)sD.magValX, (short)sD.magValY, (short)sD.magValZ);
00134         } gLED = LED_OFF;
00135 
00136 
00137         //acc: z-axis 1g min-blinking//acc: z-axis 1g min-blinking
00138         rLED = abs(sD.accValZ);
00139         
00140                 if (sD.slider) 
00141             slcd.printf(" %3.0f", sD.slider); //night->light .. 1->0
00142         else
00143             slcd.printf("%1.3f", sD.light); //left->right .. 0->100%
00144 
00145         
00146         wait(0.05); //wait 50ms
00147     }
00148 }
00149 
00150 
00151 
00152 
00153 
00154 //_____________________________________________________________//
00155 //======== FUNC() =============================================//
00156 //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯//
00157 //Lab2add
00158 void SLCD_blinking_msg_wait(char *slcd_msg1, char *slcd_msg2)
00159 {
00160     char wait4sw1=0; //~500ms blinking
00161     slcd.CharPosition=0;
00162     //wait for Press SW1 - to start mag calibration
00163     while(sw1 == PRESS_ON); //wait for release
00164     while(sw1 == PRESS_OFF) { //wait for press
00165         if (++wait4sw1 < 150) //300ms
00166             slcd.printf(slcd_msg1);
00167         else //200ms
00168             slcd.printf(slcd_msg2);
00169         wait(0.002);
00170         slcd.CharPosition=0;
00171     }
00172     while(sw1 == PRESS_ON); //wait for release
00173 }
00174