diff --git a/main.c b/main.c index a9bc986..785217a 100644 --- a/main.c +++ b/main.c @@ -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; i2.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 +}