Sakis Kasampalis
/
two-way-switch
toggle button style
Revision 0:ccd27d3bca28, committed 2015-03-15
- Comitter:
- faif
- Date:
- Sun Mar 15 17:23:48 2015 +0000
- Commit message:
- revised
Changed in this revision
diff -r 000000000000 -r ccd27d3bca28 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sun Mar 15 17:23:48 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912 \ No newline at end of file
diff -r 000000000000 -r ccd27d3bca28 two_way_switch.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/two_way_switch.cpp Sun Mar 15 17:23:48 2015 +0000 @@ -0,0 +1,35 @@ +#include "mbed.h" +#include "two_way_switch.h" + +int main (void) +{ + DigitalOut state = myled; + + while (true) + { + if (button) + { + /* toggle the state of the led */ + toggle_state(state); + + /* to avoid the bouncing effect */ + while (button) + { + wait(BOUNCE_DELAY); + } + } + } +} + +void toggle_state(DigitalOut& s) +{ + switch(s) + { + case true: + s = false; + break; + case false: + s = true; + break; + } +} \ No newline at end of file
diff -r 000000000000 -r ccd27d3bca28 two_way_switch.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/two_way_switch.h Sun Mar 15 17:23:48 2015 +0000 @@ -0,0 +1,13 @@ +#ifndef TWO_WAY_SWITCH_H +#define TWO_WAY_SWITCH_H + +enum {LedOff = 0, LedOn = 1}; + +const float BOUNCE_DELAY = 0.2; // in seconds + +DigitalIn button(p20); +DigitalOut myled(LED2); + +void toggle_state(DigitalOut& state); + +#endif