pi_clock

Crude and unpolished clock for Raspberry Pi
git clone https://git.e1e0.net/pi_clock.git
Log | Files | Refs | README | LICENSE

commit 7f80fcc410064c1bb265ae9562d3d5a610f9dbbe
parent 9f7c1ce7ff18b7912565e1567ff4f930499b5dd4
Author: Paco Esteban <paco@e1e0.net>
Date:   Sat,  1 Aug 2020 19:56:39 +0200

better wday and month functions

Diffstat:
Mclock.c | 99+++++++++++++++++++++----------------------------------------------------------
1 file changed, 26 insertions(+), 73 deletions(-)

diff --git a/clock.c b/clock.c @@ -41,8 +41,8 @@ #define PIC_VERSION "v0.5.0" lcd_i2c_t *init_lcd(void); -void setmonth(char *, int); -void setwday(char *, int); +char * setmonth(int); +char * setwday(int); void usage(); int getExtTag(char *, char *); int init_pins(void); @@ -54,8 +54,8 @@ main(int argc, char *argv[]) time_t t; struct tm *tm; char *linebuff; - char str_wday[4]; - char str_month[4]; + char *str_wday; + char *str_month; char extFile[255] = ""; char extTag[8]; char script[255]; @@ -153,14 +153,21 @@ main(int argc, char *argv[]) err(1, "cannot print line0\n"); - setwday(str_wday, tm->tm_wday); - setmonth(str_month, tm->tm_mon); + str_wday = setwday(tm->tm_wday); + if (str_wday == NULL) + err(1, "str_day"); + str_month = setmonth(tm->tm_mon); + if (str_month == NULL) + err(1, "str_month"); asprintf(&linebuff, "%s, %s %2d %4d", str_wday, str_month, tm->tm_mday, 1900+tm->tm_year); if (print_line_lcd(lcd, 1, linebuff) == -1) err(1, "cannot print line1\n"); + free(str_wday); + free(str_month); + if (digitalRead(BUTTON1) == LOW) { if (backlight_status) { if (strlen(script) > 2) { @@ -191,79 +198,25 @@ main(int argc, char *argv[]) return 0; } -void -setmonth(char *str, int month) +char * +setmonth(int month) { - switch (month) { - case 0: - strcpy(str, "Jan"); - break; - case 1: - strcpy(str, "Feb"); - break; - case 2: - strcpy(str, "Mar"); - break; - case 3: - strcpy(str, "Apr"); - break; - case 4: - strcpy(str, "May"); - break; - case 5: - strcpy(str, "Jun"); - break; - case 6: - strcpy(str, "Jul"); - break; - case 7: - strcpy(str, "Ago"); - break; - case 8: - strcpy(str, "Sep"); - break; - case 9: - strcpy(str, "Oct"); - break; - case 10: - strcpy(str, "Nov"); - break; - case 11: - strcpy(str, "Dec"); - break; - default: - strcpy(str, "XXX"); + char months[12][4] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", + "Aug", "Sep", "Oct", "Nov", "Dec" }; + if (month > 11) { + return NULL; } + return strdup(months[month]); } -void -setwday(char *str, int wday) +char * +setwday(int wday) { - switch (wday) { - case 0: - strcpy(str, "Sun"); - break; - case 1: - strcpy(str, "Mon"); - break; - case 2: - strcpy(str, "Tue"); - break; - case 3: - strcpy(str, "Wed"); - break; - case 4: - strcpy(str, "Thr"); - break; - case 5: - strcpy(str, "Fri"); - break; - case 6: - strcpy(str, "Sat"); - break; - default: - strcpy(str, "XXX"); + char days[7][4] = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" }; + if (wday > 6) { + return NULL; } + return strdup(days[wday]); } int