application: minor fixes
This commit is contained in:
parent
2cff4f0af5
commit
0cbaf714d2
103
application.c
103
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<LED_WS2812B_LEDS; 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
|
||||
}
|
||||
// show minutes in green (override hours)
|
||||
for (uint16_t led=0; led<LED_WS2812B_LEDS && led_minute>0; 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<LED_WS2812B_LEDS; 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)
|
||||
}
|
||||
// show hours in blue (override minutes)
|
||||
for (uint16_t led=0; led<LED_WS2812B_LEDS && led_hour>0; 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<time) {
|
||||
if (display_time+ticks_hour<=time) { // first set hours
|
||||
display_time += ticks_hour; // increment hours
|
||||
} else if (display_time+ticks_minute<=time) { // second set minutes
|
||||
display_time += ticks_minute; // increment minutes
|
||||
} else if (display_time+ticks_second<=time) { // third set seconds
|
||||
display_time += ticks_second; // increment seconds
|
||||
if (display_time+TICKS_HOUR<=time) { // first set hours
|
||||
display_time += TICKS_HOUR; // increment hours
|
||||
} else if (display_time+TICKS_MINUTE<=time) { // second set minutes
|
||||
display_time += TICKS_MINUTE; // increment minutes
|
||||
} else if (display_time+TICKS_SECOND<=time) { // third set seconds
|
||||
display_time += TICKS_SECOND; // increment seconds
|
||||
} else { // finally set time
|
||||
display_time = time;
|
||||
}
|
||||
|
@ -386,7 +386,7 @@ void main(void)
|
|||
board_setup(); // setup board
|
||||
usart_setup(); // setup USART (for printing)
|
||||
usb_cdcacm_setup(); // setup USB CDC ACM (for printing)
|
||||
printf("welcome to the CuVoodoo STM32F1 example application\n"); // print welcome message
|
||||
printf("welcome to the CuVoodoo LED clock\n"); // print welcome message
|
||||
|
||||
#if !(DEBUG)
|
||||
// show watchdog information
|
||||
|
@ -402,7 +402,7 @@ void main(void)
|
|||
|
||||
// setup RTC
|
||||
printf("setup internal RTC: ");
|
||||
rtc_auto_awake(RCC_LSE, 32768/ticks_second-1); // ensure internal RTC is on, uses the 32.678 kHz LSE, and the prescale is set to our tick speed, else update backup registers accordingly (power off the micro-controller for the change to take effect)
|
||||
rtc_auto_awake(RCC_LSE, 32768/TICKS_SECOND-1); // ensure internal RTC is on, uses the 32.678 kHz LSE, and the prescale is set to our tick speed, else update backup registers accordingly (power off the micro-controller for the change to take effect)
|
||||
rtc_interrupt_enable(RTC_SEC); // enable RTC interrupt on "seconds"
|
||||
nvic_enable_irq(NVIC_RTC_IRQ); // allow the RTC to interrupt
|
||||
printf("OK\n");
|
||||
|
@ -458,11 +458,12 @@ void main(void)
|
|||
|
||||
// get date and time
|
||||
uint32_t ticks_time = rtc_get_counter_val(); // get time/date from internal RTC
|
||||
printf("current time: %02lu:%02lu:%02lu\n", ticks_time/ticks_hour, (ticks_time%ticks_hour)/ticks_minute, (ticks_time%ticks_minute)/ticks_second); // display time
|
||||
printf("current time: %02u:%02u:%02u\n", ticks_time/TICKS_HOUR, (ticks_time%TICKS_HOUR)/TICKS_MINUTE, (ticks_time%TICKS_MINUTE)/TICKS_SECOND); // display time
|
||||
clock_animate_time(ticks_time); // set time with animation
|
||||
|
||||
// main loop
|
||||
printf("command input: ready\n");
|
||||
led_on(); // indicate everything is OK and the main loop will start
|
||||
bool action = false; // if an action has been performed don't go to sleep
|
||||
button_flag = false; // reset button flag
|
||||
char c = '\0'; // to store received character
|
||||
|
@ -512,7 +513,7 @@ void main(void)
|
|||
action = true; // action has been performed
|
||||
uint8_t* dcf77_time = rtc_dcf77_time(); // get time
|
||||
if (dcf77_time) { // ensure it's valid
|
||||
ticks_time = dcf77_time[1]*ticks_hour+dcf77_time[0]*ticks_minute; // calculate new time
|
||||
ticks_time = dcf77_time[1]*TICKS_HOUR+dcf77_time[0]*TICKS_MINUTE; // calculate new time
|
||||
#if defined(EXTERNAL_RTC) && EXTERNAL_RTC
|
||||
rtc_ds1307_ticks = ticks_time; // set new time
|
||||
rtc_ds1307_write_time(0, dcf77_time[0], dcf77_time[1], ((dcf77_time[3]+1)%8)+1, dcf77_time[2], dcf77_time[4], dcf77_time[5]); // set date and time
|
||||
|
@ -531,22 +532,22 @@ void main(void)
|
|||
rtc_internal_tick_flag = false; // reset flag
|
||||
ticks_time = rtc_get_counter_val(); // copy time from internal RTC for processing
|
||||
action = true; // action has been performed
|
||||
if ((ticks_time%(ticks_second/10))==0) { // one tenth of a second passed
|
||||
if ((ticks_time%(TICKS_SECOND/10))==0) { // one tenth of a second passed
|
||||
adc_start_conversion_regular(ADC1); // start measuring ambient luminosity
|
||||
}
|
||||
if ((ticks_time%ticks_second)==0) { // one second passed
|
||||
led_toggle(); // LED toggling confuses the 32.768 kHz oscillator on the blue pill
|
||||
if ((ticks_time%TICKS_SECOND)==0) { // one second passed
|
||||
//led_toggle(); // LED toggling confuses the 32.768 kHz oscillator on the blue pill
|
||||
}
|
||||
if ((ticks_time%ticks_minute)==0) { // one minute passed
|
||||
printf("%02u:%02u:%02u\n", ticks_time/ticks_hour, (ticks_time%ticks_hour)/ticks_minute, (ticks_time%ticks_minute)/ticks_second); // display external time
|
||||
if ((ticks_time%TICKS_MINUTE)==0) { // one minute passed
|
||||
printf("%02u:%02u:%02u\n", ticks_time/TICKS_HOUR, (ticks_time%TICKS_HOUR)/TICKS_MINUTE, (ticks_time%TICKS_MINUTE)/TICKS_SECOND); // display external time
|
||||
}
|
||||
if ((ticks_time%ticks_hour)==0) { // one hours passed
|
||||
if ((ticks_time%TICKS_HOUR)==0) { // one hours passed
|
||||
clock_hours(); // show hour markers
|
||||
rtc_dcf77_on(); // switch DCF77 on to update/correct time
|
||||
printf("DCF77 receiver switched on\n"); // notify user
|
||||
}
|
||||
if (ticks_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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue