| 1 | /* $Id$ |
|---|
| 2 | * $URL$ |
|---|
| 3 | * |
|---|
| 4 | * driver for serdisplib displays |
|---|
| 5 | * |
|---|
| 6 | * Copyright (C) 2005 Michael Reinelt <michael@reinelt.co.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 | |
|---|
| 27 | /* |
|---|
| 28 | * |
|---|
| 29 | * exported fuctions: |
|---|
| 30 | * |
|---|
| 31 | * struct DRIVER drv_serdisplib |
|---|
| 32 | * |
|---|
| 33 | */ |
|---|
| 34 | |
|---|
| 35 | #include "config.h" |
|---|
| 36 | |
|---|
| 37 | #include <stdlib.h> |
|---|
| 38 | #include <stdio.h> |
|---|
| 39 | #include <string.h> |
|---|
| 40 | #include <unistd.h> |
|---|
| 41 | |
|---|
| 42 | #include <serdisplib/serdisp.h> |
|---|
| 43 | |
|---|
| 44 | /* Fixme: This should be removed as soon as serdisp.h |
|---|
| 45 | * contains this macros |
|---|
| 46 | */ |
|---|
| 47 | #ifndef SERDISP_VERSION_GET_MAJOR |
|---|
| 48 | #define SERDISP_VERSION_GET_MAJOR(_c) ((int)( (_c) >> 8 )) |
|---|
| 49 | #define SERDISP_VERSION_GET_MINOR(_c) ((int)( (_c) & 0xFF )) |
|---|
| 50 | #endif |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | #include "debug.h" |
|---|
| 54 | #include "cfg.h" |
|---|
| 55 | #include "qprintf.h" |
|---|
| 56 | #include "plugin.h" |
|---|
| 57 | #include "drv.h" |
|---|
| 58 | #include "drv_generic_graphic.h" |
|---|
| 59 | |
|---|
| 60 | #ifdef WITH_DMALLOC |
|---|
| 61 | #include <dmalloc.h> |
|---|
| 62 | #endif |
|---|
| 63 | |
|---|
| 64 | static char Name[] = "serdisplib"; |
|---|
| 65 | |
|---|
| 66 | static serdisp_CONN_t *sdcd; |
|---|
| 67 | static serdisp_t *dd; |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | /****************************************/ |
|---|
| 71 | /*** hardware dependant functions ***/ |
|---|
| 72 | /****************************************/ |
|---|
| 73 | |
|---|
| 74 | static void drv_SD_blit(const int row, const int col, const int height, const int width) |
|---|
| 75 | { |
|---|
| 76 | int r, c; |
|---|
| 77 | |
|---|
| 78 | for (r = row; r < row + height; r++) { |
|---|
| 79 | for (c = col; c < col + width; c++) { |
|---|
| 80 | RGBA p = drv_generic_graphic_rgb(r, c); |
|---|
| 81 | serdisp_setcolour(dd, c, r, serdisp_pack2ARGB(0xff, p.R, p.G, p.B)); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | serdisp_update(dd); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | static int drv_SD_contrast(int contrast) |
|---|
| 90 | { |
|---|
| 91 | if (contrast < 0) |
|---|
| 92 | contrast = 0; |
|---|
| 93 | if (contrast > MAX_CONTRASTSTEP) |
|---|
| 94 | contrast = MAX_CONTRASTSTEP; |
|---|
| 95 | |
|---|
| 96 | serdisp_feature(dd, FEATURE_CONTRAST, contrast); |
|---|
| 97 | |
|---|
| 98 | return contrast; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | static int drv_SD_backlight(int backlight) |
|---|
| 103 | { |
|---|
| 104 | if (backlight < FEATURE_NO) |
|---|
| 105 | backlight = FEATURE_NO; |
|---|
| 106 | if (backlight > FEATURE_YES) |
|---|
| 107 | backlight = FEATURE_YES; |
|---|
| 108 | |
|---|
| 109 | serdisp_feature(dd, FEATURE_BACKLIGHT, backlight); |
|---|
| 110 | |
|---|
| 111 | return backlight; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | |
|---|
| 115 | static int drv_SD_reverse(int reverse) |
|---|
| 116 | { |
|---|
| 117 | if (reverse < FEATURE_NO) |
|---|
| 118 | reverse = FEATURE_NO; |
|---|
| 119 | if (reverse > FEATURE_YES) |
|---|
| 120 | reverse = FEATURE_YES; |
|---|
| 121 | |
|---|
| 122 | serdisp_feature(dd, FEATURE_REVERSE, reverse); |
|---|
| 123 | |
|---|
| 124 | return reverse; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | static int drv_SD_rotate(int rotate) |
|---|
| 129 | { |
|---|
| 130 | if (rotate < 0) |
|---|
| 131 | rotate = 0; |
|---|
| 132 | if (rotate > 3) |
|---|
| 133 | rotate = 3; |
|---|
| 134 | |
|---|
| 135 | serdisp_feature(dd, FEATURE_ROTATE, rotate); |
|---|
| 136 | |
|---|
| 137 | return rotate; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | static int drv_SD_start(const char *section) |
|---|
| 143 | { |
|---|
| 144 | long version; |
|---|
| 145 | char *port, *model, *options, *s; |
|---|
| 146 | int contrast, backlight, reverse, rotate; |
|---|
| 147 | |
|---|
| 148 | version = serdisp_getversioncode(); |
|---|
| 149 | info("%s: header version %d.%d", Name, SERDISP_VERSION_MAJOR, SERDISP_VERSION_MINOR); |
|---|
| 150 | info("%s: library version %d.%d", Name, SERDISP_VERSION_GET_MAJOR(version), SERDISP_VERSION_GET_MINOR(version)); |
|---|
| 151 | |
|---|
| 152 | port = cfg_get(section, "Port", NULL); |
|---|
| 153 | if (port == NULL || *port == '\0') { |
|---|
| 154 | error("%s: no '%s.Port' entry from %s", Name, section, cfg_source()); |
|---|
| 155 | return -1; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | /* opening the output device */ |
|---|
| 159 | sdcd = SDCONN_open(port); |
|---|
| 160 | if (sdcd == NULL) { |
|---|
| 161 | error("%s: open(%s) failed: %s", Name, port, sd_geterrormsg()); |
|---|
| 162 | return -1; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | model = cfg_get(section, "Model", ""); |
|---|
| 167 | if (model == NULL || *model == '\0') { |
|---|
| 168 | error("%s: no '%s.Model' entry from %s", Name, section, cfg_source()); |
|---|
| 169 | return -1; |
|---|
| 170 | } |
|---|
| 171 | info("%s: using model '%s'", Name, model); |
|---|
| 172 | |
|---|
| 173 | options = cfg_get(section, "Options", ""); |
|---|
| 174 | info("%s: using options '%s'", Name, options); |
|---|
| 175 | |
|---|
| 176 | /* opening and initialising the display */ |
|---|
| 177 | dd = serdisp_init(sdcd, model, options); |
|---|
| 178 | if (dd == NULL) { |
|---|
| 179 | error("%s: init(%s, %s, %s) failed: %s", Name, port, model, options, sd_geterrormsg()); |
|---|
| 180 | SDCONN_close(sdcd); |
|---|
| 181 | return -1; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | DROWS = serdisp_getheight(dd); |
|---|
| 185 | DCOLS = serdisp_getwidth(dd); |
|---|
| 186 | info("%s: display size %dx%d", Name, DCOLS, DROWS); |
|---|
| 187 | |
|---|
| 188 | XRES = -1; |
|---|
| 189 | YRES = -1; |
|---|
| 190 | s = cfg_get(section, "Font", "6x8"); |
|---|
| 191 | if (s == NULL || *s == '\0') { |
|---|
| 192 | error("%s: no '%s.Font' entry from %s", Name, section, cfg_source()); |
|---|
| 193 | return -1; |
|---|
| 194 | } |
|---|
| 195 | if (sscanf(s, "%dx%d", &XRES, &YRES) != 2 || XRES < 1 || YRES < 1) { |
|---|
| 196 | error("%s: bad Font '%s' from %s", Name, s, cfg_source()); |
|---|
| 197 | return -1; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | /* Fixme: provider other fonts someday... */ |
|---|
| 201 | if (XRES != 6 && YRES != 8) { |
|---|
| 202 | error("%s: bad Font '%s' from %s (only 6x8 at the moment)", Name, s, cfg_source()); |
|---|
| 203 | return -1; |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | /* clear display */ |
|---|
| 207 | serdisp_clear(dd); |
|---|
| 208 | |
|---|
| 209 | if (cfg_number(section, "Contrast", 0, 0, MAX_CONTRASTSTEP, &contrast) > 0) { |
|---|
| 210 | drv_SD_contrast(contrast); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | if (cfg_number(section, "Backlight", 0, 0, 1, &backlight) > 0) { |
|---|
| 214 | drv_SD_backlight(backlight); |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | if (cfg_number(section, "Reverse", 0, 0, 1, &reverse) > 0) { |
|---|
| 218 | drv_SD_reverse(reverse); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | if (cfg_number(section, "Rotate", 0, 0, 3, &rotate) > 0) { |
|---|
| 222 | drv_SD_rotate(rotate); |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | return 0; |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | |
|---|
| 229 | /****************************************/ |
|---|
| 230 | /*** plugins ***/ |
|---|
| 231 | /****************************************/ |
|---|
| 232 | |
|---|
| 233 | static void plugin_contrast(RESULT * result, RESULT * arg1) |
|---|
| 234 | { |
|---|
| 235 | double contrast; |
|---|
| 236 | |
|---|
| 237 | contrast = drv_SD_contrast(R2N(arg1)); |
|---|
| 238 | SetResult(&result, R_NUMBER, &contrast); |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | |
|---|
| 242 | static void plugin_backlight(RESULT * result, RESULT * arg1) |
|---|
| 243 | { |
|---|
| 244 | double backlight; |
|---|
| 245 | |
|---|
| 246 | backlight = drv_SD_backlight(R2N(arg1)); |
|---|
| 247 | SetResult(&result, R_NUMBER, &backlight); |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | static void plugin_reverse(RESULT * result, RESULT * arg1) |
|---|
| 252 | { |
|---|
| 253 | double reverse; |
|---|
| 254 | |
|---|
| 255 | reverse = drv_SD_reverse(R2N(arg1)); |
|---|
| 256 | SetResult(&result, R_NUMBER, &reverse); |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | |
|---|
| 260 | static void plugin_rotate(RESULT * result, RESULT * arg1) |
|---|
| 261 | { |
|---|
| 262 | double rotate; |
|---|
| 263 | |
|---|
| 264 | rotate = drv_SD_rotate(R2N(arg1)); |
|---|
| 265 | SetResult(&result, R_NUMBER, &rotate); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | /****************************************/ |
|---|
| 270 | /*** exported functions ***/ |
|---|
| 271 | /****************************************/ |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | /* list models */ |
|---|
| 275 | int drv_SD_list(void) |
|---|
| 276 | { |
|---|
| 277 | printf("%s", "any"); |
|---|
| 278 | return 0; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | /* initialize driver & display */ |
|---|
| 283 | int drv_SD_init(const char *section, const int quiet) |
|---|
| 284 | { |
|---|
| 285 | int ret; |
|---|
| 286 | |
|---|
| 287 | info("%s: %s", Name, "$Rev$"); |
|---|
| 288 | |
|---|
| 289 | /* real worker functions */ |
|---|
| 290 | drv_generic_graphic_real_blit = drv_SD_blit; |
|---|
| 291 | |
|---|
| 292 | /* start display */ |
|---|
| 293 | if ((ret = drv_SD_start(section)) != 0) |
|---|
| 294 | return ret; |
|---|
| 295 | |
|---|
| 296 | /* initialize generic graphic driver */ |
|---|
| 297 | if ((ret = drv_generic_graphic_init(section, Name)) != 0) |
|---|
| 298 | return ret; |
|---|
| 299 | |
|---|
| 300 | if (!quiet) { |
|---|
| 301 | char buffer[40]; |
|---|
| 302 | qprintf(buffer, sizeof(buffer), "%s %dx%d", Name, DCOLS, DROWS); |
|---|
| 303 | if (drv_generic_graphic_greet(buffer, NULL)) { |
|---|
| 304 | sleep(3); |
|---|
| 305 | drv_generic_graphic_clear(); |
|---|
| 306 | } |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | /* register plugins */ |
|---|
| 310 | AddFunction("LCD::contrast", 1, plugin_contrast); |
|---|
| 311 | AddFunction("LCD::backlight", 1, plugin_backlight); |
|---|
| 312 | AddFunction("LCD::reverse", 1, plugin_reverse); |
|---|
| 313 | AddFunction("LCD::rotate", 1, plugin_rotate); |
|---|
| 314 | |
|---|
| 315 | return 0; |
|---|
| 316 | } |
|---|
| 317 | |
|---|
| 318 | |
|---|
| 319 | /* close driver & display */ |
|---|
| 320 | int drv_SD_quit(const int quiet) |
|---|
| 321 | { |
|---|
| 322 | |
|---|
| 323 | info("%s: shutting down.", Name); |
|---|
| 324 | |
|---|
| 325 | drv_generic_graphic_clear(); |
|---|
| 326 | |
|---|
| 327 | if (!quiet) { |
|---|
| 328 | drv_generic_graphic_greet("goodbye!", NULL); |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | drv_generic_graphic_quit(); |
|---|
| 332 | |
|---|
| 333 | serdisp_quit(dd); |
|---|
| 334 | |
|---|
| 335 | return (0); |
|---|
| 336 | } |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | DRIVER drv_serdisplib = { |
|---|
| 340 | .name = Name, |
|---|
| 341 | .list = drv_SD_list, |
|---|
| 342 | .init = drv_SD_init, |
|---|
| 343 | .quit = drv_SD_quit, |
|---|
| 344 | }; |
|---|