#include <avr/io.h>

#include "lights.h"

typedef struct {
	uint16_t target, pwm;
	int16_t err_sum;
	unsigned char mode, state;
	union {
		unsigned char probe_steady, mode_changed;
	};
	uint16_t mode_pwm[N_PWMLED_MODES];
	int16_t err_sums[N_PWMLED_MODES];
} pwmled_t;

pwmled_t pwmleds[N_PWMLEDS];

#define PWMLED2_TESTING_WITH_350MA_LED

#define SENSE_MOHM	33	/* 0.033 Ohm */
/*
 * Voltage in uV at ADC reading == 1 is 1100/gain/1024
 * ADC module returns sum of 1 << PWMLED_ADC_SHIFT measurements
 * Voltage in uV measured is current in mA * sense resistance in mOhm
 */
#define MA_GAIN_TO_ADC(ma, gain) ((uint16_t) \
	((uint32_t)(ma) \
	* (SENSE_MOHM) \
	* (1 << (PWMLED_ADC_SHIFT)) \
	* 1024 \
	/ (1100000/(gain))))

static uint16_t adc_max[N_PWMLEDS] = {
#ifdef TESTING_FW
	MA_GAIN_TO_ADC( 400, 20),
	MA_GAIN_TO_ADC(  30, 20),
	MA_GAIN_TO_ADC( 800,  1)
#else
	MA_GAIN_TO_ADC( 900, 20),
	MA_GAIN_TO_ADC(  30, 20),
	MA_GAIN_TO_ADC(2500,  1)
#endif
};

static uint16_t adc_vals[N_PWMLEDS*N_PWMLED_MODES] = {
#ifdef TESTING_FW
	/* pwmled0 */
	MA_GAIN_TO_ADC(  50, 20),
	MA_GAIN_TO_ADC( 100, 20),
	MA_GAIN_TO_ADC( 200, 20),
	MA_GAIN_TO_ADC( 350, 20),
	/* pwmled1 */
	MA_GAIN_TO_ADC(   5, 20),
	MA_GAIN_TO_ADC(  10, 20),
	MA_GAIN_TO_ADC(  15, 20),
	MA_GAIN_TO_ADC(  20, 20),
	/* pwmled2 */
	MA_GAIN_TO_ADC(  50,  1),
	MA_GAIN_TO_ADC(  80,  1),
	MA_GAIN_TO_ADC( 150,  1),
	MA_GAIN_TO_ADC( 200,  1)
#else
	/* pwmled0 */
	MA_GAIN_TO_ADC(  50, 20),
	MA_GAIN_TO_ADC( 100, 20),
	MA_GAIN_TO_ADC( 200, 20),
	MA_GAIN_TO_ADC( 350, 20),
	/* pwmled1 */
	MA_GAIN_TO_ADC(   5, 20),
	MA_GAIN_TO_ADC(  10, 20),
	MA_GAIN_TO_ADC(  18, 20),
	MA_GAIN_TO_ADC(  23, 20),
	/* pwmled2 */
	MA_GAIN_TO_ADC( 150,  1),
	MA_GAIN_TO_ADC( 300,  1),
	MA_GAIN_TO_ADC( 800,  1),
	MA_GAIN_TO_ADC(1500,  1)
#endif
};

#define ST_DISABLED 0
#define ST_OFF      1
#define ST_PROBING  2
#define ST_ON       3
// The above are constructed so that the following work:
#define ST_IS_ON(s)	((s) & 0x02)
#define ST_CAN_SET_MODE(s)	((s) & 0x01)

void init_pwmled()
{
	unsigned char i, j;

	for (i = 0; i < N_PWMLEDS; i++) {
		pwmled_t *led = pwmleds + i;
		led->err_sum = 0;
		led->target = adc_vals[i*N_PWMLED_MODES];
		led->pwm = 0;
		led->mode = 1;
		led->state = ST_PROBING;
		led->probe_steady = 0;

		for (j = 0; j < N_PWMLED_MODES; j++) {
			led->mode_pwm[j] = 0;
			led->err_sums[j] = 0;
		}
	}
}

void pwmled_set_mode(unsigned char n, unsigned char mode)
{
	pwmled_t *led = pwmleds + n;

	if (!ST_CAN_SET_MODE(led->state))
		return;

	if (led->mode) { // save the previous state
		led->mode_pwm[led->mode - 1] = led->pwm;
		led->err_sums[led->mode - 1] = led->err_sum;
	}

	led->mode = mode;

	if (mode > 0 && mode <= N_PWMLED_MODES) {
		led->target = adc_vals[n*N_PWMLED_MODES + mode - 1];
		led->state = ST_ON;
		led->pwm = led->mode_pwm[mode - 1];
		led->err_sum = led->err_sums[mode - 1];
		led->mode_changed = 1;
		pwm_set(n, led->pwm);
	} else {
		led->state = ST_OFF;
		pwm_off(n);
	}
}

#define PWMLED_PROBE_STEADY_COUNT 10

static inline unsigned char pwmled_probed_ok(unsigned char n, uint16_t old_pwm)
{
	pwmled_t *led = pwmleds + n;

	if (led->pwm == old_pwm) {
		if (led->probe_steady < PWMLED_PROBE_STEADY_COUNT)
			led->probe_steady++;
	} else {
		led->probe_steady = 0;
	}

	if (led->probe_steady < PWMLED_PROBE_STEADY_COUNT
		&& old_pwm <= led->pwm)
		return 0;

	// probed OK
	led->mode_pwm[led->mode - 1] = led->pwm;
	led->err_sums[led->mode - 1] = 0;

	// next mode to probe?
	if (led->mode < N_PWMLED_MODES) {
		led->probe_steady = 0;
		led->err_sum = 0;

		led->mode++;
		led->target = adc_vals[n*N_PWMLED_MODES+led->mode-1];

		return 0;
	} else {
		unsigned char i;

		led->state = ST_OFF;
		pwm_off(n);

		log_byte(0xF0);
		log_byte(n);
		log_word(jiffies);

		for (i = 0; i < N_PWMLED_MODES; i++)
			log_word(led->mode_pwm[i]);

		log_flush();

		pattern_reload();

		return 1;
	}
}

static inline void pwmled_err(unsigned char n)
{
	pwmleds[n].state = ST_DISABLED;
	pwm_off(n);

	log_byte(0xF1);
	log_byte(n);
	log_word(jiffies);
	log_flush();
}


void pwmled_adc(unsigned char n, uint16_t adcval)
{
	pwmled_t *led = pwmleds + n;
	uint16_t old_pwm;
	int32_t sum;
	unsigned char shift;

	if (!ST_IS_ON(led->state))
		return;

	if (led->state == ST_ON && led->mode_changed) {
		led->mode_changed--;
		return;
	}
	// FIXME: test for maximum adcval value (adc_max[n])

	old_pwm = led->pwm;

	shift = led->state == ST_PROBING ? 3 : 8;

	sum = ((int32_t)led->pwm << shift)
		+ led->err_sum + led->target - adcval;

	if (sum < 0)
		sum = 0;

	led->pwm = sum >> shift;
	sum -= led->pwm << shift;
	led->err_sum = sum;

	if (led->pwm >= PWM_MAX
		|| (n == 1 && led->pwm > PWM_MAX/2 && adcval < 0x08)) {
		pwmled_err(n);
		return;
	}

	if (led->state == ST_PROBING)
		if (pwmled_probed_ok(n, old_pwm))
			return;

	if (led->pwm == old_pwm)
		return;

	pwm_set(n, led->pwm);
}

