New xbee sent protocol

Dependencies:   mbed C12832 XBee

main.cpp

Committer:
nnoitre
Date:
2016-03-23
Revision:
2:2bf280435a91
Parent:
1:9ffff91aeeb2
Child:
3:a7a7dc5e687f

File content as of revision 2:2bf280435a91:

#include "mbed.h"
#include "C12832.h"
#include "XBee.h"

DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut myled3(LED3);
DigitalOut myled4(LED4);

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=0b00000001;
char Item,Status,Checksum;

int i;

bool sending=1;

char control[4];
int teller;

Timer t;

void send(){
    xbee.putc(0x7E);
    xbee.putc(Source);
    xbee.putc(Des);
    xbee.putc(Item);
    xbee.putc(Status);
    xbee.putc(Checksum);
    xbee.putc(0x7E);
}

 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) {
        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(!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;
                    while(i<5){
                        control[i]=0;
                        i++;
                    }
                    i=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(t>=1){
            t.stop();
            t.reset();
            send();
            t.start();
        }
    }
}