~siretart/lcd4linux/debian

« back to all changes in this revision

Viewing changes to drv_Pertelian.c

  • Committer: Reinhard Tartler
  • Date: 2011-04-27 17:24:15 UTC
  • mto: This revision was merged to the branch mainline in revision 750.
  • Revision ID: siretart@tauware.de-20110427172415-6n4aptmvmz0eztvm
Tags: upstream-0.11.0~svn1143
ImportĀ upstreamĀ versionĀ 0.11.0~svn1143

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: drv_Pertelian.c 975 2009-01-18 11:16:20Z michael $
 
2
 * $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_Pertelian.c $
 
3
 *
 
4
 * Pertelian lcd4linux driver
 
5
 * Copyright (C) 2007 Andy Powell <lcd4linux@automted.it> 
 
6
 *
 
7
 * Copyright (C) 2005 Michael Reinelt <michael@reinelt.co.at>
 
8
 * Copyright (C) 2005, 2006, 2007 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 
9
 *
 
10
 * This file is part of LCD4Linux.
 
11
 *
 
12
 * LCD4Linux is free software; you can redistribute it and/or modify
 
13
 * it under the terms of the GNU General Public License as published by
 
14
 * the Free Software Foundation; either version 2, or (at your option)
 
15
 * any later version.
 
16
 *
 
17
 * LCD4Linux is distributed in the hope that it will be useful,
 
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
20
 * GNU General Public License for more details.
 
21
 *
 
22
 * You should have received a copy of the GNU General Public License
 
23
 * along with this program; if not, write to the Free Software
 
24
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
25
 *
 
26
 */
 
27
 
 
28
/* 
 
29
 *
 
30
 * exported fuctions:
 
31
 *
 
32
 * struct DRIVER drv_Pertelian
 
33
 *
 
34
 */
 
35
 
 
36
#include "config.h"
 
37
 
 
38
#include <stdlib.h>
 
39
#include <stdio.h>
 
40
#include <unistd.h>
 
41
#include <string.h>
 
42
#include <errno.h>
 
43
 
 
44
#include "debug.h"
 
45
#include "cfg.h"
 
46
#include "qprintf.h"
 
47
#include "udelay.h"
 
48
#include "plugin.h"
 
49
#include "widget.h"
 
50
#include "widget_text.h"
 
51
#include "widget_icon.h"
 
52
#include "widget_bar.h"
 
53
#include "drv.h"
 
54
 
 
55
#include "drv_generic_text.h"
 
56
#include "drv_generic_serial.h"
 
57
 
 
58
static char Name[] = "Pertelian";
 
59
 
 
60
static unsigned char rowoffset[4] = { 0x80, 0x80 + 0x40, 0x80 + 0x14, 0x80 + 0x40 + 0x14 };
 
61
 
 
62
#define PERTELIAN_LCDCOMMAND 0xfe
 
63
 
 
64
/****************************************/
 
65
/***  hardware dependant functions    ***/
 
66
/****************************************/
 
67
 
 
68
 
 
69
static int drv_Pertelian_open(const char *section)
 
70
{
 
71
    if (drv_generic_serial_open(section, Name, 0) < 0)
 
72
        return -1;
 
73
    return 0;
 
74
}
 
75
 
 
76
 
 
77
static int drv_Pertelian_close(void)
 
78
{
 
79
    drv_generic_serial_close();
 
80
    return 0;
 
81
}
 
82
 
 
83
 
 
84
static void drv_Pertelian_send(const char *data, const unsigned int len)
 
85
{
 
86
    unsigned int i;
 
87
 
 
88
    /* Pertelian interface seems to offer no buffering at all
 
89
       so we have to slow things down a tad yes 1usec is enough  */
 
90
 
 
91
    for (i = 0; i < len; i++) {
 
92
        drv_generic_serial_write(&data[i], 1);
 
93
        usleep(100);
 
94
    }
 
95
}
 
96
 
 
97
 
 
98
static void drv_Pertelian_clear(void)
 
99
{
 
100
    char cmd[2];
 
101
 
 
102
    cmd[0] = PERTELIAN_LCDCOMMAND;
 
103
    cmd[1] = 0x01;
 
104
    drv_Pertelian_send(cmd, 2);
 
105
}
 
