Fork of original senior design repo
Dependencies: SDFileSystem mbed-rtos mbed wave_player emic2
Fork of BAT_senior_design by
button.cpp@23:b2e53ce54b3e, 2017-11-10 (annotated)
- Committer:
- aismail1997
- Date:
- Fri Nov 10 16:22:04 2017 +0000
- Revision:
- 23:b2e53ce54b3e
- Parent:
- 22:23970cf718ee
added iterators for the arrays
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aismail1997 | 10:21268d8bf979 | 1 | #include "mbed.h" |
aismail1997 | 10:21268d8bf979 | 2 | #include "button.h" |
aismail1997 | 21:6931917c70cd | 3 | #include "emic2.h" |
aismail1997 | 21:6931917c70cd | 4 | |
aismail1997 | 22:23970cf718ee | 5 | //emic2 myTTS(p28, p27); //serial RX,TX pins to emic |
aismail1997 | 21:6931917c70cd | 6 | //DigitalOut led4(LED4); |
aismail1997 | 10:21268d8bf979 | 7 | |
aismail1997 | 12:b80dde24e9bc | 8 | // button constructor |
aismail1997 | 20:c5df903f068a | 9 | button::button(PwmOut servo, DigitalIn pb, int id) |
aismail1997 | 21:6931917c70cd | 10 | : servo(servo), pb(pb), state(0), press(0), id(id) {} |
aismail1997 | 20:c5df903f068a | 11 | /*button::button(PwmOut servo, DigitalIn pb, AnalogIn lp) |
aismail1997 | 20:c5df903f068a | 12 | : servo(servo), pb(pb), linpot(lp), press(0), state(0) {} |
aismail1997 | 20:c5df903f068a | 13 | */ |
aismail1997 | 10:21268d8bf979 | 14 | |
aismail1997 | 10:21268d8bf979 | 15 | // FUNCTIONS |
aismail1997 | 12:b80dde24e9bc | 16 | |
aismail1997 | 12:b80dde24e9bc | 17 | // get servo pin |
aismail1997 | 10:21268d8bf979 | 18 | PwmOut button::getServoPin() |
aismail1997 | 10:21268d8bf979 | 19 | { |
aismail1997 | 10:21268d8bf979 | 20 | return servo; |
aismail1997 | 10:21268d8bf979 | 21 | } |
aismail1997 | 10:21268d8bf979 | 22 | |
aismail1997 | 13:581a3b02f4c3 | 23 | // get servo pin |
aismail1997 | 18:ceac47be2e64 | 24 | void button::setState(int mystate) |
aismail1997 | 13:581a3b02f4c3 | 25 | { |
aismail1997 | 13:581a3b02f4c3 | 26 | state = mystate; |
aismail1997 | 13:581a3b02f4c3 | 27 | } |
aismail1997 | 13:581a3b02f4c3 | 28 | |
aismail1997 | 20:c5df903f068a | 29 | void button::setPress(int mypress) |
aismail1997 | 20:c5df903f068a | 30 | { |
aismail1997 | 20:c5df903f068a | 31 | press = mypress; |
aismail1997 | 20:c5df903f068a | 32 | } |
aismail1997 | 20:c5df903f068a | 33 | |
aismail1997 | 13:581a3b02f4c3 | 34 | // get servo pin |
aismail1997 | 18:ceac47be2e64 | 35 | /*void button::setMode(int mymode) |
aismail1997 | 13:581a3b02f4c3 | 36 | { |
aismail1997 | 13:581a3b02f4c3 | 37 | mode = mymode; |
aismail1997 | 13:581a3b02f4c3 | 38 | }*/ |
aismail1997 | 13:581a3b02f4c3 | 39 | |
aismail1997 | 12:b80dde24e9bc | 40 | // get current state of the button |
aismail1997 | 18:ceac47be2e64 | 41 | int button::getState() |
aismail1997 | 10:21268d8bf979 | 42 | { |
aismail1997 | 10:21268d8bf979 | 43 | return state; |
aismail1997 | 18:ceac47be2e64 | 44 | } |
aismail1997 | 18:ceac47be2e64 | 45 | |
aismail1997 | 20:c5df903f068a | 46 | int button::getID() |
aismail1997 | 20:c5df903f068a | 47 | { |
aismail1997 | 20:c5df903f068a | 48 | return id; |
aismail1997 | 20:c5df903f068a | 49 | } |
aismail1997 | 20:c5df903f068a | 50 | |
aismail1997 | 20:c5df903f068a | 51 | |
aismail1997 | 18:ceac47be2e64 | 52 | int button::getPress() |
aismail1997 | 18:ceac47be2e64 | 53 | { |
aismail1997 | 18:ceac47be2e64 | 54 | return press; |
aismail1997 | 18:ceac47be2e64 | 55 | } |
aismail1997 | 18:ceac47be2e64 | 56 | |
aismail1997 | 20:c5df903f068a | 57 | // get current state of the button |
aismail1997 | 20:c5df903f068a | 58 | int button::getLp() |
aismail1997 | 20:c5df903f068a | 59 | { |
aismail1997 | 21:6931917c70cd | 60 | /* if (linpot < 2) |
aismail1997 | 21:6931917c70cd | 61 | return 1; |
aismail1997 | 21:6931917c70cd | 62 | else*/ |
aismail1997 | 21:6931917c70cd | 63 | return 0; |
aismail1997 | 20:c5df903f068a | 64 | } |
aismail1997 | 10:21268d8bf979 | 65 | |
aismail1997 | 12:b80dde24e9bc | 66 | // move servo into the slot |
aismail1997 | 10:21268d8bf979 | 67 | void button::moveServoIn() |
aismail1997 | 10:21268d8bf979 | 68 | { |
aismail1997 | 10:21268d8bf979 | 69 | //myled = 1; |
aismail1997 | 10:21268d8bf979 | 70 | // rotate 90 degrees one way |
aismail1997 | 10:21268d8bf979 | 71 | for(int i=3; i<=7; i++) { |
aismail1997 | 10:21268d8bf979 | 72 | servo = i/100.0; |
aismail1997 | 10:21268d8bf979 | 73 | wait(0.01); |
aismail1997 | 10:21268d8bf979 | 74 | } |
aismail1997 | 10:21268d8bf979 | 75 | } |
aismail1997 | 10:21268d8bf979 | 76 | |
aismail1997 | 12:b80dde24e9bc | 77 | // move servo out of the slot |
aismail1997 | 10:21268d8bf979 | 78 | void button::moveServoOut() |
aismail1997 | 10:21268d8bf979 | 79 | { |
aismail1997 | 10:21268d8bf979 | 80 | //myled = 0; |
aismail1997 | 10:21268d8bf979 | 81 | for(int i=7; i>3; i--) { |
aismail1997 | 10:21268d8bf979 | 82 | servo = i/100.0; |
aismail1997 | 10:21268d8bf979 | 83 | wait(0.01); |
aismail1997 | 10:21268d8bf979 | 84 | } |
aismail1997 | 10:21268d8bf979 | 85 | } |
aismail1997 | 18:ceac47be2e64 | 86 | |
aismail1997 | 18:ceac47be2e64 | 87 | int button::updateState() |
aismail1997 | 18:ceac47be2e64 | 88 | { |
aismail1997 | 18:ceac47be2e64 | 89 | //myled = 0; |
aismail1997 | 18:ceac47be2e64 | 90 | // state 0 - button is up, pb = 0 |
aismail1997 | 18:ceac47be2e64 | 91 | if (pb == 0 && state == 3) { |
aismail1997 | 18:ceac47be2e64 | 92 | // nothing happens here, servo is still |
aismail1997 | 18:ceac47be2e64 | 93 | state = 0; |
aismail1997 | 18:ceac47be2e64 | 94 | } |
aismail1997 | 18:ceac47be2e64 | 95 | // state 1 - button is moving down, pb = 1 |
aismail1997 | 18:ceac47be2e64 | 96 | if (pb == 1 && state == 0) { |
aismail1997 | 18:ceac47be2e64 | 97 | moveServoIn(); |
aismail1997 | 18:ceac47be2e64 | 98 | state = 1; |
aismail1997 | 18:ceac47be2e64 | 99 | press = 1; |
aismail1997 | 21:6931917c70cd | 100 | |
aismail1997 | 21:6931917c70cd | 101 | // Speaker says stuff |
aismail1997 | 21:6931917c70cd | 102 | /*myTTS.volume(18); //max volume |
aismail1997 | 21:6931917c70cd | 103 | myTTS.speakf("S");//Speak command starts with "S" |
aismail1997 | 21:6931917c70cd | 104 | myTTS.speakf("Hey, you pressed a pin!"); // Send the desired string to convert to speech |
aismail1997 | 21:6931917c70cd | 105 | myTTS.speakf("\r"); //marks end of speak command*/ |
aismail1997 | 18:ceac47be2e64 | 106 | } |
aismail1997 | 18:ceac47be2e64 | 107 | // state 2 - button is down, pb = 0 |
aismail1997 | 18:ceac47be2e64 | 108 | if (pb == 0 && state == 1) { |
aismail1997 | 18:ceac47be2e64 | 109 | // nothing happens here, servo is still |
aismail1997 | 18:ceac47be2e64 | 110 | state = 2; |
aismail1997 | 18:ceac47be2e64 | 111 | } |
aismail1997 | 18:ceac47be2e64 | 112 | // state 3 - button is moving up, pb = 1 |
aismail1997 | 18:ceac47be2e64 | 113 | if (pb == 1 && state == 2) { |
aismail1997 | 18:ceac47be2e64 | 114 | moveServoOut(); |
aismail1997 | 18:ceac47be2e64 | 115 | state = 3; |
aismail1997 | 18:ceac47be2e64 | 116 | press = 0; |
aismail1997 | 18:ceac47be2e64 | 117 | } |
aismail1997 | 18:ceac47be2e64 | 118 | // state 4 - handle debouncing while button is down |
aismail1997 | 18:ceac47be2e64 | 119 | /*if (pb1 = 1 && state == 2) { |
aismail1997 | 18:ceac47be2e64 | 120 | count++; |
aismail1997 | 18:ceac47be2e64 | 121 | }*/ |
aismail1997 | 18:ceac47be2e64 | 122 | return state; |
aismail1997 | 18:ceac47be2e64 | 123 | } |
aismail1997 | 18:ceac47be2e64 | 124 | |
aismail1997 | 23:b2e53ce54b3e | 125 | void button::setup() { |
aismail1997 | 23:b2e53ce54b3e | 126 | for(int i=0; i<=3; i++) { |
aismail1997 | 23:b2e53ce54b3e | 127 | servo = i/100.0; |
aismail1997 | 23:b2e53ce54b3e | 128 | wait(0.01); |
aismail1997 | 23:b2e53ce54b3e | 129 | } |
aismail1997 | 23:b2e53ce54b3e | 130 | } |