Programm for decoding radio-signals sent by a ETH-Window-Shutter-Contact, received with a RFM12B-module

Dependencies:   TextLCD mbed

main.cpp

Committer:
charly
Date:
2011-03-02
Revision:
0:96794c9fc5a3
Child:
1:fc72e0bdb693

File content as of revision 0:96794c9fc5a3:

#include "mbed.h"

#include "TextLCD.h"

TextLCD lcd(p30, p29, p28, p27, p26, p25, TextLCD::LCD16x2); // rs, e, d0-d3

#include "eth_comfort.h"
#include "rfm.h"
#include "rfm12b.h"

rfm12b rfm12b_spi(p11, p12, p13, p14, p18); // mosi, miso, sclk, cs, rxdata


Serial pc(USBTX, USBRX); // tx, rx

// mbed LEDs
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);



//---------------------------------------------------------------------

void init(void)
{

  
      rbyte=0;
      bit_cnt=0;
      buffer_cnt=0;

      decode=0;
      
      pack_ok=0;           // this will be set by ISR, when byte was received
      
    
      // init the eth_receiver
      eth_init();
      
      // Interrupt on every bit-change
      rfm12b_spi.attachISR(&ISR);
      
      // Init the RFM12B
      rfm12b_spi.RFM_init();
      
}

//---------------------------------------------------------------------
//
//---------------------------------------------------------------------

int main()
{
uint8_t blocklength=0;
unsigned int i,j=0,k;
unsigned long timeout=0;
unsigned long tick=0;
uint16_t crc, swapped;
bool  crc_ok = false;

  pc.baud(115200);

  pc.printf("\n\rConnected to mbed\n\r");
  lcd.printf("Hello!\n");

  init();
  
                                                               // state =start
  startbit=0;
  do{

      if (pack_ok==1){
            timeout=0;
            pack_ok=0;
            j=0;
      };
      if(timeout<10000){
            timeout++;
      }
      else if(timeout==10000){
                  if(buffer_cnt>8){
                        if     (buf[2]==0x10) blocklength=10;
                        else if(buf[2]==0x20) blocklength=9;
                        else                  blocklength=99;
                        j=0;
                        crc_ok = false;
                        for(i=0;i<=buffer_cnt;i++){
                              pc.printf("%02X ",buf[i]);
                              j++;
                              if(j==blocklength){
                                    //check crc
                                    if(blocklength==9){
                                       crc=0xbdb7;
                                       for(k=0;k<7;k++){                           // crc over first 7 byte
                                           crc=calcCRC16r(buf[k],crc,0x8408);
                                       }
                                       //swap the two crc-bytes
                                       swapped = ((crc >> 8) & 0xff) | ((crc << 8) & 0xff00);
                                       pc.printf("CRC: %04X ",swapped);
                                       if (((buf[7]<<8) | buf[8]) == swapped) crc_ok = true;
                                       else crc_ok = false;
                                       pc.printf("%s", (crc_ok==true) ? "OK" : "Not OK");
                                       if (crc_ok) {
                                          pc.printf("\n\rCounter: %02X\n\r",buf[1]);
                                          pc.printf(    " Dev-ID: %02X %02X %02X\n\r",buf[3],buf[4],buf[5]);
                                          //pc.printf(    "Battery: %s\n\r", (buf[6]&0x80 != 0x00) ? "WEAK" : "GOOD");
                                          pc.printf(    "Window : %s\n\r\n\r", (buf[6]&0x01 != 0x00) ? "OPEN" : "CLOSE");
                                          lcd.cls();
                                          lcd.printf("#:%02X ID: %02X%02X%02X\n",buf[1],buf[3],buf[4],buf[5]);
                                          lcd.printf("Window : %s\n", (buf[6]&0x01 != 0x00) ? "OPEN" : "CLOSE");
                                       }
                                    }
                                    pc.printf("\n\r");
                              }
                         }
                        
                        
                        //start receive from beginning
                        buffer_cnt=0;
                        bit_cnt=0;
                        timeout++;
                        startbit=0;
                        state=0;

                        pc.printf("\n\r-----------------------------\n\r");
                        for(i=0;i<1023;i++)buf[i]=0;
                  };
                  timeout = 0;
      };
      tick++;
     //delay_ms(100);
     if(tick>=2000000){
            tick=0;
            led1= !led1;
            led3=0;               // reset receive-led
      };
}while(1==1);

}