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