#ifndef ADC_H
#define ADC_H

  #include <stdint.h>
  #include <stdbool.h>

  typedef uint32_t tADCValue_t;
  typedef enum
  {
    eADCRes_12bit,
    eADCRes_10bit,
    eADCRes_8bit,
    eADCRes_6bit,   
  }
  eADC_Resolution_t;

  static inline uint32_t ADC_GetFullScale( eADC_Resolution_t resolution )
  {
     switch( resolution )
     {
        case eADCRes_12bit: return 1 + 0xFFF;
        case eADCRes_10bit: return 1 + 0x3FF;
        case  eADCRes_8bit: return 1 + 0xFF;
        case  eADCRes_6bit: return 1 + 0x3F;
        default: return 0;
     }
  }

  typedef struct
  { 
      bool (*Init)( eADC_Resolution_t resolution );
      bool (*Start)( uint32_t chid );
      bool (*Measure)( uint32_t timeout, tADCValue_t * pResult );
      void (*Stop)( uint32_t chid );
      uint32_t (*GetVoltage)( tADCValue_t * pResult );
      void (*DeInit)();
  }
  ADC_Handle_t;
  #ifdef STM32L1XX_IT_C
  extern ADC_HandleTypeDef Adc1Handle; // for stm32l1xx_it.c
  #endif

  extern const ADC_Handle_t ADC1Handle;

  // ADC_VREF_VOLTAGE_mV is VREFINT voltage (see datasheet)
  #define ADC_VREF_VOLTAGE_mV  1224
  #define ADC_VRED_V           3.3
  #define R13 56*10e3
  #define R32 511*10e3
  #define KDIVIDER() (R13 / (R13 + R32))

#endif