Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Gateway_Cotosys Gateway_Cotosys_os5
RFDecoder.cpp@0:bcd4927f951a, 2019-06-02 (annotated)
- Committer:
- Thrillex13
- Date:
- Sun Jun 02 19:56:03 2019 +0000
- Revision:
- 0:bcd4927f951a
- Child:
- 1:adf26c4f20cd
Version Inicial, Detecta los codigos a veces con error
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 | 0:bcd4927f951a | 49 | if(dur0>50){ |
| Thrillex13 | 0:bcd4927f951a | 50 | addressFull = dur0; |
| Thrillex13 | 0:bcd4927f951a | 51 | return true; |
| Thrillex13 | 0:bcd4927f951a | 52 | }else{ |
| Thrillex13 | 0:bcd4927f951a | 53 | return false; |
| Thrillex13 | 0:bcd4927f951a | 54 | } |
| Thrillex13 | 0:bcd4927f951a | 55 | } |
| Thrillex13 | 0:bcd4927f951a | 56 | */ |
| Thrillex13 | 0:bcd4927f951a | 57 | |
| Thrillex13 | 0:bcd4927f951a | 58 | //If time at "0" is between 5359 us (23 cycles of 233us) and 9959 us (23 cycles of 433 us). |
| Thrillex13 | 0:bcd4927f951a | 59 | if((dur0 > 5000) && (dur0 < 13000) && !startbit){ |
| Thrillex13 | 0:bcd4927f951a | 60 | //calculate wave length - lambda |
| Thrillex13 | 0:bcd4927f951a | 61 | lambda_RX = (dur0 / 23); |
| Thrillex13 | 0:bcd4927f951a | 62 | |
| Thrillex13 | 0:bcd4927f951a | 63 | //Reset variables |
| Thrillex13 | 0:bcd4927f951a | 64 | dur0 = 0; |
| Thrillex13 | 0:bcd4927f951a | 65 | buffer = 0; |
| Thrillex13 | 0:bcd4927f951a | 66 | counter = 0; |
| Thrillex13 | 0:bcd4927f951a | 67 | |
| Thrillex13 | 0:bcd4927f951a | 68 | startbit = true; |
| Thrillex13 | 0:bcd4927f951a | 69 | } |
| Thrillex13 | 0:bcd4927f951a | 70 | } |
| Thrillex13 | 0:bcd4927f951a | 71 | |
| Thrillex13 | 0:bcd4927f951a | 72 | //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 | 73 | if (startbit && counter < 28){ |
| Thrillex13 | 0:bcd4927f951a | 74 | ++counter; |
| Thrillex13 | 0:bcd4927f951a | 75 | |
| Thrillex13 | 0:bcd4927f951a | 76 | dur1 = _rx.read_high_us(); |
| Thrillex13 | 0:bcd4927f951a | 77 | |
| Thrillex13 | 0:bcd4927f951a | 78 | 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 | 79 | buffer = (buffer << 1) + 1; // add "1" on data buffer |
| Thrillex13 | 0:bcd4927f951a | 80 | } |
| Thrillex13 | 0:bcd4927f951a | 81 | 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 | 82 | buffer = (buffer << 1); // add "0" on data buffer |
| Thrillex13 | 0:bcd4927f951a | 83 | } |
| Thrillex13 | 0:bcd4927f951a | 84 | else{ |
| Thrillex13 | 0:bcd4927f951a | 85 | startbit = false; //Reset the loop |
| Thrillex13 | 0:bcd4927f951a | 86 | } |
| Thrillex13 | 0:bcd4927f951a | 87 | } |
| Thrillex13 | 0:bcd4927f951a | 88 | |
| Thrillex13 | 0:bcd4927f951a | 89 | //Check if all 28 bits were received (20 of Address + 4 of Data + 4 of Anti-Code) |
| Thrillex13 | 0:bcd4927f951a | 90 | if (counter==28) |
| Thrillex13 | 0:bcd4927f951a | 91 | { |
| Thrillex13 | 0:bcd4927f951a | 92 | |
| Thrillex13 | 0:bcd4927f951a | 93 | // Check if Anti-Code is OK (last 4 bits of buffer equal "0101") |
| Thrillex13 | 0:bcd4927f951a | 94 | if ((bitRead(buffer, 0) == 1) && (bitRead(buffer, 1) == 0) && (bitRead(buffer, 2) == 1) && (bitRead(buffer, 3) == 0)){ |
| Thrillex13 | 0:bcd4927f951a | 95 | counter = 0; |
| Thrillex13 | 0:bcd4927f951a | 96 | startbit = false; |
| Thrillex13 | 0:bcd4927f951a | 97 | |
| Thrillex13 | 0:bcd4927f951a | 98 | //Get ADDRESS COMPLETO from Buffer |
| Thrillex13 | 0:bcd4927f951a | 99 | addressFull = buffer; |
| Thrillex13 | 0:bcd4927f951a | 100 | |
| Thrillex13 | 0:bcd4927f951a | 101 | //If a valid data is received, return OK |
| Thrillex13 | 0:bcd4927f951a | 102 | return true; |
| Thrillex13 | 0:bcd4927f951a | 103 | } |
| Thrillex13 | 0:bcd4927f951a | 104 | else |
| Thrillex13 | 0:bcd4927f951a | 105 | { |
| Thrillex13 | 0:bcd4927f951a | 106 | //Reset the loop |
| Thrillex13 | 0:bcd4927f951a | 107 | startbit = false; |
| Thrillex13 | 0:bcd4927f951a | 108 | } |
| Thrillex13 | 0:bcd4927f951a | 109 | |
| Thrillex13 | 0:bcd4927f951a | 110 | } |
| Thrillex13 | 0:bcd4927f951a | 111 | |
| Thrillex13 | 0:bcd4927f951a | 112 | //If none valid data is received, return NULL and FALSE values |
| Thrillex13 | 0:bcd4927f951a | 113 | addressFull = 0; |
| Thrillex13 | 0:bcd4927f951a | 114 | return false; |
| Thrillex13 | 0:bcd4927f951a | 115 | } |
| Thrillex13 | 0:bcd4927f951a | 116 | |
| Thrillex13 | 0:bcd4927f951a | 117 | bool RFDecoder::bitRead( int N, int pos) |
| Thrillex13 | 0:bcd4927f951a | 118 | { // pos = 7 6 5 4 3 2 1 0 |
| Thrillex13 | 0:bcd4927f951a | 119 | int b = N >> pos ; // Shift bits |
| Thrillex13 | 0:bcd4927f951a | 120 | b = b & 1 ; // get only the last bit |
| Thrillex13 | 0:bcd4927f951a | 121 | return b ; |
| Thrillex13 | 0:bcd4927f951a | 122 | } |
| Thrillex13 | 0:bcd4927f951a | 123 |