#include "packet.h"
#include "uart.h"

/* 5 byte packets.  5 bytes * 10 bits/byte * 8 khz = 400 kbits/sec */

void packet_send_adc_dac(int16_t adc, uint16_t dac, int overflow)
{
	char b[4];
	/* Packet format for DAC/ADC values:
	   10100000  ffffAaaa  aaaaaaaa  Dddddddd  dddddddd

	   ffff = flags, default 0000
	   ...1 = possible overflow
	   Aaaaaaaaaaaa = 12-bit ADC value (2s compliment signed)
	   Dddddddddddddddd = 16-bit DAC command (unsigned)
	*/

	uart1_put(PACKET_ADC_DAC);

	b[0] = (adc & 0x0F00) >> 8;
	if (overflow)
		b[0] |= 0x10;
	b[1] = adc & 0xFF;
	b[2] = (dac & 0xFF00) >> 8;
	b[3] = dac & 0xFF;
	uart1_put(b[0]);
	uart1_put(b[1]);
	uart1_put(b[2]);
	uart1_put(b[3]);
}

void packet_send_calibration(float scale)
{
	/* Packet format for calibration data:
	   A1 xx xx xx xx

	   where xx xx xx xx = 32-bit floating point value, little
	   endian IEEE 754 */

	uint8_t *b = (uint8_t *)&scale;

	uart1_put(PACKET_CALIBRATION);
	uart1_put(b[0]);
	uart1_put(b[1]);
	uart1_put(b[2]);
	uart1_put(b[3]);
}
