Created
October 29, 2012 12:54
-
-
Save Crenshinibon/3973365 to your computer and use it in GitHub Desktop.
Carrier signal receiving simple RF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
byte leftSideData[MAX_LEFTSIDE_KEYS]={0}; | |
uint8_t receivingIndex = 0; | |
boolean receiving = false; | |
const byte CARRIER=B10101010,PRE=0x5E,ADDR=0x10,NOTHING=0xFE,ERROR=0x77; | |
int receiveState = CARRIER; | |
const byte START = 0xD0; | |
const byte STOP = 0xDF; | |
static void receiveLeftKeyState(){ | |
//package begin flag received | |
if(!receiving && receivePackage() == START){ | |
receiving = true; | |
} | |
if(receiving){ | |
byte data = receivePackage(); | |
if(data != NOTHING && | |
data != STOP && | |
data != ERROR && | |
receivingIndex < MAX_LEFTSIDE_KEYS){ | |
leftSideData[receivingIndex]=data; | |
receivingIndex++; | |
} | |
else if(data == STOP || receivingIndex == MAX_LEFTSIDE_KEYS){ | |
receiving = false; | |
stateChanged = true; | |
integrateLeftKeys(); | |
receivingIndex = 0; | |
memset(leftSideData,0x00,MAX_LEFTSIDE_KEYS); | |
} | |
else if(data == ERROR){ | |
receiving = false; | |
receivingIndex = 0; | |
memset(leftSideData,0x00,MAX_LEFTSIDE_KEYS); | |
} | |
} | |
} | |
static byte receivePackage(){ | |
byte erg = NOTHING; | |
//check whole packages available | |
if(Serial434.available()){ | |
if(receiveState == CARRIER){ | |
byte in = Serial434.read(); | |
if(in == PRE){ | |
receiveState = PRE; | |
} | |
} | |
if(receiveState == PRE) { | |
if(Serial434.available() > 2){ | |
byte addr = Serial434.read(); | |
byte data = Serial434.read(); | |
byte chck = Serial434.read(); | |
if(addr == ADDR && addr + data == chck){ | |
erg = data; | |
} | |
else{ | |
erg = ERROR; | |
} | |
receiveState = CARRIER; | |
} | |
} | |
} | |
return erg; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment