New xbee sent protocol
Dependencies: mbed C12832 XBee
Diff: main.cpp
- Revision:
- 1:9ffff91aeeb2
- Parent:
- 0:ce2085295a51
- Child:
- 2:2bf280435a91
--- a/main.cpp Thu Mar 10 14:20:19 2016 +0000 +++ b/main.cpp Tue Mar 22 12:01:02 2016 +0000 @@ -1,84 +1,216 @@ #include "mbed.h" +#include "C12832.h" +#include "XBee.h" DigitalOut myled1(LED1); DigitalOut myled2(LED2); DigitalOut myled3(LED3); DigitalOut myled4(LED4); -DigitalIn down(p12); -DigitalIn left(p13); -DigitalIn up(p15); -DigitalIn right(p16); +InterruptIn down(p12); +InterruptIn left(p13); +InterruptIn up(p15); +InterruptIn right(p16); Serial xbee(p9,p10); +C12832 lcd(p5, p7, p6, p8, p11); //Used for the LCD + char Source=0b00000010; -char Des,Item,Status,Checksum; +char Des=0b00000001; +char Item,Status,Checksum; + +bool sending=1; +char control[4]; +int teller; +Timer t; void send(){ xbee.putc(0x7E); - myled1=1; - wait(0.01); xbee.putc(Source); - myled2=1; - wait(0.01); xbee.putc(Des); - myled3=1; - wait(0.01); xbee.putc(Item); - myled4=1; - wait(0.01); xbee.putc(Status); - myled1=0; - wait(0.01); xbee.putc(Checksum); - myled2=0; - wait(0.1); xbee.putc(0x7E); - myled3=0; - wait(0.1); - xbee.putc(0xD); - myled4=0; +} + + void CalChecksumup(){ + Status=0b00000000; + myled1=1; + lcd.cls(); + lcd.locate(0,3); + if(up.read()&&!left.read()&&!right.read()){ + lcd.printf("up"); + Item=0b00000000; + } + if(up.read()&&right.read()){ + lcd.printf("upright"); + Item=0b00000001; + } + if(right.read()&&!up.read()&&!down.read()){ + lcd.printf("right"); + Item=0b00000010; + } + if(down.read()&&right.read()){ + lcd.printf("downright"); + Item=0b00000011; + } + if(down.read()&&!left.read()&&!right.read()){ + lcd.printf("down"); + Item=0b00000100; + } + if(down.read()&&left.read()){ + lcd.printf("downleft"); + Item=0b00000101; + } + if(left.read()&&!up.read()&&!down.read()){ + lcd.printf("left"); + Item=0b00000110; + } + if(up.read()&&left.read()){ + lcd.printf("upleft"); + Item=0b00000111; + } + Checksum=(Source+Des+Item+Status)%255; + sending=0; + send(); + t.start(); +} + + void CalChecksumdown(){ + Status=0b00000001; + myled1=0; + lcd.cls(); + lcd.locate(0,3); + if(Item==0b00000000){ + lcd.printf("back"); + Item=0b00000000; + } + if(Item==0b00000001){ + lcd.printf("back"); + Item=0b00000001; + if(up.read()){ + Item=0b00000000; + Status=0b00000000; + send(); + } + if(right.read()){ + Item=0b00000010; + Status=0b00000000; + send(); + } + } + if(Item==0b00000010){ + lcd.printf("back"); + Item=0b00000010; + } + if(Item==0b00000011){ + if(right.read()){ + Item=0b00000010; + Status=0b00000000; + send(); + } + if(down.read()){ + Item=0b00000100; + Status=0b00000000; + send(); + } + lcd.printf("back"); + Item=0b00000011; + } + if(Item==0b00000100){ + lcd.printf("back"); + Item=0b00000100; + } + if(Item==0b00000101){ + if(down.read()){ + Item=0b00000100; + Status=0b00000000; + send(); + } + if(left.read()){ + Item=0b00000110; + Status=0b00000000; + send(); + } + lcd.printf("back"); + Item=0b00000101; + } + if(Item==0b00000110){ + lcd.printf("back"); + Item=0b00000110; + } + if(Item==0b00000111){ + if(left.read()){ + Item=0b00000110; + Status=0b00000000; + send(); + } + if(up.read()){ + Item=0b00000000; + Status=0b00000000; + send(); + } + lcd.printf("back"); + Item=0b00000111; + } + Checksum=(Source+Des+Item+Status)%255; + sending=0; + send(); + t.start(); } int main() { + xbee.baud(57600); while(1) { - xbee.baud(57600); - if(up&&!left&&!right){ - myled1 = 1; - } - if(up&&right){ - myled1=1; - myled2=1; - } - if(right&&!up&&!down){ - myled2 = 1; - } - if(down&&right){ - myled2=1; - myled3=1; - } - if(down&&!left&&!right){ - myled3 = 1; + if(sending){ + up.rise(&CalChecksumup); + up.fall(&CalChecksumdown); + down.rise(&CalChecksumup); + down.fall(&CalChecksumdown); + left.rise(&CalChecksumup); + left.fall(&CalChecksumdown); + right.rise(&CalChecksumup); + right.fall(&CalChecksumdown); } - if(down&&left){ - myled3=1; - myled4=1; - } - if(left&&!up&&!down){ - myled4 = 1; + if(!sending){ + if(xbee.readable()) { //Checks if the data is readable + xbee.gets(control,5); //Get the first character that's in the buffer + if(control[3]==0b00001111){ + sending=1; + lcd.cls(); + lcd.locate(0,3); + lcd.printf("ACK"); + t.stop(); + t.reset(); + teller=0; + } + if(control[3]==0b11110000){ + lcd.cls(); + lcd.locate(0,3); + lcd.printf("NACK"); + if(teller<10){ + send(); + teller++; + } + if(teller==10){ + sending=1; + teller=0; + } + lcd.cls(); + lcd.locate(0,3); + lcd.printf("NO REACTION"); + } + } } - if(up&&left){ - myled1=1; - myled4=1; + if(t>=1){ + t.stop(); + t.reset(); + send(); + t.start(); } - wait(0.2); - myled1=0; - myled2=0; - myled3=0; - myled4=0; - send(); } -} +} \ No newline at end of file