From: Jan "Yenya" Kasprzak Date: Sat, 17 Nov 2012 22:34:41 +0000 (+0100) Subject: pwmled.c: use mA-based values instead of PWM value-based X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=1e25c1abc4ab96791d475138a75253d452b8022d;p=bike-lights.git pwmled.c: use mA-based values instead of PWM value-based --- diff --git a/firmware/pwmled.c b/firmware/pwmled.c index 5ddb783..90eb870 100644 --- a/firmware/pwmled.c +++ b/firmware/pwmled.c @@ -3,15 +3,51 @@ #include "lights.h" static uint16_t pwm_vals[N_PWMLEDS*N_PWMLED_MODES]; -static uint16_t pwm_max[N_PWMLEDS] = { 0x70, 0x70, 0x1F0 }; -static uint16_t adc_max[N_PWMLEDS] = { 0x70, 0x70, 0xF0 }; +static uint16_t pwm_max[N_PWMLEDS] = { + PWM_MAX/2, + PWM_MAX - (PWM_MAX >> 4), // step-up + PWM_MAX/2 +}; + +#define PWMLED2_TESTING_WITH_350MA_LED + +#define SENSE_MOHM 33 /* 0.033 Ohm */ +#define MA_MOHM_GAIN_TO_ADC(ma, mohm, gain) (\ + ((unsigned long)(ma))*(mohm) /* voltage at sensing resistor in uV */ \ + /(1100000UL/gain/1024UL) /* voltage of ADC reading == 1 */ \ +) +static uint16_t adc_max[N_PWMLEDS] = { + MA_MOHM_GAIN_TO_ADC( 400, SENSE_MOHM, 20), + MA_MOHM_GAIN_TO_ADC( 30, SENSE_MOHM, 20), +#ifdef PWMLED2_TESTING_WITH_350MA_LED + MA_MOHM_GAIN_TO_ADC( 400, SENSE_MOHM, 1) +#else + MA_MOHM_GAIN_TO_ADC(2500, SENSE_MOHM, 1) +#endif +}; static uint16_t adc_vals[N_PWMLEDS*N_PWMLED_MODES] = { /* pwmled0 */ - 0x04, 0x14, 0x24, 0x38, + MA_MOHM_GAIN_TO_ADC( 20, SENSE_MOHM, 20), + MA_MOHM_GAIN_TO_ADC( 50, SENSE_MOHM, 20), + MA_MOHM_GAIN_TO_ADC( 100, SENSE_MOHM, 20), + MA_MOHM_GAIN_TO_ADC( 350, SENSE_MOHM, 20), /* pwmled1 */ - 0x04, 0x14, 0x24, 0x38, + MA_MOHM_GAIN_TO_ADC( 5, SENSE_MOHM, 20), + MA_MOHM_GAIN_TO_ADC( 12, SENSE_MOHM, 20), + MA_MOHM_GAIN_TO_ADC( 16, SENSE_MOHM, 20), + MA_MOHM_GAIN_TO_ADC( 20, SENSE_MOHM, 20), /* pwmled2 */ - 0x0c, 0x24, 0x48, 0x90, +#ifdef PWMLED2_TESTING_WITH_350MA_LED + MA_MOHM_GAIN_TO_ADC( 100, SENSE_MOHM, 1), + MA_MOHM_GAIN_TO_ADC( 140, SENSE_MOHM, 1), + MA_MOHM_GAIN_TO_ADC( 250, SENSE_MOHM, 1), + MA_MOHM_GAIN_TO_ADC( 350, SENSE_MOHM, 1), +#else + MA_MOHM_GAIN_TO_ADC( 150, SENSE_MOHM, 1), + MA_MOHM_GAIN_TO_ADC( 350, SENSE_MOHM, 1), + MA_MOHM_GAIN_TO_ADC( 700, SENSE_MOHM, 1), + MA_MOHM_GAIN_TO_ADC(2400, SENSE_MOHM, 1), +#endif }; // TODO: maybe convert this to bitmask to simplify pwmled_needs_adc() ?