1
/* $Id: drv_LW_ABP.c 1071 2009-11-22 05:27:53Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_LW_ABP.c $
4
* driver for Logic Way GmbH ABP08 (serial) and ABP09 (USB) appliance control panels
5
* http://www.logicway.de/pages/mde-hardware.shtml#ABP.AMC
7
* Copyright (C) 2009 Arndt Kritzner <kritzner@logicway.de>
8
* Copyright (C) 2005 Michael Reinelt <michael@reinelt.co.at>
9
* Copyright (C) 2005, 2006, 2007, 2009 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
11
* This file is part of LCD4Linux.
13
* LCD4Linux is free software; you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation; either version 2, or (at your option)
18
* LCD4Linux is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
23
* You should have received a copy of the GNU General Public License
24
* along with this program; if not, write to the Free Software
25
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
33
* struct DRIVER drv_LW_ABP
43
#include <sys/types.h>
55
#include "widget_text.h"
56
#include "widget_bar.h"
57
#include "widget_keypad.h"
60
/* text mode display? */
61
#include "drv_generic_text.h"
62
#include "drv_generic_keypad.h"
65
#include "drv_generic_serial.h"
69
#include "drv_generic_i2c.h"
77
static char Name[] = "LW_ABP";
78
static char logicway[] = "www.logicway.de";
80
/* ring buffer for bytes received from the display */
81
static unsigned char RingBuffer[256];
82
static unsigned int RingRPos = 0;
83
static unsigned int RingWPos = 0;
85
static int button = -1;
86
static char *colors[] =
87
{ "r 0", "r 1", "g 0", "g 1", "b 0", "b 1", "c 0", "c 1", "m 0", "m 1", "y 0", "y 1", "w 0", "w 1", NULL };
89
#define TIMESYNC_FIRST 600
90
#define TIMESYNC_INTERVAL 24*3600
91
static time_t next_timesync;
93
static char *switchcontact = NULL;
94
static int statresult = 0;
95
static time_t switchcontact_lastchange = 0;
96
static time_t switchcontact_time = 0;
97
#define SWITCH_ACTION_DURATION 1000
99
/****************************************/
100
/*** hardware dependant functions ***/
101
/****************************************/
103
static int drv_LW_ABP_open(const char *section)
105
/* open serial port */
106
/* don't mind about device, speed and stuff, this function will take care of */
108
if (drv_generic_serial_open(section, Name, 0) < 0)
115
static int drv_LW_ABP_close(void)
117
drv_generic_serial_close();
123
/* dummy function that sends something to the display */
124
static void drv_LW_ABP_send(const char *data, const unsigned int len)
126
/* send data to the serial port is easy... */
127
drv_generic_serial_write(data, len);
131
/* text mode displays only */
132
static void drv_LW_ABP_reset(void)
134
char cmd[] = "lcd init\r\n";
136
/* do whatever is necessary to initialize the display */
137
drv_LW_ABP_send(cmd, strlen(cmd));
140
static void drv_LW_ABP_clear(void)
142
char cmd[] = "lcd clear\r\n";
144
/* do whatever is necessary to clear the display */
145
drv_LW_ABP_send(cmd, strlen(cmd));
148
static int drv_LW_ABP_time(unsigned char force)
150
char cmd[] = "date set ";
152
time_t t = time(NULL);
154
if (force || (t > next_timesync)) {
155
/* do whatever is necessary to set clock on the display */
156
sprintf(command, "%s%lu\r\n", cmd, (unsigned long) t);
157
drv_LW_ABP_send(command, strlen(command));
158
next_timesync = t + TIMESYNC_INTERVAL;
159
info("%s: synced time to %lu, next is %lu\n", Name, (unsigned long) t, (unsigned long) next_timesync);
166
static int drv_LW_ABP_switchcontact(void)
168
char cmd[] = "switch time ";
170
struct stat statinfo;
175
if ((switchcontact != NULL) && (*switchcontact != '\0')) {
176
if (stat(switchcontact, &statinfo) == -1) {
177
if (statresult != -1) {
178
error("%s: specified switch contact control file %s not found", Name, switchcontact);
179
switchcontact_time = 0;
180
sprintf(command, "%s%lu %u\r\n", cmd, (unsigned long) switchcontact_time, SWITCH_ACTION_DURATION);
181
drv_LW_ABP_send(command, strlen(command));
182
info("%s: switchtime deactivated", Name);
186
if (statresult == -1) {
187
info("%s: specified switch contact control file %s appeared", Name, switchcontact);
190
if ((switchcontact_lastchange == 0) || (switchcontact_lastchange < statinfo.st_mtime)) {
191
info("%s: specified switch contact control file %s has changed (%lu < %lu)", Name, switchcontact,
192
(unsigned long) switchcontact_lastchange, (unsigned long) statinfo.st_mtime);
193
switchcontact_lastchange = statinfo.st_mtime;
194
configfile = fopen(switchcontact, "r");
195
if (configfile == NULL) {
196
error("%s: could not open specified switch contact control file %s", Name, switchcontact);
199
while (!feof(configfile)) {
200
if (fgets(inputline, 100, configfile) != NULL) {
201
if ((scanresult = sscanf(inputline, "switchtime=%lu", &switchcontact_time)) == 1) {
202
info("%s: switchtime read %lu (file %s)", Name, (unsigned long) switchcontact_time,
209
if (config_entries > 0) {
210
if (switchcontact_time < time(NULL)) {
211
switchcontact_time = 0;
212
info("%s: switchtime is in past %lu (now %lu), so deactivated", Name,
213
(unsigned long) switchcontact_time, time(NULL));
216
switchcontact_time = 0;
218
if (switchcontact_time != 0) {
219
sprintf(command, "%s%lu %u\r\n", cmd, (unsigned long) switchcontact_time,
220
SWITCH_ACTION_DURATION);
221
drv_LW_ABP_send(command, strlen(command));
222
info("%s: switchtime set to %lu", Name, (unsigned long) switchcontact_time);
224
sprintf(command, "%s%lu %u\r\n", cmd, (unsigned long) switchcontact_time,
225
SWITCH_ACTION_DURATION);
226
drv_LW_ABP_send(command, strlen(command));
227
info("%s: switchtime deactivated", Name);
237
/* text mode displays only */
238
static void drv_LW_ABP_write(const int row, const int col, const char *data, int len)
240
char cmd[] = "lcd set line ";
244
/* do the cursor positioning here */
245
drv_LW_ABP_send(cmd, strlen(cmd));
246
sprintf(row_, "%d", row + 1);
247
drv_LW_ABP_send(row_, strlen(row_));
249
drv_LW_ABP_send(",", 1);
250
sprintf(col_, "%d", col + 1);
251
drv_LW_ABP_send(col_, strlen(col_));
253
drv_LW_ABP_send(" ", 1);
255
/* send string to the display */
256
drv_LW_ABP_send(data, len);
257
drv_LW_ABP_send("\r\n", 2);
261
static unsigned char byte(int pos)
265
if ((unsigned) pos >= sizeof(RingBuffer))
266
pos -= sizeof(RingBuffer);
270
pos += sizeof(RingBuffer);
272
return RingBuffer[pos];
275
static void drv_LW_ABP_process_button(void)
278
debug("%s: Key Activity: %d", Name, button);
279
drv_generic_keypad_press(button);
282
static int drv_LW_ABP_poll(void)
286
/* read into RingBuffer */
290
num = drv_generic_serial_poll(buffer, sizeof(buffer));
293
/* put result into RingBuffer */
294
for (n = 0; n < num; n++) {
295
RingBuffer[RingWPos++] = (unsigned char) buffer[n];
296
if (RingWPos >= sizeof(RingBuffer))
303
/* process RingBuffer */
308
num = RingWPos - RingRPos;
310
num += sizeof(RingBuffer);
311
/* minimum packet size=3 */
313
if (strlen(received) > 0) {
314
debug("%s: received: %s", Name, received);
318
if (byte(0) != '[') {
321
for (n = 0; (n < num) && ((unsigned) n < (sizeof(command) - 1)); n++) {
322
command[n] = byte(n);
323
if (command[n] == ']') {
329
if (command[n - 1] != ']') {
330
if (strlen(command) < 4) {
331
if (strlen(received) > 0) {
332
debug("%s: received: %s", Name, received);
338
info("%s: command read from keypad: %s\n", Name, command);
339
if (sscanf(command, "[T%d]", &button) == 1) {
340
info("%s: button %d pressed\n", Name, button);
344
/* increment read pointer */
345
RingRPos += strlen(command);
346
if (RingRPos >= sizeof(RingBuffer))
347
RingRPos -= sizeof(RingBuffer);
348
/* a packet arrived */
349
if (strlen(received) > 0) {
350
debug("%s: received: %s", Name, received);
358
if (strlen(received) > 0) {
359
debug("%s: received: %s", Name, received);
365
debug("%s: dropping garbage byte %02x", Name, byte(0));
366
else if ((strlen(received) + 2) > sizeof(received)) {
367
debug("%s: received: %s", Name, received);
370
sprintf(received, "%s%c", received, byte(0));
374
if (RingRPos >= sizeof(RingBuffer))
383
static void drv_LW_ABP_timer(void __attribute__ ((unused)) * notused)
385
while (drv_LW_ABP_poll()) {
386
drv_LW_ABP_process_button();
390
drv_LW_ABP_switchcontact();
393
static int drv_LW_ABP_keypad(const int num)
399
debug("%s: Key Up", Name);
400
val += WIDGET_KEY_PRESSED;
401
val += WIDGET_KEY_UP;
404
debug("%s: Key Down", Name);
405
val += WIDGET_KEY_PRESSED;
406
val += WIDGET_KEY_DOWN;
409
debug("%s: Key Left / Cancel", Name);
410
val += WIDGET_KEY_PRESSED;
411
val += WIDGET_KEY_LEFT;
412
// val += WIDGET_KEY_CANCEL;
415
debug("%s: Key Right / Confirm", Name);
416
val += WIDGET_KEY_PRESSED;
417
val += WIDGET_KEY_RIGHT;
418
// val += WIDGET_KEY_CONFIRM;
421
debug("%s: Unbound Key '%d'", Name, num);
428
static char *drv_LW_ABP_background(char *color)
430
static char *Background = NULL;
431
char cmd[] = "lcd set color ";
434
for (idx = 0; colors[idx] != NULL; idx++) {
435
if (strcmp(color, colors[idx]) == 0)
438
if (colors[idx] != NULL) {
439
Background = colors[idx];
440
debug("%s: set background color %s", Name, Background);
441
drv_LW_ABP_send(cmd, strlen(cmd));
442
drv_LW_ABP_send(color, strlen(color));
443
drv_LW_ABP_send("\r\n", 2);
449
static int drv_LW_ABP_contrast(int contrast)
455
/* start text mode display */
456
static int drv_LW_ABP_start(const char *section)
459
int rows = -1, cols = -1;
463
s = cfg_get(section, "Size", NULL);
464
if (s == NULL || *s == '\0') {
465
error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
468
if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
469
error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
477
next_timesync = time(NULL) + TIMESYNC_FIRST;
479
/* open communication with the display */
480
if (drv_LW_ABP_open(section) < 0) {
484
drv_LW_ABP_reset(); /* initialize display */
486
background = cfg_get(section, "Background", NULL);
487
if ((background != NULL) && (*background != '\0')) {
488
if (drv_LW_ABP_background(background) == NULL) {
489
debug("%s: wrong background color specified: %s", Name, background);
493
switchcontact = cfg_get(section, "Switchcontact", NULL);
495
if (cfg_number(section, "Contrast", 0, 0, 255, &contrast) > 0) {
496
drv_LW_ABP_contrast(contrast);
499
drv_LW_ABP_clear(); /* clear display */
504
/****************************************/
506
/****************************************/
508
static void plugin_contrast(RESULT * result, RESULT * arg1)
512
contrast = drv_LW_ABP_contrast(R2N(arg1));
513
SetResult(&result, R_NUMBER, &contrast);
516
static void plugin_background(RESULT * result, const int argc, RESULT * argv[])
522
color = drv_LW_ABP_background(NULL);
523
SetResult(&result, R_STRING, &color);
526
color = drv_LW_ABP_background(R2S(argv[0]));
527
SetResult(&result, R_STRING, &color);
530
error("%s.backlight(): wrong number of parameters (%d)", Name, argc);
531
SetResult(&result, R_STRING, "");
536
/****************************************/
537
/*** widget callbacks ***/
538
/****************************************/
541
/* using drv_generic_text_draw(W) */
542
/* using drv_generic_text_icon_draw(W) */
543
/* using drv_generic_text_bar_draw(W) */
544
/* using drv_generic_gpio_draw(W) */
547
/****************************************/
548
/*** exported functions ***/
549
/****************************************/
553
int drv_LW_ABP_list(void)
555
printf("Logic Way ABP driver");
560
/* initialize driver & display */
561
/* use this function for a text display */
562
int drv_LW_ABP_init(const char *section, const int quiet)
567
info("%s: %s", Name, "$Rev: 1071 $");
569
/* display preferences */
570
XRES = 5; /* pixel width of one char */
571
YRES = 8; /* pixel height of one char */
572
CHARS = 0; /* number of user-defineable characters */
573
CHAR0 = 0; /* ASCII of first user-defineable char */
574
GOTO_COST = 10; /* number of bytes a goto command requires */
576
/* real worker functions */
577
drv_generic_text_real_write = drv_LW_ABP_write;
578
drv_generic_keypad_real_press = drv_LW_ABP_keypad;
580
/* regularly process display answers */
581
timer_add(drv_LW_ABP_timer, NULL, 100, 0);
584
if ((ret = drv_LW_ABP_start(section)) != 0)
589
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
590
if (drv_generic_text_greet(buffer, logicway)) {
596
/* initialize generic text driver */
597
if ((ret = drv_generic_text_init(section, Name)) != 0)
600
/* initialize generic icon driver */
601
if ((ret = drv_generic_text_icon_init()) != 0)
604
/* initialize generic bar driver */
605
if ((ret = drv_generic_text_bar_init(0)) != 0)
608
/* initialize generic key pad driver */
609
if ((ret = drv_generic_keypad_init(section, Name)) != 0)
612
/* add fixed chars to the bar driver */
613
drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */
615
/* register text widget */
617
wc.draw = drv_generic_text_draw;
618
widget_register(&wc);
620
/* register bar widget */
622
wc.draw = drv_generic_text_bar_draw;
623
widget_register(&wc);
625
/* register plugins */
626
AddFunction("LCD::contrast", 1, plugin_contrast);
627
AddFunction("LCD::background", -1, plugin_background);
634
/* close driver & display */
635
/* use this function for a text display */
636
int drv_LW_ABP_quit(const int quiet)
638
char switchactive[] = "switch scheduled";
640
struct tm switchtime_info;
642
info("%s: shutting down.", Name);
644
drv_generic_text_quit();
645
drv_generic_keypad_quit();
651
if (switchcontact_time < time(NULL)) {
652
switchcontact_time = 0;
653
info("%s: switchtime is in past %lu (now %lu), so deactivated", Name, (unsigned long) switchcontact_time,
656
if (switchcontact_time == 0) {
658
drv_generic_text_greet("goodbye!", NULL);
661
drv_LW_ABP_write(0, 0, logicway, strlen(logicway));
662
drv_LW_ABP_write(1, 0, switchactive, strlen(switchactive));
663
localtime_r(&switchcontact_time, &switchtime_info);
664
if (strftime(timeinfo, sizeof(timeinfo), "for %a %X", &switchtime_info) > 0) {
665
drv_LW_ABP_write(2, 0, timeinfo, strlen(timeinfo));
669
debug("closing connection");
676
/* use this one for a text display */
677
DRIVER drv_LW_ABP = {
679
.list = drv_LW_ABP_list,
680
.init = drv_LW_ABP_init,
681
.quit = drv_LW_ABP_quit,