would be Simon game, as a demonstrator for buttons debouncing and \"collecting\" ---

Committer:
wek
Date:
Tue Dec 14 00:45:37 2010 +0000
Revision:
2:fe450a70f8a4
Parent:
0:408abc8a3d3e
Finally, the real Simon game. Enjoy! ;-)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wek 0:408abc8a3d3e 1 /*
wek 0:408abc8a3d3e 2 Copyright (c) 2010 Andy Kirkham
wek 0:408abc8a3d3e 3
wek 0:408abc8a3d3e 4 Permission is hereby granted, free of charge, to any person obtaining a copy
wek 0:408abc8a3d3e 5 of this software and associated documentation files (the "Software"), to deal
wek 0:408abc8a3d3e 6 in the Software without restriction, including without limitation the rights
wek 0:408abc8a3d3e 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
wek 0:408abc8a3d3e 8 copies of the Software, and to permit persons to whom the Software is
wek 0:408abc8a3d3e 9 furnished to do so, subject to the following conditions:
wek 0:408abc8a3d3e 10
wek 0:408abc8a3d3e 11 The above copyright notice and this permission notice shall be included in
wek 0:408abc8a3d3e 12 all copies or substantial portions of the Software.
wek 0:408abc8a3d3e 13
wek 0:408abc8a3d3e 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
wek 0:408abc8a3d3e 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
wek 0:408abc8a3d3e 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
wek 0:408abc8a3d3e 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
wek 0:408abc8a3d3e 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
wek 0:408abc8a3d3e 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
wek 0:408abc8a3d3e 20 THE SOFTWARE.
wek 0:408abc8a3d3e 21 */
wek 0:408abc8a3d3e 22
wek 0:408abc8a3d3e 23 #ifndef DEBOUNCEIN_H
wek 0:408abc8a3d3e 24 #define DEBOUNCEIN_H
wek 0:408abc8a3d3e 25
wek 0:408abc8a3d3e 26 #include "mbed.h"
wek 0:408abc8a3d3e 27
wek 0:408abc8a3d3e 28 /** DebounceIn adds mechanical switch debouncing to DigitialIn.
wek 0:408abc8a3d3e 29 *
wek 0:408abc8a3d3e 30 * Example:
wek 0:408abc8a3d3e 31 * @code
wek 0:408abc8a3d3e 32 * #include "mbed.h"
wek 0:408abc8a3d3e 33 * #include "DebounceIn.h"
wek 0:408abc8a3d3e 34 *
wek 0:408abc8a3d3e 35 * DebounceIn d(p5);
wek 0:408abc8a3d3e 36 * DigitialOut led1(LED1);
wek 0:408abc8a3d3e 37 * DigitialOut led2(LED2);
wek 0:408abc8a3d3e 38 *
wek 0:408abc8a3d3e 39 * int main() {
wek 0:408abc8a3d3e 40 * while(1) {
wek 0:408abc8a3d3e 41 * led1 = d;
wek 0:408abc8a3d3e 42 * led2 = d.read();
wek 0:408abc8a3d3e 43 * }
wek 0:408abc8a3d3e 44 * }
wek 0:408abc8a3d3e 45 * @endcode
wek 0:408abc8a3d3e 46 *
wek 0:408abc8a3d3e 47 * @see set_debounce_us() To change the sampling frequency.
wek 0:408abc8a3d3e 48 * @see set_samples() To alter the number of samples.
wek 0:408abc8a3d3e 49 *
wek 0:408abc8a3d3e 50 * This example shows one input displayed by two outputs. The input
wek 0:408abc8a3d3e 51 * is debounced by the default 10ms.
wek 0:408abc8a3d3e 52 */
wek 0:408abc8a3d3e 53
wek 0:408abc8a3d3e 54 class DebounceIn : public DigitalIn {
wek 0:408abc8a3d3e 55 public:
wek 0:408abc8a3d3e 56
wek 0:408abc8a3d3e 57 /** set_debounce_us
wek 0:408abc8a3d3e 58 *
wek 0:408abc8a3d3e 59 * Sets the debounce sample period time in microseconds, default is 1000 (1ms)
wek 0:408abc8a3d3e 60 *
wek 0:408abc8a3d3e 61 * @param int i The debounce sample period time to set.
wek 0:408abc8a3d3e 62 */
wek 0:408abc8a3d3e 63 void set_debounce_us(int i) { _ticker.attach_us(this, &DebounceIn::_callback, i); }
wek 0:408abc8a3d3e 64
wek 0:408abc8a3d3e 65 /** set_samples
wek 0:408abc8a3d3e 66 *
wek 0:408abc8a3d3e 67 * Defines the number of samples before switching the shadow
wek 0:408abc8a3d3e 68 * definition of the pin.
wek 0:408abc8a3d3e 69 *
wek 0:408abc8a3d3e 70 * @param int i The number of samples.
wek 0:408abc8a3d3e 71 */
wek 0:408abc8a3d3e 72 void set_samples(int i) { _samples = i; }
wek 0:408abc8a3d3e 73
wek 0:408abc8a3d3e 74 /** read
wek 0:408abc8a3d3e 75 *
wek 0:408abc8a3d3e 76 * Read the value of the debounced pin.
wek 0:408abc8a3d3e 77 */
wek 0:408abc8a3d3e 78 int read(void) { return _shadow; }
wek 0:408abc8a3d3e 79
wek 0:408abc8a3d3e 80 #ifdef MBED_OPERATORS
wek 0:408abc8a3d3e 81 /** operator int()
wek 0:408abc8a3d3e 82 *
wek 0:408abc8a3d3e 83 * Read the value of the debounced pin.
wek 0:408abc8a3d3e 84 */
wek 0:408abc8a3d3e 85 operator int() { return read(); }
wek 0:408abc8a3d3e 86 #endif
wek 0:408abc8a3d3e 87
wek 0:408abc8a3d3e 88 int pressed;
wek 0:408abc8a3d3e 89 /** Constructor
wek 0:408abc8a3d3e 90 *
wek 0:408abc8a3d3e 91 * @param PinName pin The pin to assign as an input.
wek 0:408abc8a3d3e 92 */
wek 0:408abc8a3d3e 93 DebounceIn(PinName pin, const char *name = NULL) : DigitalIn(pin, name) { _counter = 0; _samples = 10; set_debounce_us(1000); pressed = 0;};
wek 0:408abc8a3d3e 94
wek 0:408abc8a3d3e 95 protected:
wek 0:408abc8a3d3e 96 void _callback(void) {
wek 0:408abc8a3d3e 97 if (!DigitalIn::read()) { // negated as button is pressed when 0
wek 0:408abc8a3d3e 98 if (_counter < _samples) _counter++;
wek 0:408abc8a3d3e 99 if ((_counter == _samples) && (_shadow == 0)) {
wek 0:408abc8a3d3e 100 _shadow = 1;
wek 0:408abc8a3d3e 101 pressed = 1;
wek 0:408abc8a3d3e 102 }
wek 0:408abc8a3d3e 103 }
wek 0:408abc8a3d3e 104 else {
wek 0:408abc8a3d3e 105 if (_counter > 0) _counter--;
wek 0:408abc8a3d3e 106 if (_counter == 0) _shadow = 0;
wek 0:408abc8a3d3e 107 }
wek 0:408abc8a3d3e 108 }
wek 0:408abc8a3d3e 109
wek 0:408abc8a3d3e 110 Ticker _ticker;
wek 0:408abc8a3d3e 111 int _shadow;
wek 0:408abc8a3d3e 112 int _counter;
wek 0:408abc8a3d3e 113 int _samples;
wek 0:408abc8a3d3e 114 };
wek 0:408abc8a3d3e 115
wek 0:408abc8a3d3e 116 #endif
wek 0:408abc8a3d3e 117