Library to read 433MHz codes with decoder HT6P20
Dependents: Gateway_Cotosys Gateway_Cotosys_os5
RFDecoder.cpp@1:adf26c4f20cd, 2019-06-11 (annotated)
- Committer:
- Thrillex13
- Date:
- Tue Jun 11 00:23:55 2019 +0000
- Revision:
- 1:adf26c4f20cd
- Parent:
- 0:bcd4927f951a
Initial. Library to read 433MHz RF transmitters with decoder HT6P20
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Thrillex13 | 0:bcd4927f951a | 1 | /* |
Thrillex13 | 0:bcd4927f951a | 2 | RFDecoder - Ported from the Lucas Maziero Arduino libary to decode RF controls based on chip HT6P20 |
Thrillex13 | 0:bcd4927f951a | 3 | Copyright (c) 2011 Miguel Maldonado. All right reserved. |
Thrillex13 | 0:bcd4927f951a | 4 | |
Thrillex13 | 0:bcd4927f951a | 5 | Project home: https://github.com/lucasmaziero/RF_HT6P20 |
Thrillex13 | 0:bcd4927f951a | 6 | |
Thrillex13 | 0:bcd4927f951a | 7 | This library is free software; you can redistribute it and/or |
Thrillex13 | 0:bcd4927f951a | 8 | modify it under the terms of the GNU Lesser General Public |
Thrillex13 | 0:bcd4927f951a | 9 | License as published by the Free Software Foundation; either |
Thrillex13 | 0:bcd4927f951a | 10 | version 2.1 of the License, or (at your option) any later version. |
Thrillex13 | 0:bcd4927f951a | 11 | |
Thrillex13 | 0:bcd4927f951a | 12 | This library is distributed in the hope that it will be useful, |
Thrillex13 | 0:bcd4927f951a | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
Thrillex13 | 0:bcd4927f951a | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
Thrillex13 | 0:bcd4927f951a | 15 | Lesser General Public License for more details. |
Thrillex13 | 0:bcd4927f951a | 16 | */ |
Thrillex13 | 0:bcd4927f951a | 17 | |
Thrillex13 | 0:bcd4927f951a | 18 | #include "RFDecoder.h" |
Thrillex13 | 0:bcd4927f951a | 19 | #include "mbed.h" |
Thrillex13 | 0:bcd4927f951a | 20 | #include <Pulse.h> |
Thrillex13 | 0:bcd4927f951a | 21 | |
Thrillex13 | 0:bcd4927f951a | 22 | |
Thrillex13 | 0:bcd4927f951a | 23 | /** Class constructor. |
Thrillex13 | 0:bcd4927f951a | 24 | * The constructor assigns the specified pinout, attatches |
Thrillex13 | 0:bcd4927f951a | 25 | * an Interrupt to the receive pin. |
Thrillex13 | 0:bcd4927f951a | 26 | * @param tx Transmitter pin of the RF module. |
Thrillex13 | 0:bcd4927f951a | 27 | * @param rx Receiver pin of the RF module. |
Thrillex13 | 0:bcd4927f951a | 28 | */ |
Thrillex13 | 0:bcd4927f951a | 29 | |
Thrillex13 | 0:bcd4927f951a | 30 | RFDecoder::RFDecoder(PinName tx, PinName rx) : _tx(DigitalOut(tx)), _rx(PulseInOut(rx)){ |
Thrillex13 | 0:bcd4927f951a | 31 | RFDecoder::addressFull = NULL; |
Thrillex13 | 0:bcd4927f951a | 32 | |
Thrillex13 | 0:bcd4927f951a | 33 | } |
Thrillex13 | 0:bcd4927f951a | 34 | |
Thrillex13 | 0:bcd4927f951a | 35 | unsigned long RFDecoder::getCode(){ |
Thrillex13 | 0:bcd4927f951a | 36 | return(addressFull); |
Thrillex13 | 0:bcd4927f951a | 37 | } |
Thrillex13 | 0:bcd4927f951a | 38 | |
Thrillex13 | 0:bcd4927f951a | 39 | bool RFDecoder::available(){ |
Thrillex13 | 0:bcd4927f951a | 40 | static bool startbit=0; //checks if start bit was identified |
Thrillex13 | 0:bcd4927f951a | 41 | static int counter; //received bits counter: 20 of Address + 4 of Data + 4 of EndCode (Anti-Code) |
Thrillex13 | 0:bcd4927f951a | 42 | static unsigned long buffer; //buffer for received data storage |
Thrillex13 | 0:bcd4927f951a | 43 | |
Thrillex13 | 0:bcd4927f951a | 44 | int dur0, dur1; // pulses durations (auxiliary) |
Thrillex13 | 0:bcd4927f951a | 45 | |
Thrillex13 | 0:bcd4927f951a | 46 | if (!startbit) { // Check the PILOT CODE until START BIT; |
Thrillex13 | 0:bcd4927f951a | 47 | dur0 = _rx.read_low_us(); |
Thrillex13 | 0:bcd4927f951a | 48 | |
Thrillex13 | 1:adf26c4f20cd | 49 | //If time at "0" is between 9200 us (23 cycles of 400us) and 13800 us (23 cycles of 600 us). |
Thrillex13 | 1:adf26c4f20cd | 50 | if((dur0 > 9200) && (dur0 < 13800) && !startbit){ |
Thrillex13 | 0:bcd4927f951a | 51 | //calculate wave length - lambda |
Thrillex13 | 0:bcd4927f951a | 52 | lambda_RX = (dur0 / 23); |
Thrillex13 | 0:bcd4927f951a | 53 | |
Thrillex13 | 0:bcd4927f951a | 54 | //Reset variables |
Thrillex13 | 0:bcd4927f951a | 55 | dur0 = 0; |
Thrillex13 | 0:bcd4927f951a | 56 | buffer = 0; |
Thrillex13 | 0:bcd4927f951a | 57 | counter = 0; |
Thrillex13 | 0:bcd4927f951a | 58 | |
Thrillex13 | 0:bcd4927f951a | 59 | startbit = true; |
Thrillex13 | 0:bcd4927f951a | 60 | } |
Thrillex13 | 0:bcd4927f951a | 61 | } |
Thrillex13 | 0:bcd4927f951a | 62 | |
Thrillex13 | 0:bcd4927f951a | 63 | //If Start Bit is OK, then starts measure os how long the signal is level "1" and check is value is into acceptable range. |
Thrillex13 | 0:bcd4927f951a | 64 | if (startbit && counter < 28){ |
Thrillex13 | 0:bcd4927f951a | 65 | ++counter; |
Thrillex13 | 0:bcd4927f951a | 66 | |
Thrillex13 | 0:bcd4927f951a | 67 | dur1 = _rx.read_high_us(); |
Thrillex13 | 0:bcd4927f951a | 68 | |
Thrillex13 | 0:bcd4927f951a | 69 | if((dur1 > 0.5 * lambda_RX) && (dur1 < (1.5 * lambda_RX))){ //If pulse width at "1" is between "0.5 and 1.5 lambda", means that pulse is only one lambda, so the data is "1". |
Thrillex13 | 0:bcd4927f951a | 70 | buffer = (buffer << 1) + 1; // add "1" on data buffer |
Thrillex13 | 0:bcd4927f951a | 71 | } |
Thrillex13 | 0:bcd4927f951a | 72 | else if((dur1 > 1.5 * lambda_RX) && (dur1 < (2.5 * lambda_RX))){ //If pulse width at "1" is between "1.5 and 2.5 lambda", means that pulse is two lambdas, so the data is "0". |
Thrillex13 | 0:bcd4927f951a | 73 | buffer = (buffer << 1); // add "0" on data buffer |
Thrillex13 | 0:bcd4927f951a | 74 | } |
Thrillex13 | 0:bcd4927f951a | 75 | else{ |
Thrillex13 | 0:bcd4927f951a | 76 | startbit = false; //Reset the loop |
Thrillex13 | 0:bcd4927f951a | 77 | } |
Thrillex13 | 0:bcd4927f951a | 78 | } |
Thrillex13 | 0:bcd4927f951a | 79 | |
Thrillex13 | 0:bcd4927f951a | 80 | //Check if all 28 bits were received (20 of Address + 4 of Data + 4 of Anti-Code) |
Thrillex13 | 0:bcd4927f951a | 81 | if (counter==28) |
Thrillex13 | 0:bcd4927f951a | 82 | { |
Thrillex13 | 0:bcd4927f951a | 83 | |
Thrillex13 | 0:bcd4927f951a | 84 | // Check if Anti-Code is OK (last 4 bits of buffer equal "0101") |
Thrillex13 | 0:bcd4927f951a | 85 | if ((bitRead(buffer, 0) == 1) && (bitRead(buffer, 1) == 0) && (bitRead(buffer, 2) == 1) && (bitRead(buffer, 3) == 0)){ |
Thrillex13 | 0:bcd4927f951a | 86 | counter = 0; |
Thrillex13 | 0:bcd4927f951a | 87 | startbit = false; |
Thrillex13 | 0:bcd4927f951a | 88 | |
Thrillex13 | 0:bcd4927f951a | 89 | //Get ADDRESS COMPLETO from Buffer |
Thrillex13 | 0:bcd4927f951a | 90 | addressFull = buffer; |
Thrillex13 | 0:bcd4927f951a | 91 | |
Thrillex13 | 0:bcd4927f951a | 92 | //If a valid data is received, return OK |
Thrillex13 | 0:bcd4927f951a | 93 | return true; |
Thrillex13 | 0:bcd4927f951a | 94 | } |
Thrillex13 | 0:bcd4927f951a | 95 | else |
Thrillex13 | 0:bcd4927f951a | 96 | { |
Thrillex13 | 0:bcd4927f951a | 97 | //Reset the loop |
Thrillex13 | 0:bcd4927f951a | 98 | startbit = false; |
Thrillex13 | 0:bcd4927f951a | 99 | } |
Thrillex13 | 0:bcd4927f951a | 100 | |
Thrillex13 | 0:bcd4927f951a | 101 | } |
Thrillex13 | 0:bcd4927f951a | 102 | |
Thrillex13 | 0:bcd4927f951a | 103 | //If none valid data is received, return NULL and FALSE values |
Thrillex13 | 0:bcd4927f951a | 104 | addressFull = 0; |
Thrillex13 | 0:bcd4927f951a | 105 | return false; |
Thrillex13 | 0:bcd4927f951a | 106 | } |
Thrillex13 | 0:bcd4927f951a | 107 | |
Thrillex13 | 0:bcd4927f951a | 108 | bool RFDecoder::bitRead( int N, int pos) |
Thrillex13 | 0:bcd4927f951a | 109 | { // pos = 7 6 5 4 3 2 1 0 |
Thrillex13 | 0:bcd4927f951a | 110 | int b = N >> pos ; // Shift bits |
Thrillex13 | 0:bcd4927f951a | 111 | b = b & 1 ; // get only the last bit |
Thrillex13 | 0:bcd4927f951a | 112 | return b ; |
Thrillex13 | 0:bcd4927f951a | 113 | } |
Thrillex13 | 0:bcd4927f951a | 114 |