#include "Roll.h"
#include "Sail.h"
#include "Accelerometer.h"
#include "stdlib.h"

ADC_TypeDef * ROLL_ADC = ADC1;
const int ROLL_X_CHANNEL = LL_ADC_CHANNEL_11;
const int ROLL_Y_CHANNEL = LL_ADC_CHANNEL_10;
GPIO_TypeDef * ROLL_GPIO = GPIOC;
const int ROLL_X_PIN = LL_GPIO_PIN_0;
const int ROLL_Y_PIN = LL_GPIO_PIN_1;

const int TRIGGER_ANGLE = 40;

int Roll_isEmergencyState = 0;

void Roll_conf(void)
{
	Accelerometer_conf(ROLL_ADC, ROLL_GPIO, ROLL_X_PIN, ROLL_Y_PIN);
}

void Roll_start(void)
{
	Accelerometer_start(ROLL_ADC);
}

int Roll_getEmergencyState(void)
{
	return Roll_isEmergencyState;
}

void Roll_background(void)
{
	const int xAngle = Accelerometer_getAngle(ROLL_ADC, ROLL_X_CHANNEL);
	//const int yAngle = Accelerometer_getAngle(ROLL_ADC, ROLL_Y_CHANNEL);
	
	const int currentState = abs(xAngle) >= 40;
	if (Roll_isEmergencyState && !currentState)
		Sail_setEmergency(1);
	else if (!Roll_isEmergencyState && currentState)
		Sail_setEmergency(0);
}
