update lcd

This commit is contained in:
Olaf Rempel 2012-02-14 20:29:05 +01:00
parent e3d2dae9ea
commit 9f0c15cb02
5 changed files with 130 additions and 26 deletions

View File

@ -216,6 +216,7 @@ static int diskto_check_cb(void *privdata)
continue; continue;
} }
} }
close(fd);
/* args[2]: /* args[2]:
* 0x00 - standby * 0x00 - standby

75
lcd.c
View File

@ -81,6 +81,7 @@ struct lcddev {
struct event_timeout *reset_timeout; struct event_timeout *reset_timeout;
struct event_timeout *backlight_timeout; struct event_timeout *backlight_timeout;
struct event_timeout *scroll_timeout; struct event_timeout *scroll_timeout;
struct event_timeout *update_timeout;
}; };
void lcd_close(struct lcddev *dev) void lcd_close(struct lcddev *dev)
@ -95,6 +96,11 @@ void lcd_close(struct lcddev *dev)
dev->backlight_timeout = NULL; dev->backlight_timeout = NULL;
} }
if (dev->update_timeout) {
event_remove_timeout(dev->update_timeout);
dev->update_timeout = NULL;
}
if (dev->scroll_timeout) { if (dev->scroll_timeout) {
event_remove_timeout(dev->scroll_timeout); event_remove_timeout(dev->scroll_timeout);
dev->scroll_timeout = NULL; dev->scroll_timeout = NULL;
@ -327,11 +333,28 @@ int lcd_trigger_backlight(struct lcddev *dev, int timeout)
return retval; return retval;
} }
static int lcd_update_timeout_cb(void *privdata)
{
struct lcddev *dev = (struct lcddev *)privdata;
dev->update_timeout = NULL;
int update = dev->event_callback(dev, LCD_EVENT_UPDATE, dev->event_privdata);
/* update only if backlight is on (display visible) */
if ((update != 0) && (dev->backlight_state != 0)) {
struct timeval tv = { .tv_sec = update / 1000, .tv_usec = (update % 1000) * 1000 };
dev->update_timeout = event_add_timeout(&tv, lcd_update_timeout_cb, dev);
}
return -1; /* singleshot */
}
static int lcd_read_cb(int fd, void *privdata) static int lcd_read_cb(int fd, void *privdata)
{ {
struct lcddev *dev = (struct lcddev *)privdata; struct lcddev *dev = (struct lcddev *)privdata;
char buf[4]; char buf[4];
int update = 0;
int size = (dev->state != LCD_STATE_INITIALIZING) ? sizeof(buf) : 2; int size = (dev->state != LCD_STATE_INITIALIZING) ? sizeof(buf) : 2;
int len = lcd_read(dev, buf, size); int len = lcd_read(dev, buf, size);
@ -348,7 +371,7 @@ static int lcd_read_cb(int fd, void *privdata)
dev->state = LCD_STATE_READY; dev->state = LCD_STATE_READY;
/* trigger application to set data */ /* trigger application to set data */
dev->event_callback(dev, LCD_EVENT_INIT, dev->event_privdata); update = dev->event_callback(dev, LCD_EVENT_INIT, dev->event_privdata);
} }
} else if (dev->state == LCD_STATE_READY) { } else if (dev->state == LCD_STATE_READY) {
@ -359,13 +382,24 @@ static int lcd_read_cb(int fd, void *privdata)
return 0; return 0;
if (memcmp(buf, expect1, sizeof(buf)) == 0) { if (memcmp(buf, expect1, sizeof(buf)) == 0) {
dev->event_callback(dev, LCD_EVENT_BUTTON1, dev->event_privdata); update = dev->event_callback(dev, LCD_EVENT_BUTTON1, dev->event_privdata);
} else if (memcmp(buf, expect2, sizeof(buf)) == 0) { } else if (memcmp(buf, expect2, sizeof(buf)) == 0) {
dev->event_callback(dev, LCD_EVENT_BUTTON2, dev->event_privdata); update = dev->event_callback(dev, LCD_EVENT_BUTTON2, dev->event_privdata);
} }
} }
if (dev->update_timeout != NULL) {
event_remove_timeout(dev->update_timeout);
dev->update_timeout = NULL;
}
/* update only if backlight is on (display visible) */
if ((update != 0) && (dev->backlight_state != 0)) {
struct timeval tv = { .tv_sec = update / 1000, .tv_usec = (update % 1000) * 1000 };
dev->update_timeout = event_add_timeout(&tv, lcd_update_timeout_cb, dev);
}
return 0; return 0;
} }
@ -429,15 +463,36 @@ static int lcd_scroll_timeout_cb(void *privdata)
int lcd_setlines(struct lcddev *dev, int scroll_speed, const char *line1, const char *line2) int lcd_setlines(struct lcddev *dev, int scroll_speed, const char *line1, const char *line2)
{ {
dev->line_data[0] = line1; int i;
dev->line_length[0] = (line1 != NULL) ? strlen(line1) : 0; for (i = 0; i < 2; i++) {
lcd_setline(dev, 0, dev->line_length[0], dev->line_data[0]); if (dev->line_data[i] != NULL) {
free((void *)dev->line_data[i]);
}
dev->line_data[1] = line2; dev->line_data[i] = NULL;
dev->line_length[1] = (line2 != NULL) ? strlen(line2) : 0; dev->line_length[i] = 0;
}
if (line1 != NULL) {
dev->line_data[0] = strdup(line1);
if (dev->line_data[0] != NULL) {
dev->line_length[0] = strlen(line1);
}
}
if (line2 != NULL) {
dev->line_data[1] = strdup(line2);
if (dev->line_data[1] != NULL) {
dev->line_length[1] = strlen(line2);
}
}
lcd_setline(dev, 0, dev->line_length[0], dev->line_data[0]);
lcd_setline(dev, 1, dev->line_length[1], dev->line_data[1]); lcd_setline(dev, 1, dev->line_length[1], dev->line_data[1]);
int scroll_enable = (dev->line_length[0] > 16 && dev->line_length[1] > 16); int scroll_enable = (dev->line_length[0] > 16) || (dev->line_length[1] > 16);
if (dev->scroll_timeout && ((dev->scroll_speed != scroll_speed) || !scroll_enable)) { if (dev->scroll_timeout && ((dev->scroll_speed != scroll_speed) || !scroll_enable)) {
event_remove_timeout(dev->scroll_timeout); event_remove_timeout(dev->scroll_timeout);
dev->scroll_timeout = NULL; dev->scroll_timeout = NULL;
@ -447,7 +502,7 @@ int lcd_setlines(struct lcddev *dev, int scroll_speed, const char *line1, const
dev->scroll_pos = 0; dev->scroll_pos = 0;
if ((dev->scroll_timeout == NULL) && scroll_enable && (scroll_speed > 0)) { if ((dev->scroll_timeout == NULL) && scroll_enable && (scroll_speed > 0)) {
struct timeval tv = { .tv_usec = 1000 * dev->scroll_speed }; struct timeval tv = { .tv_sec = dev->scroll_speed / 1000, .tv_usec = (dev->scroll_speed % 1000) * 1000 };
dev->scroll_timeout = event_add_timeout(&tv, lcd_scroll_timeout_cb, dev); dev->scroll_timeout = event_add_timeout(&tv, lcd_scroll_timeout_cb, dev);
} }

9
lcd.h
View File

@ -1,10 +1,11 @@
#ifndef _LCD_H_ #ifndef _LCD_H_
#define _LCD_H_ #define _LCD_H_
#define LCD_EVENT_INIT 0x00 #define LCD_EVENT_INIT 0x00 /* lcd initialized */
#define LCD_EVENT_BUTTON1 0x01 #define LCD_EVENT_UPDATE 0x01 /* update data */
#define LCD_EVENT_BUTTON2 0x02 #define LCD_EVENT_BUTTON1 0x02 /* button1 pressed */
#define LCD_EVENT_BACKLIGHT 0x04 #define LCD_EVENT_BUTTON2 0x03 /* button2 pressed */
#define LCD_EVENT_BACKLIGHT 0x04 /* backlight timeout */
struct lcddev; /* private data */ struct lcddev; /* private data */

61
qnapd.c
View File

@ -27,6 +27,9 @@
#include <sys/types.h> #include <sys/types.h>
#include <signal.h> #include <signal.h>
#include <sys/utsname.h>
#include <time.h>
#include "configfile.h" #include "configfile.h"
#include "disktimeout.h" #include "disktimeout.h"
#include "event.h" #include "event.h"
@ -62,13 +65,41 @@ static int button_callback(struct lcddev *dev, int button, void *privdata)
page = (page > 0) ? page -1 : PAGE_MAX; page = (page > 0) ? page -1 : PAGE_MAX;
} else if (button == LCD_EVENT_BACKLIGHT) { } else if (button == LCD_EVENT_BACKLIGHT) {
return 0; return 0; /* no update requested */
} }
if (button != LCD_EVENT_UPDATE) {
lcd_trigger_backlight(dev, LCD_TIMEOUT);
}
int update = 0;
switch (page) { switch (page) {
case 0: case 7:
lcd_setlines(dev, 500, "Debian GNU/Linux 5.0", "Linux 2.6.32.27-486 i586"); {
FILE *fp = fopen("/etc/issue.net", "r");
char line1[64];
char line2[64];
memset(line1, 0x00, sizeof(line1));
memset(line2, 0x00, sizeof(line2));
if (fp != NULL) {
fread(line1, 1, sizeof(line1), fp);
fclose(fp);
} else {
strcpy(line1, "<unknown system>");
}
struct utsname utsbuf;
uname(&utsbuf);
snprintf(line2, sizeof(line2), "%s %s", utsbuf.sysname, utsbuf.release);
lcd_setlines(dev, 500, line1, line2);
}
break; break;
case 1: case 1:
lcd_setlines(dev, 0, "Hostname:", "storenix.lan"); lcd_setlines(dev, 0, "Hostname:", "storenix.lan");
break; break;
@ -87,13 +118,29 @@ static int button_callback(struct lcddev *dev, int button, void *privdata)
case 6: case 6:
lcd_setlines(dev, 0, "Uptime:", " XXXd XX:XX:XX"); lcd_setlines(dev, 0, "Uptime:", " XXXd XX:XX:XX");
break; break;
case 7: case 0:
lcd_setlines(dev, 0, "Time: XX:XX:XX", "Date: XX-XX-XXXX"); {
char line1[20];
char line2[20];
struct tm tmp;
time_t now;
time(&now);
localtime_r(&now, &tmp);
strftime(line1, sizeof(line1), "Time: %H:%M:%S", &tmp);
strftime(line2, sizeof(line2), "Date: %d-%m-%Y", &tmp);
lcd_setlines(dev, 0, line1, line2);
update = 1000;
}
break; break;
} }
lcd_trigger_backlight(dev, LCD_TIMEOUT); return update;
return 1;
} }
static int restart_var; static int restart_var;

View File

@ -1,7 +1,7 @@
[global] [global]
#lcddevice /dev/ttyS0 #lcddevice /dev/ttyS0
#lcddevice dummy lcddevice dummy
pidfile qnapd.pid pidfile qnapd.pid
logfile qnapd.log logfile qnapd.log