#include <stdint.h>
#include <stdio.h>
#include "twi_master.h"
#include "twi_ow_functions.h"

void Twim_Owi_Single_Bit_High(uint8_t Device)
{
	twi_data[0] = TWI_OWI_OW_SINGLE_BIT;
	twi_data[1] = TWI_OWI_BIT_BYTE_HIGH;
	twi_bytes_to_transceive = 2;
	Twim_Write_Data(Device, twi_bytes_to_transceive, twi_data);
}

void Twim_Owi_Single_Bit_Low(uint8_t Device)
{
	twi_data[0] = TWI_OWI_OW_SINGLE_BIT;
	twi_data[1] = TWI_OWI_BIT_BYTE_LOW;
	twi_bytes_to_transceive = 2;
	Twim_Write_Data(Device, twi_bytes_to_transceive, twi_data);
}

void Twim_Owi_Reset_Device(uint8_t Device)
{
	twi_data[0] = TWI_OWI_DEVICE_RESET;
	twi_bytes_to_transceive = 1;
	Twim_Write_Data(Device, twi_bytes_to_transceive, twi_data);
}

void Twim_Owi_Set_Configuration(uint8_t Device, uint8_t configuration_nibble)
{
	twi_data[0] = TWI_OWI_WRITE_CONFIGURATION;
	twi_data[1] = configuration_nibble | (((~configuration_nibble)&0x0F)<<4); // upper nibble has to be the one's complement of the lower nibble
	twi_bytes_to_transceive = 2;
	Twim_Write_Data(Device, twi_bytes_to_transceive, twi_data);

	snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("config sent: %x"), twi_data[1]);
	UART0_Send_Message_String(NULL,0);

	// Read back the configuration register to validate writing process
	twi_bytes_to_transceive = 1;
	Twim_Read_Data(Device, twi_bytes_to_transceive, twi_data);
	if(twi_data[0] != configuration_nibble)
	{
		snprintf_P(uart_message_string, BUFFER_SIZE - 1, PSTR("Error at OWI set configuration"));
		UART0_Send_Message_String(NULL,0);
	}
}

void Twim_Owi_Read_Status(uint8_t Device)
{
	twi_bytes_to_transceive = 2;
	twi_data[0] = TWI_OWI_SET_READ_POINTER;
	twi_data[1] = TWI_OWI_STATUS_REGISTER;
	Twim_Write_Data(Device, twi_bytes_to_transceive, twi_data);

	twi_bytes_to_transceive = 1;
	Twim_Read_Data(Device, twi_bytes_to_transceive, twi_data);
}

void Twim_Owi_Ow_Reset(uint8_t Device)
{
	twi_bytes_to_transceive = 1;
	twi_data[0] = TWI_OWI_OW_RESET;
	Twim_Write_Data(Device, twi_bytes_to_transceive, twi_data);
}

uint8_t Twim_Owi_Busy_Return(uint8_t Device)
{
	Twim_Owi_Read_Status(Device);
	return (twi_data[0] & 0x01); // return busy flag, 1 == busy
}
