Working read code with mode button

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Testnew by BAT

Committer:
aismail1997
Date:
Wed Nov 01 15:22:13 2017 +0000
Revision:
23:c5df903f068a
Parent:
21:ceac47be2e64
Child:
24:6931917c70cd
Updated button and buttonarray classes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aismail1997 12:21268d8bf979 1 #include "mbed.h"
aismail1997 12:21268d8bf979 2 #include "button.h"
aismail1997 12:21268d8bf979 3
aismail1997 15:b80dde24e9bc 4 // button constructor
aismail1997 23:c5df903f068a 5 button::button(PwmOut servo, DigitalIn pb, int id)
aismail1997 23:c5df903f068a 6 : servo(servo), pb(pb), state(0), press(0), id(id){}
aismail1997 23:c5df903f068a 7 /*button::button(PwmOut servo, DigitalIn pb, AnalogIn lp)
aismail1997 23:c5df903f068a 8 : servo(servo), pb(pb), linpot(lp), press(0), state(0) {}
aismail1997 23:c5df903f068a 9 */
aismail1997 12:21268d8bf979 10
aismail1997 12:21268d8bf979 11 // FUNCTIONS
aismail1997 15:b80dde24e9bc 12
aismail1997 15:b80dde24e9bc 13 // get servo pin
aismail1997 12:21268d8bf979 14 PwmOut button::getServoPin()
aismail1997 12:21268d8bf979 15 {
aismail1997 12:21268d8bf979 16 return servo;
aismail1997 12:21268d8bf979 17 }
aismail1997 12:21268d8bf979 18
aismail1997 16:581a3b02f4c3 19 // get servo pin
aismail1997 21:ceac47be2e64 20 void button::setState(int mystate)
aismail1997 16:581a3b02f4c3 21 {
aismail1997 16:581a3b02f4c3 22 state = mystate;
aismail1997 16:581a3b02f4c3 23 }
aismail1997 16:581a3b02f4c3 24
aismail1997 23:c5df903f068a 25 void button::setPress(int mypress)
aismail1997 23:c5df903f068a 26 {
aismail1997 23:c5df903f068a 27 press = mypress;
aismail1997 23:c5df903f068a 28 }
aismail1997 23:c5df903f068a 29
aismail1997 16:581a3b02f4c3 30 // get servo pin
aismail1997 21:ceac47be2e64 31 /*void button::setMode(int mymode)
aismail1997 16:581a3b02f4c3 32 {
aismail1997 16:581a3b02f4c3 33 mode = mymode;
aismail1997 16:581a3b02f4c3 34 }*/
aismail1997 16:581a3b02f4c3 35
aismail1997 15:b80dde24e9bc 36 // get current state of the button
aismail1997 21:ceac47be2e64 37 int button::getState()
aismail1997 12:21268d8bf979 38 {
aismail1997 12:21268d8bf979 39 return state;
aismail1997 21:ceac47be2e64 40 }
aismail1997 21:ceac47be2e64 41
aismail1997 23:c5df903f068a 42 int button::getID()
aismail1997 23:c5df903f068a 43 {
aismail1997 23:c5df903f068a 44 return id;
aismail1997 23:c5df903f068a 45 }
aismail1997 23:c5df903f068a 46
aismail1997 23:c5df903f068a 47
aismail1997 21:ceac47be2e64 48 int button::getPress()
aismail1997 21:ceac47be2e64 49 {
aismail1997 21:ceac47be2e64 50 return press;
aismail1997 21:ceac47be2e64 51 }
aismail1997 21:ceac47be2e64 52
aismail1997 23:c5df903f068a 53 // get current state of the button
aismail1997 23:c5df903f068a 54 int button::getLp()
aismail1997 23:c5df903f068a 55 {
aismail1997 23:c5df903f068a 56 /* if (linpot < 2)
aismail1997 23:c5df903f068a 57 return 1;
aismail1997 23:c5df903f068a 58 else*/
aismail1997 23:c5df903f068a 59 return 0;
aismail1997 23:c5df903f068a 60 }
aismail1997 12:21268d8bf979 61
aismail1997 15:b80dde24e9bc 62 // move servo into the slot
aismail1997 12:21268d8bf979 63 void button::moveServoIn()
aismail1997 12:21268d8bf979 64 {
aismail1997 12:21268d8bf979 65 //myled = 1;
aismail1997 12:21268d8bf979 66 // rotate 90 degrees one way
aismail1997 12:21268d8bf979 67 for(int i=3; i<=7; i++) {
aismail1997 12:21268d8bf979 68 servo = i/100.0;
aismail1997 12:21268d8bf979 69 wait(0.01);
aismail1997 12:21268d8bf979 70 }
aismail1997 12:21268d8bf979 71 }
aismail1997 12:21268d8bf979 72
aismail1997 15:b80dde24e9bc 73 // move servo out of the slot
aismail1997 12:21268d8bf979 74 void button::moveServoOut()
aismail1997 12:21268d8bf979 75 {
aismail1997 12:21268d8bf979 76 //myled = 0;
aismail1997 12:21268d8bf979 77 for(int i=7; i>3; i--) {
aismail1997 12:21268d8bf979 78 servo = i/100.0;
aismail1997 12:21268d8bf979 79 wait(0.01);
aismail1997 12:21268d8bf979 80 }
aismail1997 12:21268d8bf979 81 }
aismail1997 21:ceac47be2e64 82
aismail1997 21:ceac47be2e64 83 int button::updateState()
aismail1997 21:ceac47be2e64 84 {
aismail1997 21:ceac47be2e64 85 //myled = 0;
aismail1997 21:ceac47be2e64 86 // state 0 - button is up, pb = 0
aismail1997 21:ceac47be2e64 87 if (pb == 0 && state == 3) {
aismail1997 21:ceac47be2e64 88 // nothing happens here, servo is still
aismail1997 21:ceac47be2e64 89 state = 0;
aismail1997 21:ceac47be2e64 90 }
aismail1997 21:ceac47be2e64 91 // state 1 - button is moving down, pb = 1
aismail1997 21:ceac47be2e64 92 if (pb == 1 && state == 0) {
aismail1997 21:ceac47be2e64 93 moveServoIn();
aismail1997 21:ceac47be2e64 94 state = 1;
aismail1997 21:ceac47be2e64 95 press = 1;
aismail1997 21:ceac47be2e64 96 }
aismail1997 21:ceac47be2e64 97 // state 2 - button is down, pb = 0
aismail1997 21:ceac47be2e64 98 if (pb == 0 && state == 1) {
aismail1997 21:ceac47be2e64 99 // nothing happens here, servo is still
aismail1997 21:ceac47be2e64 100 state = 2;
aismail1997 21:ceac47be2e64 101 }
aismail1997 21:ceac47be2e64 102 // state 3 - button is moving up, pb = 1
aismail1997 21:ceac47be2e64 103 if (pb == 1 && state == 2) {
aismail1997 21:ceac47be2e64 104 moveServoOut();
aismail1997 21:ceac47be2e64 105 state = 3;
aismail1997 21:ceac47be2e64 106 press = 0;
aismail1997 21:ceac47be2e64 107 }
aismail1997 21:ceac47be2e64 108 // state 4 - handle debouncing while button is down
aismail1997 21:ceac47be2e64 109 /*if (pb1 = 1 && state == 2) {
aismail1997 21:ceac47be2e64 110 count++;
aismail1997 21:ceac47be2e64 111 }*/
aismail1997 21:ceac47be2e64 112 return state;
aismail1997 21:ceac47be2e64 113 }
aismail1997 21:ceac47be2e64 114