![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
KL46Z-lab2
Dependencies: SLCD- mbed TSI MMA8451Q USBDevice MAG3110
main_lab2.cpp@0:29277ae50860, 2019-02-21 (annotated)
- Committer:
- Lokkus
- Date:
- Thu Feb 21 07:41:00 2019 +0000
- Revision:
- 0:29277ae50860
KL46Z-lab2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lokkus | 0:29277ae50860 | 1 | //USB Academy - Lab2 rev 00 |
Lokkus | 0:29277ae50860 | 2 | //_____________________________________________________________// |
Lokkus | 0:29277ae50860 | 3 | //======== INCLUDES ===========================================// |
Lokkus | 0:29277ae50860 | 4 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// |
Lokkus | 0:29277ae50860 | 5 | #include "mbed.h" |
Lokkus | 0:29277ae50860 | 6 | #include "MMA8451Q.h" |
Lokkus | 0:29277ae50860 | 7 | #include "MAG3110.h" |
Lokkus | 0:29277ae50860 | 8 | #include "SLCD.h" |
Lokkus | 0:29277ae50860 | 9 | #include "TSISensor.h" |
Lokkus | 0:29277ae50860 | 10 | |
Lokkus | 0:29277ae50860 | 11 | //#include "MAG3110.h" |
Lokkus | 0:29277ae50860 | 12 | //#include "SLCD.h" |
Lokkus | 0:29277ae50860 | 13 | //#include "TSISensor.h" |
Lokkus | 0:29277ae50860 | 14 | |
Lokkus | 0:29277ae50860 | 15 | //#include "USBMouse.h" //Lab1-Hid |
Lokkus | 0:29277ae50860 | 16 | //#include "USBSerial.h" //Lab2-cdc |
Lokkus | 0:29277ae50860 | 17 | //#include "USBHostMSD.h" //Lab3-Msd |
Lokkus | 0:29277ae50860 | 18 | |
Lokkus | 0:29277ae50860 | 19 | |
Lokkus | 0:29277ae50860 | 20 | |
Lokkus | 0:29277ae50860 | 21 | |
Lokkus | 0:29277ae50860 | 22 | |
Lokkus | 0:29277ae50860 | 23 | //_____________________________________________________________// |
Lokkus | 0:29277ae50860 | 24 | //======== DEFINES & VARIABLES ================================// |
Lokkus | 0:29277ae50860 | 25 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// |
Lokkus | 0:29277ae50860 | 26 | #define LED_ON 0 //outON, pwmON |
Lokkus | 0:29277ae50860 | 27 | #define LED_OFF 1 //outOFF,pwmOFF |
Lokkus | 0:29277ae50860 | 28 | DigitalOut gLED(LED_GREEN); //PTD5 |
Lokkus | 0:29277ae50860 | 29 | |
Lokkus | 0:29277ae50860 | 30 | #define rLEDperiod 150 //[ms] |
Lokkus | 0:29277ae50860 | 31 | PwmOut rLED(LED_RED); //PTE29 |
Lokkus | 0:29277ae50860 | 32 | |
Lokkus | 0:29277ae50860 | 33 | #define PRESS_ON 0 |
Lokkus | 0:29277ae50860 | 34 | #define PRESS_OFF 1 |
Lokkus | 0:29277ae50860 | 35 | DigitalIn sw1(PTC3); //if(sw1) Release else Press |
Lokkus | 0:29277ae50860 | 36 | DigitalIn sw3(PTC12); //while(sw3); wait for Press |
Lokkus | 0:29277ae50860 | 37 | |
Lokkus | 0:29277ae50860 | 38 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
Lokkus | 0:29277ae50860 | 39 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
Lokkus | 0:29277ae50860 | 40 | |
Lokkus | 0:29277ae50860 | 41 | SLCD slcd; //[88:88][8.8.8.8] SegmentLCD |
Lokkus | 0:29277ae50860 | 42 | |
Lokkus | 0:29277ae50860 | 43 | AnalogIn light(PTE22); //analog-light input |
Lokkus | 0:29277ae50860 | 44 | |
Lokkus | 0:29277ae50860 | 45 | TSISensor slider; //Capacitive Touch Slider |
Lokkus | 0:29277ae50860 | 46 | |
Lokkus | 0:29277ae50860 | 47 | MAG3110 mag(PTE25, PTE24); //Magnetometer |
Lokkus | 0:29277ae50860 | 48 | |
Lokkus | 0:29277ae50860 | 49 | Serial usb_osda(USBTX, USBRX); //OpenSDA Terminal |
Lokkus | 0:29277ae50860 | 50 | #define pf usb_osda //printf out -> osda (lab1,2,3) |
Lokkus | 0:29277ae50860 | 51 | |
Lokkus | 0:29277ae50860 | 52 | |
Lokkus | 0:29277ae50860 | 53 | struct KL46_SENSOR_DATA { |
Lokkus | 0:29277ae50860 | 54 | int sw1State; |
Lokkus | 0:29277ae50860 | 55 | int sw3State; |
Lokkus | 0:29277ae50860 | 56 | float accValX; |
Lokkus | 0:29277ae50860 | 57 | float accValY; |
Lokkus | 0:29277ae50860 | 58 | float accValZ; |
Lokkus | 0:29277ae50860 | 59 | float slider; |
Lokkus | 0:29277ae50860 | 60 | float light; |
Lokkus | 0:29277ae50860 | 61 | int magValX; |
Lokkus | 0:29277ae50860 | 62 | int magValY; |
Lokkus | 0:29277ae50860 | 63 | int magValZ; |
Lokkus | 0:29277ae50860 | 64 | |
Lokkus | 0:29277ae50860 | 65 | } sensorData; |
Lokkus | 0:29277ae50860 | 66 | #define sD sensorData |
Lokkus | 0:29277ae50860 | 67 | |
Lokkus | 0:29277ae50860 | 68 | void SLCD_blinking_msg_wait(char *slcd_msg1, char *slcd_msg2); |
Lokkus | 0:29277ae50860 | 69 | |
Lokkus | 0:29277ae50860 | 70 | |
Lokkus | 0:29277ae50860 | 71 | |
Lokkus | 0:29277ae50860 | 72 | //_____________________________________________________________// |
Lokkus | 0:29277ae50860 | 73 | //======== MAIN() =============================================// |
Lokkus | 0:29277ae50860 | 74 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// |
Lokkus | 0:29277ae50860 | 75 | int main(void) |
Lokkus | 0:29277ae50860 | 76 | { |
Lokkus | 0:29277ae50860 | 77 | //---- MAIN/Inits -----------------------------------------// |
Lokkus | 0:29277ae50860 | 78 | |
Lokkus | 0:29277ae50860 | 79 | sw1.mode(PullUp); |
Lokkus | 0:29277ae50860 | 80 | sw3.mode(PullUp); |
Lokkus | 0:29277ae50860 | 81 | |
Lokkus | 0:29277ae50860 | 82 | gLED = LED_ON; //Green LED ON to indicate running/writing |
Lokkus | 0:29277ae50860 | 83 | rLED = LED_OFF; //Red LED OFF |
Lokkus | 0:29277ae50860 | 84 | rLED.period(rLEDperiod); //Red LED (rLED) tsi/accZ/mag |
Lokkus | 0:29277ae50860 | 85 | |
Lokkus | 0:29277ae50860 | 86 | //---- MAIN/Inits (Wait4SW1) -> Start! --------------------// |
Lokkus | 0:29277ae50860 | 87 | //wait for Press SW1 - e.g. for HID/CDC/MSD Windows install. |
Lokkus | 0:29277ae50860 | 88 | SLCD_blinking_msg_wait(" o","PrES");//Lab1=Hid;2=cdc;3=Msd |
Lokkus | 0:29277ae50860 | 89 | |
Lokkus | 0:29277ae50860 | 90 | //---- MAIN/Inits Interface -------------------------------// |
Lokkus | 0:29277ae50860 | 91 | |
Lokkus | 0:29277ae50860 | 92 | usb_osda.baud(115200); |
Lokkus | 0:29277ae50860 | 93 | usb_osda.printf("\n___________________________________\r\n"); |
Lokkus | 0:29277ae50860 | 94 | usb_osda.printf("\nFRDM-KL46Z_Lab\r\n \r\n I am a CDC serial port @OpenSDA/mUSB. Baud=115200 \r\n"); |
Lokkus | 0:29277ae50860 | 95 | |
Lokkus | 0:29277ae50860 | 96 | |
Lokkus | 0:29277ae50860 | 97 | //---- MAIN/Inits Interface -------------------------------// |
Lokkus | 0:29277ae50860 | 98 | |
Lokkus | 0:29277ae50860 | 99 | //---- MAIN/Inits Labs ------------------------------------// |
Lokkus | 0:29277ae50860 | 100 | |
Lokkus | 0:29277ae50860 | 101 | //---- MAIN/Inits (Wait4SW1) -> Calib. eCompass -----------// |
Lokkus | 0:29277ae50860 | 102 | |
Lokkus | 0:29277ae50860 | 103 | //---- MAIN/Inits Done! (Wait4SW1) -> MANI/Loop -----------// |
Lokkus | 0:29277ae50860 | 104 | |
Lokkus | 0:29277ae50860 | 105 | gLED = LED_OFF; //Inits are done |
Lokkus | 0:29277ae50860 | 106 | |
Lokkus | 0:29277ae50860 | 107 | //---- MAIN/Loop -----------------------------------------// |
Lokkus | 0:29277ae50860 | 108 | while (1) { |
Lokkus | 0:29277ae50860 | 109 | |
Lokkus | 0:29277ae50860 | 110 | // MAIN/Loop/Sensing and Storing data -----------------// |
Lokkus | 0:29277ae50860 | 111 | sD.sw1State = sw1; sD.sw3State = sw3; |
Lokkus | 0:29277ae50860 | 112 | sD.accValX = acc.getAccX(); //accX[-1..1]->mouse (Lab1) |
Lokkus | 0:29277ae50860 | 113 | sD.accValY = acc.getAccY(); //accY[-1..1]->mouse (Lab1) |
Lokkus | 0:29277ae50860 | 114 | sD.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED |
Lokkus | 0:29277ae50860 | 115 | |
Lokkus | 0:29277ae50860 | 116 | sD.slider = slider.readPercentage() * 100; |
Lokkus | 0:29277ae50860 | 117 | sD.light = light; |
Lokkus | 0:29277ae50860 | 118 | sD.magValX = mag.readVal(MAG_OUT_X_MSB); |
Lokkus | 0:29277ae50860 | 119 | sD.magValY = mag.readVal(MAG_OUT_Y_MSB); |
Lokkus | 0:29277ae50860 | 120 | sD.magValZ = mag.readVal(MAG_OUT_Z_MSB); |
Lokkus | 0:29277ae50860 | 121 | |
Lokkus | 0:29277ae50860 | 122 | |
Lokkus | 0:29277ae50860 | 123 | // MAIN/Loop/Processing and Actions -------------------// |
Lokkus | 0:29277ae50860 | 124 | //sensor -> terminal |
Lokkus | 0:29277ae50860 | 125 | if (sD.sw1State != PRESS_OFF) { gLED = !gLED; //blinking |
Lokkus | 0:29277ae50860 | 126 | pf.printf(" \r\n"); |
Lokkus | 0:29277ae50860 | 127 | pf.printf(" Switches. Light . Slider . Accelerometer . Magnetometer . Compass\r\n"); |
Lokkus | 0:29277ae50860 | 128 | pf.printf(" SW1:SW2| LUX | TSI | accX : accY : accZ | magX: magY: maxZ | Heading\r\n"); |
Lokkus | 0:29277ae50860 | 129 | pf.printf(" %d : %d | %1.3f | %2.0f %% | %+1.3f:%+1.3f:%+1.3f| %5d:%5d:%5d | Lab3 \r\n", |
Lokkus | 0:29277ae50860 | 130 | sD.sw1State, sD.sw3State, sD.light, sD.slider, sD.accValX, sD.accValY, sD.accValZ, |
Lokkus | 0:29277ae50860 | 131 | (short)sD.magValX, (short)sD.magValY, (short)sD.magValZ); |
Lokkus | 0:29277ae50860 | 132 | } gLED = LED_OFF; |
Lokkus | 0:29277ae50860 | 133 | |
Lokkus | 0:29277ae50860 | 134 | //disable all SLCD DPs |
Lokkus | 0:29277ae50860 | 135 | slcd.DP(0, false); slcd.DP(1, false); slcd.DP(2, false); |
Lokkus | 0:29277ae50860 | 136 | |
Lokkus | 0:29277ae50860 | 137 | |
Lokkus | 0:29277ae50860 | 138 | //acc: z-axis 1g min-blinking//acc: z-axis 1g min-blinking |
Lokkus | 0:29277ae50860 | 139 | |
Lokkus | 0:29277ae50860 | 140 | rLED = abs(sD.accValZ); |
Lokkus | 0:29277ae50860 | 141 | |
Lokkus | 0:29277ae50860 | 142 | if (sD.slider) |
Lokkus | 0:29277ae50860 | 143 | slcd.printf(" %3.0f", sD.slider); //night->light .. 1->0 |
Lokkus | 0:29277ae50860 | 144 | else |
Lokkus | 0:29277ae50860 | 145 | slcd.printf("%1.3f", sD.light); //left->right .. 0->100% |
Lokkus | 0:29277ae50860 | 146 | |
Lokkus | 0:29277ae50860 | 147 | |
Lokkus | 0:29277ae50860 | 148 | wait(0.05); //wait 50ms |
Lokkus | 0:29277ae50860 | 149 | } |
Lokkus | 0:29277ae50860 | 150 | } |
Lokkus | 0:29277ae50860 | 151 | |
Lokkus | 0:29277ae50860 | 152 | |
Lokkus | 0:29277ae50860 | 153 | |
Lokkus | 0:29277ae50860 | 154 | |
Lokkus | 0:29277ae50860 | 155 | |
Lokkus | 0:29277ae50860 | 156 | //_____________________________________________________________// |
Lokkus | 0:29277ae50860 | 157 | //======== FUNC() =============================================// |
Lokkus | 0:29277ae50860 | 158 | //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// |
Lokkus | 0:29277ae50860 | 159 | //Lab2add |
Lokkus | 0:29277ae50860 | 160 | void SLCD_blinking_msg_wait(char *slcd_msg1, char *slcd_msg2) |
Lokkus | 0:29277ae50860 | 161 | { |
Lokkus | 0:29277ae50860 | 162 | char wait4sw1=0; //~500ms blinking |
Lokkus | 0:29277ae50860 | 163 | slcd.CharPosition=0; |
Lokkus | 0:29277ae50860 | 164 | //wait for Press SW1 - to start mag calibration |
Lokkus | 0:29277ae50860 | 165 | while(sw1 == PRESS_ON); //wait for release |
Lokkus | 0:29277ae50860 | 166 | while(sw1 == PRESS_OFF) { //wait for press |
Lokkus | 0:29277ae50860 | 167 | if (++wait4sw1 < 150) //300ms |
Lokkus | 0:29277ae50860 | 168 | slcd.printf(slcd_msg1); |
Lokkus | 0:29277ae50860 | 169 | else //200ms |
Lokkus | 0:29277ae50860 | 170 | slcd.printf(slcd_msg2); |
Lokkus | 0:29277ae50860 | 171 | wait(0.002); |
Lokkus | 0:29277ae50860 | 172 | slcd.CharPosition=0; |
Lokkus | 0:29277ae50860 | 173 | } |
Lokkus | 0:29277ae50860 | 174 | while(sw1 == PRESS_ON); //wait for release |
Lokkus | 0:29277ae50860 | 175 | } |
Lokkus | 0:29277ae50860 | 176 |