/*
 * UART.c
 *
 *  Created on: Jan 28, 2012
 *      Author: RomanM
 */

#include <inttypes.h>
#include <ctype.h>
#include "hal_UART.h"
#include "CmdLine/cmdline.h"

#ifndef UART_IN_BUFFSIZE
#define	UART_IN_BUFFSIZE	64
#endif

static char uartInBuff[UART_IN_BUFFSIZE];
static uint16_t	uartInIndex = 0;

void UART_Init(void) {
	hal_UART_Init();
}

void UART_SendString(char *str) {
	while (*str) {
		hal_UART_TxWait();
		hal_UART_SendByte(*(str++));
	}
}

void UART_InHandler(void) {
	char c = hal_UART_ReadByte();

	if (isprint((int)c)) {
		uartInBuff[uartInIndex++] = c;
	}
	else if (c == 0x0a) {
		uartInBuff[uartInIndex] = 0;
		uartInIndex = 0;
		CmdLineProcess(uartInBuff);
	}
}

