Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SDFileSystem emic2 mbed-rtos mbed
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 00014 // FUNCTIONS 00015 // get servo pin 00016 PwmOut button::getServoPin() 00017 { 00018 return servo; 00019 } 00020 00021 // set button state 00022 void button::setState(int mystate) 00023 { 00024 state = mystate; 00025 } 00026 00027 void button::setPress(int mypress) 00028 { 00029 press = mypress; 00030 } 00031 00032 // get current state of the button 00033 int button::getState() 00034 { 00035 return state; 00036 } 00037 00038 int button::getID() 00039 { 00040 return id; 00041 } 00042 00043 00044 int button::getPress() 00045 { 00046 return press; 00047 } 00048 00049 // get current state of the button 00050 int button::getLp() 00051 { 00052 return 0; 00053 } 00054 00055 // move servo into the slot 00056 void button::moveServoIn() 00057 { 00058 // rotate 90 degrees one way 00059 for(int i=4; i<=7; i++) { 00060 servo = i/100.0; 00061 wait(0.01); 00062 } 00063 switch (id) { 00064 case 1: 00065 led2 = 0; 00066 led3 = 0; 00067 led4 = 1; 00068 break; 00069 case 2: 00070 led2 = 0; 00071 led3 = 1; 00072 led4 = 0; 00073 break; 00074 case 3: 00075 led2 = 0; 00076 led3 = 1; 00077 led4 = 1; 00078 break; 00079 case 4: 00080 led2 = 1; 00081 led3 = 0; 00082 led4 = 0; 00083 break; 00084 case 5: 00085 led2 = 1; 00086 led3 = 0; 00087 led4 = 1; 00088 break; 00089 case 6: 00090 led2 = 1; 00091 led3 = 1; 00092 led4 = 0; 00093 break; 00094 } 00095 } 00096 00097 // move servo out of the slot 00098 void button::moveServoOut() 00099 { 00100 for(int i=7; i>4; i--) { 00101 servo = i/100.0; 00102 wait(0.01); 00103 } 00104 led2 = 0; 00105 led3 = 0; 00106 led4 = 0; 00107 } 00108 00109 int button::updateState() 00110 { 00111 // state 0 - button is up, pb = 0 00112 if (pb == 0 && state == 3) { 00113 // nothing happens here, servo is still 00114 state = 0; 00115 } 00116 // state 1 - button is moving down, pb = 1 00117 if (pb == 1 && state == 0) { 00118 moveServoIn(); 00119 state = 1; 00120 press = 1; 00121 } 00122 // state 2 - button is down, pb = 0 00123 if (pb == 0 && state == 1) { 00124 // nothing happens here, servo is still 00125 state = 2; 00126 } 00127 // state 3 - button is moving up, pb = 1 00128 if (pb == 1 && state == 2) { 00129 moveServoOut(); 00130 state = 3; 00131 press = 0; 00132 } 00133 // state 4 - handle debouncing while button is down 00134 return state; 00135 } 00136 00137 void button::setup() { 00138 for(int i=0; i<=4; i++) { 00139 servo = i/100.0; 00140 wait(0.01); 00141 } 00142 }
Generated on Fri Jul 15 2022 04:33:16 by
1.7.2