]> www.fi.muni.cz Git - bike-lights.git/commitdiff
pwmled.c: use mA-based values instead of PWM value-based
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 17 Nov 2012 22:34:41 +0000 (23:34 +0100)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Sat, 17 Nov 2012 22:34:41 +0000 (23:34 +0100)
firmware/pwmled.c

index 5ddb783b6c3762a8a268d3495670a65e783610bf..90eb87017a2bee065244a665eeb8f7d95cd15ecc 100644 (file)
@@ -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() ?