1
/* $Id: drv_Sample.c 975 2009-01-18 11:16:20Z michael $
2
* $URL: https://ssl.bulix.org/svn/lcd4linux/trunk/drv_Sample.c $
4
* sample lcd4linux driver
6
* Copyright (C) 2005 Michael Reinelt <michael@reinelt.co.at>
7
* Copyright (C) 2005, 2006, 2007 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
9
* This file is part of LCD4Linux.
11
* LCD4Linux is free software; you can redistribute it and/or modify
12
* it under the terms of the GNU General Public License as published by
13
* the Free Software Foundation; either version 2, or (at your option)
16
* LCD4Linux is distributed in the hope that it will be useful,
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
* GNU General Public License for more details.
21
* You should have received a copy of the GNU General Public License
22
* along with this program; if not, write to the Free Software
23
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
31
* struct DRIVER drv_Sample
49
#include "widget_text.h"
50
#include "widget_icon.h"
51
#include "widget_bar.h"
54
/* text mode display? */
55
#include "drv_generic_text.h"
57
/* graphic display? */
58
#include "drv_generic_graphic.h"
61
#include "drv_generic_gpio.h"
64
#include "drv_generic_serial.h"
67
#include "drv_generic_parport.h"
71
#include "drv_generic_i2c.h"
75
static char Name[] = "Sample";
78
/* for parallel port displays only */
79
/* use whatever lines you need */
80
static unsigned char SIGNAL_RS;
81
static unsigned char SIGNAL_EX;
85
/****************************************/
86
/*** hardware dependant functions ***/
87
/****************************************/
89
/* low-level parallel port stuff */
90
/* example for sending one byte over the wire */
91
static void drv_Sample_bang(const unsigned int data)
93
/* put data on DB1..DB8 */
94
drv_generic_parport_data(data & 0xff);
96
/* set/clear some signals */
97
drv_generic_parport_control(SIGNAL_RS, SIGNAL_RS);
99
/* data setup time (e.g. 200 ns) */
103
/* signal pulse width 500ns */
104
drv_generic_parport_toggle(SIGNAL_EX, 1, 500);
106
/* wait for command completion (e.g. 100 us) */
111
static int drv_Sample_open(const char *section)
113
/* open serial port */
114
/* don't mind about device, speed and stuff, this function will take care of */
116
if (drv_generic_serial_open(section, Name, 0) < 0)
120
/* opening the parallel port is a bit more tricky: */
121
/* you have to do all the bit-banging yourself... */
123
if (drv_generic_parport_open(section, Name) != 0) {
124
error("%s: could not initialize parallel port!", Name);
128
/* read the wiring from config */
129
if ((SIGNAL_EX = drv_generic_parport_wire_ctrl("EX", "STROBE")) == 0xff)
131
if ((SIGNAL_RS = drv_generic_parport_wire_ctrl("RS", "INIT")) == 0xff)
134
/* clear all signals */
135
drv_generic_parport_control(SIGNAL_EX | SIGNAL_RS, 0);
137
/* set direction: write */
138
drv_generic_parport_direction(0);
144
static int drv_Sample_close(void)
146
/* close whatever port you've opened */
147
drv_generic_parport_close();
148
drv_generic_serial_close();
154
/* dummy function that sends something to the display */
155
static void drv_Sample_send(const char *data, const unsigned int len)
159
/* send data to the serial port is easy... */
160
drv_generic_serial_write(data, len);
162
/* sending data to the parallel port is a bit more tricky... */
163
for (i = 0; i < len; i++) {
164
drv_Sample_bang(*data++);
169
/* text mode displays only */
170
static void drv_Sample_clear(void)
174
/* do whatever is necessary to clear the display */
175
/* assume 0x01 to be a 'clear display' command */
177
drv_Sample_send(cmd, 1);
181
/* text mode displays only */
182
static void drv_Sample_write(const int row, const int col, const char *data, int len)
186
/* do the cursor positioning here */
187
/* assume 0x02 to be a 'Goto' command */
191
drv_Sample_send(cmd, 3);
193
/* send string to the display */
194
drv_Sample_send(data, len);
198
/* text mode displays only */
199
static void drv_Sample_defchar(const int ascii, const unsigned char *matrix)
204
/* call the 'define character' function */
205
/* assume 0x03 to be the 'defchar' command */
209
/* send bitmap to the display */
210
for (i = 0; i < 8; i++) {
211
cmd[i + 2] = *matrix++;
213
drv_Sample_send(cmd, 10);
217
/* for graphic displays only */
218
static void drv_Sample_blit(const int row, const int col, const int height, const int width)
222
for (r = row; r < row + height; r++) {
223
for (c = col; c < col + width; c++) {
224
/* drv_generic_graphic_black() returns 1 if pixel is black */
225
/* drv_generic_graphic_gray() returns a gray value 0..255 */
226
/* drv_generic_graphic_rgb() returns a RGB color */
227
if (drv_generic_graphic_black(r, c)) {
237
/* remove unless you have GPO's */
238
static int drv_Sample_GPO(const int num, const int val)
242
/* assume 0x42 to be the 'GPO' command */
245
cmd[2] = (val > 0) ? 1 : 0;
247
drv_Sample_send(cmd, 3);
253
/* example function used in a plugin */
254
static int drv_Sample_contrast(int contrast)
258
/* adjust limits according to the display */
264
/* call a 'contrast' function */
265
/* assume 0x04 to be the 'set contrast' command */
268
drv_Sample_send(cmd, 2);
274
/* start text mode display */
275
static int drv_Sample_start(const char *section)
278
int rows = -1, cols = -1;
282
s = cfg_get(section, "Size", NULL);
283
if (s == NULL || *s == '\0') {
284
error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
287
if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) {
288
error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source());
296
/* number of GPO's; remove if you don't have them */
299
/* open communication with the display */
300
if (drv_Sample_open(section) < 0) {
304
/* reset & initialize display */
305
/* assume 0x00 to be a 'reset' command */
307
drv_Sample_send(cmd, 0);
309
if (cfg_number(section, "Contrast", 0, 0, 255, &contrast) > 0) {
310
drv_Sample_contrast(contrast);
313
drv_Sample_clear(); /* clear display */
319
/* start graphic display */
320
static int drv_Sample_start2(const char *section)
326
/* read display size from config */
327
s = cfg_get(section, "Size", NULL);
328
if (s == NULL || *s == '\0') {
329
error("%s: no '%s.Size' entry from %s", Name, section, cfg_source());
335
if (sscanf(s, "%dx%d", &DCOLS, &DROWS) != 2 || DCOLS < 1 || DROWS < 1) {
336
error("%s: bad Size '%s' from %s", Name, s, cfg_source());
340
s = cfg_get(section, "Font", "6x8");
341
if (s == NULL || *s == '\0') {
342
error("%s: no '%s.Font' entry from %s", Name, section, cfg_source());
348
if (sscanf(s, "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) {
349
error("%s: bad Font '%s' from %s", Name, s, cfg_source());
353
/* Fixme: provider other fonts someday... */
354
if (XRES != 6 && YRES != 8) {
355
error("%s: bad Font '%s' from %s (only 6x8 at the moment)", Name, s, cfg_source());
359
/* you surely want to allocate a framebuffer or something... */
361
/* open communication with the display */
362
if (drv_Sample_open(section) < 0) {
366
/* reset & initialize display */
367
/* assume 0x00 to be a 'reset' command */
369
drv_Sample_send(cmd, 1);
371
if (cfg_number(section, "Contrast", 0, 0, 255, &contrast) > 0) {
372
drv_Sample_contrast(contrast);
379
/****************************************/
381
/****************************************/
383
static void plugin_contrast(RESULT * result, RESULT * arg1)
387
contrast = drv_Sample_contrast(R2N(arg1));
388
SetResult(&result, R_NUMBER, &contrast);
392
/****************************************/
393
/*** widget callbacks ***/
394
/****************************************/
397
/* using drv_generic_text_draw(W) */
398
/* using drv_generic_text_icon_draw(W) */
399
/* using drv_generic_text_bar_draw(W) */
400
/* using drv_generic_gpio_draw(W) */
403
/****************************************/
404
/*** exported functions ***/
405
/****************************************/
409
int drv_Sample_list(void)
411
printf("Sample driver");
416
/* initialize driver & display */
417
/* use this function for a text display */
418
int drv_Sample_init(const char *section, const int quiet)
423
info("%s: %s", Name, "$Rev: 975 $");
425
/* display preferences */
426
XRES = 5; /* pixel width of one char */
427
YRES = 8; /* pixel height of one char */
428
CHARS = 8; /* number of user-defineable characters */
429
CHAR0 = 0; /* ASCII of first user-defineable char */
430
GOTO_COST = 2; /* number of bytes a goto command requires */
432
/* real worker functions */
433
drv_generic_text_real_write = drv_Sample_write;
434
drv_generic_text_real_defchar = drv_Sample_defchar;
436
/* remove unless you have GPO's */
437
drv_generic_gpio_real_set = drv_Sample_GPO;
441
if ((ret = drv_Sample_start(section)) != 0)
446
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
447
if (drv_generic_text_greet(buffer, "www.bwct.de")) {
453
/* initialize generic text driver */
454
if ((ret = drv_generic_text_init(section, Name)) != 0)
457
/* initialize generic icon driver */
458
if ((ret = drv_generic_text_icon_init()) != 0)
461
/* initialize generic bar driver */
462
if ((ret = drv_generic_text_bar_init(0)) != 0)
465
/* add fixed chars to the bar driver */
466
drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */
469
/* initialize generic GPIO driver */
470
/* remove unless you have GPO's */
471
if ((ret = drv_generic_gpio_init(section, Name)) != 0)
474
/* register text widget */
476
wc.draw = drv_generic_text_draw;
477
widget_register(&wc);
479
/* register icon widget */
481
wc.draw = drv_generic_text_icon_draw;
482
widget_register(&wc);
484
/* register bar widget */
486
wc.draw = drv_generic_text_bar_draw;
487
widget_register(&wc);
489
/* register plugins */
490
AddFunction("LCD::contrast", 1, plugin_contrast);
496
/* initialize driver & display */
497
/* use this function for a graphic display */
498
int drv_Sample_init2(const char *section, const int quiet)
502
/* real worker functions */
503
drv_generic_graphic_real_blit = drv_Sample_blit;
505
/* remove unless you have GPO's */
506
drv_generic_gpio_real_set = drv_Sample_GPO;
509
if ((ret = drv_Sample_start2(section)) != 0)
512
/* initialize generic graphic driver */
513
if ((ret = drv_generic_graphic_init(section, Name)) != 0)
518
qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS);
519
if (drv_generic_graphic_greet(buffer, NULL)) {
521
drv_generic_graphic_clear();
525
/* register plugins */
526
AddFunction("LCD::contrast", 1, plugin_contrast);
533
/* close driver & display */
534
/* use this function for a text display */
535
int drv_Sample_quit(const int quiet)
538
info("%s: shutting down.", Name);
540
drv_generic_text_quit();
542
/* remove unless you have GPO's */
543
drv_generic_gpio_quit();
550
drv_generic_text_greet("goodbye!", NULL);
553
debug("closing connection");
559
/* close driver & display */
560
/* use this function for a graphic display */
561
int drv_Sample_quit2(const int quiet)
564
info("%s: shutting down.", Name);
567
drv_generic_graphic_clear();
569
/* remove unless you have GPO's */
570
drv_generic_gpio_quit();
574
drv_generic_graphic_greet("goodbye!", NULL);
577
drv_generic_graphic_quit();
579
debug("closing connection");
586
/* use this one for a text display */
587
DRIVER drv_Sample = {
589
.list = drv_Sample_list,
590
.init = drv_Sample_init,
591
.quit = drv_Sample_quit,
595
/* use this one for a graphic display */
596
DRIVER drv_Sample2 = {
598
.list = drv_Sample_list,
599
.init = drv_Sample_init2,
600
.quit = drv_Sample_quit2,