miniProject-Wireless Pong

main.cpp

Committer:
cmulady
Date:
2012-10-04
Revision:
6:5563f0026858
Parent:
5:2e08cc167fff
Child:
7:c9ff6b5c8507

File content as of revision 6:5563f0026858:

#include "mbed.h"
#include "rtos.h"
#include "NokiaLCD.h"
#include "XMIT_IR.h"

#define FPS 5
#define UART_TIMEOUT 500

/****************************************
|=======================================|
|MBED Connections:                      |
|   -p5 : DIO on Sparkfun Nokia LCD     |
|   -p7 : CLK on Sparkfun Nokia LCD     |
|   -p8 : CS  on Sparkfun Nokia LCD     |
|   -p9 : RST on Sparkfun Nokia LCD     |
|   -p22: GND on Sparkfun IR Xmtr       |
|=======================================|
****************************************/

//Pin Setup
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);
NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
Serial device(p13, p14);  // tx, rx
Serial pc(USBTX,USBRX);
PwmOut IRLED_mod(p21);

//Global Vars
char buffer[32];
unsigned char irdata_out=0;
unsigned char irdata_in=0;
char irdatOUT[10];
char irdatIN[10];
char error_code=0;

//Function Prototypes
void BlinkAlive(void const* arguments);
void UpdateLCD(void const* arguments);
void IRStuff(void const* arguments);
void MakePacket(char* data,int len);
char CheckPacket(char* data, int data_size);


int main()
{

    //LCD init
    lcd.background(0x000000);

    //PWM init
    IRLED_mod.period(1.0/38000.0);  //38kHz Modulation Freq
    IRLED_mod = 0.5;                //pulse width = 50%

    //Serial init
    device.baud(2400);

    //PC serial init
    pc.baud(19200);

    //Thread init
    Thread thread_blinkalive(BlinkAlive);
    Thread thread_updatelcd(UpdateLCD);
    Thread thread_irstuff(IRStuff);



    while(1) {
        thread_updatelcd.signal_set(0x1);
        Thread::wait(1000/FPS);

    }
}

void UpdateLCD(void const* arguments)
{
    /*while(true) {
        led2 = 1;
        lcd.locate(0,1);
        lcd.printf("Debug:");

        lcd.locate(0,3);
        time_t seconds = time(NULL);
        strftime(buffer, 32, "%I:%M:%S %p\n", localtime(&seconds));
        lcd.printf("%s", buffer);

        lcd.locate(0,4);
        lcd.printf("IR_OUT=0x%02X,0x%02X", irdatOUT[0],irdatOUT[1]);
        lcd.locate(0,5);
        lcd.printf("IR_IN= 0x%02X,0x%02X", irdatIN[0],irdatIN[1]);
        lcd.locate(0,6);
        lcd.printf("Error= 0x%02X", error_code);



        //End - Sleep thread
        led2 = 0;
        Thread::signal_wait(0x1);
    }*/
}

void IRStuff(void const* arguments)
{
    while(true) {
        //error_code = CheckPacket(irdatIN,2);
        
        while(device.readable())
        {
            char tempdata = device.getc();
            if(tempdata==0x02) pc.printf("\n");
            pc.printf("0x%02X.",tempdata);
        }
        
        //pc.printf("\n\nERROR=0x%02X\n\n",error_code);
        Thread::wait(3000);
    }

}

void BlinkAlive(void const* arguments)
{
    while(true) {
        led1 = !led1;

        irdatOUT[0] = 0xA5;
        irdatOUT[1] = ++irdata_out;
        MakePacket(irdatOUT,2);

        Thread::wait(20);
    }
}

void MakePacket(char* data,int len)
{
    //pc.printf("\nMaking Packet:\n\t");
    char check =0x0;
    device.putc(0x02);
    //pc.printf("0x%02X.",0x02);
    for(int i=0; i<len; i++) {
        check^=data[i];
        device.putc(data[i]);
        //pc.printf("0x%02X.",data[i]);
    }
    device.putc(check);
    //pc.printf("0x%02X.",check);
    //pc.printf("\nDone making packet.\n");
}

char  CheckPacket(char* data, int data_size)
//returns success(0) or failure(error code)
{
    Timer t;
    t.reset();
    t.start();
    char tempdata=0x0;
    char check=0x0;

    pc.printf("\nChecking Packet\n\t");
    //Data available
    if(!device.readable()) {
        pc.printf("no data");
        return 0x4; //no data
    }

    while(!device.readable());
    //while(!device.readable() && t.read_ms()<=UART_TIMEOUT);
    //if(t.read_ms()>=UART_TIMEOUT)
    //    return 0x3; //timeout error
    tempdata = device.getc();

    //STX recieved
    if(tempdata!=0x02) {
        pc.printf("found bad data: 0x%02X",tempdata);
        if(tempdata==0xFF) device.getc(); //flush
        return 0x1; //bad start byte
    }

    //Get Data
    for(int i=0; i<data_size; i++) {
        while(!device.readable());
        //while(!device.readable() && t.read_ms()<=UART_TIMEOUT);
        //if(t.read_ms()>=UART_TIMEOUT)
        //    return 0x3; //timeout error
        data[i] = device.getc();
        check ^= data[i];
    }
    //Get Checksum
    while(!device.readable());
    //while(!device.readable() && t.read_ms()<=UART_TIMEOUT);
    //if(t.read_ms()>=UART_TIMEOUT)
    //    return 0x3; //timeout error
    tempdata = device.getc();
    if(tempdata!=check)
        return 0x2; //bad checksum


    return 0;

}