#include <avr/io.h>
#include <util/delay.h>

#include "timer.h"
#include "uart.h"
#include "twi.h"
#include "si5351.h"
#include "wspr.h"

int main(void) {

    uart_init();
    twi_init();

    timer_init();

    sei();

    si5351_init(25000000, 800000000, 500000000);
    si5351_ms_set_source(SI5351_MS0, SI5351_PLLA);

    //si5351_ms_set_freq(SI5351_MS0, 7040100);
    //si5351_ms_enable_output(SI5351_MS0);
    //si5351_ms_write_params(SI5351_MS0, WSPR_SYMBOLS[3]);


    uint8_t wspr_buffer[WSPR_LENGTH];
    wspr_encode("DL1SSK", "JN39", 27, wspr_buffer);

    char line[16];
    while(1) {
        uint8_t res = 0;
        while(res != 1) {
            uart_puts("?\n");
            res = uart_get_line(line, 16);
        }

        uint32_t realtime = strtoul(line,NULL,10);
        timer_set(realtime);
        uart_puts("Time set\n");

        while(timestamp % 120 != 1) {
            _delay_ms(25);
        }

        wspr_transmit(SI5351_MS0, wspr_buffer);
        uart_puts("Transmission send\n");
    }


    while(1);
}
