Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MAG3110 MMA8451Q SLCD- TSI USBDevice mbed
main_lab2.cpp
00001 //USB Academy - Lab2 rev 00 00002 //_____________________________________________________________// 00003 //======== INCLUDES ===========================================// 00004 //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// 00005 #include "mbed.h" 00006 #include "MMA8451Q.h" // akcelerometr 00007 #include "MAG3110.h" // magnetometr 00008 #include "SLCD.h" // wyswietlacz 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 SLCD slcd; //[88:88][8.8.8.8] SegmentLCD 00024 00025 AnalogIn light(PTE22); //analog-light input 00026 00027 TSISensor slider; //Capacitive Touch Slider 00028 00029 MAG3110 mag(PTE25, PTE24); //Magnetometer 00030 00031 Serial usb_osda(USBTX, USBRX); //OpenSDA Terminal 00032 00033 #define pf usb_osda //printf out -> osda (lab1,2,3) 00034 00035 #define LED_ON 0 //outON, pwmON 00036 #define LED_OFF 1 //outOFF,pwmOFF 00037 DigitalOut gLED(LED_GREEN); //PTD5 00038 00039 #define rLEDperiod 150 //[ms] 00040 PwmOut rLED(LED_RED); //PTE29 00041 00042 #define PRESS_ON 0 00043 #define PRESS_OFF 1 00044 DigitalIn sw1(PTC3); //if(sw1) Release else Press 00045 DigitalIn sw3(PTC12); //while(sw3); wait for Press 00046 00047 #define MMA8451_I2C_ADDRESS (0x1d<<1) 00048 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); 00049 00050 struct KL46_SENSOR_DATA { 00051 int sw1State; 00052 int sw3State; 00053 float accValX; 00054 float accValY; 00055 float accValZ; 00056 00057 float slider; 00058 float light; 00059 int magValX; 00060 int magValY; 00061 int magValZ; 00062 00063 } sensorData; 00064 #define sD sensorData 00065 00066 void SLCD_blinking_msg_wait(char *slcd_msg1, char *slcd_msg2); // wyśw na wyśw 2 komunikaty i czeka na przycisk sw1 00067 00068 //Lab2add 00069 void SLCD_blinking_msg_wait(char *slcd_msg1, char *slcd_msg2) 00070 { 00071 char wait4sw1=0; //~500ms blinking 00072 slcd.CharPosition=0; 00073 //wait for Press SW1 - to start mag calibration 00074 while(sw1 == PRESS_ON); //wait for release 00075 while(sw1 == PRESS_OFF) { //wait for press 00076 if (++wait4sw1 < 150) //300ms 00077 slcd.printf(slcd_msg1); 00078 else //200ms 00079 slcd.printf(slcd_msg2); 00080 wait(0.002); 00081 slcd.CharPosition=0; 00082 } 00083 while(sw1 == PRESS_ON); //wait for release 00084 } 00085 00086 00087 00088 //_____________________________________________________________// 00089 //======== MAIN() =============================================// 00090 //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// 00091 int main(void) 00092 { 00093 //---- MAIN/Inits -----------------------------------------// 00094 00095 sw1.mode(PullUp); // rezystor podciagajacy 00096 sw3.mode(PullUp); // rezystor podciagajacy 00097 00098 SLCD_blinking_msg_wait(" o","PrES");//Lab1=Hid;2=cdc;3=Msd 00099 00100 usb_osda.baud(115200); 00101 usb_osda.printf("\n___________________________________\r\n"); 00102 usb_osda.printf("\nFRDM-KL46Z_Lab\r\n \r\n I am a CDC serial port @OpenSDA/mUSB. Baud=115200 \r\n"); 00103 00104 00105 gLED = LED_ON; //Green LED ON to indicate running/writing 00106 rLED = LED_OFF; //Red LED OFF 00107 rLED.period(rLEDperiod); //Red LED (rLED) tsi/accZ/mag 00108 00109 //---- MAIN/Inits (Wait4SW1) -> Start! --------------------// 00110 00111 //---- MAIN/Inits Interface -------------------------------// 00112 00113 //---- MAIN/Inits Labs ------------------------------------// 00114 00115 //---- MAIN/Inits (Wait4SW1) -> Calib. eCompass -----------// 00116 00117 //---- MAIN/Inits Done! (Wait4SW1) -> MANI/Loop -----------// 00118 00119 gLED = LED_OFF; //Inits are done 00120 00121 //---- MAIN/Loop -----------------------------------------// 00122 while (1) { 00123 00124 //disable all SLCD DPs 00125 slcd.DP(0, false); slcd.DP(1, false); slcd.DP(2, false); 00126 00127 // MAIN/Loop/Sensing and Storing data -----------------// 00128 sD.sw1State = sw1; sD.sw3State = sw3; 00129 sD.accValX = acc.getAccX(); //accX[-1..1]->mouse (Lab1) 00130 sD.accValY = acc.getAccY(); //accY[-1..1]->mouse (Lab1) 00131 sD.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED 00132 00133 sD.slider = slider.readPercentage() * 100; 00134 sD.light = light; 00135 sD.magValX = mag.readVal(MAG_OUT_X_MSB); 00136 sD.magValY = mag.readVal(MAG_OUT_Y_MSB); 00137 sD.magValZ = mag.readVal(MAG_OUT_Z_MSB); 00138 00139 // MAIN/Loop/Processing and Actions -------------------// 00140 00141 //sensor -> terminal 00142 if (sD.sw1State != PRESS_OFF) { gLED = !gLED; //blinking 00143 pf.printf(" \r\n"); 00144 pf.printf(" Switches. Light . Slider . Accelerometer . Magnetometer . Compass\r\n"); 00145 pf.printf(" SW1:SW2| LUX | TSI | accX : accY : accZ | magX: magY: maxZ | Heading\r\n"); 00146 pf.printf(" %d : %d | %1.3f | %2.0f %% | %+1.3f:%+1.3f:%+1.3f| %5d:%5d:%5d | Lab3 \r\n", 00147 sD.sw1State, sD.sw3State, sD.light, sD.slider, sD.accValX, sD.accValY, sD.accValZ, 00148 (short)sD.magValX, (short)sD.magValY, (short)sD.magValZ); 00149 } gLED = LED_OFF; 00150 00151 00152 00153 //acc: z-axis 1g min-blinking//acc: z-axis 1g min-blinking 00154 rLED = abs(sD.accValZ); 00155 00156 // jak jest dotkniety czujnik pojemnosciowy to odczyt z niego jest na wyswietlaczu, jesli nie to wyswietlany jest odczyt z czujnika swiatla 00157 if (sD.slider) 00158 slcd.printf(" %3.0f", sD.slider); //left->right .. 0->100% 00159 else 00160 slcd.printf("%1.3f", sD.light); //night->light .. 1->0 00161 00162 wait(0.05); //wait 50ms 00163 } 00164 } 00165 00166 00167 00168 00169 00170 //_____________________________________________________________// 00171 //======== FUNC() =============================================// 00172 //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// 00173
Generated on Wed Jul 13 2022 21:19:18 by
