Working read code with mode button
Dependencies: SDFileSystem emic2 mbed-rtos mbed
Fork of BAT_senior_design_Testnew by
button.cpp@1:f3d363ca2343, 2017-12-04 (annotated)
- Committer:
- nnguyen45
- Date:
- Mon Dec 04 20:14:24 2017 +0000
- Revision:
- 1:f3d363ca2343
- Parent:
- 0:5887cb744114
- Child:
- 43:82d67fb53e61
Updated button array to include almost all letters
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nnguyen45 | 0:5887cb744114 | 1 | #include "mbed.h" |
nnguyen45 | 0:5887cb744114 | 2 | #include "button.h" |
nnguyen45 | 0:5887cb744114 | 3 | #include "emic2.h" |
nnguyen45 | 0:5887cb744114 | 4 | |
nnguyen45 | 0:5887cb744114 | 5 | //emic2 myTTS(p28, p27); //serial RX,TX pins to emic |
nnguyen45 | 0:5887cb744114 | 6 | DigitalOut led4(LED4); |
nnguyen45 | 0:5887cb744114 | 7 | DigitalOut led3(LED3); |
nnguyen45 | 0:5887cb744114 | 8 | DigitalOut led2(LED2); |
nnguyen45 | 0:5887cb744114 | 9 | |
nnguyen45 | 0:5887cb744114 | 10 | // button constructor |
nnguyen45 | 0:5887cb744114 | 11 | button::button(PwmOut servo, DigitalIn pb, int id) |
nnguyen45 | 0:5887cb744114 | 12 | : servo(servo), pb(pb), state(0), press(0), id(id) {} |
nnguyen45 | 0:5887cb744114 | 13 | |
nnguyen45 | 0:5887cb744114 | 14 | // FUNCTIONS |
nnguyen45 | 0:5887cb744114 | 15 | // get servo pin |
nnguyen45 | 0:5887cb744114 | 16 | PwmOut button::getServoPin() |
nnguyen45 | 0:5887cb744114 | 17 | { |
nnguyen45 | 0:5887cb744114 | 18 | return servo; |
nnguyen45 | 0:5887cb744114 | 19 | } |
nnguyen45 | 0:5887cb744114 | 20 | |
nnguyen45 | 1:f3d363ca2343 | 21 | // set button state |
nnguyen45 | 0:5887cb744114 | 22 | void button::setState(int mystate) |
nnguyen45 | 0:5887cb744114 | 23 | { |
nnguyen45 | 0:5887cb744114 | 24 | state = mystate; |
nnguyen45 | 0:5887cb744114 | 25 | } |
nnguyen45 | 0:5887cb744114 | 26 | |
nnguyen45 | 0:5887cb744114 | 27 | void button::setPress(int mypress) |
nnguyen45 | 0:5887cb744114 | 28 | { |
nnguyen45 | 0:5887cb744114 | 29 | press = mypress; |
nnguyen45 | 0:5887cb744114 | 30 | } |
nnguyen45 | 0:5887cb744114 | 31 | |
nnguyen45 | 0:5887cb744114 | 32 | // get current state of the button |
nnguyen45 | 0:5887cb744114 | 33 | int button::getState() |
nnguyen45 | 0:5887cb744114 | 34 | { |
nnguyen45 | 0:5887cb744114 | 35 | return state; |
nnguyen45 | 0:5887cb744114 | 36 | } |
nnguyen45 | 0:5887cb744114 | 37 | |
nnguyen45 | 0:5887cb744114 | 38 | int button::getID() |
nnguyen45 | 0:5887cb744114 | 39 | { |
nnguyen45 | 0:5887cb744114 | 40 | return id; |
nnguyen45 | 0:5887cb744114 | 41 | } |
nnguyen45 | 0:5887cb744114 | 42 | |
nnguyen45 | 0:5887cb744114 | 43 | |
nnguyen45 | 0:5887cb744114 | 44 | int button::getPress() |
nnguyen45 | 0:5887cb744114 | 45 | { |
nnguyen45 | 0:5887cb744114 | 46 | return press; |
nnguyen45 | 0:5887cb744114 | 47 | } |
nnguyen45 | 0:5887cb744114 | 48 | |
nnguyen45 | 0:5887cb744114 | 49 | // get current state of the button |
nnguyen45 | 0:5887cb744114 | 50 | int button::getLp() |
nnguyen45 | 0:5887cb744114 | 51 | { |
nnguyen45 | 0:5887cb744114 | 52 | return 0; |
nnguyen45 | 0:5887cb744114 | 53 | } |
nnguyen45 | 0:5887cb744114 | 54 | |
nnguyen45 | 0:5887cb744114 | 55 | // move servo into the slot |
nnguyen45 | 0:5887cb744114 | 56 | void button::moveServoIn() |
nnguyen45 | 0:5887cb744114 | 57 | { |
nnguyen45 | 0:5887cb744114 | 58 | // rotate 90 degrees one way |
nnguyen45 | 0:5887cb744114 | 59 | for(int i=4; i<=7; i++) { |
nnguyen45 | 0:5887cb744114 | 60 | servo = i/100.0; |
nnguyen45 | 0:5887cb744114 | 61 | wait(0.01); |
nnguyen45 | 0:5887cb744114 | 62 | } |
nnguyen45 | 0:5887cb744114 | 63 | switch (id) { |
nnguyen45 | 0:5887cb744114 | 64 | case 1: |
nnguyen45 | 0:5887cb744114 | 65 | led2 = 0; |
nnguyen45 | 0:5887cb744114 | 66 | led3 = 0; |
nnguyen45 | 0:5887cb744114 | 67 | led4 = 1; |
nnguyen45 | 0:5887cb744114 | 68 | break; |
nnguyen45 | 0:5887cb744114 | 69 | case 2: |
nnguyen45 | 0:5887cb744114 | 70 | led2 = 0; |
nnguyen45 | 0:5887cb744114 | 71 | led3 = 1; |
nnguyen45 | 0:5887cb744114 | 72 | led4 = 0; |
nnguyen45 | 0:5887cb744114 | 73 | break; |
nnguyen45 | 0:5887cb744114 | 74 | case 3: |
nnguyen45 | 0:5887cb744114 | 75 | led2 = 0; |
nnguyen45 | 0:5887cb744114 | 76 | led3 = 1; |
nnguyen45 | 0:5887cb744114 | 77 | led4 = 1; |
nnguyen45 | 0:5887cb744114 | 78 | break; |
nnguyen45 | 0:5887cb744114 | 79 | case 4: |
nnguyen45 | 0:5887cb744114 | 80 | led2 = 1; |
nnguyen45 | 0:5887cb744114 | 81 | led3 = 0; |
nnguyen45 | 0:5887cb744114 | 82 | led4 = 0; |
nnguyen45 | 0:5887cb744114 | 83 | break; |
nnguyen45 | 0:5887cb744114 | 84 | case 5: |
nnguyen45 | 0:5887cb744114 | 85 | led2 = 1; |
nnguyen45 | 0:5887cb744114 | 86 | led3 = 0; |
nnguyen45 | 0:5887cb744114 | 87 | led4 = 1; |
nnguyen45 | 0:5887cb744114 | 88 | break; |
nnguyen45 | 0:5887cb744114 | 89 | case 6: |
nnguyen45 | 0:5887cb744114 | 90 | led2 = 1; |
nnguyen45 | 0:5887cb744114 | 91 | led3 = 1; |
nnguyen45 | 0:5887cb744114 | 92 | led4 = 0; |
nnguyen45 | 0:5887cb744114 | 93 | break; |
nnguyen45 | 0:5887cb744114 | 94 | } |
nnguyen45 | 0:5887cb744114 | 95 | } |
nnguyen45 | 0:5887cb744114 | 96 | |
nnguyen45 | 0:5887cb744114 | 97 | // move servo out of the slot |
nnguyen45 | 0:5887cb744114 | 98 | void button::moveServoOut() |
nnguyen45 | 0:5887cb744114 | 99 | { |
nnguyen45 | 0:5887cb744114 | 100 | for(int i=7; i>4; i--) { |
nnguyen45 | 0:5887cb744114 | 101 | servo = i/100.0; |
nnguyen45 | 0:5887cb744114 | 102 | wait(0.01); |
nnguyen45 | 0:5887cb744114 | 103 | } |
nnguyen45 | 0:5887cb744114 | 104 | led2 = 0; |
nnguyen45 | 0:5887cb744114 | 105 | led3 = 0; |
nnguyen45 | 0:5887cb744114 | 106 | led4 = 0; |
nnguyen45 | 0:5887cb744114 | 107 | } |
nnguyen45 | 0:5887cb744114 | 108 | |
nnguyen45 | 0:5887cb744114 | 109 | int button::updateState() |
nnguyen45 | 0:5887cb744114 | 110 | { |
nnguyen45 | 0:5887cb744114 | 111 | // state 0 - button is up, pb = 0 |
nnguyen45 | 0:5887cb744114 | 112 | if (pb == 0 && state == 3) { |
nnguyen45 | 0:5887cb744114 | 113 | // nothing happens here, servo is still |
nnguyen45 | 0:5887cb744114 | 114 | state = 0; |
nnguyen45 | 0:5887cb744114 | 115 | } |
nnguyen45 | 0:5887cb744114 | 116 | // state 1 - button is moving down, pb = 1 |
nnguyen45 | 0:5887cb744114 | 117 | if (pb == 1 && state == 0) { |
nnguyen45 | 0:5887cb744114 | 118 | moveServoIn(); |
nnguyen45 | 0:5887cb744114 | 119 | state = 1; |
nnguyen45 | 0:5887cb744114 | 120 | press = 1; |
nnguyen45 | 0:5887cb744114 | 121 | } |
nnguyen45 | 0:5887cb744114 | 122 | // state 2 - button is down, pb = 0 |
nnguyen45 | 0:5887cb744114 | 123 | if (pb == 0 && state == 1) { |
nnguyen45 | 0:5887cb744114 | 124 | // nothing happens here, servo is still |
nnguyen45 | 0:5887cb744114 | 125 | state = 2; |
nnguyen45 | 0:5887cb744114 | 126 | } |
nnguyen45 | 0:5887cb744114 | 127 | // state 3 - button is moving up, pb = 1 |
nnguyen45 | 0:5887cb744114 | 128 | if (pb == 1 && state == 2) { |
nnguyen45 | 0:5887cb744114 | 129 | moveServoOut(); |
nnguyen45 | 0:5887cb744114 | 130 | state = 3; |
nnguyen45 | 0:5887cb744114 | 131 | press = 0; |
nnguyen45 | 0:5887cb744114 | 132 | } |
nnguyen45 | 0:5887cb744114 | 133 | // state 4 - handle debouncing while button is down |
nnguyen45 | 0:5887cb744114 | 134 | return state; |
nnguyen45 | 0:5887cb744114 | 135 | } |
nnguyen45 | 0:5887cb744114 | 136 | |
nnguyen45 | 0:5887cb744114 | 137 | void button::setup() { |
nnguyen45 | 0:5887cb744114 | 138 | for(int i=0; i<=4; i++) { |
nnguyen45 | 0:5887cb744114 | 139 | servo = i/100.0; |
nnguyen45 | 0:5887cb744114 | 140 | wait(0.01); |
nnguyen45 | 0:5887cb744114 | 141 | } |
nnguyen45 | 0:5887cb744114 | 142 | } |