#pragma once
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_adc.h"
#include "stm32f4xx_dma.h"
#include "stm32f4xx_dma2d.h"
#include "time_service.h"
#include "Adafruit_TCS34725.h"
#include "math.h"

#define ADC_CHANNELS 9

#define max(x, y) (((x) > (y)) ? (x) : (y))
#define min(x, y) (((x) < (y)) ? (x) : (y))

int32_t map(int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max);

class LineSensors
{
    public:
        LineSensors();
        void start();
	
        uint16_t getRawValue(uint8_t channel); //get sensor value from buffer
		int16_t getValue(uint8_t channel); //get mapped to 0-1000 sensor value
		bool isLeftOnGreen();
		bool isRightOnGreen();
		bool isCross();
		double readSHARP();
	
		int8_t left; //markers counters
		int8_t right;
		int8_t silver; //silver line counter
		int8_t hill;
	
		void sensorsCalibration(); //line sensors min-max calibration
		uint16_t sensors_min[6]; //calibration values
		uint16_t sensors_max[6];
	
		void colorsCalibration(); //color sensors min-max calibration
		uint16_t left_min[4]; //RGB channels calibration values
		uint16_t left_max[4];
		uint16_t right_min[4];
		uint16_t right_max[4];
    private:
        uint16_t m_data[ADC_CHANNELS]; //buffer for ADC channels
		I2c m_wire1;
		I2c m_wire2;
		Adafruit_TCS34725 m_left;
		Adafruit_TCS34725 m_right;
	
		double calculateHue(int16_t r, int16_t g, int16_t b); 
};
