Guest User

hcs301

a guest
Sep 24th, 2016
2,574
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.83 KB | None | 0 0
  1. #define LED_PIN     13
  2. #define HCS_RECIEVER_PIN  2 // pin is connected to a receiver for remote controls
  3.  
  4. class HCS301 {
  5. public:
  6. unsigned BattaryLow :
  7.  1;  
  8. unsigned Repeat :
  9.  1;
  10. unsigned BtnNoSound :
  11.  1;
  12. unsigned BtnOpen :
  13.  1;
  14. unsigned BtnClose :
  15.  1;
  16. unsigned BtnRing :
  17.  1;
  18.  unsigned long SerialNum;
  19.  unsigned long Encript;
  20.  void print();
  21. };
  22.  
  23. volatile boolean    HCS_Listening = true;      
  24. byte                HCS_preamble_count = 0;
  25. uint32_t            HCS_last_change = 0;
  26. //uint32_t          HCS_start_preamble = 0;
  27. uint8_t             HCS_bit_counter;                
  28. uint8_t             HCS_bit_array[66];              
  29. #define             HCS_TE      400                
  30. #define             HCS_Te2_3   600                 // HCS_TE * 3 / 2
  31.  
  32. HCS301 hcs301;
  33.  
  34. void setup()
  35. {
  36.  Serial.begin(115200);
  37.  pinMode(HCS_RECIEVER_PIN, INPUT);
  38.  attachInterrupt(0, HCS_interrupt, CHANGE);
  39.  Serial.println("Setup OK");
  40. }
  41.  
  42. void loop()
  43. {
  44.  long CurTime = millis();
  45.  
  46.  if(HCS_Listening == false){
  47.    HCS301 msg;
  48.    memcpy(&msg,&hcs301,sizeof(HCS301));
  49.    HCS_Listening = true;
  50.    Serial.println(String("KeyFb#")+String(msg.SerialNum));
  51.  }
  52. }
  53.  
  54. void HCS301::print(){
  55.  String btn;
  56.  if (BtnRing == 1) btn += "Ring";
  57.  if (BtnClose == 1) btn += "Close";
  58.  if (BtnOpen == 1) btn += "Open";
  59.  if (BtnNoSound == 1) btn += "NoSound";
  60.  String it2;
  61.  it2 += "Encript ";
  62.  it2 += Encript;
  63.  it2 += " Serial ";
  64.  it2 += SerialNum;
  65.  it2 += " ";
  66.  it2 += btn;
  67.  it2 += " BattaryLow=";
  68.  it2 += BattaryLow;
  69.  it2 += " Rep=";
  70.  it2 += Repeat;
  71.  Serial.println(it2);
  72. }
  73.  
  74. void HCS_interrupt(){
  75.  if(HCS_Listening == false){
  76.    return;
  77.  }
  78.  uint32_t cur_timestamp = micros();
  79.  uint8_t  cur_status = digitalRead(HCS_RECIEVER_PIN);
  80.  uint32_t pulse_duration = cur_timestamp - HCS_last_change;
  81.  HCS_last_change         = cur_timestamp;
  82.  if(HCS_preamble_count < 12){
  83.    if(cur_status == HIGH){
  84.      if( ((pulse_duration > 150) && (pulse_duration < 500)) || HCS_preamble_count == 0){
  85.  
  86.        //if(HCS_preamble_count == 0){
  87.        //  HCS_start_preamble = cur_timestamp;
  88.        //}
  89.      }
  90.      else {
  91.        
  92.        HCS_preamble_count = 0;
  93.        goto exit;
  94.  
  95.      }
  96.    }
  97.    else {
  98.  
  99.      if((pulse_duration > 300) && (pulse_duration < 600)){
  100.  
  101.        HCS_preamble_count ++;
  102.        if(HCS_preamble_count == 12){
  103.  
  104.          //HCS_Te = (cur_timestamp - HCS_start_preamble) / 23;  
  105.          //HCS_Te2_3 = HCS_Te * 3 / 2;
  106.          HCS_bit_counter = 0;
  107.          goto exit;
  108.        }
  109.      }
  110.      else {
  111.  
  112.        HCS_preamble_count = 0;
  113.        goto exit;
  114.      }
  115.    }
  116.  }
  117.  
  118.  if(HCS_preamble_count == 12){
  119.    if(cur_status == HIGH){
  120.      if(((pulse_duration > 250) && (pulse_duration < 900)) || HCS_bit_counter == 0){
  121.  
  122.      }
  123.      else {
  124.  
  125.        HCS_preamble_count = 0;
  126.        goto exit;
  127.      }
  128.    }
  129.    else {
  130.  
  131.      if((pulse_duration > 250) && (pulse_duration < 900)){
  132.        HCS_bit_array[65 - HCS_bit_counter] = (pulse_duration > HCS_Te2_3) ? 0 : 1;
  133.        HCS_bit_counter++;
  134.        if(HCS_bit_counter == 66){
  135.  
  136.          HCS_Listening = false;
  137.          HCS_preamble_count = 0;
  138.          hcs301.Repeat = HCS_bit_array[0];
  139.          hcs301.BattaryLow = HCS_bit_array[1];
  140.          hcs301.BtnNoSound = HCS_bit_array[2];
  141.          hcs301.BtnOpen = HCS_bit_array[3];
  142.          hcs301.BtnClose = HCS_bit_array[4];
  143.          hcs301.BtnRing = HCS_bit_array[5];
  144.          hcs301.SerialNum = 0;
  145.          for(int i = 6; i < 34;i++){
  146.            hcs301.SerialNum = (hcs301.SerialNum << 1) + HCS_bit_array[i];
  147.          };
  148.          uint32_t Encript = 0;
  149.          for(int i = 34; i < 66;i++){
  150.            Encript = (Encript << 1) + HCS_bit_array[i];
  151.          };
  152.          hcs301.Encript = Encript;
  153.        }
  154.      }
  155.      else {
  156.  
  157.        HCS_preamble_count = 0;
  158.        goto exit;
  159.      }
  160.    }
  161.  }
  162. exit:;
  163.  //digitalWrite(LED_PIN,cur_status);
  164. }
Advertisement
Add Comment
Please, Sign In to add comment