106
 
 
107
 
 
108
static void drv_Pertelian_write(const int row, const int col, const char *data, int len)
 
109
{
 
110
    char cmd[3];
 
111
 
 
112
    cmd[0] = PERTELIAN_LCDCOMMAND;
 
113
    cmd[1] = ((rowoffset[row]) + (col));
 
114
    drv_Pertelian_send(cmd, 2);
 
115
    drv_Pertelian_send(data, len);
 
116
}
 
117
 
 
118
 
 
119
static void drv_Pertelian_defchar(const int ascii, const unsigned char *matrix)
 
120
{
 
121
    char cmd[11] = "";
 
122
    int i;
 
123
 
 
124
    cmd[0] = PERTELIAN_LCDCOMMAND;
 
125
    cmd[1] = (0x40 + (8 * ascii));
 
126
    for (i = 0; i < 8; i++) {
 
127
        cmd[i + 2] = matrix[i] & 0x1f;
 
128
    }
 
129
    drv_Pertelian_send(cmd, 10);
 
130
}
 
131
 
 
132
 
 
133
static int drv_Pertelian_backlight(int backlight)
 
134
{
 
135
    char cmd[2];
 
136
 
 
137
    if (backlight <= 0)
 
138
        backlight = 2;
 
139
    else if (backlight >= 1)
 
140
        backlight = 3;
 
141
 
 
142
    cmd[0] = PERTELIAN_LCDCOMMAND;
 
143
    cmd[1] = backlight;
 
144
 
 
145
    drv_Pertelian_send(cmd, 2);
 
146
 
 
147
    return backlight;
 
148
}
 
149
 
 
150
 
 
151
static int drv_Pertelian_start(const char *section)
 
152
{
 
153
    int backlight;
 
154
    int rows = -1, cols = -1;
 
155
    char *s;
 
156
    char cmd[12] = "";
 
157
 
 
158
    s = cfg_get(section, "Size", NULL);
 
159
    if (s == NULL || *s == '\0') {
 
160
        error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
 
161
        return -1;
 
162
    }
 
163
    if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
 
164
        error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
 
165
        free(s);
 
166
        return -1;
 
167
    }
 
168
 
 
169
    DROWS = rows;
 
170
    DCOLS = cols;
 
171
 
 
172
    /* open communication with the display */
 
173
    if (drv_Pertelian_open(section) < 0) {
 
174
        return -1;
 
175
    }
 
176
 
 
177
    /* reset & initialize display */
 
178
    cmd[0] = PERTELIAN_LCDCOMMAND;
 
179
    cmd[1] = 0x38;
 
180
    cmd[2] = PERTELIAN_LCDCOMMAND;
 
181
    cmd[3] = 0x06;
 
182
    cmd[4] = PERTELIAN_LCDCOMMAND;
 
183
    cmd[5] = 0x10;              /* move cursor on data write */
 
184
    cmd[6] = PERTELIAN_LCDCOMMAND;
 
185
    cmd[7] = 0x0c;
 
186
    cmd[8] = 0x0c;
 
187
 
 
188
    drv_Pertelian_send(cmd, 8);
 
189
 
 
190
    if (cfg_number(section, "Backlight", 0, 0, 1, &backlight) > 0) {
 
191
        drv_Pertelian_backlight(backlight);
 
192
    }
 
193
 
 
194
    drv_Pertelian_clear();      /* clear display */
 
195
    return 0;
 
196
}
 
197
 
 
198
 
 
199
/****************************************/
 
200
/***            plugins               ***/
 
201
/****************************************/
 
202
 
 
203
static void plugin_backlight(RESULT * result, RESULT * arg1)
 
204
{
 
205
    double backlight = 0;
 
206
    backlight = drv_Pertelian_backlight(R2N(arg1));
 
207
    SetResult(&result, R_NUMBER, &backlight);
 
208
}
 
209
 
 
210
/****************************************/
 
211
/***        widget callbacks          ***/
 
212
/****************************************/
 
213
 
 
214
 
 
215
/* using drv_generic_text_draw(W) */
 
216
/* using drv_generic_text_icon_draw(W) */
 
217
/* using drv_generic_text_bar_draw(W) */
 
