#ifndef ADC_H_
#define ADC_H_

#define ADC_INTERRUPT_PRIO		7

void init_adc(void);
void trig_adc(void);
uint16_t get_cc1_val(void);
uint16_t get_cc2_val(void);

// USB-C Flip status
// Source (junction box) has 56k pullup resistor
// CC pins both have 5.1k pulldown resistor
// With 5V applied by the source, the value on the
// active CC pin will be 5*(5.1/(56+5.1)) = 0.417V
// With a 3.3V adc reference, the ADC counts should be 517 / 4095
#define CC_VOLTAGE_SOURCE		(5.0)
#define CC_HOST_PULLUP			(56000.0)
#define CC_DEVICE_PULLDOWN		(5100.0)
#define CC_PIN_ACTIVE_VOLTAGE	(CC_VOLTAGE_SOURCE * (CC_DEVICE_PULLDOWN) / (CC_DEVICE_PULLDOWN + CC_HOST_PULLUP))
#define ADC_REF_VOLTS			(3.3)
#define ADC_MAX_COUNTS			(4095.0)
#define CC_PIN_ACTIVE_COUNTS	(CC_PIN_ACTIVE_VOLTAGE / ADC_REF_VOLTS * ADC_MAX_COUNTS)

#define CC_ACTIVE_THRESH_HIGH	((uint16_t)(CC_PIN_ACTIVE_COUNTS*1.05))
#define CC_ACTIVE_THRESH_LOW	((uint16_t)(CC_PIN_ACTIVE_COUNTS*0.95))

// Inactive threshhold - value should be darn close to zero
#define CC_INACTIVE_THRESH		((uint16_t)(0.05 / ADC_REF_VOLTS * ADC_MAX_COUNTS))

// Wrong JunctionBox insertion threshhold - 
// 5V will be directly applied to CC wire (since it expects
// to be connected to Vconn at the junction box) so the ADC
// will be reading a very high value
#define CC_WRONG_WAY_THRESH		((uint16_t)(3.0 / ADC_REF_VOLTS * ADC_MAX_COUNTS))

typedef enum 
{
	Flip_Undefined,
	Flip_CC1,
	Flip_CC2,
	Flip_CC1_JcnBoxWrongWay,
	Flip_CC2_JcnBoxWrongWay
} USB_Flip_State_t;

USB_Flip_State_t get_flip_status(void);

#endif /* ADC_H_ */
