Infrared4Arduino 1.2.3
Loading...
Searching...
No Matches
HashDecoder.cpp
Go to the documentation of this file.
1#include "HashDecoder.h"
2
3uint32_t HashDecoder::compare(microseconds_t oldVal, microseconds_t newVal) {
4 return
5 newVal < 3 * (oldVal / 4) ? 0
6 : newVal > 5 * (oldVal / 4) ? 2
7 : 1;
8}
9
10bool HashDecoder::tryDecode(const IrReader& irCapturer, Stream& stream) {
11 HashDecoder decoder(irCapturer);
12 return decoder.printDecode(stream);
13}
14
15void HashDecoder::decode(const microseconds_t* data, size_t length) {
16 if (length < minMeaningfulLength)
17 return;
18
19 for (unsigned int i = 0; i < length - offset - 1; i++) {
20 uint32_t value = compare(data[i], data[i + offset]);
21 hash = (hash * FNVprime) ^ value;
22 }
23
24 setValid(true);
25}
26
27void HashDecoder::decode(const IrReader& irReader) {
28 size_t length = irReader.getDataLength();
29 if (length < minMeaningfulLength)
30 return;
31
32 for (unsigned int i = 0; i < length - offset - 1; i++) {
33 uint32_t value = compare(irReader.getDuration(i), irReader.getDuration(i + offset));
34 hash = (hash * FNVprime) ^ value;
35 }
36
37 setValid(true);
38}
39
40uint32_t HashDecoder::decodeHash(const IrSequence& irSequence) {
41 HashDecoder decoder(irSequence);
42 return decoder.getHash();
43}
44
45uint32_t HashDecoder::decodeHash(const IrReader& irReader) {
46 HashDecoder decoder(irReader);
47 return decoder.getHash();
48}
This file defines a hash based "decoder class".
uint16_t microseconds_t
Type for durations in micro seconds.
Definition: InfraredTypes.h:15
A decoder class using FNV-1 hashes of length 32.
Definition: HashDecoder.h:19
uint32_t getHash() const
Returns the hash value.
Definition: HashDecoder.h:86
static bool tryDecode(const IrReader &irReader, Stream &stream)
Convenience function; constructs a HashDecoder and calls its printDecode.
Definition: HashDecoder.cpp:10
static uint32_t decodeHash(const IrSequence &irSequence)
Definition: HashDecoder.cpp:40
void setValid(bool valid_)
Definition: IrDecoder.h:60
bool printDecode(Stream &stream) const
If valid, prints the decode to the stream.
Definition: IrDecoder.h:48
Abstract base class for all IR readers, capturing or receiving.
Definition: IrReader.h:30
virtual size_t getDataLength() const =0
Returns the number of collected durations.
virtual microseconds_t getDuration(unsigned int index) const =0
Returns the index-th duration, if possible.
This class consists of a vector of durations.
Definition: IrSequence.h:11