218
/* using drv_generic_gpio_draw(W) */
 
219
 
 
220
 
 
221
/****************************************/
 
222
/***        exported functions        ***/
 
223
/****************************************/
 
224
 
 
225
 
 
226
/* list models */
 
227
int drv_Pertelian_list(void)
 
228
{
 
229
    printf("Pertelian X2040 displays");
 
230
    return 0;
 
231
}
 
232
 
 
233
 
 
234
/* initialize driver & display */
 
235
int drv_Pertelian_init(const char *section, const int quiet)
 
236
{
 
237
    WIDGET_CLASS wc;
 
238
    int ret;
 
239
 
 
240
    info("%s: %s", Name, "$Rev: 975 $");
 
241
 
 
242
    /* display preferences */
 
243
    XRES = 5;                   /* pixel width of one char  */
 
244
    YRES = 8;                   /* pixel height of one char  */
 
245
    CHARS = 8;                  /* number of user-defineable characters */
 
246
    CHAR0 = 0;                  /* ASCII of first user-defineable char */
 
247
    GOTO_COST = 2;              /* number of bytes a goto command requires */
 
248
 
 
249
    /* real worker functions */
 
250
    drv_generic_text_real_write = drv_Pertelian_write;
 
251
    drv_generic_text_real_defchar = drv_Pertelian_defchar;
 
252
 
 
253
 
 
254
    /* start display */
 
255
    if ((ret = drv_Pertelian_start(section)) != 0)
 
256
        return ret;
 
257
 
 
258
    if (!quiet) {
 
259
        char buffer[40];
 
260
        qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
 
261
        if (drv_generic_text_greet(buffer, "LinITX.com")) {
 
262
            sleep(3);
 
263
            drv_Pertelian_clear();
 
264
        }
 
265
    }
 
266
 
 
267
    /* initialize generic text driver */
 
268
    if ((ret = drv_generic_text_init(section, Name)) != 0)
 
269
        return ret;
 
270
 
 
271
    /* initialize generic icon driver */
 
272
    if ((ret = drv_generic_text_icon_init()) != 0)
 
273
        return ret;
 
274
 
 
275
    /* initialize generic bar driver */
 
276
    if ((ret = drv_generic_text_bar_init(0)) != 0)
 
277
        return ret;
 
278
 
 
279
    /* add fixed chars to the bar driver */
 
280
    drv_generic_text_bar_add_segment(0, 0, 255, 32);    /* ASCII  32 = blank */
 
281
 
 
282
 
 
283
    /* register text widget */
 
284
    wc = Widget_Text;
 
285
    wc.draw = drv_generic_text_draw;
 
286
    widget_register(&wc);
 
287
 
 
288
    /* register icon widget */
 
289
    wc = Widget_Icon;
 
290
    wc.draw = drv_generic_text_icon_draw;
 
291
    widget_register(&wc);
 
292
 
 
293
    /* register bar widget */
 
294
    wc = Widget_Bar;
 
295
    wc.draw = drv_generic_text_bar_draw;
 
296
    widget_register(&wc);
 
297
    AddFunction("LCD::backlight", 1, plugin_backlight);
 
298
    return 0;
 
299
}
 
300
 
 
301
 
 
302
/* close driver & display */
 
303
int drv_Pertelian_quit(const int quiet)
 
304
{
 
305
 
 
306
    info("%s: shutting down.", Name);
 
307
 
 
308
    drv_generic_text_quit();
 
309
 
 
310
    /* clear display */
 
311
    drv_Pertelian_clear();
 
312
 
 
313
    /* say goodbye... */
 
314
    if (!quiet) {
 
315
        drv_generic_text_greet("goodbye!", NULL);
 
316
    }
 
317
 
 
318
    drv_Pertelian_backlight(0);
 
319
    debug("closing connection");
 
320
    drv_Pertelian_close();
 
321
 
 
322
    return (0);
 
323
}
 
324
 
 
325
 
 
326
DRIVER drv_Pertelian = {
 
327
    .name = Name,
 
328
    .list = drv_Pertelian_list,
 
329
    .init = drv_Pertelian_init,
 
330
    .quit = drv_Pertelian_quit,
 
331
};