Andrew Bell
/
lab2_button
nearly working
main.cpp@1:093493ce0f37, 2021-02-09 (annotated)
- Committer:
- andrewbw01
- Date:
- Tue Feb 09 16:53:53 2021 +0000
- Revision:
- 1:093493ce0f37
- Parent:
- 0:4592a95188f3
almost working
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewbw01 | 0:4592a95188f3 | 1 | #include "mbed.h" |
andrewbw01 | 1:093493ce0f37 | 2 | #include <string> |
andrewbw01 | 1:093493ce0f37 | 3 | #include <iostream> |
andrewbw01 | 1:093493ce0f37 | 4 | using namespace std; |
andrewbw01 | 0:4592a95188f3 | 5 | |
andrewbw01 | 0:4592a95188f3 | 6 | BusOut LED_Disp(p7,p11,p9,p8,p5,p6,p10,p12); |
andrewbw01 | 0:4592a95188f3 | 7 | |
andrewbw01 | 1:093493ce0f37 | 8 | InterruptIn button1(p14); |
andrewbw01 | 1:093493ce0f37 | 9 | InterruptIn button2(p15); |
andrewbw01 | 1:093493ce0f37 | 10 | volatile bool button1Up = true; |
andrewbw01 | 1:093493ce0f37 | 11 | volatile bool button2Up = true; |
andrewbw01 | 1:093493ce0f37 | 12 | volatile unsigned int counter = 0; |
andrewbw01 | 1:093493ce0f37 | 13 | |
andrewbw01 | 1:093493ce0f37 | 14 | void DisplayNumber(int num, BusOut &Bus) |
andrewbw01 | 1:093493ce0f37 | 15 | { |
andrewbw01 | 1:093493ce0f37 | 16 | num = num % 10; |
andrewbw01 | 0:4592a95188f3 | 17 | |
andrewbw01 | 1:093493ce0f37 | 18 | switch(num) |
andrewbw01 | 1:093493ce0f37 | 19 | { |
andrewbw01 | 1:093493ce0f37 | 20 | case 0: |
andrewbw01 | 1:093493ce0f37 | 21 | LED_Disp = ~0x3F; |
andrewbw01 | 1:093493ce0f37 | 22 | break; |
andrewbw01 | 1:093493ce0f37 | 23 | case 1: |
andrewbw01 | 1:093493ce0f37 | 24 | LED_Disp = ~0x06; |
andrewbw01 | 1:093493ce0f37 | 25 | break; |
andrewbw01 | 1:093493ce0f37 | 26 | case 2: |
andrewbw01 | 1:093493ce0f37 | 27 | LED_Disp = ~0x5B; |
andrewbw01 | 1:093493ce0f37 | 28 | break; |
andrewbw01 | 1:093493ce0f37 | 29 | case 3: |
andrewbw01 | 1:093493ce0f37 | 30 | LED_Disp = ~0x4F; |
andrewbw01 | 1:093493ce0f37 | 31 | break; |
andrewbw01 | 1:093493ce0f37 | 32 | case 4: |
andrewbw01 | 1:093493ce0f37 | 33 | LED_Disp = ~0x66; |
andrewbw01 | 1:093493ce0f37 | 34 | break; |
andrewbw01 | 1:093493ce0f37 | 35 | case 5: |
andrewbw01 | 1:093493ce0f37 | 36 | LED_Disp = ~0x6D; |
andrewbw01 | 1:093493ce0f37 | 37 | break; |
andrewbw01 | 1:093493ce0f37 | 38 | case 6: |
andrewbw01 | 1:093493ce0f37 | 39 | LED_Disp = ~0x7D; |
andrewbw01 | 1:093493ce0f37 | 40 | break; |
andrewbw01 | 1:093493ce0f37 | 41 | case 7: |
andrewbw01 | 1:093493ce0f37 | 42 | LED_Disp = ~0x07; |
andrewbw01 | 1:093493ce0f37 | 43 | break; |
andrewbw01 | 1:093493ce0f37 | 44 | case 8: |
andrewbw01 | 1:093493ce0f37 | 45 | LED_Disp = ~0x7F; |
andrewbw01 | 1:093493ce0f37 | 46 | break; |
andrewbw01 | 1:093493ce0f37 | 47 | case 9: |
andrewbw01 | 1:093493ce0f37 | 48 | LED_Disp = ~0x67; |
andrewbw01 | 1:093493ce0f37 | 49 | break; |
andrewbw01 | 1:093493ce0f37 | 50 | } |
andrewbw01 | 1:093493ce0f37 | 51 | } |
andrewbw01 | 1:093493ce0f37 | 52 | |
andrewbw01 | 1:093493ce0f37 | 53 | void up1(){ |
andrewbw01 | 1:093493ce0f37 | 54 | button1Up = true; |
andrewbw01 | 1:093493ce0f37 | 55 | } |
andrewbw01 | 1:093493ce0f37 | 56 | |
andrewbw01 | 1:093493ce0f37 | 57 | void down1(){ |
andrewbw01 | 1:093493ce0f37 | 58 | button1Up = false; |
andrewbw01 | 1:093493ce0f37 | 59 | } |
andrewbw01 | 1:093493ce0f37 | 60 | |
andrewbw01 | 1:093493ce0f37 | 61 | void up2(){ |
andrewbw01 | 1:093493ce0f37 | 62 | button2Up = true; |
andrewbw01 | 1:093493ce0f37 | 63 | } |
andrewbw01 | 1:093493ce0f37 | 64 | |
andrewbw01 | 1:093493ce0f37 | 65 | void down2(){ |
andrewbw01 | 1:093493ce0f37 | 66 | button2Up = false; |
andrewbw01 | 0:4592a95188f3 | 67 | } |
andrewbw01 | 0:4592a95188f3 | 68 | |
andrewbw01 | 0:4592a95188f3 | 69 | int main() |
andrewbw01 | 0:4592a95188f3 | 70 | { |
andrewbw01 | 1:093493ce0f37 | 71 | button1.rise(&down1); |
andrewbw01 | 1:093493ce0f37 | 72 | button1.fall(&up1); |
andrewbw01 | 1:093493ce0f37 | 73 | button2.rise(&down2); |
andrewbw01 | 1:093493ce0f37 | 74 | button2.fall(&up2); |
andrewbw01 | 1:093493ce0f37 | 75 | |
andrewbw01 | 1:093493ce0f37 | 76 | while(1){ |
andrewbw01 | 1:093493ce0f37 | 77 | LED_Disp = -1; |
andrewbw01 | 1:093493ce0f37 | 78 | while(!button1Up) |
andrewbw01 | 1:093493ce0f37 | 79 | { |
andrewbw01 | 1:093493ce0f37 | 80 | DisplayNumber(counter, LED_Disp); |
andrewbw01 | 1:093493ce0f37 | 81 | counter++; |
andrewbw01 | 1:093493ce0f37 | 82 | |
andrewbw01 | 1:093493ce0f37 | 83 | } |
andrewbw01 | 1:093493ce0f37 | 84 | while(!button2Up) |
andrewbw01 | 1:093493ce0f37 | 85 | { |
andrewbw01 | 1:093493ce0f37 | 86 | DisplayNumber(counter, LED_Disp); |
andrewbw01 | 1:093493ce0f37 | 87 | counter--; |
andrewbw01 | 1:093493ce0f37 | 88 | |
andrewbw01 | 1:093493ce0f37 | 89 | } |
andrewbw01 | 0:4592a95188f3 | 90 | } |
andrewbw01 | 1:093493ce0f37 | 91 | |
andrewbw01 | 1:093493ce0f37 | 92 | |
andrewbw01 | 0:4592a95188f3 | 93 | } |
andrewbw01 | 1:093493ce0f37 | 94 | |
andrewbw01 | 1:093493ce0f37 | 95 | |
andrewbw01 | 1:093493ce0f37 | 96 | |
andrewbw01 | 1:093493ce0f37 | 97 | |
andrewbw01 | 1:093493ce0f37 | 98 | |
andrewbw01 | 1:093493ce0f37 | 99 | |
andrewbw01 | 0:4592a95188f3 | 100 | |
andrewbw01 | 1:093493ce0f37 | 101 | |
andrewbw01 | 1:093493ce0f37 | 102 | |
andrewbw01 | 1:093493ce0f37 | 103 | |
andrewbw01 | 0:4592a95188f3 | 104 |