From 2db5472a8daf555968511c03a5239c82fd220c5f Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Sun, 17 Apr 2011 17:37:38 +0200 Subject: [PATCH] change lcd_* interface --- lcd.c | 35 ++++++++----- lcd.h | 7 ++- qnaplcd.c | 143 +++++++++++++++++++++++++++--------------------------- 3 files changed, 100 insertions(+), 85 deletions(-) diff --git a/lcd.c b/lcd.c index 0245e0c..b662d63 100644 --- a/lcd.c +++ b/lcd.c @@ -33,13 +33,14 @@ struct lcddev { struct termios oldtio; int backlight_timeout; - int (*button_cb)(struct lcddev *dev, int button); + int (*button_cb)(struct lcddev *dev, int button, void *privdata); + void *privdata; struct event_fd *read_event; struct event_timeout *backlight_event; }; -static void lcd_close(struct lcddev *dev) +void lcd_close(struct lcddev *dev) { tcsetattr(dev->fd, TCSANOW, &dev->oldtio); close(dev->fd); @@ -91,7 +92,7 @@ static int lcd_read(struct lcddev *dev, const char *buf, int len) #if _LCD_DEBUG fprintf(stderr, "lcd_read[%d/%d]: ", cnt, len); for (i = 0; i < cnt; i++) - fprintf(stderr, "0x%X ", buf[i]); + fprintf(stderr, "0x%X ", (unsigned char)buf[i]); fprintf(stderr, "\n"); #endif return (cnt != 0) ? cnt : retval; @@ -105,7 +106,7 @@ static int lcd_write(struct lcddev *dev, char *buf, int len) #if _LCD_DEBUG fprintf(stderr, "lcd_write[%d/%d]: ", retval, len); for (i = 0; i < retval; i++) - fprintf(stderr, "0x%X ", buf[i]); + fprintf(stderr, "0x%X ", (unsigned char)buf[i]); fprintf(stderr, "\n"); #endif return retval; @@ -177,48 +178,56 @@ static int lcd_read_cb(int fd, void *privdata) return 0; } + int retval = 0; char expect1[] = A125_EVENT_BUTTON1; char expect2[] = A125_EVENT_BUTTON2; + if (memcmp(buf, expect1, sizeof(buf)) == 0) { - dev->button_cb(dev, LCD_BUTTON1); - lcd_trigger_backlight(dev); + retval = dev->button_cb(dev, LCD_BUTTON1, dev->privdata); } else if (memcmp(buf, expect2, sizeof(buf)) == 0) { - dev->button_cb(dev, LCD_BUTTON2); + retval = dev->button_cb(dev, LCD_BUTTON2, dev->privdata); + } + + if (retval) { lcd_trigger_backlight(dev); } return 0; } -int lcd_init(const char *devicename, int backlight_timeout, int (*button_cb)(struct lcddev *dev, int button)) +struct lcddev * lcd_init(const char *devicename, + int backlight_timeout, + int (*button_cb)(struct lcddev *dev, int button, void *privdata), + void *privdata) { struct lcddev *dev = malloc(sizeof(struct lcddev)); if (dev == NULL) { log_print(LOG_ERROR, "lcd_init(): out of memory"); - return -1; + return NULL; } memset(dev, 0, sizeof(struct lcddev)); if (lcd_open(dev, devicename) < 0) { free(dev); - return -1; + return NULL; } if (lcd_reset(dev) < 0) { log_print(LOG_ERROR, "lcd_init(): could not reset LCD"); lcd_close(dev); free(dev); - return -1; + return NULL; } dev->backlight_timeout = backlight_timeout; lcd_trigger_backlight(dev); dev->button_cb = button_cb; - button_cb(dev, LCD_BUTTON0); + dev->privdata = privdata; + button_cb(dev, LCD_BUTTON0, dev->privdata); dev->read_event = event_add_readfd(NULL, dev->fd, lcd_read_cb, dev); - return 0; + return dev; } diff --git a/lcd.h b/lcd.h index 811f8fa..ea4a635 100644 --- a/lcd.h +++ b/lcd.h @@ -7,7 +7,12 @@ struct lcddev; /* private data */ -int lcd_init(const char *device, int backlight_timeout, int (*button_callback)(struct lcddev *dev, int button)); +struct lcddev * lcd_init(const char *device, + int backlight_timeout, + int (*button_callback)(struct lcddev *dev, int button, void *privdata), + void *privdata); + +void lcd_close(struct lcddev *dev); int lcd_setline(struct lcddev *dev, int line, const char *text); #endif /* _LCD_H_ */ diff --git a/qnaplcd.c b/qnaplcd.c index e4c7258..4e9d29e 100644 --- a/qnaplcd.c +++ b/qnaplcd.c @@ -52,7 +52,7 @@ static struct option opts[] = { static int page; -static int button_callback(struct lcddev *dev, int button) +static int button_callback(struct lcddev *dev, int button, void *privdata) { if (button == LCD_BUTTON2) page = (page < PAGE_MAX) ? page +1 : 0; @@ -119,95 +119,96 @@ int check_restart(int *maxfd, void *readfds, void *writefds, struct timeval *tim int main(int argc, char *argv[]) { - char *config = DEFAULT_CONFIG; - int code, arg = 0, debug = 0; + char *config = DEFAULT_CONFIG; + int code, arg = 0, debug = 0; - do { - code = getopt_long(argc, argv, "c:dh", opts, &arg); + do { + code = getopt_long(argc, argv, "c:dh", opts, &arg); - switch (code) { - case 'c': /* config */ - config = optarg; - break; + switch (code) { + case 'c': /* config */ + config = optarg; + break; - case 'd': /* debug */ - debug = 1; - break; + case 'd': /* debug */ + debug = 1; + break; - case 'h': /* help */ - printf("Usage: qnaplcd [options]\n" - "Options: \n" - " --config -c configfile use this configfile\n" - " --debug -d do not fork and log to stderr\n" - " --help -h this help\n" - "\n"); - exit(0); - break; + case 'h': /* help */ + printf("Usage: qnaplcd [options]\n" + "Options: \n" + " --config -c configfile use this configfile\n" + " --debug -d do not fork and log to stderr\n" + " --help -h this help\n" + "\n"); + exit(0); + break; - case '?': /* error */ - exit(1); - break; + case '?': /* error */ + exit(1); + break; - default: /* unknown / all options parsed */ - break; - } - } while (code != -1); + default: /* unknown / all options parsed */ + break; + } + } while (code != -1); - /* parse config file */ - if (config_parse(config) < 0) - exit(1); + /* parse config file */ + if (config_parse(config) < 0) + exit(1); - if (!debug) { - /* check pidfile */ - const char *pidfile = config_get_string("global", "pidfile", DEFAULT_PIDFILE); - if (pidfile_check(pidfile, 1) != 0) { - log_print(LOG_ERROR, "qnaplcd already running"); - exit(1); - } + if (!debug) { + /* check pidfile */ + const char *pidfile = config_get_string("global", "pidfile", DEFAULT_PIDFILE); + if (pidfile_check(pidfile, 1) != 0) { + log_print(LOG_ERROR, "qnaplcd already running"); + exit(1); + } - /* start logging */ - const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); - if (log_init(logfile) < 0) - exit(1); + /* start logging */ + const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); + if (log_init(logfile) < 0) + exit(1); - /* zum daemon mutieren */ - if (daemon(-1, 0) < 0) { - log_print(LOG_ERROR, "failed to daemonize"); - exit(1); - } + /* zum daemon mutieren */ + if (daemon(-1, 0) < 0) { + log_print(LOG_ERROR, "failed to daemonize"); + exit(1); + } - /* create pidfile */ - if (pidfile_create(pidfile) < 0) { - log_print(LOG_ERROR, "failed to create pidfile %s", pidfile); - exit(1); - } - } + /* create pidfile */ + if (pidfile_create(pidfile) < 0) { + log_print(LOG_ERROR, "failed to create pidfile %s", pidfile); + exit(1); + } + } - signal_init(); - signal_add_callback(SIGHUP, trigger_restart, (void *)&restart_var); + signal_init(); + signal_add_callback(SIGHUP, trigger_restart, (void *)&restart_var); - log_print(LOG_EVERYTIME, "qnaplcd started (pid:%d)", getpid()); + log_print(LOG_EVERYTIME, "qnaplcd started (pid:%d)", getpid()); - while (1) { - if (lcd_init(LCD_DEVICE, LCD_TIMEOUT, &button_callback) < 0) - break; + while (1) { + struct lcddev *dev = lcd_init(LCD_DEVICE, LCD_TIMEOUT, &button_callback, NULL); + if (dev == NULL) + break; - /* exited on restart / SIGUSR1 */ - event_loop(check_restart, NULL, (void *)&restart_var); + /* exited on restart / SIGUSR1 */ + event_loop(check_restart, NULL, (void *)&restart_var); - // TODO: need lcd_close() + lcd_close(dev); - config_free(); + config_free(); - if (config_parse(config) < 0) - break; + if (config_parse(config) < 0) + break; - const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); - if (!debug && log_init(logfile) < 0) - break; + const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); + if (!debug && log_init(logfile) < 0) + break; - log_print(LOG_EVERYTIME, "bcastfwd restarted (pid:%d) ", getpid()); - } + log_print(LOG_EVERYTIME, "bcastfwd restarted (pid:%d) ", getpid()); + } - return 0; + return 0; }