From 1d5dfb4d273a61877eb5937f93fbd266b782bc7a Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Tue, 18 Jun 2013 16:34:33 +0200 Subject: [PATCH] PWM LEDs driven by a single pattern Pattern sources renumbered: 0 - all three PWM LEDs 1 - status led 2 - illumination LED 4 - laser LED --- firmware/buttons.c | 2 +- firmware/control.c | 84 +++++++++++++++------------------------------- firmware/lights.h | 17 +++++++--- firmware/pattern.c | 44 +++++++++++------------- firmware/pwmled.c | 8 +---- 5 files changed, 61 insertions(+), 94 deletions(-) diff --git a/firmware/buttons.c b/firmware/buttons.c index f611c27..ac41c06 100644 --- a/firmware/buttons.c +++ b/firmware/buttons.c @@ -37,7 +37,7 @@ static uint16_t user_params_starttime; static void inline set_status_led(unsigned char n, pattern_t *pattern) { - led_set_pattern(n + N_PWMLEDS, pattern); + led_set_pattern(n + 1, pattern); } unsigned char buttons_setup_in_progress() diff --git a/firmware/control.c b/firmware/control.c index a219f19..1ac813a 100644 --- a/firmware/control.c +++ b/firmware/control.c @@ -104,6 +104,24 @@ static pattern_t slow3_pattern[] = { PATTERN_END }; +static pattern_t tmp1_pattern[] = { + { PWM_PAT(3, 0, 0), D_1 }, + { PWM_PAT(0, 0, 1), D_1 }, + { PWM_PAT(3, 0, 0), D_1 }, + { PWM_PAT(0, 0, 1), D_1 }, + { PWM_PAT(0, 0, 0), D_3 }, + { PWM_PAT(0, 1, 0), D_1 }, + { PWM_PAT(0, 0, 0), D_5 }, + { PWM_PAT(1, 0, 0), D_1 }, + { PWM_PAT(0, 0, 3), D_1 }, + { PWM_PAT(1, 0, 0), D_1 }, + { PWM_PAT(0, 0, 3), D_1 }, + { PWM_PAT(0, 0, 0), D_5 }, + { PWM_PAT(0, 1, 0), D_1 }, + { PWM_PAT(0, 0, 0), D_3 }, + PATTERN_END +}; + static unsigned char dim_mode, towbar_mode, braking; void init_control() @@ -117,16 +135,16 @@ void brake_on() { braking = 1; gpio_set(0, 1); - led_set_pattern(N_PWMLEDS, status_led_pattern_select()); - led_set_pattern(0, pwmled0_pattern_select()); + led_set_pattern(N_STATUS_LED, status_led_pattern_select()); + led_set_pattern(0, pwmled_pattern_select()); } void brake_off() { braking = 0; gpio_set(0, 0); - led_set_pattern(N_PWMLEDS, status_led_pattern_select()); - led_set_pattern(0, pwmled0_pattern_select()); + led_set_pattern(N_STATUS_LED, status_led_pattern_select()); + led_set_pattern(0, pwmled_pattern_select()); } void toggle_dim_mode() @@ -137,16 +155,14 @@ void toggle_dim_mode() void set_panic_mode() { - if (!dim_mode) - led_set_pattern(0, panic_pattern); - - led_set_pattern(1, panic_pattern); - led_set_pattern(2, panic_pattern); - led_set_pattern(4, panic_pattern); + led_set_pattern(0, panic_pattern); + led_set_pattern(N_ILLUM_LED, panic_pattern); } -pattern_t *pwmled0_pattern_select() +pattern_t *pwmled_pattern_select() { + return tmp1_pattern; + if (battery_critical) return on1_pattern; @@ -165,52 +181,6 @@ pattern_t *pwmled0_pattern_select() } } -pattern_t *pwmled1_pattern_select() -{ -#ifndef TESTING_FW - return NULL; -#else - if (battery_critical) - return on1_pattern; -#endif - - if (towbar_mode) { - switch (ambient_zone) { - case 0: - case 1: - return dim_mode ? on2_pattern : on1_pattern; - case 2: return dim_mode ? NULL : on2_pattern; - case 3: - default: return dim_mode ? NULL : on3_pattern; - } - } else { - switch (ambient_zone) { - case 0: return dim_mode ? slow1_pattern : normal2_pattern; - case 1: return dim_mode ? slow2_pattern : normal3_pattern; - case 2: return dim_mode ? NULL : normal4_pattern; - case 3: - default: return NULL; - } - } -} - -pattern_t *pwmled2_pattern_select() -{ -#ifndef TESTING_FW - if (battery_critical) - return on1_pattern; -#endif - - switch (ambient_zone) { - case 0: return dim_mode ? on2_pattern : on3_pattern; - case 1: return dim_mode ? slow1_pattern : normal2_pattern; - case 2: - case 3: - default: - return dim_mode ? slow2_pattern : normal3_pattern; - } -} - pattern_t *status_led_pattern_select() { if (braking) diff --git a/firmware/lights.h b/firmware/lights.h index 7e7e2a4..a3dbb63 100644 --- a/firmware/lights.h +++ b/firmware/lights.h @@ -3,9 +3,13 @@ #define TESTING_FW 1 -#define N_LEDS 7 +#define N_LEDS 4 +#define N_STATUS_LED 1 +#define N_ILLUM_LED 2 +#define N_LASER_LED 4 + #define N_PWMLEDS 3 -#define N_PWMLED_MODES 4 +#define N_PWMLED_MODES 3 #define N_BUTTONS 2 @@ -86,6 +90,11 @@ typedef struct { #define D_13 6 #define D_21 7 +#define PWM_PAT(L2, L1, L0) ( \ + (((L2) & 3) << 3) | \ + (((L1) & 1) << 2) | \ + (((L0) & 3)) ) + void init_pattern(); void patterns_next_tick(); void led_set_pattern(unsigned char led, pattern_t *pattern); @@ -118,9 +127,7 @@ void brake_on(); void brake_off(); void toggle_dim_mode(); void set_panic_mode(); -pattern_t *pwmled0_pattern_select(); -pattern_t *pwmled1_pattern_select(); -pattern_t *pwmled2_pattern_select(); +pattern_t *pwmled_pattern_select(); pattern_t *status_led_pattern_select(); pattern_t *illumination_led_pattern_select(); pattern_t *laser_pattern_select(); diff --git a/firmware/pattern.c b/firmware/pattern.c index 3354c6f..6692258 100644 --- a/firmware/pattern.c +++ b/firmware/pattern.c @@ -87,10 +87,12 @@ pattern_t off_pattern[] = { static void led_set_mode(unsigned char n, unsigned char mode) { - if (n < N_PWMLEDS) { - pwmled_set_mode(n, mode); + if (n == 0) { + pwmled_set_mode(0, mode & 3); + pwmled_set_mode(1, (mode >> 2) & 1); + pwmled_set_mode(2, (mode >> 3) & 3); } else if (n < N_LEDS) { - gpio_set(n - N_PWMLEDS, mode); + gpio_set(n - 1, mode); } } @@ -113,7 +115,7 @@ void init_pattern() for (i = 0; i < N_LEDS; i++) led_set_pattern(i, NULL); - led_set_pattern(N_PWMLEDS+1, boot_pattern); + led_set_pattern(N_ILLUM_LED, boot_pattern); } pattern_t *number_pattern(unsigned char num, unsigned char inv) @@ -135,12 +137,10 @@ pattern_t *number_pattern(unsigned char num, unsigned char inv) static pattern_t *pattern_select(unsigned char n) { switch(n) { - case 0: return pwmled0_pattern_select(); - case 1: return pwmled1_pattern_select(); - case 2: return pwmled2_pattern_select(); - case 3: return status_led_pattern_select(); - case 4: return illumination_led_pattern_select(); - case 6: return laser_pattern_select(); + case 0: return pwmled_pattern_select(); + case N_STATUS_LED: return status_led_pattern_select(); + case N_ILLUM_LED: return illumination_led_pattern_select(); + case N_LASER_LED: return laser_pattern_select(); default: return NULL; } } @@ -159,20 +159,16 @@ static void inline pattern_finished(unsigned char n) led_patterns[n] = NULL; - if (n < N_PWMLEDS) { - for (i = 0; i < N_PWMLEDS; i++) - if (led_patterns[i]) - return; - - /* all pwmleds finished; restart them */ - for (i = 0; i < N_PWMLEDS; i++) - led_set_pattern(i, pattern_select(i)); - } else if (n == 3) { - if (!led_patterns[4]) - led_set_pattern(4, pattern_select(4)); - } else if (n == 4) { - if (!led_patterns[3]) - led_set_pattern(3, pattern_select(3)); + if (n == 0) { + led_set_pattern(0, pattern_select(0)); + } else if (n == N_STATUS_LED) { + if (!led_patterns[N_ILLUM_LED]) + led_set_pattern(N_ILLUM_LED, + pattern_select(N_ILLUM_LED)); + } else if (n == N_ILLUM_LED) { + if (!led_patterns[N_STATUS_LED]) + led_set_pattern(N_STATUS_LED, + pattern_select(N_STATUS_LED)); } else { led_set_pattern(n, pattern_select(n)); } diff --git a/firmware/pwmled.c b/firmware/pwmled.c index db1cb86..27bdc0e 100644 --- a/firmware/pwmled.c +++ b/firmware/pwmled.c @@ -47,33 +47,27 @@ static uint16_t adc_vals[N_PWMLEDS*N_PWMLED_MODES] = { /* 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) + MA_GAIN_TO_ADC( 150, 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 }; -- 2.43.5