/**************************************************************************//**
  \file boardabstraction.c

  \brief This module control hw access

  \author

  \internal
    History:
******************************************************************************/

/*****************************************************************************\*
Includes section
******************************************************************************/
#include <appTimer.h>
#include <string.h>
#include <types.h>
#include "blank.h"
#include "boardabstraction.h"


/*****************************************************************************\*
Types section
******************************************************************************/
// namerena data
uint16_t adcData = 0; 



// USART Tx buffer
uint8_t usartTxBuffer[USART_TX_BUFFER_SIZE];


// blink timer
static HAL_AppTimer_t blinkTimer = {
 .interval = LED_BLINK_INTERVAL,
 .mode = TIMER_REPEAT_MODE,
};

// timer pro blink led red, yellow
static HAL_AppTimer_t blinkTimer1 = {
 .interval = LED_BLINK_INTERVAL,
 .mode = TIMER_ONE_SHOT_MODE,
};



static void NetworkJoiningFired();
static void BlinkRed();
static void BlinkYellow();
static void adcReadInd();

// USART descriptor init (required by stack)
static HAL_UsartDescriptor_t usartDescriptor =
{
  .tty             = APP_USART_CHANNEL,
  .mode            = USART_MODE_ASYNC,
  .baudrate        = APP_USART_BAUDRATE,
  .dataLength      = USART_DATA8,
  .parity          = USART_PARITY_NONE,
  .stopbits        = USART_STOPBIT_1,
  .rxBuffer        = NULL,
  .rxBufferLength  = 0,
  .txBuffer        = usartTxBuffer,
  .txBufferLength  = USART_TX_BUFFER_SIZE,
  .rxCallback      = NULL,
  .txCallback      = NULL,
  .flowControl     = USART_FLOW_CONTROL_NONE,
};





		
	
/*****************************************************************************\*
  Private functions
******************************************************************************/

void NetworkSendingDataIndication()
{
  BSP_OnLed(LED_YELLOW);
}

void NetworkSendingDataEndIndication()
{
  BSP_OffLed(LED_YELLOW);
}


void NetworkJoiningIndication()
{
  HAL_StopAppTimer(&blinkTimer);
  BSP_OffLed(LED_RED);
  BSP_OffLed(LED_YELLOW);
  BSP_OffLed(LED_GREEN);

  blinkTimer.callback = NetworkJoiningFired;
  HAL_StartAppTimer(&blinkTimer);
}


void NetworkJoinedIndication()
{
  HAL_StopAppTimer(&blinkTimer);
  BSP_OffLed(LED_RED);
  BSP_OffLed(LED_YELLOW);
  BSP_OnLed(LED_GREEN);
}


void NetworkFailedIndication()
{
  HAL_StopAppTimer(&blinkTimer);
  BSP_OnLed(LED_RED);
  BSP_OffLed(LED_YELLOW);
  BSP_OffLed(LED_GREEN);
}


static void NetworkJoiningFired()
{
  BSP_ToggleLed(LED_GREEN);
}
static void BlinkRed()
{
  BSP_OffLed(LED_RED);
}

static void BlinkYellow()
{
  BSP_OffLed(LED_YELLOW);
}


//Nacten dat z ADC prevodniku
uint16_t ReadAdc(){
	if(HAL_ReadAdc(HAL_ADC_CHANNEL1) == -1)
		SendStrToConsole("ADC cteni selhalo!\r\n");
	else
		SendStrToConsole("ADC cteni OK.\r\n");
	return adcData;
	
}



/*****************************************************************************\*
  Public functions
******************************************************************************/


/********************************************************************
  Description:  init
  Parameters:   none
  Returns:      none
********************************************************************/
void InitHW(void)
{
 static char str[30]; 
 ShortAddr_t nwkAddr;
 int8_t txPwr;		// pouze u rooteru

 CS_Init();
 CS_ReadParameter(CS_NWK_ADDR_ID, &nwkAddr);
 snprintf(str, sizeof(str), "Uzel:0x%04X\r\n", nwkAddr);
 HAL_OpenUsart(&usartDescriptor);
 SendStrToConsole(str);
 
 BSP_OpenLeds();		// u coordinatora
 
 // pouze u rooteru
CS_ReadParameter(CS_RF_TX_POWER_ID, &txPwr);
 snprintf(str, sizeof(str), "Vysilaci vykon [dBm]: %d\r\n", txPwr);
 SendStrToConsole(str);
 
 
 

  
// if(HAL_OpenAdc(&adcParamDescriptor) == -1)
//	SendStrToConsole("ADC otevreni selhalo!\r\n");
 //else
	//SendStrToConsole("ADC otevreni OK.\r\n");
// BSP_OpenLeds();		// u routeru
 }
 
void SendStrToConsole(char *str)
{
	// there should be verification 
	HAL_WriteUsart(&usartDescriptor, (uint8_t*)str, strlen(str));
}
