Basic example showing how to drive the 2 LEDs and read state of Joystick button.
Dependencies: mbed
main.cpp@1:29e159d631f5, 2017-06-06 (annotated)
- Committer:
- arostm
- Date:
- Tue Jun 06 14:41:33 2017 +0200
- Revision:
- 1:29e159d631f5
- Parent:
- 0:267836e77e0c
Adding new mbed library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bcostm | 0:267836e77e0c | 1 | #include "mbed.h" |
bcostm | 0:267836e77e0c | 2 | |
bcostm | 0:267836e77e0c | 3 | #define USE_PRINTF (1) |
bcostm | 0:267836e77e0c | 4 | |
bcostm | 0:267836e77e0c | 5 | // Joystick button |
bcostm | 0:267836e77e0c | 6 | InterruptIn center(JOYSTICK_CENTER); |
bcostm | 0:267836e77e0c | 7 | InterruptIn left(JOYSTICK_LEFT); |
bcostm | 0:267836e77e0c | 8 | InterruptIn right(JOYSTICK_RIGHT); |
bcostm | 0:267836e77e0c | 9 | InterruptIn up(JOYSTICK_UP); |
bcostm | 0:267836e77e0c | 10 | InterruptIn down(JOYSTICK_DOWN); |
bcostm | 0:267836e77e0c | 11 | |
bcostm | 0:267836e77e0c | 12 | // LEDs |
bcostm | 0:267836e77e0c | 13 | DigitalOut led_green(LED1); |
bcostm | 0:267836e77e0c | 14 | DigitalOut led_red(LED2); |
bcostm | 0:267836e77e0c | 15 | |
bcostm | 0:267836e77e0c | 16 | void center_released() { |
bcostm | 0:267836e77e0c | 17 | led_green = 0; |
bcostm | 0:267836e77e0c | 18 | #if USE_PRINTF == 1 |
bcostm | 0:267836e77e0c | 19 | printf("center button released\n"); |
bcostm | 0:267836e77e0c | 20 | #endif |
bcostm | 0:267836e77e0c | 21 | } |
bcostm | 0:267836e77e0c | 22 | |
bcostm | 0:267836e77e0c | 23 | void center_pressed() { |
bcostm | 0:267836e77e0c | 24 | led_green = 1; |
bcostm | 0:267836e77e0c | 25 | #if USE_PRINTF == 1 |
bcostm | 0:267836e77e0c | 26 | printf("center button pressed\n"); |
bcostm | 0:267836e77e0c | 27 | #endif |
bcostm | 0:267836e77e0c | 28 | } |
bcostm | 0:267836e77e0c | 29 | |
bcostm | 0:267836e77e0c | 30 | void left_released() { |
bcostm | 0:267836e77e0c | 31 | led_green = 0; |
bcostm | 0:267836e77e0c | 32 | #if USE_PRINTF == 1 |
bcostm | 0:267836e77e0c | 33 | printf("left button released\n"); |
bcostm | 0:267836e77e0c | 34 | #endif |
bcostm | 0:267836e77e0c | 35 | } |
bcostm | 0:267836e77e0c | 36 | |
bcostm | 0:267836e77e0c | 37 | void left_pressed() { |
bcostm | 0:267836e77e0c | 38 | led_green = 1; |
bcostm | 0:267836e77e0c | 39 | #if USE_PRINTF == 1 |
bcostm | 0:267836e77e0c | 40 | printf("left button pressed\n"); |
bcostm | 0:267836e77e0c | 41 | #endif |
bcostm | 0:267836e77e0c | 42 | } |
bcostm | 0:267836e77e0c | 43 | |
bcostm | 0:267836e77e0c | 44 | void right_released() { |
bcostm | 0:267836e77e0c | 45 | led_green = 0; |
bcostm | 0:267836e77e0c | 46 | #if USE_PRINTF == 1 |
bcostm | 0:267836e77e0c | 47 | printf("right button released\n"); |
bcostm | 0:267836e77e0c | 48 | #endif |
bcostm | 0:267836e77e0c | 49 | } |
bcostm | 0:267836e77e0c | 50 | |
bcostm | 0:267836e77e0c | 51 | void right_pressed() { |
bcostm | 0:267836e77e0c | 52 | led_green = 1; |
bcostm | 0:267836e77e0c | 53 | #if USE_PRINTF == 1 |
bcostm | 0:267836e77e0c | 54 | printf("right button pressed\n"); |
bcostm | 0:267836e77e0c | 55 | #endif |
bcostm | 0:267836e77e0c | 56 | } |
bcostm | 0:267836e77e0c | 57 | |
bcostm | 0:267836e77e0c | 58 | void up_released() { |
bcostm | 0:267836e77e0c | 59 | led_green = 0; |
bcostm | 0:267836e77e0c | 60 | #if USE_PRINTF == 1 |
bcostm | 0:267836e77e0c | 61 | printf("up button released\n"); |
bcostm | 0:267836e77e0c | 62 | #endif |
bcostm | 0:267836e77e0c | 63 | } |
bcostm | 0:267836e77e0c | 64 | |
bcostm | 0:267836e77e0c | 65 | void up_pressed() { |
bcostm | 0:267836e77e0c | 66 | led_green = 1; |
bcostm | 0:267836e77e0c | 67 | #if USE_PRINTF == 1 |
bcostm | 0:267836e77e0c | 68 | printf("up button pressed\n"); |
bcostm | 0:267836e77e0c | 69 | #endif |
bcostm | 0:267836e77e0c | 70 | } |
bcostm | 0:267836e77e0c | 71 | |
bcostm | 0:267836e77e0c | 72 | void down_released() { |
bcostm | 0:267836e77e0c | 73 | led_green = 0; |
bcostm | 0:267836e77e0c | 74 | #if USE_PRINTF == 1 |
bcostm | 0:267836e77e0c | 75 | printf("down button released\n"); |
bcostm | 0:267836e77e0c | 76 | #endif |
bcostm | 0:267836e77e0c | 77 | } |
bcostm | 0:267836e77e0c | 78 | |
bcostm | 0:267836e77e0c | 79 | void down_pressed() { |
bcostm | 0:267836e77e0c | 80 | led_green = 1; |
bcostm | 0:267836e77e0c | 81 | #if USE_PRINTF == 1 |
bcostm | 0:267836e77e0c | 82 | printf("down button pressed\n"); |
bcostm | 0:267836e77e0c | 83 | #endif |
bcostm | 0:267836e77e0c | 84 | } |
bcostm | 0:267836e77e0c | 85 | |
bcostm | 0:267836e77e0c | 86 | int main() { |
bcostm | 0:267836e77e0c | 87 | |
bcostm | 0:267836e77e0c | 88 | #if USE_PRINTF == 1 |
bcostm | 0:267836e77e0c | 89 | int i = 0; |
bcostm | 0:267836e77e0c | 90 | printf("\nStart!\n"); |
bcostm | 0:267836e77e0c | 91 | #endif |
bcostm | 0:267836e77e0c | 92 | |
bcostm | 0:267836e77e0c | 93 | // Both rise and fall edges generate an interrupt |
bcostm | 0:267836e77e0c | 94 | center.fall(¢er_released); |
bcostm | 0:267836e77e0c | 95 | center.rise(¢er_pressed); |
bcostm | 0:267836e77e0c | 96 | left.fall(&left_released); |
bcostm | 0:267836e77e0c | 97 | left.rise(&left_pressed); |
bcostm | 0:267836e77e0c | 98 | right.fall(&right_released); |
bcostm | 0:267836e77e0c | 99 | right.rise(&right_pressed); |
bcostm | 0:267836e77e0c | 100 | up.fall(&up_released); |
bcostm | 0:267836e77e0c | 101 | up.rise(&up_pressed); |
bcostm | 0:267836e77e0c | 102 | down.fall(&down_released); |
bcostm | 0:267836e77e0c | 103 | down.rise(&down_pressed); |
bcostm | 0:267836e77e0c | 104 | |
bcostm | 0:267836e77e0c | 105 | // Add pull-down on these pins |
bcostm | 0:267836e77e0c | 106 | // Warning: must be done AFTER edges setting |
bcostm | 0:267836e77e0c | 107 | left.mode(PullDown); |
bcostm | 0:267836e77e0c | 108 | right.mode(PullDown); |
bcostm | 0:267836e77e0c | 109 | up.mode(PullDown); |
bcostm | 0:267836e77e0c | 110 | down.mode(PullDown); |
bcostm | 0:267836e77e0c | 111 | |
bcostm | 0:267836e77e0c | 112 | while (1) { |
bcostm | 0:267836e77e0c | 113 | led_red = !led_red; |
bcostm | 0:267836e77e0c | 114 | wait(1.0); |
bcostm | 0:267836e77e0c | 115 | #if USE_PRINTF == 1 |
bcostm | 0:267836e77e0c | 116 | printf("main %d\n", i++); |
bcostm | 0:267836e77e0c | 117 | #endif |
bcostm | 0:267836e77e0c | 118 | } |
bcostm | 0:267836e77e0c | 119 | } |