Library to read 433MHz codes with decoder HT6P20

Dependents:   Gateway_Cotosys Gateway_Cotosys_os5

Files at this revision

API Documentation at this revision

Comitter:
Thrillex13
Date:
Sun Jun 02 19:56:03 2019 +0000
Child:
1:adf26c4f20cd
Commit message:
Version Inicial, Detecta los codigos a veces con error

Changed in this revision

RFDecoder.cpp Show annotated file Show diff for this revision Revisions of this file
RFDecoder.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RFDecoder.cpp	Sun Jun 02 19:56:03 2019 +0000
@@ -0,0 +1,123 @@
+/*
+   RFDecoder - Ported from the Lucas Maziero Arduino libary to decode RF controls based on chip HT6P20
+  Copyright (c) 2011 Miguel Maldonado.  All right reserved.
+  
+  Project home: https://github.com/lucasmaziero/RF_HT6P20
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+*/
+
+#include "RFDecoder.h"
+#include "mbed.h"
+#include <Pulse.h>
+
+
+/** Class constructor.
+ * The constructor assigns the specified pinout, attatches 
+ * an Interrupt to the receive pin. 
+ * @param tx Transmitter pin of the RF module.
+ * @param rx Receiver pin of the RF module.
+ */
+ 
+ RFDecoder::RFDecoder(PinName tx, PinName rx) : _tx(DigitalOut(tx)), _rx(PulseInOut(rx)){
+     RFDecoder::addressFull = NULL;     
+     
+     }
+ 
+unsigned long RFDecoder::getCode(){
+    return(addressFull);
+} 
+
+bool RFDecoder::available(){
+     static bool startbit=0;          //checks if start bit was identified
+     static int counter;            //received bits counter: 20 of Address + 4 of Data + 4 of EndCode (Anti-Code)
+     static unsigned long buffer;   //buffer for received data storage
+     
+     int dur0, dur1;                // pulses durations (auxiliary)
+        
+     if (!startbit) {               // Check the PILOT CODE until START BIT;
+     dur0 = _rx.read_low_us();
+/*    
+     if(dur0>50){
+         addressFull = dur0;
+         return true;      
+     }else{
+         return false;
+     }
+}
+*/
+     
+        //If time at "0" is between 5359 us (23 cycles of 233us) and 9959 us (23 cycles of 433 us).
+        if((dur0 > 5000) && (dur0 < 13000) && !startbit){
+            //calculate wave length - lambda
+            lambda_RX = (dur0 / 23);
+            
+            //Reset variables
+            dur0 = 0;
+            buffer = 0;
+            counter = 0;
+
+            startbit = true;    
+        }
+     }
+     
+     //If Start Bit is OK, then starts measure os how long the signal is level "1" and check is value is into acceptable range.
+     if (startbit && counter < 28){
+         ++counter;
+
+         dur1 = _rx.read_high_us();
+
+        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".     
+          buffer = (buffer << 1) + 1;   // add "1" on data buffer
+        }
+        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".
+          buffer = (buffer << 1);       // add "0" on data buffer
+        }
+        else{
+          startbit = false; //Reset the loop
+        }
+      }
+      
+      //Check if all 28 bits were received (20 of Address + 4 of Data + 4 of Anti-Code)
+      if (counter==28) 
+      { 
+      
+        // Check if Anti-Code is OK (last 4 bits of buffer equal "0101")
+        if ((bitRead(buffer, 0) == 1) && (bitRead(buffer, 1) == 0) && (bitRead(buffer, 2) == 1) && (bitRead(buffer, 3) == 0)){     
+          counter = 0;
+          startbit = false;
+    
+          //Get ADDRESS COMPLETO from Buffer
+          addressFull = buffer;
+          
+          //If a valid data is received, return OK
+          return true;
+        }
+        else
+        {
+          //Reset the loop
+          startbit = false;
+        }
+    
+      }
+      
+      //If none valid data is received, return NULL and FALSE values 
+      addressFull = 0; 
+      return false;       
+}
+
+bool RFDecoder::bitRead( int N, int pos)
+   {                            // pos = 7 6 5 4 3 2 1 0
+       int b = N >> pos ;       // Shift bits
+       b = b & 1 ;              // get only the last bit
+       return b ;
+   }   
+     
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/RFDecoder.h	Sun Jun 02 19:56:03 2019 +0000
@@ -0,0 +1,60 @@
+/*
+   RFDecoder - Ported from the Lucas Maziero Arduino libary to decode RF controls based on chip HT6P20
+  Copyright (c) 2011 Miguel Maldonado.  All right reserved.
+  
+  Project home: https://github.com/lucasmaziero/RF_HT6P20
+
+  This library is free software; you can redistribute it and/or
+  modify it under the terms of the GNU Lesser General Public
+  License as published by the Free Software Foundation; either
+  version 2.1 of the License, or (at your option) any later version.
+
+  This library is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+  Lesser General Public License for more details.
+*/
+#ifndef MBED_RF_HT6P20_H
+#define MBED_RF_HT6P20_H
+
+#include "mbed.h"
+#include <Pulse.h>
+
+
+class RFDecoder {
+    
+    public:
+ /** Class constructor.
+ * The constructor assigns the specified pinout, attatches 
+ * an Interrupt to the receive pin. 
+ * @param tx Transmitter pin of the RF module.
+ * @param rx Receiver pin of the RF module.
+ */
+    RFDecoder(PinName tx, PinName rx);
+    
+/**
+ * Get Code Value
+ * @return unsigned long The code received
+ */
+    unsigned long getCode();
+    
+/**
+ * Code available
+ * @return bool Code availiability
+ */
+    bool available();
+    bool bitRead( int N, int pos);
+    
+    
+    private:
+    DigitalOut _tx;
+    PulseInOut _rx;
+    
+    unsigned long addressFull;
+    int lambda_RX;
+    
+    
+};
+
+#endif
+