Working reset, flipped logic

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Nhi by BAT

Committer:
aismail1997
Date:
Fri Oct 27 15:15:00 2017 +0000
Revision:
19:ceac47be2e64
Parent:
14:581a3b02f4c3
Child:
21:c5df903f068a
cleaned up button code, added buttonarray class

Who changed what in which revision?

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