#include "stm32l0xx_hal.h"
#include "lt8920_test.h"
#include "lt8920.h"
#include "stm32l0xx_nucleo.h"
#include <stdio.h>
#include <string.h>

LT8920 lt;

uint16_t wifi_Channels[] = {2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447,
                            2452, 2457, 2462, 2467, 2472, 2484};
 
uint16_t signals[32];
char sbuf[64]; 
 
 
//
void lt_scanner_init(void)
{
    lt.begin();
    lt.setCurrentControl(15, 15);  
	//lt.setDataRate(LT8920::LT8920_1MBPS); 
	lt.setChannel(0x06);
}

//
void lt_scanner_test(void)
{
    printf("\033[2J"); // Очистить
    printf("\033[H");  // Переместить курсор в левый верхний угол
    
    for(int i = 0; i < sizeof(wifi_Channels)/sizeof(uint16_t); i++)
    {  
        lt.scanRSSI(&signals[i], wifi_Channels[i]-2402, 1); 
    }
    
    for(int i = 0; i < sizeof(wifi_Channels)/sizeof(uint16_t); i++)
    {
        sprintf(sbuf, "[%02d] %d = %04x ", i+1, wifi_Channels[i], signals[i]);
        printf(sbuf);    
        
        for(int j =0 ; j < signals[i]/2; j++)
        {
            printf("#");
        }
        printf("\r\n");
    }
    HAL_Delay(250);
}

//
void lt_common_init(void)
{
    lt.begin();
    lt.setCurrentControl(0, 0);
    lt.setDataRate(LT8920::LT8920_1MBPS);
    lt.setChannel(0x20);
}

//
void lt_sender_test(void)
{
    static uint8_t number = 0;
    char data[16] = {0};
    
    HAL_Delay(100);
    sprintf(data, "number: %d\n", number++);
    lt.sendPacket((uint8_t*)data, strlen(data) + 1);
}

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



//
void lt_receiver_test(void)
{
    if (lt.available())
    {
        uint8_t data[16];
        int packet_size = lt.read(data, 16);
        
        if (packet_size > 0)
        {
            printf("Packet read OK: %s\r\n", (char*)data);
        }
        else
        {
            printf("CRC is error!\r\n");
        }
        lt.startListening();
    }
}