#define USE_AND_OR

#include "p24F04KA200.h"
#include <stdio.h>
#include <string.h>
#include <uart.h>
#include "xbee.h"

#define MAX_PACKET_SIZE     100

#define PACKET_HEADER         0
#define PACKET_LEN_MSB        1
#define PACKET_LEN_LSB        2
#define PACKET_TYPE           3

#define TXPACKET_FRAMEID      4
#define TXPACKET_ADDR64       5
#define TXPACKET_ADDR16      13
#define TXPACKET_BROADCAST   15
#define TXPACKET_OPTIONS     16
#define TXPACKET_RFDATA      17

#define RXPACKET_ADDR64       4
#define RXPACKET_ADDR16      12
#define RXPACKET_OPTIONS     14
#define RXPACKET_RFDATA      15

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

void xbee_init()
{
    CloseUART1();
    OpenUART1(UART_EN, UART_TX_ENABLE, 25);
}
// -----------------------------------------------------------------------------

void xbee_putchar(byte c, byte *checksum)
{
    while (BusyUART1()) ;
    WriteUART1(c);
    
    if (checksum != NULL)
        *checksum = *checksum + c;
}
// -----------------------------------------------------------------------------

void xbee_sendString(char *str)
{
    byte packet[17];
    byte checksum = 0, i;

    memset(packet, 0, 17);
    
    packet[PACKET_HEADER]  = 0x7E;
    packet[PACKET_LEN_MSB] = 0;
    packet[PACKET_LEN_LSB] = strlen(str) + 14;
    packet[PACKET_TYPE]    = 0x10;
    
    packet[TXPACKET_ADDR16]   = 0xFF;
    packet[TXPACKET_ADDR16+1] = 0xFE;

    for (i = 0; i < 3; i++)
        xbee_putchar(packet[i], NULL);
    for (i = 3; i < 17; i++)
        xbee_putchar(packet[i], &checksum);

    while (*str)
        xbee_putchar(*str++, &checksum);

    xbee_putchar(0xFF - checksum, NULL);
}
// -----------------------------------------------------------------------------

void xbee_led(byte on)
{
    byte packet[17];
    byte checksum = 0, i;

    memset(packet, 0, 17);
    
    packet[PACKET_HEADER]  = 0x7E;
    packet[PACKET_LEN_MSB] = 0;
    packet[PACKET_LEN_LSB] = 5;
    packet[PACKET_TYPE]    = 0x08;
    
    packet[5] = 'D';
    packet[6] = '0';
    packet[7] = 4;

    for (i = 0; i < 3; i++)
        xbee_putchar(packet[i], NULL);
    for (i = 3; i < 8; i++)
        xbee_putchar(packet[i], &checksum);
    
    xbee_putchar(0xFF - checksum, NULL);
    
    checksum = 0;
    packet[6] = '1';
    packet[7] = on ? 5 : 4;
    
    for (i = 0; i < 3; i++)
        xbee_putchar(packet[i], NULL);
    for (i = 3; i < 8; i++)
        xbee_putchar(packet[i], &checksum);
    
    xbee_putchar(0xFF - checksum, NULL);
}    
// -----------------------------------------------------------------------------

void xbee_remoteLed(byte on)
{
    byte packet[20];
    byte checksum = 0, i;

    memset(packet, 0, 20);
    
    packet[PACKET_HEADER]  = 0x7E;
    packet[PACKET_LEN_MSB] = 0;
    packet[PACKET_LEN_LSB] = 16;
    packet[PACKET_TYPE]    = 0x17;
    
    packet[13] = 0xFF;
    packet[14] = 0xFE;
    packet[15] = 0x02;
    packet[16] = 'D';
    packet[17] = '4';
    packet[18] = on ? 4 : 5;

    for (i = 0; i < 3; i++)
        xbee_putchar(packet[i], NULL);
    for (i = 3; i < 19; i++)
        xbee_putchar(packet[i], &checksum);
    
    xbee_putchar(0xFF - checksum, NULL);
}
// -----------------------------------------------------------------------------

void xbee_packetReceived(byte *packet)
{
    if (packet[PACKET_TYPE] == 0x90)
    {
        dataReceived(packet+RXPACKET_RFDATA, packet[PACKET_LEN_LSB]-12);
    }    
}    
// -----------------------------------------------------------------------------

void xbee_readyRead()
{
    static byte packet[MAX_PACKET_SIZE];
    static word idx = 0;
    static word len = 0;
    static byte dataValid = 0;
    
    byte b;

    while (DataRdyUART1())
    {
        b = ReadUART1();

        if ((idx == 0) && (b == 0x7E))
            dataValid = 1;
            
        if (dataValid)
            packet[idx++] = b;
        
        if (idx == 4)
        {
            len = ((word)(packet[1]) << 8) + packet[2];
            if (len > MAX_PACKET_SIZE-4)
            {
                len = 0;
                idx = 0;
                dataValid = 0;
            }
        }
        
        if (idx == len+4)
        {
            xbee_packetReceived(packet);
            idx = 0;
            len = 0;
            dataValid = 0;
        }
    }
}
// -----------------------------------------------------------------------------
