BAT / Mbed 2 deprecated BAT_Read_Final

Dependencies:   SDFileSystem emic2 mbed-rtos mbed

Fork of BAT_senior_design_Testnew by BAT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers button.cpp Source File

button.cpp

00001 #include "mbed.h"
00002 #include "button.h"
00003 #include "emic2.h"
00004 
00005 //emic2 myTTS(p28, p27); //serial RX,TX pins to emic
00006 DigitalOut led4(LED4);
00007 DigitalOut led3(LED3);
00008 DigitalOut led2(LED2);
00009 
00010 // button constructor
00011 button::button(PwmOut servo, DigitalIn pb, int id)
00012     : servo(servo), pb(pb), state(0), press(0), id(id) {}
00013 /*button::button(PwmOut servo, DigitalIn pb, AnalogIn lp)
00014     : servo(servo), pb(pb), linpot(lp), press(0), state(0) {}
00015 */
00016 //Serial pc(USBTX, USBRX);
00017 
00018 // FUNCTIONS
00019 
00020 // get servo pin
00021 PwmOut button::getServoPin()
00022 {
00023     return servo;
00024 }
00025 
00026 // get servo pin
00027 void button::setState(int mystate)
00028 {
00029     state = mystate;
00030 }
00031 
00032 void button::setPress(int mypress)
00033 {
00034     press = mypress;
00035 }
00036 
00037 // get servo pin
00038 /*void button::setMode(int mymode)
00039 {
00040     mode = mymode;
00041 }*/
00042 
00043 // get current state of the button
00044 int button::getState()
00045 {
00046     return state;
00047 }
00048 
00049 int button::getID()
00050 {
00051     return id;
00052 }
00053 
00054 
00055 int button::getPress()
00056 {
00057     //pc.printf("%d", press);
00058     return press;
00059 }
00060 
00061 // get current state of the button
00062 int button::getLp()
00063 {
00064     /*    if (linpot < 2)
00065             return 1;
00066         else*/
00067     return 0;
00068 }
00069 
00070 // move servo into the slot
00071 void button::moveServoIn()
00072 {
00073     //myled = 1;
00074     // rotate 90 degrees one way
00075     for(int i=4; i<=7; i++) {
00076         servo = i/100.0;
00077         wait(0.01);
00078     }
00079     //press = 1;
00080     switch (id) {
00081         case 1:
00082             led2 = 0;
00083             led3 = 0;
00084             led4 = 1;
00085             break;
00086         case 2:
00087             led2 = 0;
00088             led3 = 1;
00089             led4 = 0;
00090             break;
00091         case 3:
00092             led2 = 0;
00093             led3 = 1;
00094             led4 = 1;
00095             break;
00096         case 4:
00097             led2 = 1;
00098             led3 = 0;
00099             led4 = 0;
00100             break;
00101         case 5:
00102             led2 = 1;
00103             led3 = 0;
00104             led4 = 1;
00105             break;
00106         case 6:
00107             led2 = 1;
00108             led3 = 1;
00109             led4 = 0;
00110             break;
00111     }
00112 }
00113 
00114 // move servo out of the slot
00115 void button::moveServoOut()
00116 {
00117     //myled = 0;
00118     for(int i=7; i>4; i--) {
00119         servo = i/100.0;
00120         wait(0.01);
00121     }
00122     led2 = 0;
00123     led3 = 0;
00124     led4 = 0;
00125 }
00126 
00127 int button::updateState()
00128 {
00129     //myled = 0;
00130     // state 0 - button is up, pb = 0
00131     if (pb == 0 && state == 3) {
00132         // nothing happens here, servo is still
00133         state = 0;
00134     }
00135     // state 1 - button is moving down, pb = 1
00136     if (pb == 1 && state == 0) {
00137         moveServoIn();
00138         state = 1;
00139         press = 1;
00140 
00141         // Speaker says stuff
00142         /*myTTS.volume(18); //max volume
00143         myTTS.speakf("S");//Speak command starts with "S"
00144         myTTS.speakf("Hey, you pressed a pin!");  // Send the desired string to convert to speech
00145         myTTS.speakf("\r"); //marks end of speak command*/
00146     }
00147     // state 2 - button is down, pb = 0
00148     if (pb == 0 && state == 1) {
00149         // nothing happens here, servo is still
00150         state = 2;
00151     }
00152     // state 3 - button is moving up, pb = 1
00153     if (pb == 1 && state == 2) {
00154         moveServoOut();
00155         state = 3;
00156         press = 0;
00157     }
00158     // state 4 - handle debouncing while button is down
00159     /*if (pb1 = 1 && state == 2) {
00160         count++;
00161     }*/
00162     return state;
00163 }
00164 
00165 void button::setup() {
00166     for(int i=0; i<=4; i++) {
00167         servo = i/100.0;
00168         wait(0.01);
00169     }
00170 }