| 1 | /* $Id: drv_LCDTerm.c,v 1.4 2005/05/08 04:32:44 reinelt Exp $ |
|---|
| 2 | * |
|---|
| 3 | * driver for the LCDTerm serial-to-HD44780 adapter boards |
|---|
| 4 | * http://www.bobblick.com/techref/projects/lcdterm/lcdterm.html |
|---|
| 5 | * |
|---|
| 6 | * Copyright (C) 2005 Michael Reinelt <reinelt@eunet.at> |
|---|
| 7 | * Copyright (C) 2005 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net> |
|---|
| 8 | * |
|---|
| 9 | * This file is part of LCD4Linux. |
|---|
| 10 | * |
|---|
| 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) |
|---|
| 14 | * any later version. |
|---|
| 15 | * |
|---|
| 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. |
|---|
| 20 | * |
|---|
| 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. |
|---|
| 24 | * |
|---|
| 25 | * |
|---|
| 26 | * $Log: drv_LCDTerm.c,v $ |
|---|
| 27 | * Revision 1.4 2005/05/08 04:32:44 reinelt |
|---|
| 28 | * CodingStyle added and applied |
|---|
| 29 | * |
|---|
| 30 | * Revision 1.3 2005/01/22 22:57:57 reinelt |
|---|
| 31 | * LCD-Linux driver added |
|---|
| 32 | * |
|---|
| 33 | * Revision 1.2 2005/01/18 06:30:23 reinelt |
|---|
| 34 | * added (C) to all copyright statements |
|---|
| 35 | * |
|---|
| 36 | * Revision 1.1 2005/01/15 13:13:57 reinelt |
|---|
| 37 | * LCDTerm driver added, take 2 |
|---|
| 38 | * |
|---|
| 39 | */ |
|---|
| 40 | |
|---|
| 41 | /* |
|---|
| 42 | * |
|---|
| 43 | * exported fuctions: |
|---|
| 44 | * |
|---|
| 45 | * struct DRIVER drv_LCDTerm |
|---|
| 46 | * |
|---|
| 47 | */ |
|---|
| 48 | |
|---|
| 49 | #include "config.h" |
|---|
| 50 | |
|---|
| 51 | #include <stdlib.h> |
|---|
| 52 | #include <unistd.h> |
|---|
| 53 | |
|---|
| 54 | #include "debug.h" |
|---|
| 55 | #include "cfg.h" |
|---|
| 56 | #include "qprintf.h" |
|---|
| 57 | #include "plugin.h" |
|---|
| 58 | #include "widget.h" |
|---|
| 59 | #include "widget_text.h" |
|---|
| 60 | #include "widget_icon.h" |
|---|
| 61 | #include "widget_bar.h" |
|---|
| 62 | #include "drv.h" |
|---|
| 63 | #include "drv_generic_text.h" |
|---|
| 64 | #include "drv_generic_serial.h" |
|---|
| 65 | |
|---|
| 66 | #define LCD_CLEAR 0x03 |
|---|
| 67 | #define LCD_CMD 0x12 |
|---|
| 68 | #define LCD_DATA 0x14 |
|---|
| 69 | |
|---|
| 70 | static char Name[] = "LCDTerm"; |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | /****************************************/ |
|---|
| 74 | /*** hardware dependant functions ***/ |
|---|
| 75 | /****************************************/ |
|---|
| 76 | |
|---|
| 77 | static void drv_LT_clear(void) |
|---|
| 78 | { |
|---|
| 79 | char cmd[1]; |
|---|
| 80 | |
|---|
| 81 | cmd[0] = LCD_CLEAR; /* clear display */ |
|---|
| 82 | drv_generic_serial_write(cmd, 1); /* clear screen */ |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | static int drv_LT_send(const char request, const char value) |
|---|
| 87 | { |
|---|
| 88 | char buf[2]; |
|---|
| 89 | |
|---|
| 90 | buf[0] = request; |
|---|
| 91 | buf[1] = value; |
|---|
| 92 | drv_generic_serial_write(buf, 2); |
|---|
| 93 | |
|---|
| 94 | return 0; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | static void drv_LT_command(const char cmd) |
|---|
| 99 | { |
|---|
| 100 | drv_LT_send(LCD_CMD, cmd); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | static void drv_LT_write(const int row, const int col, const char *data, int len) |
|---|
| 105 | { |
|---|
| 106 | int pos; |
|---|
| 107 | |
|---|
| 108 | /* 16x4 Displays use a slightly different layout */ |
|---|
| 109 | if (DCOLS == 16 && DROWS == 4) { |
|---|
| 110 | pos = (row % 2) * 64 + (row / 2) * 16 + col; |
|---|
| 111 | } else { |
|---|
| 112 | pos = (row % 2) * 64 + (row / 2) * 20 + col; |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | drv_LT_command(0x80 | pos); |
|---|
| 116 | |
|---|
| 117 | while (len--) { |
|---|
| 118 | drv_LT_send(LCD_DATA, *data++); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | static void drv_LT_defchar(const int ascii, const unsigned char *matrix) |
|---|
| 123 | { |
|---|
| 124 | int i; |
|---|
| 125 | |
|---|
| 126 | drv_LT_command(0x40 | 8 * ascii); |
|---|
| 127 | |
|---|
| 128 | for (i = 0; i < 8; i++) { |
|---|
| 129 | drv_LT_send(LCD_DATA, *matrix++ & 0x1f); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | static int drv_LT_start(const char *section, const int quiet) |
|---|
| 135 | { |
|---|
| 136 | int rows = -1, cols = -1; |
|---|
| 137 | char *s; |
|---|
| 138 | |
|---|
| 139 | if (drv_generic_serial_open(section, Name, 0) < 0) |
|---|
| 140 | return -1; |
|---|
| 141 | |
|---|
| 142 | s = cfg_get(section, "Size", NULL); |
|---|
| 143 | if (s == NULL || *s == '\0') { |
|---|
| 144 | error("%s: no '%s.Size' entry from %s", Name, section, cfg_source()); |
|---|
| 145 | return -1; |
|---|
| 146 | } |
|---|
| 147 | if (sscanf(s, "%dx%d", &cols, &rows) != 2 || rows < 1 || cols < 1) { |
|---|
| 148 | error("%s: bad %s.Size '%s' from %s", Name, section, s, cfg_source()); |
|---|
| 149 | free(s); |
|---|
| 150 | return -1; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | DROWS = rows; |
|---|
| 154 | DCOLS = cols; |
|---|
| 155 | |
|---|
| 156 | /* initialize display */ |
|---|
| 157 | drv_LT_command(0x29); /* 8 Bit mode, 1/16 duty cycle, 5x8 font */ |
|---|
| 158 | drv_LT_command(0x08); /* Display off, cursor off, blink off */ |
|---|
| 159 | drv_LT_command(0x0c); /* Display on, cursor off, blink off */ |
|---|
| 160 | drv_LT_command(0x06); /* curser moves to right, no shift */ |
|---|
| 161 | |
|---|
| 162 | drv_LT_clear(); /* clear display */ |
|---|
| 163 | |
|---|
| 164 | if (!quiet) { |
|---|
| 165 | char buffer[40]; |
|---|
| 166 | qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); |
|---|
| 167 | if (drv_generic_text_greet(buffer, "www.bwct.de")) { |
|---|
| 168 | sleep(3); |
|---|
| 169 | drv_LT_clear(); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | return 0; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | /****************************************/ |
|---|
| 178 | /*** plugins ***/ |
|---|
| 179 | /****************************************/ |
|---|
| 180 | |
|---|
| 181 | /* none */ |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | /****************************************/ |
|---|
| 185 | /*** widget callbacks ***/ |
|---|
| 186 | /****************************************/ |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | /* using drv_generic_text_draw(W) */ |
|---|
| 190 | /* using drv_generic_text_icon_draw(W) */ |
|---|
| 191 | /* using drv_generic_text_bar_draw(W) */ |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | /****************************************/ |
|---|
| 195 | /*** exported functions ***/ |
|---|
| 196 | /****************************************/ |
|---|
| 197 | |
|---|
| 198 | |
|---|
| 199 | /* list models */ |
|---|
| 200 | int drv_LT_list(void) |
|---|
| 201 | { |
|---|
| 202 | printf("generic"); |
|---|
| 203 | return 0; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | /* initialize driver & display */ |
|---|
| 208 | int drv_LT_init(const char *section, const int quiet) |
|---|
| 209 | { |
|---|
| 210 | WIDGET_CLASS wc; |
|---|
| 211 | int asc255bug; |
|---|
| 212 | int ret; |
|---|
| 213 | |
|---|
| 214 | /* display preferences */ |
|---|
| 215 | XRES = 5; /* pixel width of one char */ |
|---|
| 216 | YRES = 8; /* pixel height of one char */ |
|---|
| 217 | CHARS = 8; /* number of user-defineable characters */ |
|---|
| 218 | CHAR0 = 0; /* ASCII of first user-defineable char */ |
|---|
| 219 | |
|---|
| 220 | /* Fixme: */ |
|---|
| 221 | GOTO_COST = 2; /* number of bytes a goto command requires */ |
|---|
| 222 | |
|---|
| 223 | /* real worker functions */ |
|---|
| 224 | drv_generic_text_real_write = drv_LT_write; |
|---|
| 225 | drv_generic_text_real_defchar = drv_LT_defchar; |
|---|
| 226 | |
|---|
| 227 | |
|---|
| 228 | /* start display */ |
|---|
| 229 | if ((ret = drv_LT_start(section, quiet)) != 0) |
|---|
| 230 | return ret; |
|---|
| 231 | |
|---|
| 232 | /* initialize generic text driver */ |
|---|
| 233 | if ((ret = drv_generic_text_init(section, Name)) != 0) |
|---|
| 234 | return ret; |
|---|
| 235 | |
|---|
| 236 | /* initialize generic icon driver */ |
|---|
| 237 | if ((ret = drv_generic_text_icon_init()) != 0) |
|---|
| 238 | return ret; |
|---|
| 239 | |
|---|
| 240 | /* initialize generic bar driver */ |
|---|
| 241 | if ((ret = drv_generic_text_bar_init(0)) != 0) |
|---|
| 242 | return ret; |
|---|
| 243 | |
|---|
| 244 | /* add fixed chars to the bar driver */ |
|---|
| 245 | /* most displays have a full block on ascii 255, but some have kind of */ |
|---|
| 246 | /* an 'inverted P'. If you specify 'asc255bug 1 in the config, this */ |
|---|
| 247 | /* char will not be used, but rendered by the bar driver */ |
|---|
| 248 | cfg_number(section, "asc255bug", 0, 0, 1, &asc255bug); |
|---|
| 249 | drv_generic_text_bar_add_segment(0, 0, 255, 32); /* ASCII 32 = blank */ |
|---|
| 250 | if (!asc255bug) |
|---|
| 251 | drv_generic_text_bar_add_segment(255, 255, 255, 255); /* ASCII 255 = block */ |
|---|
| 252 | |
|---|
| 253 | /* register text widget */ |
|---|
| 254 | wc = Widget_Text; |
|---|
| 255 | wc.draw = drv_generic_text_draw; |
|---|
| 256 | widget_register(&wc); |
|---|
| 257 | |
|---|
| 258 | /* register icon widget */ |
|---|
| 259 | wc = Widget_Icon; |
|---|
| 260 | wc.draw = drv_generic_text_icon_draw; |
|---|
| 261 | widget_register(&wc); |
|---|
| 262 | |
|---|
| 263 | /* register bar widget */ |
|---|
| 264 | wc = Widget_Bar; |
|---|
| 265 | wc.draw = drv_generic_text_bar_draw; |
|---|
| 266 | widget_register(&wc); |
|---|
| 267 | |
|---|
| 268 | /* register plugins */ |
|---|
| 269 | /* none */ |
|---|
| 270 | |
|---|
| 271 | return 0; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | /* close driver & display */ |
|---|
| 276 | int drv_LT_quit(const int quiet) |
|---|
| 277 | { |
|---|
| 278 | |
|---|
| 279 | info("%s: shutting down.", Name); |
|---|
| 280 | |
|---|
| 281 | drv_generic_text_quit(); |
|---|
| 282 | |
|---|
| 283 | /* clear display */ |
|---|
| 284 | drv_LT_clear(); |
|---|
| 285 | |
|---|
| 286 | /* say goodbye... */ |
|---|
| 287 | if (!quiet) { |
|---|
| 288 | drv_generic_text_greet("goodbye!", NULL); |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | drv_generic_serial_close(); |
|---|
| 292 | |
|---|
| 293 | return (0); |
|---|
| 294 | } |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | DRIVER drv_LCDTerm = { |
|---|
| 298 | name:Name, |
|---|
| 299 | list:drv_LT_list, |
|---|
| 300 | init:drv_LT_init, |
|---|
| 301 | quit:drv_LT_quit, |
|---|
| 302 | }; |
|---|