Jan Waclawek
/
Simon
would be Simon game, as a demonstrator for buttons debouncing and \"collecting\" ---
Revision 0:408abc8a3d3e, committed 2010-12-13
- Comitter:
- wek
- Date:
- Mon Dec 13 23:34:17 2010 +0000
- Child:
- 1:3acf57259c58
- Commit message:
- initial revision
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebounceIn_.h Mon Dec 13 23:34:17 2010 +0000 @@ -0,0 +1,117 @@ +/* + Copyright (c) 2010 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef DEBOUNCEIN_H +#define DEBOUNCEIN_H + +#include "mbed.h" + +/** DebounceIn adds mechanical switch debouncing to DigitialIn. + * + * Example: + * @code + * #include "mbed.h" + * #include "DebounceIn.h" + * + * DebounceIn d(p5); + * DigitialOut led1(LED1); + * DigitialOut led2(LED2); + * + * int main() { + * while(1) { + * led1 = d; + * led2 = d.read(); + * } + * } + * @endcode + * + * @see set_debounce_us() To change the sampling frequency. + * @see set_samples() To alter the number of samples. + * + * This example shows one input displayed by two outputs. The input + * is debounced by the default 10ms. + */ + +class DebounceIn : public DigitalIn { + public: + + /** set_debounce_us + * + * Sets the debounce sample period time in microseconds, default is 1000 (1ms) + * + * @param int i The debounce sample period time to set. + */ + void set_debounce_us(int i) { _ticker.attach_us(this, &DebounceIn::_callback, i); } + + /** set_samples + * + * Defines the number of samples before switching the shadow + * definition of the pin. + * + * @param int i The number of samples. + */ + void set_samples(int i) { _samples = i; } + + /** read + * + * Read the value of the debounced pin. + */ + int read(void) { return _shadow; } + +#ifdef MBED_OPERATORS + /** operator int() + * + * Read the value of the debounced pin. + */ + operator int() { return read(); } +#endif + + int pressed; + /** Constructor + * + * @param PinName pin The pin to assign as an input. + */ + DebounceIn(PinName pin, const char *name = NULL) : DigitalIn(pin, name) { _counter = 0; _samples = 10; set_debounce_us(1000); pressed = 0;}; + + protected: + void _callback(void) { + if (!DigitalIn::read()) { // negated as button is pressed when 0 + if (_counter < _samples) _counter++; + if ((_counter == _samples) && (_shadow == 0)) { + _shadow = 1; + pressed = 1; + } + } + else { + if (_counter > 0) _counter--; + if (_counter == 0) _shadow = 0; + } + } + + Ticker _ticker; + int _shadow; + int _counter; + int _samples; +}; + +#endif + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DebounceIn__copy.h Mon Dec 13 23:34:17 2010 +0000 @@ -0,0 +1,117 @@ +/* + Copyright (c) 2010 Andy Kirkham + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. +*/ + +#ifndef DEBOUNCEIN_H +#define DEBOUNCEIN_H + +#include "mbed.h" + +/** DebounceIn adds mechanical switch debouncing to DigitialIn. + * + * Example: + * @code + * #include "mbed.h" + * #include "DebounceIn.h" + * + * DebounceIn d(p5); + * DigitialOut led1(LED1); + * DigitialOut led2(LED2); + * + * int main() { + * while(1) { + * led1 = d; + * led2 = d.read(); + * } + * } + * @endcode + * + * @see set_debounce_us() To change the sampling frequency. + * @see set_samples() To alter the number of samples. + * + * This example shows one input displayed by two outputs. The input + * is debounced by the default 10ms. + */ + +class DebounceIn : public DigitalIn { + public: + + /** set_debounce_us + * + * Sets the debounce sample period time in microseconds, default is 1000 (1ms) + * + * @param int i The debounce sample period time to set. + */ + void set_debounce_us(int i) { _ticker.attach_us(this, &DebounceIn::_callback, i); } + + /** set_samples + * + * Defines the number of samples before switching the shadow + * definition of the pin. + * + * @param int i The number of samples. + */ + void set_samples(int i) { _samples = i; } + + /** read + * + * Read the value of the debounced pin. + */ + int read(void) { return _shadow; } + +#ifdef MBED_OPERATORS + /** operator int() + * + * Read the value of the debounced pin. + */ + operator int() { return read(); } +#endif + + int pressed; + /** Constructor + * + * @param PinName pin The pin to assign as an input. + */ + DebounceIn(PinName pin, const char *name = NULL) : DigitalIn(pin, name) { _counter = 0; _samples = 10; set_debounce_us(1000); pressed = 0;}; + + protected: + void _callback(void) { + if (DigitalIn::read()) { + if (_counter < _samples) _counter++; + if ((_counter == _samples) && (_shadow == 0)) { + _shadow = 1; + pressed = 1; + } + } + else { + if (_counter > 0) _counter--; + if (_counter == 0) _shadow = 0; + } + } + + Ticker _ticker; + int _shadow; + int _counter; + int _samples; +}; + +#endif + \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Dec 13 23:34:17 2010 +0000 @@ -0,0 +1,91 @@ +#include "mbed.h" +#include "DebounceIn_.h" +#include <stdbool.h> + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); +DigitalOut led4(LED4); +DebounceIn button1(p15); +DebounceIn button2(p16); +DebounceIn button3(p17); +DebounceIn button4(p21); + +class CSimonButton { + _Bool pressed; + int button; + public: + CSimonButton(); + _Bool IsPressed(void); + int GetButton(void) {return button;}; + void Handle(void); +}; + +CSimonButton::CSimonButton() { + button1.mode(PullUp); + button2.mode(PullUp); + button3.mode(PullUp); + button4.mode(PullUp); + pressed = 0; +} + +_Bool CSimonButton::IsPressed(void) { + _Bool tmp; + + tmp = pressed; + pressed = 0; + return tmp; +} + +void CSimonButton::Handle(void) { + if (button1.pressed) { + button1.pressed = 0; + pressed = 1; + button = 1; + } + if (button2.pressed) { + button2.pressed = 0; + pressed = 1; + button = 2; + } + if (button3.pressed) { + button3.pressed = 0; + pressed = 1; + button = 3; + } + if (button4.pressed) { + button4.pressed = 0; + pressed = 1; + button = 4; + } + +} + + +uint8_t ledState; + +int main() { + CSimonButton simonButton; + while (1) { + led1 = (ledState & 0x01) ? 1 : 0; + led2 = (ledState & 0x02) ? 1 : 0; + led3 = (ledState & 0x04) ? 1 : 0; + led4 = (ledState & 0x08) ? 1 : 0; + + simonButton.Handle(); + + if (simonButton.IsPressed()) { + ledState ^= 1 << (simonButton.GetButton() - 1); + } + } +} +/* +int main() { + while(1) { + myled = 1; + wait(0.2); + myled = 0; + wait(0.2); + } +} +*/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Dec 13 23:34:17 2010 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/e2ac27c8e93e