change lcd_* interface

This commit is contained in:
Olaf Rempel 2011-04-17 17:37:38 +02:00
parent bc54aecceb
commit 2db5472a8d
3 changed files with 100 additions and 85 deletions

35
lcd.c
View File

@ -33,13 +33,14 @@ struct lcddev {
struct termios oldtio; struct termios oldtio;
int backlight_timeout; 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_fd *read_event;
struct event_timeout *backlight_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); tcsetattr(dev->fd, TCSANOW, &dev->oldtio);
close(dev->fd); close(dev->fd);
@ -91,7 +92,7 @@ static int lcd_read(struct lcddev *dev, const char *buf, int len)
#if _LCD_DEBUG #if _LCD_DEBUG
fprintf(stderr, "lcd_read[%d/%d]: ", cnt, len); fprintf(stderr, "lcd_read[%d/%d]: ", cnt, len);
for (i = 0; i < cnt; i++) for (i = 0; i < cnt; i++)
fprintf(stderr, "0x%X ", buf[i]); fprintf(stderr, "0x%X ", (unsigned char)buf[i]);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
#endif #endif
return (cnt != 0) ? cnt : retval; return (cnt != 0) ? cnt : retval;
@ -105,7 +106,7 @@ static int lcd_write(struct lcddev *dev, char *buf, int len)
#if _LCD_DEBUG #if _LCD_DEBUG
fprintf(stderr, "lcd_write[%d/%d]: ", retval, len); fprintf(stderr, "lcd_write[%d/%d]: ", retval, len);
for (i = 0; i < retval; i++) for (i = 0; i < retval; i++)
fprintf(stderr, "0x%X ", buf[i]); fprintf(stderr, "0x%X ", (unsigned char)buf[i]);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
#endif #endif
return retval; return retval;
@ -177,48 +178,56 @@ static int lcd_read_cb(int fd, void *privdata)
return 0; return 0;
} }
int retval = 0;
char expect1[] = A125_EVENT_BUTTON1; char expect1[] = A125_EVENT_BUTTON1;
char expect2[] = A125_EVENT_BUTTON2; char expect2[] = A125_EVENT_BUTTON2;
if (memcmp(buf, expect1, sizeof(buf)) == 0) { if (memcmp(buf, expect1, sizeof(buf)) == 0) {
dev->button_cb(dev, LCD_BUTTON1); retval = dev->button_cb(dev, LCD_BUTTON1, dev->privdata);
lcd_trigger_backlight(dev);
} else if (memcmp(buf, expect2, sizeof(buf)) == 0) { } 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); lcd_trigger_backlight(dev);
} }
return 0; 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)); struct lcddev *dev = malloc(sizeof(struct lcddev));
if (dev == NULL) { if (dev == NULL) {
log_print(LOG_ERROR, "lcd_init(): out of memory"); log_print(LOG_ERROR, "lcd_init(): out of memory");
return -1; return NULL;
} }
memset(dev, 0, sizeof(struct lcddev)); memset(dev, 0, sizeof(struct lcddev));
if (lcd_open(dev, devicename) < 0) { if (lcd_open(dev, devicename) < 0) {
free(dev); free(dev);
return -1; return NULL;
} }
if (lcd_reset(dev) < 0) { if (lcd_reset(dev) < 0) {
log_print(LOG_ERROR, "lcd_init(): could not reset LCD"); log_print(LOG_ERROR, "lcd_init(): could not reset LCD");
lcd_close(dev); lcd_close(dev);
free(dev); free(dev);
return -1; return NULL;
} }
dev->backlight_timeout = backlight_timeout; dev->backlight_timeout = backlight_timeout;
lcd_trigger_backlight(dev); lcd_trigger_backlight(dev);
dev->button_cb = button_cb; 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); dev->read_event = event_add_readfd(NULL, dev->fd, lcd_read_cb, dev);
return 0; return dev;
} }

7
lcd.h
View File

@ -7,7 +7,12 @@
struct lcddev; /* private data */ 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); int lcd_setline(struct lcddev *dev, int line, const char *text);
#endif /* _LCD_H_ */ #endif /* _LCD_H_ */

143
qnaplcd.c
View File

