From 0cbaf714d2302a03c2e423b3584513becf5a39ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Sun, 8 Oct 2017 18:09:52 +0200 Subject: [PATCH] application: minor fixes --- application.c | 103 +++++++++++++++++++++++++------------------------- 1 file changed, 52 insertions(+), 51 deletions(-) diff --git a/application.c b/application.c index 582f0c5..8a0c8d8 100644 --- a/application.c +++ b/application.c @@ -64,15 +64,15 @@ volatile bool photoresistor_flag = false; /**< flag set when ambient luminosity * @{ */ /** the number of ticks in one second (32768 divisor greater than 256*LED_WS2812B_LEDS/60) */ -#define TICKS_PER_SECOND 256UL +#define TICKS_PER_SECOND (256UL) /** number of ticks in one second */ -const uint32_t ticks_second = TICKS_PER_SECOND; +#define TICKS_SECOND (TICKS_PER_SECOND) /** number of ticks in one minute */ -const uint32_t ticks_minute = 60*TICKS_PER_SECOND; +#define TICKS_MINUTE (60*TICKS_SECOND) /** number of ticks in one hour */ -const uint32_t ticks_hour = 60*60*TICKS_PER_SECOND; +#define TICKS_HOUR (60*TICKS_MINUTE) /** number of ticks in one midday (12 hours) */ -const uint32_t ticks_midday = 12*60*60*TICKS_PER_SECOND; +#define TICKS_MIDDAY (12*TICKS_HOUR) /** @} */ /** @defgroup photoresistor_adc ADC used to ambient luminosity @@ -175,11 +175,11 @@ static void process_command(char* str) } else if (0==strcmp(word,"time")) { word = strtok(NULL,delimiter); if (!word) { - printf("time: %02u:%02u:%02u\n", rtc_get_counter_val()/ticks_hour, (rtc_get_counter_val()%ticks_hour)/ticks_minute, (rtc_get_counter_val()%ticks_minute)/ticks_second); // get and print time from internal RTC + printf("time: %02U:%02U:%02U\n", rtc_get_counter_val()/TICKS_HOUR, (rtc_get_counter_val()%TICKS_HOUR)/TICKS_MINUTE, (rtc_get_counter_val()%TICKS_MINUTE)/TICKS_SECOND); // get and print time from internal RTC } else if (strlen(word)!=8 || word[0]<'0' || word[0]>'2' || word[1]<'0' || word[1]>'9' || word[3]<'0' || word[3]>'5' || word[4]<'0' || word[4]>'9' || word[6]<'0' || word[6]>'5' || word[7]<'0' || word[7]>'9') { // time format is incorrect goto error; } else { - rtc_set_counter_val(((word[0]-'0')*10+(word[1]-'0')*1)*ticks_hour+((word[3]-'0')*10+(word[4]-'0')*1)*ticks_minute+((word[6]-'0')*10+(word[7]-'0')*1)*ticks_second); // set time in internal RTC counter + rtc_set_counter_val(((word[0]-'0')*10+(word[1]-'0')*1)*TICKS_HOUR+((word[3]-'0')*10+(word[4]-'0')*1)*TICKS_MINUTE+((word[6]-'0')*10+(word[7]-'0')*1)*TICKS_SECOND); // set time in internal RTC counter printf("time set\n"); } } else if (0==strcmp(word,"DCF77")) { @@ -223,8 +223,8 @@ static void clock_clear(void) */ static void clock_show_time(uint32_t time) { - uint32_t led_hour = (LED_WS2812B_LEDS*(256*(uint64_t)(time%ticks_midday)))/ticks_midday; // scale to LED brightnesses for hours - uint32_t led_minute = (LED_WS2812B_LEDS*(256*(uint64_t)(time%ticks_hour)))/ticks_hour; // scale to LED brightnesses for minutes + uint32_t led_hour = (LED_WS2812B_LEDS*256ULL*(time%TICKS_MIDDAY))/TICKS_MIDDAY; // scale to LED brightnesses for hours + uint32_t led_minute = (LED_WS2812B_LEDS*256ULL*(time%TICKS_HOUR))/TICKS_HOUR; // scale to LED brightnesses for minutes if (led_hour>=LED_WS2812B_LEDS*256 || led_minute>=LED_WS2812B_LEDS*256) { // a calculation error occurred return; } @@ -232,57 +232,57 @@ static void clock_show_time(uint32_t time) if (led_hour>led_minute) { // show hours in blue (and clear other LEDs) for (uint16_t led=0; led=0xff) { // full hours - clock_leds[led*3+2] = 0xff; + clock_leds[led*3+2] = 0xff; // set blue (hours) to full } else { // running hours - clock_leds[led*3+2] = led_hour; + clock_leds[led*3+2] = led_hour; // set blue (hours) to remaining } - led_hour -= clock_leds[led*3+2]; + led_hour -= clock_leds[led*3+2]; // subtract displayed value } // show minutes in green (override hours) for (uint16_t led=0; led0; led++) { - clock_leds[led*3+0] = 0; + clock_leds[led*3+0] = 0; // clear red (seconds) if (led_minute>=0xff) { // full minutes - clock_leds[led*3+1] = 0xff; + clock_leds[led*3+1] = 0xff; // set green (minutes) to full } else { // running minutes - clock_leds[led*3+1] = led_minute; + clock_leds[led*3+1] = led_minute; // set green (minutes) to remaining } - led_minute -= clock_leds[led*3+1]; - clock_leds[led*3+2] = 0; + led_minute -= clock_leds[led*3+1]; // subtract displayed value + clock_leds[led*3+2] = 0; // clear blue (hours) } } else { // show minutes in green (and clear other LEDs) for (uint16_t led=0; led=0xff) { // full minutes - clock_leds[led*3+1] = 0xff; + clock_leds[led*3+1] = 0xff; // set green (minutes) to full } else { // running minutes - clock_leds[led*3+1] = led_minute; + clock_leds[led*3+1] = led_minute; // set green (minutes) to remaining } - led_minute -= clock_leds[led*3+1]; - clock_leds[led*3+2] = 0; + led_minute -= clock_leds[led*3+1]; // subtract displayed value + clock_leds[led*3+2] = 0; // clear blue (hours) } // show hours in blue (override minutes) for (uint16_t led=0; led0; led++) { - clock_leds[led*3+0] = 0; - clock_leds[led*3+1] = 0; + clock_leds[led*3+0] = 0; // clear red (seconds) + clock_leds[led*3+1] = 0; // clear green (minutes) if (led_hour>=0xff) { // full hours - clock_leds[led*3+2] = 0xff; + clock_leds[led*3+2] = 0xff; // set blue (hours) to full } else { // running hours - clock_leds[led*3+2] = led_hour; + clock_leds[led*3+2] = led_hour; // set blue (hours) to remaining } - led_hour -= clock_leds[led*3+2]; + led_hour -= clock_leds[led*3+2]; // subtract displayed value } } // don't show seconds on full minute (better for first time setting, barely visible else) - if (time%ticks_minute==0) { + if (time%TICKS_MINUTE==0) { return; } - uint32_t led_second = (LED_WS2812B_LEDS*(256*(uint64_t)(time%ticks_minute)))/ticks_minute; // scale to LED brightnesses for seconds + uint32_t led_second = (LED_WS2812B_LEDS*(256*(uint64_t)(time%TICKS_MINUTE)))/TICKS_MINUTE; // scale to LED brightnesses for seconds uint8_t brightness_second = led_second%256; // get brightness for seconds for last LED - uint16_t second_led = (LED_WS2812B_LEDS*(time%ticks_minute))/ticks_minute; // get LED for seconds (we only use the last LED as runner instead of all LEDs as arc) + uint16_t second_led = (LED_WS2812B_LEDS*(time%TICKS_MINUTE))/TICKS_MINUTE; // get LED for seconds (we only use the last LED as runner instead of all LEDs as arc) // set seconds LED clock_leds[second_led*3+0] = brightness_second; //clock_leds[second_led*3+1] = 0; // clear other colors (minutes/hours indication) @@ -310,7 +310,7 @@ static void clock_leds_set(void) */ static void clock_set_time(uint32_t time) { - clock_show_time(time); // set time + clock_show_time(time); // convert time to LED values clock_leds_set(); // set the colors of all LEDs led_ws2812b_transmit(); // transmit set color } @@ -323,12 +323,12 @@ static void clock_animate_time(uint32_t time) { static uint32_t display_time = 0; // the time to display while (display_time=ticks_midday*2) { // one day passed - rtc_set_counter_val(rtc_get_counter_val()%ticks_midday); // reset time counter + if (ticks_time>=TICKS_MIDDAY*2) { // one day passed + rtc_set_counter_val(rtc_get_counter_val()%TICKS_MIDDAY); // reset time counter } clock_set_time(ticks_time); // set time }