My take on a demo for the Freescale FRDM KL25Z board showing off it's main features all at once together with USB CDC serial.
Dependencies: FRDM_MMA8451Q TSI USBDevice mbed
main.cpp@5:55ef207399fb, 2014-01-07 (annotated)
- Committer:
- shutay
- Date:
- Tue Jan 07 09:36:19 2014 +0000
- Revision:
- 5:55ef207399fb
- Parent:
- 4:85ecc94a7643
Added API documentation.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shutay | 4:85ecc94a7643 | 1 | //*********************************************************** |
shutay | 4:85ecc94a7643 | 2 | // Freescale Freedom KL25Z Demo |
shutay | 4:85ecc94a7643 | 3 | // Jason CJ Tay (jason.tay@vagler.com) |
shutay | 4:85ecc94a7643 | 4 | // 30 October 2013 |
shutay | 4:85ecc94a7643 | 5 | //*********************************************************** |
shutay | 4:85ecc94a7643 | 6 | // Key things demo'd: |
shutay | 4:85ecc94a7643 | 7 | // 1. USB application serial, USB CDC serial, not over the debug port. |
shutay | 4:85ecc94a7643 | 8 | // 2. Capacitive Touch |
shutay | 4:85ecc94a7643 | 9 | // 3. Accelerometer |
shutay | 4:85ecc94a7643 | 10 | // 4. RGB LED |
shutay | 4:85ecc94a7643 | 11 | //*********************************************************** |
emilmont | 0:f59179afee57 | 12 | |
shutay | 4:85ecc94a7643 | 13 | #include "mbed.h" |
shutay | 4:85ecc94a7643 | 14 | #include "USBSerial.h" |
shutay | 4:85ecc94a7643 | 15 | #include "TSISensor.h" |
shutay | 4:85ecc94a7643 | 16 | #include "MMA8451Q.h" |
shutay | 4:85ecc94a7643 | 17 | |
shutay | 4:85ecc94a7643 | 18 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
emilmont | 0:f59179afee57 | 19 | |
shutay | 5:55ef207399fb | 20 | /// Freescale Freedom KL25Z board has an RGB LED on it. |
shutay | 4:85ecc94a7643 | 21 | DigitalOut ledRed(LED_RED); |
shutay | 4:85ecc94a7643 | 22 | DigitalOut ledGreen(LED_GREEN); |
shutay | 4:85ecc94a7643 | 23 | DigitalOut ledBlue(LED_BLUE); |
shutay | 4:85ecc94a7643 | 24 | |
shutay | 5:55ef207399fb | 25 | /** Setup the serial over USB virtual COM port. |
shutay | 5:55ef207399fb | 26 | * Note that the connection is made over the application USB port, NOT the CMSIS-DAP USB port. |
shutay | 5:55ef207399fb | 27 | * If you would like to use the debug/programmer USB port instead, the the following code: |
shutay | 5:55ef207399fb | 28 | * |
shutay | 5:55ef207399fb | 29 | * @code |
shutay | 5:55ef207399fb | 30 | * Serial pc(USBTX,USBRX); |
shutay | 5:55ef207399fb | 31 | * @endcode |
shutay | 5:55ef207399fb | 32 | * |
shutay | 5:55ef207399fb | 33 | * Notice that the default mbed USB CDC device will block on this declaration until you attach the KL25Z board |
shutay | 5:55ef207399fb | 34 | * to your PC's USB port and the device enumerates. After that, program execution will begin. |
shutay | 5:55ef207399fb | 35 | */ |
shutay | 4:85ecc94a7643 | 36 | USBSerial pc; |
shutay | 4:85ecc94a7643 | 37 | |
shutay | 4:85ecc94a7643 | 38 | int main() |
shutay | 4:85ecc94a7643 | 39 | { |
shutay | 4:85ecc94a7643 | 40 | char c; |
emilmont | 0:f59179afee57 | 41 | int i=0; |
shutay | 5:55ef207399fb | 42 | /// Capacitive touch sensor, defined as tsi. |
shutay | 4:85ecc94a7643 | 43 | TSISensor tsi; |
shutay | 5:55ef207399fb | 44 | /// 3-axis MEMS accelerometer. |
shutay | 4:85ecc94a7643 | 45 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
shutay | 4:85ecc94a7643 | 46 | |
emilmont | 0:f59179afee57 | 47 | pc.printf("\nHello World!\n"); |
shutay | 4:85ecc94a7643 | 48 | |
emilmont | 1:32eacc4f6beb | 49 | while (true) { |
shutay | 4:85ecc94a7643 | 50 | pc.printf("\nTell me what you want!\nPress 't' to read the capacitive touch slider...\nPress 'a' to read the accelerometer...\n"); |
shutay | 4:85ecc94a7643 | 51 | pc.printf("Press 'r' to turn the LED red...\nPress 'g' to turn the LED green...\nPress 'b' to turn the LED blue...\n>>>\n"); |
shutay | 4:85ecc94a7643 | 52 | c = pc.getc(); |
shutay | 4:85ecc94a7643 | 53 | switch(c) { |
shutay | 4:85ecc94a7643 | 54 | case 't': |
shutay | 4:85ecc94a7643 | 55 | pc.printf("The current touch sensor reading is %f\n", tsi.readPercentage()); |
shutay | 4:85ecc94a7643 | 56 | break; |
shutay | 4:85ecc94a7643 | 57 | case 'a': |
shutay | 4:85ecc94a7643 | 58 | pc.printf("X: %f\nY: %f\nZ: %f\n", acc.getAccX(), acc.getAccY(), acc.getAccZ()); |
shutay | 4:85ecc94a7643 | 59 | break; |
shutay | 4:85ecc94a7643 | 60 | case 'r': |
shutay | 4:85ecc94a7643 | 61 | for(int j=0; j<4; j++) { |
shutay | 4:85ecc94a7643 | 62 | wait(0.5); |
shutay | 4:85ecc94a7643 | 63 | pc.printf("%d\n",i); |
shutay | 4:85ecc94a7643 | 64 | i++; |
shutay | 4:85ecc94a7643 | 65 | ledRed = !ledRed; |
shutay | 4:85ecc94a7643 | 66 | } |
shutay | 4:85ecc94a7643 | 67 | break; |
shutay | 4:85ecc94a7643 | 68 | case 'g': |
shutay | 4:85ecc94a7643 | 69 | for(int j=0; j<4; j++) { |
shutay | 4:85ecc94a7643 | 70 | wait(0.5); |
shutay | 4:85ecc94a7643 | 71 | pc.printf("%d\n",i); |
shutay | 4:85ecc94a7643 | 72 | i++; |
shutay | 4:85ecc94a7643 | 73 | ledGreen = !ledGreen; |
shutay | 4:85ecc94a7643 | 74 | } |
shutay | 4:85ecc94a7643 | 75 | break; |
shutay | 4:85ecc94a7643 | 76 | case 'b': |
shutay | 4:85ecc94a7643 | 77 | for(int j=0; j<4; j++) { |
shutay | 4:85ecc94a7643 | 78 | wait(0.5); |
shutay | 4:85ecc94a7643 | 79 | pc.printf("%d\n",i); |
shutay | 4:85ecc94a7643 | 80 | i++; |
shutay | 4:85ecc94a7643 | 81 | ledBlue = !ledBlue; |
shutay | 4:85ecc94a7643 | 82 | } |
shutay | 4:85ecc94a7643 | 83 | break; |
shutay | 4:85ecc94a7643 | 84 | default: |
shutay | 4:85ecc94a7643 | 85 | pc.printf("Oops. Don't have anything for you. Please try again.\n\n"); |
shutay | 4:85ecc94a7643 | 86 | } |
emilmont | 0:f59179afee57 | 87 | } |
emilmont | 0:f59179afee57 | 88 | } |