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_ */

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;
@ -189,13 +189,14 @@ int main(int argc, char *argv[])
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);
if (dev == NULL)
break; 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();