]> www.fi.muni.cz Git - bike-lights.git/commitdiff
PWMLED: proof-of-concept brightness setting
authorJan "Yenya" Kasprzak <kas@fi.muni.cz>
Tue, 18 Jun 2013 15:36:41 +0000 (17:36 +0200)
committerJan "Yenya" Kasprzak <kas@fi.muni.cz>
Wed, 26 Jun 2013 09:10:11 +0000 (11:10 +0200)
In order to save space for patterns, we set the brightness
independently from pattern. Each brightness has only two levels
for PWMLED 0 and 2, and one for PWMLED 1. Patterns can then use
two-bit values for each PWMLED (one-bit for PWMLED 1), with the
following meaning:

0: off
1: level 1
2: level 2
3: also level 2, with a separate state stored. This can be used for
saving regulation value for a single current level with
and without other outputs running, or with different levels
of other output. This is in order to avoid flicker when one
PWMLED (usually the front one) is steady on, and the others
are blinking.

firmware/lights.h
firmware/pwmled.c

index a3dbb636c61ac88c8d5be2c161963f4825091b6d..25e2233141a152175b8038cde3f4e4879749b58d 100644 (file)
@@ -61,6 +61,14 @@ void susp_tmr();
 void init_pwmled();
 void pwmled_adc(unsigned char n, uint16_t adcval);
 void pwmled_set_mode(unsigned char n, unsigned char mode);
+void pwmled_set_brightness(uint16_t brightness);
+#define PWMLED_BRIGHTNESS(l0_lo, l0_hi, l1, l2_lo, l2_hi) ( \
+          (uint16_t)(l0_lo) \
+       | ((uint16_t)(l0_hi) << 3) \
+       | ((uint16_t)(l1)    << 6) \
+       | ((uint16_t)(l2_lo) << 9) \
+       | ((uint16_t)(l2_hi) << 12) \
+       )
 
 /* gpio.c */
 void init_gpio();
index 27bdc0e54ce8d4b2d67c67756fc7da80cba44cde..b8f702a766f2b9b39b5dac9b16ce0890d4fd6e22 100644 (file)
@@ -42,36 +42,28 @@ static uint16_t adc_max[N_PWMLEDS] = {
 #endif
 };
 
-static uint16_t adc_vals[N_PWMLEDS*N_PWMLED_MODES] = {
-#ifdef TESTING_FW
-       /* pwmled0 */
+static uint16_t adc_targets_0[] = {
        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 */
+};
+
+static uint16_t adc_targets_1[] = {
        MA_GAIN_TO_ADC(   5, 20),
        MA_GAIN_TO_ADC(  10, 20),
        MA_GAIN_TO_ADC(  20, 20),
-       /* pwmled2 */
+};
+
+static uint16_t adc_targets_2[] = {
        MA_GAIN_TO_ADC(  50,  1),
-       MA_GAIN_TO_ADC(  80,  1),
-       MA_GAIN_TO_ADC( 150,  1)
-#else
-       /* pwmled0 */
-       MA_GAIN_TO_ADC(  50, 20),
-       MA_GAIN_TO_ADC( 100, 20),
-       MA_GAIN_TO_ADC( 350, 20),
-       /* pwmled1 */
-       MA_GAIN_TO_ADC(   5, 20),
-       MA_GAIN_TO_ADC(  10, 20),
-       MA_GAIN_TO_ADC(  23, 20),
-       /* pwmled2 */
-       MA_GAIN_TO_ADC( 150,  1),
-       MA_GAIN_TO_ADC( 300,  1),
-       MA_GAIN_TO_ADC(1500,  1)
-#endif
+       MA_GAIN_TO_ADC( 100,  1),
+       MA_GAIN_TO_ADC( 200,  1),
+       MA_GAIN_TO_ADC( 350,  1),
 };
 
+static uint16_t adc_vals[N_PWMLEDS*N_PWMLED_MODES];
+
 #define ST_DISABLED 0
 #define ST_OFF      1
 #define ST_PROBING  2
@@ -99,6 +91,8 @@ void init_pwmled()
                        led->err_sums[j] = 0;
                }
        }
+
+       pwmled_set_brightness(PWMLED_BRIGHTNESS(0, 2, 1, 0, 2));
 }
 
 void pwmled_set_mode(unsigned char n, unsigned char mode)
@@ -128,6 +122,26 @@ void pwmled_set_mode(unsigned char n, unsigned char mode)
        }
 }
 
+void pwmled_set_brightness(uint16_t brightness)
+{
+       unsigned char i;
+
+       adc_vals[0] = adc_targets_0[brightness & 0x7];
+       adc_vals[1] = adc_targets_0[(brightness >> 3) & 0x7];
+       adc_vals[2] = adc_vals[1];
+
+       adc_vals[3] = adc_targets_1[(brightness >> 6) & 0x7];
+       adc_vals[4] = adc_vals[3];
+       adc_vals[5] = adc_vals[3];
+
+       adc_vals[6] = adc_targets_2[(brightness >> 9) & 0x7];
+       adc_vals[7] = adc_targets_2[(brightness >> 12) & 0x7];
+       adc_vals[8] = adc_vals[7];
+
+       for (i = 0; i < N_PWMLEDS; i++)
+               pwmleds[i].err_sum = 0;
+}
+
 #define PWMLED_PROBE_STEADY_COUNT 10
 
 static inline unsigned char pwmled_probed_ok(unsigned char n, uint16_t old_pwm)