Mirror with some correction
Dependencies: mbed FastIO FastPWM USBDevice
IRRemote/IRRemote.h@116:7a67265d7c19, 2021-10-01 (annotated)
- Committer:
- arnoz
- Date:
- Fri Oct 01 08:19:46 2021 +0000
- Revision:
- 116:7a67265d7c19
- Parent:
- 77:0b96f6867312
- Correct information regarding your last merge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mjr | 77:0b96f6867312 | 1 | // IR Remote Control send/receive library - common definitions |
mjr | 77:0b96f6867312 | 2 | |
mjr | 77:0b96f6867312 | 3 | #ifndef _IRREMOTE_H_ |
mjr | 77:0b96f6867312 | 4 | #define _IRREMOTE_H_ |
mjr | 77:0b96f6867312 | 5 | |
mjr | 77:0b96f6867312 | 6 | namespace IRRemote |
mjr | 77:0b96f6867312 | 7 | { |
mjr | 77:0b96f6867312 | 8 | |
mjr | 77:0b96f6867312 | 9 | // Tolerance range for timing inaccuracies, as a percentage +/- of the |
mjr | 77:0b96f6867312 | 10 | // reference time from the protocol specification. Our measured signal |
mjr | 77:0b96f6867312 | 11 | // times will never exactly match the specification times, because |
mjr | 77:0b96f6867312 | 12 | // neither the receiver nor the transmitter have perfect clocks. And |
mjr | 77:0b96f6867312 | 13 | // even if we had a perfect clock, we'd still be a little off due to |
mjr | 77:0b96f6867312 | 14 | // latencies in the detector hardware, the detector's signal processing |
mjr | 77:0b96f6867312 | 15 | // (demodulation, filtering), and our own latencies in detecting and |
mjr | 77:0b96f6867312 | 16 | // responding to signals on the input pin. So we need to add a little |
mjr | 77:0b96f6867312 | 17 | // wiggle room in interpreting times: if the spec says we should see an |
mjr | 77:0b96f6867312 | 18 | // X-microsecond IR-ON state (a "mark"), we can expect it to actually |
mjr | 77:0b96f6867312 | 19 | // range from X-error to X+error. This tolerance value sets the error |
mjr | 77:0b96f6867312 | 20 | // range we'll accept. |
mjr | 77:0b96f6867312 | 21 | // |
mjr | 77:0b96f6867312 | 22 | // Typical Arduino IR receiver implementations use 30% tolerances. I |
mjr | 77:0b96f6867312 | 23 | // think we could be quite a bit tighter than that if we wanted to be, |
mjr | 77:0b96f6867312 | 24 | // since the biggest source of timing inaccuracies on the Arduino systems |
mjr | 77:0b96f6867312 | 25 | // is probably the Arduino itself - the CPU is fairly slow to start with, |
mjr | 77:0b96f6867312 | 26 | // and most of the Arduino code I've seen uses polling to detect signal |
mjr | 77:0b96f6867312 | 27 | // edges, which is inherently high-latency. We're on a relatively fast |
mjr | 77:0b96f6867312 | 28 | // CPU, and we use interrupts to detect edges. In my own measurements |
mjr | 77:0b96f6867312 | 29 | // of miscellaneous remotes, our readings seem to be within about 3% of |
mjr | 77:0b96f6867312 | 30 | // spec, so I think 30% is far looser than we really need to be. |
mjr | 77:0b96f6867312 | 31 | // |
mjr | 77:0b96f6867312 | 32 | // The main reason to prefer tighter tolerances is that we want to |
mjr | 77:0b96f6867312 | 33 | // work with many different transmitters from different vendors, using |
mjr | 77:0b96f6867312 | 34 | // different protocols. Higher tolerances mean more ambiguity in |
mjr | 77:0b96f6867312 | 35 | // identifying timing signatures. The tradeoff, of course, is that a |
mjr | 77:0b96f6867312 | 36 | // lower tolerance means more chance of rejecting borderline signals |
mjr | 77:0b96f6867312 | 37 | // that we could have interpreted properly. This will probably have |
mjr | 77:0b96f6867312 | 38 | // to be fine-tuned over time based on practical experience. For now, |
mjr | 77:0b96f6867312 | 39 | // it appears that the super loose Arduino levels work just fine, in |
mjr | 77:0b96f6867312 | 40 | // that we can still easily identify protocols. |
mjr | 77:0b96f6867312 | 41 | //const float tolerance = 0.3f; |
mjr | 77:0b96f6867312 | 42 | const int toleranceShl8 = 77; // tolerance*256 |
mjr | 77:0b96f6867312 | 43 | |
mjr | 77:0b96f6867312 | 44 | // Check a reading against a reference value using a specified |
mjr | 77:0b96f6867312 | 45 | // base value for figuring the tolerance. This is useful when |
mjr | 77:0b96f6867312 | 46 | // looking for multiples of a base value, because it pegs the |
mjr | 77:0b96f6867312 | 47 | // tolerance window to the base value rather than allowing it |
mjr | 77:0b96f6867312 | 48 | // to grow at each multiple. |
mjr | 77:0b96f6867312 | 49 | inline bool inRange(int reading, int referenceVal, int baseVal) |
mjr | 77:0b96f6867312 | 50 | { |
mjr | 77:0b96f6867312 | 51 | int delta = (baseVal * toleranceShl8) >> 8; |
mjr | 77:0b96f6867312 | 52 | return reading > referenceVal - delta && reading < referenceVal + delta; |
mjr | 77:0b96f6867312 | 53 | } |
mjr | 77:0b96f6867312 | 54 | |
mjr | 77:0b96f6867312 | 55 | // Check a reading against a reference value, applying the tolerance |
mjr | 77:0b96f6867312 | 56 | // range to the reference value. |
mjr | 77:0b96f6867312 | 57 | inline bool inRange(int reading, int referenceVal) |
mjr | 77:0b96f6867312 | 58 | { |
mjr | 77:0b96f6867312 | 59 | return inRange(reading, referenceVal, referenceVal); |
mjr | 77:0b96f6867312 | 60 | } |
mjr | 77:0b96f6867312 | 61 | |
mjr | 77:0b96f6867312 | 62 | // Test a reading against a reference value to see if it's out of |
mjr | 77:0b96f6867312 | 63 | // range of the reference value above. This is equivalent to asking |
mjr | 77:0b96f6867312 | 64 | // if the value is NOT inRange() AND is higher than the reference |
mjr | 77:0b96f6867312 | 65 | // value. |
mjr | 77:0b96f6867312 | 66 | inline bool aboveRange(uint32_t val, uint32_t ref, uint32_t baseVal) |
mjr | 77:0b96f6867312 | 67 | { |
mjr | 77:0b96f6867312 | 68 | return val > ref + ((baseVal*(256-toleranceShl8)) >> 8); |
mjr | 77:0b96f6867312 | 69 | } |
mjr | 77:0b96f6867312 | 70 | |
mjr | 77:0b96f6867312 | 71 | // Test a reading against a reference value to see if it's in range |
mjr | 77:0b96f6867312 | 72 | // or above the range |
mjr | 77:0b96f6867312 | 73 | inline bool inRangeOrAbove(uint32_t val, uint32_t ref, uint32_t baseVal) |
mjr | 77:0b96f6867312 | 74 | { |
mjr | 77:0b96f6867312 | 75 | int delta = (baseVal * toleranceShl8) >> 8; |
mjr | 77:0b96f6867312 | 76 | return val > ref - delta; |
mjr | 77:0b96f6867312 | 77 | } |
mjr | 77:0b96f6867312 | 78 | } |
mjr | 77:0b96f6867312 | 79 | |
mjr | 77:0b96f6867312 | 80 | #endif |