@ -52,7 +52,7 @@ static struct option opts[] = {
static int page; 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) if (button == LCD_BUTTON2)
page = (page < PAGE_MAX) ? page +1 : 0; 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[]) int main(int argc, char *argv[])
{ {
char *config = DEFAULT_CONFIG; char *config = DEFAULT_CONFIG;
int code, arg = 0, debug = 0; int code, arg = 0, debug = 0;
do { do {
code = getopt_long(argc, argv, "c:dh", opts, &arg); code = getopt_long(argc, argv, "c:dh", opts, &arg);
switch (code) { switch (code) {
case 'c': /* config */ case 'c': /* config */
config = optarg; config = optarg;
break; break;
case 'd': /* debug */ case 'd': /* debug */
debug = 1; debug = 1;
break; break;
case 'h': /* help */ case 'h': /* help */
printf("Usage: qnaplcd [options]\n" printf("Usage: qnaplcd [options]\n"
"Options: \n" "Options: \n"
" --config -c configfile use this configfile\n" " --config -c configfile use this configfile\n"
" --debug -d do not fork and log to stderr\n" " --debug -d do not fork and log to stderr\n"
" --help -h this help\n" " --help -h this help\n"
"\n"); "\n");
exit(0); exit(0);
break; break;
case '?': /* error */ case '?': /* error */
exit(1); exit(1);
break; break;
default: /* unknown / all options parsed */ default: /* unknown / all options parsed */
break; break;
} }
} while (code != -1); } while (code != -1);
/* parse config file */ /* parse config file */
if (config_parse(config) < 0) if (config_parse(config) < 0)
exit(1); exit(1);
if (!debug) { if (!debug) {
/* check pidfile */ /* check pidfile */
const char *pidfile = config_get_string("global", "pidfile", DEFAULT_PIDFILE); const char *pidfile = config_get_string("global", "pidfile", DEFAULT_PIDFILE);
if (pidfile_check(pidfile, 1) != 0) { if (pidfile_check(pidfile, 1) != 0) {
log_print(LOG_ERROR, "qnaplcd already running"); log_print(LOG_ERROR, "qnaplcd already running");
exit(1); exit(1);
} }
/* start logging */ /* start logging */
const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
if (log_init(logfile) < 0) if (log_init(logfile) < 0)
exit(1); exit(1);
/* zum daemon mutieren */ /* zum daemon mutieren */
if (daemon(-1, 0) < 0) { if (daemon(-1, 0) < 0) {
log_print(LOG_ERROR, "failed to daemonize"); log_print(LOG_ERROR, "failed to daemonize");
exit(1); exit(1);
} }
/* create pidfile */ /* create pidfile */
if (pidfile_create(pidfile) < 0) { if (pidfile_create(pidfile) < 0) {
log_print(LOG_ERROR, "failed to create pidfile %s", pidfile); log_print(LOG_ERROR, "failed to create pidfile %s", pidfile);
exit(1); exit(1);
} }
} }
signal_init(); signal_init();
signal_add_callback(SIGHUP, trigger_restart, (void *)&restart_var); 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) { while (1) {
if (lcd_init(LCD_DEVICE, LCD_TIMEOUT, &button_callback) < 0) struct lcddev *dev = lcd_init(LCD_DEVICE, LCD_TIMEOUT, &button_callback, NULL);
break; if (dev == NULL)
break;
/* exited on restart / SIGUSR1 */ /* exited on restart / SIGUSR1 */
event_loop(check_restart, NULL, (void *)&restart_var); event_loop(check_restart, NULL, (void *)&restart_var);
// TODO: need lcd_close() lcd_close(dev);
config_free(); config_free();
if (config_parse(config) < 0) if (config_parse(config) < 0)
break; break;
const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE); const char *logfile = config_get_string("global", "logfile", DEFAULT_LOGFILE);
if (!debug && log_init(logfile) < 0) if (!debug && log_init(logfile) < 0)
break; break;
log_print(LOG_EVERYTIME, "bcastfwd restarted (pid:%d) ", getpid()); log_print(LOG_EVERYTIME, "bcastfwd restarted (pid:%d) ", getpid());
} }
return 0; return 0;
} }