| 1 | /* IRLib_P00_HashRaw.h |
|---|
| 2 | * Part of IRLib Library for Arduino receiving, decoding, and sending |
|---|
| 3 | * infrared signals. See COPYRIGHT.txt and LICENSE.txt for more information. |
|---|
| 4 | */ |
|---|
| 5 | /* |
|---|
| 6 | * If you have a protocol which is unsupported by this library, you can still receive |
|---|
| 7 | * and transmit the data. You can store the raw data values in a buffer and retransmit the |
|---|
| 8 | * data exactly as you received it. Of course it takes a lot of memory to store such data |
|---|
| 9 | * so it is inefficient but it is better than nothing. |
|---|
| 10 | * If all you need to do is detect a unique value for an unsupported protocol and you do |
|---|
| 11 | * not need to resend the data, you can use the hash code decoder. It looks at the |
|---|
| 12 | * array of raw timing values and create a 32 bit value based on the data. It is |
|---|
| 13 | * highly likely to be unique however you cannot reverse engineer the process. |
|---|
| 14 | * You cannot re-create the original data stream for 32 bit hash code. |
|---|
| 15 | * This module also implements the raw send method. You have to have the original |
|---|
| 16 | * timing values. |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #ifndef IRLIB_HASHRAW_H |
|---|
| 20 | #define IRLIB_HASHRAW_H |
|---|
| 21 | #define IR_SEND_RAW case 0: IRsendRaw::send((uint16_t*)data,data2,khz); break; |
|---|
| 22 | #define IR_DECODE_HASH if(IRdecodeHash::decode()) return true; |
|---|
| 23 | #ifdef IRLIB_HAVE_COMBO |
|---|
| 24 | #define PV_IR_DECODE_HASH ,public virtual IRdecodeHash |
|---|
| 25 | #define PV_IR_SEND_RAW ,public virtual IRsendRaw |
|---|
| 26 | #else |
|---|
| 27 | #define PV_IR_DECODE_HASH public virtual IRdecodeHash |
|---|
| 28 | #define PV_IR_SEND_RAW public virtual IRsendRaw |
|---|
| 29 | #endif |
|---|
| 30 | |
|---|
| 31 | #ifdef IRLIBSENDBASE_H |
|---|
| 32 | /* The first parameter to the "IRendRaw" method is a pointer to the first element of an |
|---|
| 33 | * array of uint16_t values. These values are the raw timing values in microseconds. Note |
|---|
| 34 | * it is possible to simply pass "(uint16_t*) &My_Decoder.decodeBuffer[1]" if you have just |
|---|
| 35 | * received a code and wish to echo it. You have to point to the index "1" because index "0" |
|---|
| 36 | * of that buffer contains the gap between successive frames data and it should be ignored. |
|---|
| 37 | * If the frequency to be used in transmission is not specified, it defaults to 38kHz. |
|---|
| 38 | */ |
|---|
| 39 | class IRsendRaw: public virtual IRsendBase { |
|---|
| 40 | public: |
|---|
| 41 | void send(uint16_t *buf, uint8_t len, uint8_t khz) { |
|---|
| 42 | enableIROut(khz); |
|---|
| 43 | for (uint8_t i = 0; i < len; i++) { |
|---|
| 44 | if (i & 1) { |
|---|
| 45 | space(buf[i]); |
|---|
| 46 | } |
|---|
| 47 | else { |
|---|
| 48 | mark(buf[i]); |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | space(0); // Just to be sure |
|---|
| 52 | } |
|---|
| 53 | }; |
|---|
| 54 | #endif //IRLIBSENDBASE_H |
|---|
| 55 | |
|---|
| 56 | #ifdef IRLIBDECODEBASE_H |
|---|
| 57 | /* This Hash decoder is based on IRhashcode Copyright 2010 Ken Shirriff |
|---|
| 58 | * For details see http://www.righto.com/2010/01/using-arbitrary-remotes-with-arduino.html |
|---|
| 59 | * Use FNV hash algorithm: http://isthe.com/chongo/tech/comp/fnv/#FNV-param |
|---|
| 60 | * Converts the raw code values into a 32-bit hash code. |
|---|
| 61 | * Hopefully this code is unique for each button. |
|---|
| 62 | */ |
|---|
| 63 | #define FNV_PRIME_32 16777619 |
|---|
| 64 | #define FNV_BASIS_32 2166136261 |
|---|
| 65 | // Compare two tick values, return 0 if v1 is lower, 1 if equal, and 2 if v2 is higher |
|---|
| 66 | #define TICKS_COMPARE(v1,v2) ( (v2< v1*0.8)?0:( (v1< v2*0.8)?2:1) ) |
|---|
| 67 | class IRdecodeHash: public virtual IRdecodeBase { |
|---|
| 68 | public: |
|---|
| 69 | bool decode(void) { |
|---|
| 70 | value = FNV_BASIS_32; |
|---|
| 71 | for (int i = 1; i+2 < recvGlobal.decodeLength; i++) { |
|---|
| 72 | value = (value * FNV_PRIME_32) ^ TICKS_COMPARE(recvGlobal.decodeBuffer[i], recvGlobal.decodeBuffer[i+2]); |
|---|
| 73 | } |
|---|
| 74 | protocolNum = UNKNOWN; |
|---|
| 75 | bits= (recvGlobal.decodeLength-3)/2;//Estimated number of bits of unknown protocol |
|---|
| 76 | //Note that value is always 32 bit hash code. |
|---|
| 77 | return true; |
|---|
| 78 | } |
|---|
| 79 | }; |
|---|
| 80 | #endif //IRLIBDECODEBASE_H |
|---|
| 81 | |
|---|
| 82 | #define IRLIB_HAVE_COMBO |
|---|
| 83 | |
|---|
| 84 | #endif //IRLIB_HASHRAW_H |
|---|