#include <msp430.h>
#include "cc110l.h"

extern uint8_t *debug;
uint8_t len;

//#define BLINK1 0x40
//#define BLINK0 0x01

#define BLINK1 0x08
#define BLINK0 0x01

void delay_ms(uint16_t ms) {
    while (ms--)
        __delay_cycles(1000);
}

void blink(uint8_t val) {
    uint8_t bit = 7;

    while(1) {
        if (val & (1 << bit))
            P1OUT |= BLINK1;
        else
            P1OUT |= BLINK0;

        delay_ms(200);

        P1OUT &= ~(BLINK0 | BLINK1);
        
        delay_ms(100);

        if (bit == 0)
            break;

        bit--;
    }
}

uint8_t *gbuf;

int main() {
    char buf[16];

    uint8_t a = 0;
    for (a = 0; a < 16; a++)
        buf[a] = a + 1;

    // Disable watchdog
    WDTCTL = WDTPW | WDTHOLD;

    // 1MHz internal timer
    BCSCTL1 = CALBC1_1MHZ;
    DCOCTL = CALDCO_1MHZ;

    // P2 (XOUT/XIN) as GPIO
    P2SEL = 0x00;
    
    // Radio MOSI, CLK, LEDs as output
    P1DIR = 0x20 | 0x80 | BLINK0 | BLINK1;
    
    // Radio CS as output
    P2DIR = 0x80;
    
    // Clear outputs
    P1OUT = 0x00;
    P2OUT = 0x00;
    
    //delay_ms(200);

    //blink(0x04);

    delay_ms(200);

    //blink(radio_get_status());

    P1OUT |= BLINK0;
    radio_init();
    
    delay_ms(200);
    P1OUT &= ~BLINK0;
    P1OUT |= BLINK1;
    
    gbuf = buf;
    len = radio_recv((uint8_t *)buf);

    P1OUT &= ~(BLINK1 | BLINK0);

    delay_ms(500);

    P1OUT |= BLINK1 | BLINK0;

    while(1) ;

    return 0;
}
