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