#ifndef CALIBRATE_H
#define CALIBRATE_H

#define ADC_CLAMP_MIN 256
#define ADC_CLAMP_MAX 1792

#define OVERSAMPLE_COUNT 256

#define DAC_MIN DAC_LOW
#define DAC_MAX DAC_HIGH

#define SEEK_MAX_STEPS 1000
#define SEEK_FUZZ_ADC 100
#define SEEK_FUZZ_DAC (1+((DAC_RANGE * 5) / 65536))

#define CALIBRATE_ADC_ZERO 1024
#define CALIBRATE_ADC_LOW 512
#define CALIBRATE_ADC_HIGH 1536

/* Initialize.  Assume some relatively-safe scaling if no calibration is run */
void calibrate_init(void);

/* Given the current DAC and ADC values d1 and a1,
   compute a new DAC value d2 to give the desired ADC value a2 */
uint16_t adc_to_dac(uint16_t d1, int16_t a1, int16_t a2, float scale);

/* Calculate a new scale factor given two DAC and ADC points */
float calculate_scale(uint16_t d1, float a1, uint16_t d2, float a2);

/* Seek with the DAC to reach a specific ADC value,
   using current scaling as a starting point. */
uint16_t seek(uint16_t starting_dac, int16_t desired_adc);

/* Perform calibration */
uint16_t do_calibrate(void);

/* Oversample to get a nice ADC value */
float oversample(uint16_t dac);

extern float g_scale;  /* delta(DAC) / delta(ADC) */

#endif
