replace puts with printf and import putc
This commit is contained in:
parent
fa46b2b808
commit
2d7ad6dd86
100
main.c
100
main.c
|
@ -57,10 +57,30 @@ static char command[32] = {0};
|
|||
/** user input command index */
|
||||
uint8_t command_i = 0;
|
||||
|
||||
void _putc(char c)
|
||||
size_t putc(char c)
|
||||
{
|
||||
usart_putchar_nonblocking(c); // send byte over USART
|
||||
cdcacm_putchar(c); // send byte over USB
|
||||
size_t length = 0; // number of characters printed
|
||||
static char newline = 0; // to remember on which character we sent the newline
|
||||
if (0==c) {
|
||||
length = 0; // don't print string termination character
|
||||
} else if ('\r' == c || '\n' == c) { // send CR+LF newline for most carriage return and line feed combination
|
||||
if (0==newline || c==newline) { // send newline only if not already send (and only once on \r\n or \n\r)
|
||||
usart_putchar_nonblocking('\r'); // send CR over USART
|
||||
cdcacm_putchar('\r'); // send CR over USB
|
||||
usart_putchar_nonblocking('\n'); // send LF over USART
|
||||
cdcacm_putchar('\n'); // send LF over USB
|
||||
length += 2; // remember we printed 2 characters
|
||||
newline = c; // remember on which character we sent the newline
|
||||
} else {
|
||||
length = 0; // the \r or \n of \n\r or \r\n has already been printed
|
||||
}
|
||||
} else {
|
||||
usart_putchar_nonblocking(c); // send byte over USART
|
||||
cdcacm_putchar(c); // send byte over USB
|
||||
newline = 0; // clear new line
|
||||
length++; // remember we printed 1 character
|
||||
}
|
||||
return length; // return number of characters printed
|
||||
}
|
||||
|
||||
/** process user command
|
||||
|
@ -76,23 +96,23 @@ static void process_command(char* str)
|
|||
}
|
||||
// parse command
|
||||
if (0==strcmp(word,"help")) {
|
||||
puts("available commands:\n");
|
||||
puts("led [on|off|toggle]\n");
|
||||
puts("time [HH:MM:SS]\n");
|
||||
puts("date [YYYY-MM-DD]\n");
|
||||
printf("available commands:\n");
|
||||
printf("led [on|off|toggle]\n");
|
||||
printf("time [HH:MM:SS]\n");
|
||||
printf("date [YYYY-MM-DD]\n");
|
||||
} else if (0==strcmp(word,"led")) {
|
||||
word = strtok(NULL,delimiter);
|
||||
if (!word) {
|
||||
goto error;
|
||||
} else if (0==strcmp(word,"on")) {
|
||||
led_on(); // switch LED on
|
||||
puts("LED switched on\n"); // notify user
|
||||
printf("LED switched on\n"); // notify user
|
||||
} else if (0==strcmp(word,"off")) {
|
||||
led_off(); // switch LED off
|
||||
puts("LED switched off\n"); // notify user
|
||||
printf("LED switched off\n"); // notify user
|
||||
} else if (0==strcmp(word,"toggle")) {
|
||||
led_toggle(); // toggle LED
|
||||
puts("LED toggled\n"); // notify user
|
||||
printf("LED toggled\n"); // notify user
|
||||
} else {
|
||||
goto error;
|
||||
}
|
||||
|
@ -104,14 +124,14 @@ static void process_command(char* str)
|
|||
goto error;
|
||||
} else {
|
||||
if (!rtc_ds1307_write_hours((word[0]-'0')*10+(word[1]-'0')*1)) {
|
||||
puts("setting hours failed\n");
|
||||
printf("setting hours failed\n");
|
||||
} else if (!rtc_ds1307_write_minutes((word[3]-'0')*10+(word[4]-'0')*1)) {
|
||||
puts("setting minutes failed\n");
|
||||
printf("setting minutes failed\n");
|
||||
} else if (!rtc_ds1307_write_seconds((word[6]-'0')*10+(word[7]-'0')*1)) {
|
||||
puts("setting seconds failed\n");
|
||||
printf("setting seconds failed\n");
|
||||
} else {
|
||||
rtc_ds1307_oscillator_enable(); // be sure the oscillation is enabled
|
||||
puts("time set\n");
|
||||
printf("time set\n");
|
||||
}
|
||||
}
|
||||
} else if (0==strcmp(word,"date")) {
|
||||
|
@ -122,13 +142,13 @@ static void process_command(char* str)
|
|||
goto error;
|
||||
} else {
|
||||
if (!rtc_ds1307_write_year((word[2]-'0')*10+(word[3]-'0')*1)) {
|
||||
puts("setting year failed\n");
|
||||
printf("setting year failed\n");
|
||||
} else if (!rtc_ds1307_write_month((word[5]-'0')*10+(word[6]-'0')*1)) {
|
||||
puts("setting month failed\n");
|
||||
printf("setting month failed\n");
|
||||
} else if (!rtc_ds1307_write_date((word[8]-'0')*10+(word[9]-'0')*1)) {
|
||||
puts("setting day failed\n");
|
||||
printf("setting day failed\n");
|
||||
} else {
|
||||
puts("date set\n");
|
||||
printf("date set\n");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -137,7 +157,7 @@ static void process_command(char* str)
|
|||
|
||||
return; // command successfully processed
|
||||
error:
|
||||
puts("command not recognized. enter help to list commands\n");
|
||||
printf("command not recognized. enter help to list commands\n");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -165,73 +185,73 @@ void main(void)
|
|||
board_setup(); // setup board
|
||||
usart_setup(); // setup USART for user communication
|
||||
cdcacm_setup(); // setup USB ACM (serial) for user communication
|
||||
puts("welcome to the STM32F1 CuVoodoo example code\n"); // print welcome message
|
||||
printf("welcome to the STM32F1 CuVoodoo example code\n"); // print welcome message
|
||||
|
||||
#if !(DEBUG)
|
||||
// show watchdog information
|
||||
printf("watchdog set to (%2u.%2us)\n",WATCHDOG_PERIOD/1000, (WATCHDOG_PERIOD/10)%100);
|
||||
if (FLASH_OBR&FLASH_OBR_OPTERR) {
|
||||
puts("option bytes not set in flash: software wachtdog used (not started at reset)\n");
|
||||
printf("option bytes not set in flash: software wachtdog used (not started at reset)\n");
|
||||
} else if (FLASH_OBR&FLASH_OBR_WDG_SW) {
|
||||
puts("software wachtdog used (not started at reset)\n");
|
||||
printf("software wachtdog used (not started at reset)\n");
|
||||
} else {
|
||||
puts("hardware wachtdog used (started at reset)\n");
|
||||
printf("hardware wachtdog used (started at reset)\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
// setup internal RTC
|
||||
puts("setup internal RTC: ");
|
||||
printf("setup internal RTC: ");
|
||||
rtc_auto_awake(RCC_LSE, 32768-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
|
||||
puts("OK\n");
|
||||
printf("OK\n");
|
||||
// display uptime
|
||||
uint32_t ticks_time = rtc_get_counter_val(); // get time from internal RTC (since first start/power up)
|
||||
printf("uptime: %u.%02u:%02u:%02u\n", ticks_time/(60*60*24), (ticks_time/(60*60))%24, (ticks_time%(60*60))/60, (ticks_time%60)); // display uptime
|
||||
|
||||
// setup external RTC
|
||||
puts("setup external RTC: ");
|
||||
printf("setup external RTC: ");
|
||||
rtc_ds1307_setup(); // setup external RTC module
|
||||
puts("OK\n");
|
||||
printf("OK\n");
|
||||
// verify is external RTC is running
|
||||
if (rtc_ds1307_oscillator_disabled()) {
|
||||
puts("/!\\ RTC oscillator is disabled: the battery may be empty\n");
|
||||
printf("/!\\ RTC oscillator is disabled: the battery may be empty\n");
|
||||
rtc_ds1307_oscillator_enable(); // enable oscillator again
|
||||
}
|
||||
// display date
|
||||
uint8_t* rtc_ds1307_time = rtc_ds1307_read_time(); // get time/date from external RTC
|
||||
if (rtc_ds1307_time==NULL) {
|
||||
puts("could not get time from DS1307\n");
|
||||
printf("could not get time from DS1307\n");
|
||||
} else {
|
||||
printf("current date: 20%02u-%02u-%02u %02u:%02u:%02u\n", rtc_ds1307_time[6], rtc_ds1307_time[5], rtc_ds1307_time[4], rtc_ds1307_time[2], rtc_ds1307_time[1], rtc_ds1307_time[0]);
|
||||
}
|
||||
|
||||
// setup TM1637 and MAX7219 7-segments displays
|
||||
puts("setup 7-segment displays: ");
|
||||
printf("setup 7-segment displays: ");
|
||||
led_tm1637_setup(); // setup TM1637
|
||||
led_max7219_setup(); // setup MAX7219
|
||||
if (!led_tm1637_time(88,88)) { // test TM1637 display
|
||||
puts("could not send time to TM1637\n");
|
||||
printf("could not send time to TM1637\n");
|
||||
}
|
||||
if (!led_tm1637_on()) { // switch on TM1637 display
|
||||
puts("could not switch on TM1637\n");
|
||||
printf("could not switch on TM1637\n");
|
||||
}
|
||||
led_max7219_test(true); // test MAX7219 display
|
||||
for (uint32_t i=0; i<5000000; i++) { // wait a bit to have the user check the display
|
||||
__asm__("nop");
|
||||
}
|
||||
if (!led_tm1637_text(" ")) { // clear display
|
||||
puts("could not clear\n");
|
||||
printf("could not clear\n");
|
||||
}
|
||||
if (!led_tm1637_off()) { // switch off display
|
||||
puts("could not switch off TM1637\n");
|
||||
printf("could not switch off TM1637\n");
|
||||
}
|
||||
led_max7219_test(false); // go back in normal operation
|
||||
led_max7219_off(); // switch display off
|
||||
puts("OK\n");
|
||||
printf("OK\n");
|
||||
|
||||
// main loop
|
||||
puts("command input: ready\n");
|
||||
printf("command input: ready\n");
|
||||
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
|
||||
|
@ -269,7 +289,7 @@ void main(void)
|
|||
}
|
||||
while (button_flag) { // user pressed button
|
||||
action = true; // action has been performed
|
||||
puts("button pressed\n");
|
||||
printf("button pressed\n");
|
||||
led_toggle(); // toggle LED
|
||||
for (uint32_t i=0; i<1000000; i++) { // wait a bit to remove noise and double trigger
|
||||
__asm__("nop");
|
||||
|
@ -286,15 +306,15 @@ void main(void)
|
|||
}
|
||||
rtc_ds1307_time = rtc_ds1307_read_time(); // get time/date from external RTC
|
||||
if (rtc_ds1307_time==NULL) {
|
||||
puts("could not get time from DS1307: resetting\n");
|
||||
printf("could not get time from DS1307: resetting\n");
|
||||
rtc_ds1307_setup(); // resetting periph
|
||||
} else {
|
||||
if (!led_tm1637_time(rtc_ds1307_time[1], rtc_ds1307_time[0])) {
|
||||
puts("could not set time on TM1637\n");
|
||||
printf("could not set time on TM1637\n");
|
||||
}
|
||||
}
|
||||
if (!led_tm1637_on()) { // switch on display
|
||||
puts("could not switch on TM1637\n");
|
||||
printf("could not switch on TM1637\n");
|
||||
}
|
||||
}
|
||||
if (action) { // go to sleep if nothing had to be done, else recheck for activity
|
||||
|
|
Loading…
Reference in New Issue