adjust LED brightness depending on ambient luminosity measured by photo-resistor on ADC

This commit is contained in:
King Kévin 2016-04-04 11:32:26 +02:00
parent e16d7ae476
commit 2c0177c24b
1 changed files with 42 additions and 0 deletions

42
main.c
View File

@ -52,6 +52,7 @@
*/
volatile bool button_flag = false; /**< flag set when board user button has been pressed/released */
volatile bool time_flag = false; /**< flag set when time changed */
volatile bool photoresistor_flag = false; /**< flag set when ambient luminosity is measured */
/** @} */
#define TICKS_PER_SECOND 256 /**< the number of ticks in one second */
@ -96,6 +97,10 @@ char command[32] = {0};
uint8_t command_i = 0;
/** gamma correction lookup table (common for all colors) */
uint8_t gamma_correction_lut[256] = {0};
/** photo-resistor measurement of ambient luminosity */
volatile uint16_t photoresistor_value = 0;
/** factor to dim LED of the clock, depending on the ambient luminosity */
float clock_brithtness = 0;
int _write(int file, char *ptr, int len)
{
@ -225,6 +230,11 @@ 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
@ -417,6 +427,12 @@ int main(void)
}
printf("battery voltage: %.2fV\n", battery_voltage);
// now use interrupts to only measure ambient luminosity
channels[0] = PHOTORESISTOR_ADC_CHANNEL; // only measure ambient luminosity
adc_set_regular_sequence(ADC1, 1, channels); // set now group
adc_enable_eoc_interrupt(ADC1); // enable interrupt for end of convertion
nvic_enable_irq(NVIC_ADC1_2_IRQ); // enable ADC interrupts
// get date and time
printf("current time: %02lu:%02lu:%02lu\n", rtc_get_counter_val()/ticks_hour, (rtc_get_counter_val()%ticks_hour)/ticks_minute, (rtc_get_counter_val()%ticks_minute)/ticks_second); // display time
clock_animate_time(rtc_get_counter_val()); // set time with animation
@ -464,6 +480,9 @@ int main(void)
while (time_flag) { // time passed
time_flag = false; // reset flag
action = true; // action has been performed
if ((rtc_get_counter_val()%(ticks_second/10))==0) { // one tenth of a second passed
adc_start_conversion_regular(ADC1); // start measuring ambient luminosity
}
if ((rtc_get_counter_val()%ticks_second)==0) { // one second passed
led_toggle(); // LED activity to show we are not stuck
}
@ -477,6 +496,20 @@ int main(void)
rtc_set_counter_val(rtc_get_counter_val()%ticks_midday); // reset time counter
}
clock_set_time(rtc_get_counter_val()); // set time
//adc_start_conversion_regular(ADC1); // start measuring ambient luminosity
}
while (photoresistor_flag) { // new photo-resistor value has been measured
photoresistor_flag = false; // reset flag
action = true; // action has been performed
float photoresistor_voltage = photoresistor_value*1.2/ref_value; // calculate voltage from value
if (photoresistor_voltage<1.7) { // high ambient luminosity
clock_brithtness = 1; // set highest brightness
} else if (photoresistor_voltage>2.1) { // low ambient luminosity
clock_brithtness = 0.3; // set low brightness
} else { // intermediate ambient luminosity
clock_brithtness = 0.3+(0.7*(1-(photoresistor_voltage-1.7)/0.4)); // set variable brightness
}
//printf("%f, %f\n", photoresistor_voltage, clock_brithtness);
}
if (action) { // go to sleep if nothing had to be done, else recheck for activity
action = false;
@ -489,6 +522,7 @@ int main(void)
}
#if defined(BUTTON_ISR) && defined(BUTTON_EXTI)
/** @brief interrupt service routine called when button is pressed of released */
void BUTTON_ISR(void)
{
exti_reset_request(BUTTON_EXTI); // reset interrupt
@ -496,8 +530,16 @@ void BUTTON_ISR(void)
}
#endif
/** @brief interrupt service routine called when tick passed on RTC */
void rtc_isr(void)
{
rtc_clear_flag(RTC_SEC); // clear flag
time_flag = true; // notify to show new time
}
/** @brief interrupt service routine called when ADC conversion completed */
void adc1_2_isr(void)
{
photoresistor_value = adc_read_regular(ADC1); // read measured photo-resistor value (clears interrupt flag)
photoresistor_flag = true; // notify new ambient luminosity has been measured
}