pi_clock

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

commit 6526af117c202076fb25abb245535137389b0ec2
parent c839292b22dae996444d7c97d9bb710b051553e9
Author: Paco Esteban <paco@e1e0.net>
Date:   Sat,  1 Aug 2020 19:16:37 +0200

print line on its own function

while here use asprintf for the line buffers

Diffstat:
Mclock.c | 49++++++++++++++++++++++++++++++++++---------------
1 file changed, 34 insertions(+), 15 deletions(-)

diff --git a/clock.c b/clock.c @@ -16,6 +16,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#define _GNU_SOURCE + #include <err.h> #include <errno.h> #include <stdio.h> @@ -27,6 +29,8 @@ #include "lcd_i2c.h" +#define MAX_LINE_LEN 17 /* 16 char display + '\0' */ +#define LCD_LINES 2 #define I2C_ADDRESS 0x3f #define BUTTON1 3 #define DEF_R_INTER 120 /* default 30s (250ms * 120) */ @@ -34,7 +38,7 @@ #define DEF_BL_BEFORE 0 #define DEF_BL_AFTER 7 -#define PIC_VERSION "v0.4.0" +#define PIC_VERSION "v0.5.0" lcd_i2c_t *init_lcd(void); void setmonth(char *, int); @@ -42,14 +46,14 @@ void setwday(char *, int); void usage(); int getExtTag(char *, char *); int init_pins(void); +int print_line_lcd(lcd_i2c_t *, int, char *); int main(int argc, char *argv[]) { time_t t; struct tm *tm; - char line0[48]; - char line1[48]; + char *linebuff; char str_wday[4]; char str_month[4]; char extFile[255] = ""; @@ -133,31 +137,29 @@ main(int argc, char *argv[]) // read external file only interval * sleep if (readCount == readInterval) { if (getExtTag(extTag, extFile) < 0) { - errx(1, "Error getting external tag\n"); + err(1, "Error getting external tag\n"); } readCount = 0; } readCount++; - sprintf(line0, " %02d:%02d:%02d %-6s", + asprintf(&linebuff, " %02d:%02d:%02d %-6s", tm->tm_hour, tm->tm_min, tm->tm_sec, extTag); } else { - sprintf(line0, " %02d:%02d:%02d GMT%+ld ", + asprintf(&linebuff, " %02d:%02d:%02d GMT%+ld ", tm->tm_hour, tm->tm_min, tm->tm_sec, gmt_offset); } - lcd->x=0; - lcd->y=0; - lcd_i2c_gotoxy(lcd, lcd->x, lcd->y); - lcd_i2c_puts(lcd, line0); + if (print_line_lcd(lcd, 0, linebuff) == -1) + errx(1, "cannot print line0: %s\n"); + setwday(str_wday, tm->tm_wday); setmonth(str_month, tm->tm_mon); - sprintf(line1, "%s, %s %2d %4d", + asprintf(&linebuff, "%s, %s %2d %4d", str_wday, str_month, tm->tm_mday, 1900+tm->tm_year); - lcd->x=0; - lcd->y=1; - lcd_i2c_gotoxy(lcd, lcd->x, lcd->y); - lcd_i2c_puts(lcd, line1); + + if (print_line_lcd(lcd, 1, linebuff) == -1) + errx(1, "cannot print line1: %s\n"); if (digitalRead(BUTTON1) == LOW) { if (backlight_status) { @@ -325,3 +327,20 @@ init_pins(void) return 0; } + +int +print_line_lcd(lcd_i2c_t *lcd, int line, char *content) +{ + if (strlen(content) > MAX_LINE_LEN) + return -1; + + if (line < 0 || line > LCD_LINES) + return -1; + + lcd->x=0; + lcd->y=line; + lcd_i2c_gotoxy(lcd, lcd->x, lcd->y); + lcd_i2c_puts(lcd, content); + + return 0; +}