uwick. W
/
MameController
Mamecontroller/joystick device wrapper library
DebounceIn.h@4:4f6e38b6c07e, 2014-12-10 (annotated)
- Committer:
- uswickra
- Date:
- Wed Dec 10 05:51:13 2014 +0000
- Revision:
- 4:4f6e38b6c07e
- Parent:
- 2:018f204f6037
my controller;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uswickra | 1:89c1c7f9245a | 1 | #ifndef DEBOUNCEIN_H |
uswickra | 1:89c1c7f9245a | 2 | #define DEBOUNCEIN_H |
uswickra | 1:89c1c7f9245a | 3 | #include "mbed.h" |
uswickra | 1:89c1c7f9245a | 4 | |
uswickra | 1:89c1c7f9245a | 5 | class DebounceIn : public DigitalIn { |
uswickra | 1:89c1c7f9245a | 6 | public: |
uswickra | 1:89c1c7f9245a | 7 | |
uswickra | 1:89c1c7f9245a | 8 | /** set_debounce_us |
uswickra | 1:89c1c7f9245a | 9 | * |
uswickra | 1:89c1c7f9245a | 10 | * Sets the debounce sample period time in microseconds, default is 1000 (1ms) |
uswickra | 1:89c1c7f9245a | 11 | * |
uswickra | 1:89c1c7f9245a | 12 | * @param int i The debounce sample period time to set. |
uswickra | 1:89c1c7f9245a | 13 | */ |
uswickra | 1:89c1c7f9245a | 14 | void set_debounce_us(int i) { _ticker.attach_us(this, &DebounceIn::_callback, i); } |
uswickra | 1:89c1c7f9245a | 15 | |
uswickra | 1:89c1c7f9245a | 16 | /** set_samples |
uswickra | 1:89c1c7f9245a | 17 | * |
uswickra | 1:89c1c7f9245a | 18 | * Defines the number of samples before switching the shadow |
uswickra | 1:89c1c7f9245a | 19 | * definition of the pin. |
uswickra | 1:89c1c7f9245a | 20 | * |
uswickra | 1:89c1c7f9245a | 21 | * @param int i The number of samples. |
uswickra | 1:89c1c7f9245a | 22 | */ |
uswickra | 1:89c1c7f9245a | 23 | void set_samples(int i) { _samples = i; } |
uswickra | 1:89c1c7f9245a | 24 | |
uswickra | 1:89c1c7f9245a | 25 | /** read |
uswickra | 1:89c1c7f9245a | 26 | * |
uswickra | 1:89c1c7f9245a | 27 | * Read the value of the debounced pin. |
uswickra | 1:89c1c7f9245a | 28 | */ |
uswickra | 1:89c1c7f9245a | 29 | int read(void) { return _shadow; } |
uswickra | 1:89c1c7f9245a | 30 | |
uswickra | 1:89c1c7f9245a | 31 | #ifdef MBED_OPERATORS |
uswickra | 1:89c1c7f9245a | 32 | /** operator int() |
uswickra | 1:89c1c7f9245a | 33 | * |
uswickra | 1:89c1c7f9245a | 34 | * Read the value of the debounced pin. |
uswickra | 1:89c1c7f9245a | 35 | */ |
uswickra | 1:89c1c7f9245a | 36 | operator int() { return read(); } |
uswickra | 1:89c1c7f9245a | 37 | #endif |
uswickra | 1:89c1c7f9245a | 38 | |
uswickra | 1:89c1c7f9245a | 39 | /** Constructor |
uswickra | 1:89c1c7f9245a | 40 | * |
uswickra | 1:89c1c7f9245a | 41 | * @param PinName pin The pin to assign as an input. |
uswickra | 1:89c1c7f9245a | 42 | */ |
uswickra | 1:89c1c7f9245a | 43 | DebounceIn(PinName pin) : DigitalIn(pin) { _counter = 0; _samples = 10; set_debounce_us(1000); }; |
uswickra | 1:89c1c7f9245a | 44 | |
uswickra | 1:89c1c7f9245a | 45 | protected: |
uswickra | 1:89c1c7f9245a | 46 | void _callback(void) { |
uswickra | 1:89c1c7f9245a | 47 | if (DigitalIn::read()) { |
uswickra | 1:89c1c7f9245a | 48 | if (_counter < _samples) _counter++; |
uswickra | 1:89c1c7f9245a | 49 | if (_counter == _samples) _shadow = 1; |
uswickra | 1:89c1c7f9245a | 50 | } |
uswickra | 1:89c1c7f9245a | 51 | else { |
uswickra | 1:89c1c7f9245a | 52 | if (_counter > 0) _counter--; |
uswickra | 1:89c1c7f9245a | 53 | if (_counter == 0) _shadow = 0; |
uswickra | 1:89c1c7f9245a | 54 | } |
uswickra | 1:89c1c7f9245a | 55 | } |
uswickra | 1:89c1c7f9245a | 56 | |
uswickra | 1:89c1c7f9245a | 57 | Ticker _ticker; |
uswickra | 1:89c1c7f9245a | 58 | int _shadow; |
uswickra | 1:89c1c7f9245a | 59 | int _counter; |
uswickra | 1:89c1c7f9245a | 60 | int _samples; |
bhavk11 | 2:018f204f6037 | 61 | }; |
bhavk11 | 2:018f204f6037 | 62 | #endif |