From: R. Steve McKown Date: Sat, 31 Dec 2011 19:09:13 +0000 (-0700) Subject: Fix led_get(); reduce code size X-Git-Tag: 1.1~8 X-Git-Url: https://oss.titaniummirror.com/gitweb?p=rgblamp.git;a=commitdiff_plain;h=ce6d400970d36cfb7ec4e3a2c97470d547c7ed78 Fix led_get(); reduce code size --- diff --git a/main.c b/main.c index a8f7324..627c94a 100644 --- a/main.c +++ b/main.c @@ -144,30 +144,37 @@ signed char bright = BRIGHT_INIT; bit bright_up; signed char pbHeldCount; -/* Combine LED color value and brightness level to generate an RGB value */ -unsigned char led_get(unsigned value) +/* Combine LED color value and brightness level to generate an RGB value. + * bright will be BRIGHT_MIN...BRIGHT_MAX + * + * @param value 0...1023 (6 bit color, left shifted 4 bits, for 10 bits) + * @param b brightness value BRIGHT_MIN...BRIGHT_MAX + * @return An RGB drive value, 0...255 + */ +unsigned char led_get(int value, signed char b) { - unsigned char tmp = bright; - - if (tmp > BRIGHT_MAX) - tmp = BRIGHT_MAX; - else if (tmp < BRIGHT_MIN) - tmp = BRIGHT_MIN; - return (value * tmp) >> 8; + return (unsigned long)value * b / 4 / BRIGHT_MAX; } -/* Set the LEDs using both color and brightness values. */ +/* Set the LEDs using color values and current brightness. */ void leds_set() { - rgb_set(led_get((red).value), led_get((grn).value), led_get((blu).value), - led_get((wht).value)); + signed char b; + + if (bright > BRIGHT_MAX) + b = BRIGHT_MAX; + else if (bright < BRIGHT_MIN) + b = BRIGHT_MIN; + else + b = bright; + rgb_set(led_get((red).value, b), led_get((grn).value, b), + led_get((blu).value, b), led_get((wht).value, b)); } void start_fade() { - /* RGB PWM values are 8 bits, but computations are done in 15 - * (leaving room for a sign bit), so leds_set() uses >>7 to convert - * to PWM values. + /* RGB PWM values are 8 bits. Color values are 12 bits. See led_get() + * for the conversion of color values and brightness to RGB values. */ int newr, newg, newb, neww;