Stefan Kummer
/
Laser-Distance
#1
Fork of mbed-cloud-connect-sensor-laser-distance by
DebounceIn.h@9:f2bdb2a79528, 2018-12-10 (annotated)
- Committer:
- KStefan
- Date:
- Mon Dec 10 07:22:31 2018 +0000
- Revision:
- 9:f2bdb2a79528
OK
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
KStefan | 9:f2bdb2a79528 | 1 | #include "mbed.h" |
KStefan | 9:f2bdb2a79528 | 2 | |
KStefan | 9:f2bdb2a79528 | 3 | class DebounceIn : public DigitalIn |
KStefan | 9:f2bdb2a79528 | 4 | { |
KStefan | 9:f2bdb2a79528 | 5 | public: |
KStefan | 9:f2bdb2a79528 | 6 | Ticker ticker; |
KStefan | 9:f2bdb2a79528 | 7 | int shadow; |
KStefan | 9:f2bdb2a79528 | 8 | int counter; |
KStefan | 9:f2bdb2a79528 | 9 | int samples; |
KStefan | 9:f2bdb2a79528 | 10 | |
KStefan | 9:f2bdb2a79528 | 11 | public: |
KStefan | 9:f2bdb2a79528 | 12 | |
KStefan | 9:f2bdb2a79528 | 13 | /* Setzt die Entprellzeit (in µs), default sind 1000µs (1ms) */ |
KStefan | 9:f2bdb2a79528 | 14 | void set_debounce_us(int i) { |
KStefan | 9:f2bdb2a79528 | 15 | ticker.attach_us(callback(this,&DebounceIn::cb), i); |
KStefan | 9:f2bdb2a79528 | 16 | //ticker.attach_us(this, &DebounceIn::cb, i); |
KStefan | 9:f2bdb2a79528 | 17 | //ticker.attach_us(&DebounceIn::cb, i); |
KStefan | 9:f2bdb2a79528 | 18 | } |
KStefan | 9:f2bdb2a79528 | 19 | |
KStefan | 9:f2bdb2a79528 | 20 | /* Definiert die Anzahl der Samples um den 'Schatten'-Zustand des DigitalIn-Pins zu schalten */ |
KStefan | 9:f2bdb2a79528 | 21 | void set_samples(int i) { |
KStefan | 9:f2bdb2a79528 | 22 | samples = i; |
KStefan | 9:f2bdb2a79528 | 23 | } |
KStefan | 9:f2bdb2a79528 | 24 | |
KStefan | 9:f2bdb2a79528 | 25 | /* Liest den Wert des entprellten Pins */ |
KStefan | 9:f2bdb2a79528 | 26 | int Read(void) { |
KStefan | 9:f2bdb2a79528 | 27 | return shadow; |
KStefan | 9:f2bdb2a79528 | 28 | } |
KStefan | 9:f2bdb2a79528 | 29 | |
KStefan | 9:f2bdb2a79528 | 30 | //#ifdef MBED_OPERATORS |
KStefan | 9:f2bdb2a79528 | 31 | operator int() { return Read(); } |
KStefan | 9:f2bdb2a79528 | 32 | //#endif |
KStefan | 9:f2bdb2a79528 | 33 | |
KStefan | 9:f2bdb2a79528 | 34 | /* Konstruktor */ |
KStefan | 9:f2bdb2a79528 | 35 | DebounceIn(PinName pin) : DigitalIn(pin) { |
KStefan | 9:f2bdb2a79528 | 36 | counter = 0; |
KStefan | 9:f2bdb2a79528 | 37 | samples = 10; |
KStefan | 9:f2bdb2a79528 | 38 | // shadow = 0; |
KStefan | 9:f2bdb2a79528 | 39 | set_debounce_us(1000); |
KStefan | 9:f2bdb2a79528 | 40 | } |
KStefan | 9:f2bdb2a79528 | 41 | |
KStefan | 9:f2bdb2a79528 | 42 | |
KStefan | 9:f2bdb2a79528 | 43 | |
KStefan | 9:f2bdb2a79528 | 44 | protected: |
KStefan | 9:f2bdb2a79528 | 45 | |
KStefan | 9:f2bdb2a79528 | 46 | void cb() { |
KStefan | 9:f2bdb2a79528 | 47 | if (!DigitalIn::read()) { |
KStefan | 9:f2bdb2a79528 | 48 | if (counter < samples) counter++; |
KStefan | 9:f2bdb2a79528 | 49 | if (counter == samples) shadow = 1; |
KStefan | 9:f2bdb2a79528 | 50 | } else { |
KStefan | 9:f2bdb2a79528 | 51 | if (counter > 0) counter--; |
KStefan | 9:f2bdb2a79528 | 52 | if (counter == 0) shadow = 0; |
KStefan | 9:f2bdb2a79528 | 53 | } |
KStefan | 9:f2bdb2a79528 | 54 | } |
KStefan | 9:f2bdb2a79528 | 55 | |
KStefan | 9:f2bdb2a79528 | 56 | |
KStefan | 9:f2bdb2a79528 | 57 | }; |