#include "common.h"
#include "bus/usart.h"
#include "bus/mosfet.h"
#include "bus/pwm.h"
#include "bus/i2c.h"

#include <avr/interrupt.h>

// TODO: Convert raw data from ADS1115 to usable values.
// TODO: Implement optional sensor value check with CRC8.
// TODO: Set timeouts for polling based things like I2C.
// TODO: Write an improved serial parser (low priority).
// TODO: Either implement software PWM for the FAN03 timer
// (which will be quite complicated) or pick a chip with
// more than two 16-bit PWM outputs like the ATmega328PB.
// https://www.mikrocontroller.net/articles/Soft-PWM

int Init(void)
{
	// MOSFETS control things like the heating element
	// so they are the highest priority to initialize
	// to a default state via MOS_Init().

	MOS_Init();

	// The serial interface is required for output
	// functions like Info() and Error() and it uses
	// IRQs, so we need to initialize it as soon as
	// possible and make sure to enable interrupts.

	USART_Init();
	sei();

	Info("Initializing...");

	// The watchdog timer is clocked from a separate
	// on-chip oscillator which runs at 1MHz. Eight
	// different clock cycle periods can be selected
	// to determine the reset period. If the reset
	// period expires, the chip resets and executes
	// from the reset vector.

	// The update loop must call WDT_Reset to reset
	// the timer, kind of like a dead man's switch
	// allowing us to detect infinite loops and any
	// other error that halts execution.

	if (WDT_HasTriggered())
		Info("Unexpected system reset.");

	// WDT_Enable();
	// WDT_SetTimeout(7);

	// There is a possiblity to use interrupt signals
	// for I2C communication but only as one large
	// branching routine for the whole I2C system.
	// The blocking approach used right now is fine.

	I2C_Init();
	PWM_Init();

	MOS_Enable(MOS03);
	// MOS_Disable(MOS01);

	// Only FAN01 and FAN02 are receiving the correct
	// frequency (25 KHz) right now. The 16-bit timer on
	// the ATMega32A has two outputs so it would require
	// software PWM to have a variable frequency on PD7.

	// A simple implementation will take up around 30-50
	// percent of CPU time. Faster approaches are quite
	// complicated so it might be worth it to switch to
	// something like an ATmega328PB.

	PWM_SetValue(FAN01, 50); // Fan Peltier Hot side
	PWM_SetValue(FAN02, 50); // Fan Peltier Cold Side
	// PWM_SetValue(FAN03, 20); // Fan Heating

	// The I2C_SetChannel command changes the channel
	// setting of the PCA9546 I2C multiplexer. Any
	// command after it will be sent to the device
	// listening on that channel.

	I2C_SetChannel(AHT01);
	I2C_AHT20_Init();

	I2C_SetChannel(AHT02);
	I2C_AHT20_Init();

	I2C_SetChannel(AHT03);
	I2C_AHT20_Init();

	return 0;
}

void Update(void)
{
	float temp[3], rhum[3];
	short raw[4];

	WDT_Reset(); // Reset watchdog

	Info("Reading sensor values...");

	I2C_SetChannel(AHT01);
	I2C_AHT20_Read(&temp[0], &rhum[0]);

	I2C_SetChannel(AHT02);
	I2C_AHT20_Read(&temp[1], &rhum[1]);

	I2C_SetChannel(AHT03);
	I2C_AHT20_Read(&temp[2], &rhum[2]);

	raw[0] = I2C_ADS1115_Read(0);
	raw[1] = I2C_ADS1115_Read(1);
	raw[2] = I2C_ADS1115_Read(2);
	raw[3] = I2C_ADS1115_Read(3);

	Info("TEM0=%.2fC, RH0=%.2f%%", temp[0], rhum[0]);
	Info("TEM1=%.2fC, RH1=%.2f%%", temp[1], rhum[1]);
	Info("TEM2=%.2fC, RH2=%.2f%%", temp[2], rhum[2]);
	Info("ADC0=%04X, ADC1=%04X, ADC2=%04X, ADC3=%04X",
		raw[0], raw[1], raw[2], raw[3]);
}

int main(void)
{
	Init();

	for (;;) {
		Update();
		Sleep(1000);
	}

	return 0;
}
