fix brightness setting

This commit is contained in:
King Kévin 2016-04-04 21:52:11 +02:00
parent 2b603c3363
commit 3864026633
1 changed files with 10 additions and 18 deletions

28
main.c
View File

@ -101,12 +101,13 @@ volatile uint16_t photoresistor_value = 0;
/** photo-resistor voltage for the maximum brightness */
#define PHOTORESISTOR_MAX 1.7
/** factor to dim LED of the clock, depending on the ambient luminosity */
float clock_brithtness = 0;
float clock_brightness = 1;
/** minimum LED brightness */
#define BRIGHTNESS_MIN 0.2
/** maximum LED brightness */
#define BRIGHTNESS_MAX 1.0
/** the factor to change the brightness */
#define BRIGHTNESS_FACTOR 0.1
int _write(int file, char *ptr, int len)
{
@ -236,11 +237,6 @@ static void clock_show_time(uint32_t time)
clock_leds[led_second*3+0] = 0xff-brightness_second;
//clock_leds[led_second*3+1] = 0; // clear other colors (minutes/hours indication)
//clock_leds[led_second*3+2] = 0; // clear other colors (minutes/hours indication)
// adjust brightness
for (uint16_t i=0; i<LENGTH(clock_leds); i++) {
clock_leds[i] *= clock_brithtness;
}
}
/** @brief set the LEDs
@ -249,14 +245,8 @@ static void clock_show_time(uint32_t time)
*/
static void clock_leds_set(void)
{
static uint8_t clock_leds_old[WS2812B_LEDS*3] = {0}; // backup so to only set when value changed
for (uint16_t i=0; i<LENGTH(clock_leds)/3; i++) {
if (clock_leds[i*3+0]!=clock_leds_old[i*3+0] || clock_leds[i*3+1]!=clock_leds_old[i*3+1] || clock_leds[i*3+2]!=clock_leds_old[i*3+2]) { // only set when value changed (since this costs time)
clock_leds_old[i*3+0] = clock_leds[i*3+0]; // memorise change
clock_leds_old[i*3+1] = clock_leds[i*3+1]; // memorise change
clock_leds_old[i*3+2] = clock_leds[i*3+2]; // memorise change
ws2812b_set_rgb(i,gamma_correction_lut[clock_leds[i*3+0]],gamma_correction_lut[clock_leds[i*3+1]],gamma_correction_lut[clock_leds[i*3+2]]); // set new value (this costs time)
}
ws2812b_set_rgb(i,gamma_correction_lut[(uint8_t)(clock_leds[i*3+0]*clock_brightness)],gamma_correction_lut[(uint8_t)(clock_leds[i*3+1]*clock_brightness)],gamma_correction_lut[(uint8_t)(clock_leds[i*3+2]*clock_brightness)]); // set new value (this costs time)
}
}
@ -507,14 +497,16 @@ int main(void)
photoresistor_flag = false; // reset flag
action = true; // action has been performed
float photoresistor_voltage = photoresistor_value*1.2/ref_value; // calculate voltage from value
float new_clock_brightness = 0; // to calculate new brightness
if (photoresistor_voltage<PHOTORESISTOR_MAX) { // high ambient luminosity
clock_brithtness = BRIGHTNESS_MAX; // set highest brightness
new_clock_brightness = BRIGHTNESS_MAX; // set highest brightness
} else if (photoresistor_voltage>PHOTORESISTOR_MIN) { // low ambient luminosity
clock_brithtness = BRIGHTNESS_MIN; // set low brightness
new_clock_brightness = BRIGHTNESS_MIN; // set low brightness
} else { // intermediate ambient luminosity
clock_brithtness = BRIGHTNESS_MIN+(BRIGHTNESS_MAX-BRIGHTNESS_MIN)*(1-(photoresistor_voltage-PHOTORESISTOR_MAX)/(PHOTORESISTOR_MIN-PHOTORESISTOR_MAX)); // set variable brightness
new_clock_brightness = BRIGHTNESS_MIN+(BRIGHTNESS_MAX-BRIGHTNESS_MIN)*(1-(photoresistor_voltage-PHOTORESISTOR_MAX)/(PHOTORESISTOR_MIN-PHOTORESISTOR_MAX)); // set variable brightness
}
printf("%f, %f\n", photoresistor_voltage, clock_brithtness);
clock_brightness = clock_brightness*(1-BRIGHTNESS_FACTOR)+new_clock_brightness*BRIGHTNESS_FACTOR; // calculate new brightness based on factor
//printf("photo-resistor voltage: %f, clock brightness: %f\n", photoresistor_voltage, clock_brightness);
}
if (action) { // go to sleep if nothing had to be done, else recheck for activity
action = false;