/* --COPYRIGHT--,BSD
 * Copyright (c) 2015, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * --/COPYRIGHT--*/
/*
 * UART.c
 *
 *  Created on: Dec 23, 2014
 *      Author: a0282836
 */

#include "driverlib.h"
#include "uart.h"

//initialize UART parameters
void initUART(void)
{
    //Baudrate = 9600, clock freq = 32768Hz
    //UCBRx = 3, UCBRFx = 0, UCBRSx = 3, UCOS16 = 0
    USCI_A_UART_initParam param = {0};
    param.selectClockSource = USCI_A_UART_CLOCKSOURCE_ACLK;
    param.clockPrescalar = 3;
    param.firstModReg = 0;
    param.secondModReg = 3;
    param.parity = USCI_A_UART_NO_PARITY;
    param.msborLsbFirst = USCI_A_UART_LSB_FIRST;
    param.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;
    param.uartMode = USCI_A_UART_MODE;
    param.overSampling = USCI_A_UART_LOW_FREQUENCY_BAUDRATE_GENERATION;
    USCI_A_UART_init(USCI_A0_BASE, &param);
}

//helper function for sending a string over UART
void transmitString(unsigned char* stringArray, unsigned char strLen)
{
	unsigned char i;
	for(i=0; i<strLen; i++)
		USCI_A_UART_transmitData(USCI_A0_BASE, stringArray[i]);
}

unsigned char calendarString[CALENDAR_STRING_LENGTH] = "MM/DD/YYYY HH:MM:SS\r\n";

//helper function for sending the updated time
unsigned char* calendarToString(Calendar calendarData)
{
	calendarString[MONTH_TENS] = '0' + ((calendarData.Month & 0xF0) >> 4);
	calendarString[MONTH_ONES] = '0' + (calendarData.Month & 0x0F);

	calendarString[DAY_TENS] = '0' + ((calendarData.DayOfMonth & 0xF0) >> 4);
	calendarString[DAY_ONES] = '0' + (calendarData.DayOfMonth & 0x0F);

	calendarString[YEAR_THOUSANDS] = '0' + ((calendarData.Year & 0xF000) >> 12);
	calendarString[YEAR_HUNDREDS] = '0' + ((calendarData.Year & 0x0F00) >> 8);
	calendarString[YEAR_TENS] = '0' + ((calendarData.Year & 0x00F0) >> 4);
	calendarString[YEAR_ONES] = '0' + (calendarData.Year & 0x000F);

	calendarString[HOUR_TENS] = '0' + ((calendarData.Hours & 0xF0) >> 4);
	calendarString[HOUR_ONES] = '0' + (calendarData.Hours & 0x0F);

	calendarString[MINUTE_TENS] = '0' + ((calendarData.Minutes & 0xF0) >> 4);
	calendarString[MINUTE_ONES] = '0' + (calendarData.Minutes & 0x0F);

	calendarString[SECOND_TENS] = '0' + ((calendarData.Seconds & 0xF0) >> 4);
	calendarString[SECOND_ONES] = '0' + (calendarData.Seconds & 0x0F);

	return calendarString;
}
