graphlcd-base/glcddrivers/gu256x64-372.c
changeset 4 df6a40031aa5
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/graphlcd-base/glcddrivers/gu256x64-372.c	Wed Feb 06 17:32:55 2008 +0000
     1.3 @@ -0,0 +1,408 @@
     1.4 +/*
     1.5 + * GraphLCD driver library
     1.6 + *
     1.7 + * gu256x64-372.c  -  8-bit driver module for Noritake GU256x64-372
     1.8 + *                    VFD displays. The VFD is operating in its 8-bit
     1.9 + *                    mode connected to a single PC parallel port.
    1.10 + *
    1.11 + * based on:
    1.12 + *   gu256x32f driver module for graphlcd
    1.13 + *     (c) 2003 Andreas Brachold <vdr04 AT deltab.de>
    1.14 + *   HD61830 device
    1.15 + *     (c) 2001-2003 by Carsten Siebholz <c.siebholz AT t-online.de>
    1.16 + *   lcdproc 0.4 driver hd44780-ext8bit
    1.17 + *     (c) 1999, 1995 Benjamin Tse <blt AT comports.com>
    1.18 + *
    1.19 + * This file is released under the GNU General Public License. Refer
    1.20 + * to the COPYING file distributed with this package.
    1.21 + *
    1.22 + * (c) 2004 Andreas 'randy' Weinberger (randy AT smue.org)
    1.23 + */
    1.24 +
    1.25 +#include <errno.h>
    1.26 +#include <syslog.h>
    1.27 +#include <unistd.h>
    1.28 +#include <sys/time.h>
    1.29 +
    1.30 +#include "common.h"
    1.31 +#include "config.h"
    1.32 +#include "gu256x64-372.h"
    1.33 +#include "port.h"
    1.34 +
    1.35 +
    1.36 +namespace GLCD
    1.37 +{
    1.38 +
    1.39 +#define SCREENSOFF      0x00 // both screens are off
    1.40 +#define SCREEN1ON       0x01 // only screen #1 is on (graphic screen)
    1.41 +#define SCREEN2ON       0x02 // only screen #2 is on (graphic/character screen)
    1.42 +#define SCREENSON       0x03 // both screens are on
    1.43 +
    1.44 +#define CURS_AUTOINC    0x04 // cursor increments automatically
    1.45 +#define CURS_HOLD       0x05 // cursor holds
    1.46 +
    1.47 +#define SCREEN2CHAR     0x06 // screen #2 sets to "character" display
    1.48 +#define SCREEN2GRAPH    0x07 // screen #2 sets to "graphic" display
    1.49 +
    1.50 +#define DATA_WRITE      0x08 // data write mode
    1.51 +#define DATA_READ       0x09 // data read mode
    1.52 +
    1.53 +#define DISP_LOSTA1     0x0A // lower addr. of display start of screen #1
    1.54 +#define DISP_HISTA1     0x0B // upper addr. of display start of screen #1
    1.55 +#define DISP_LOSTA2     0x0C // lower addr. of display start of screen #2
    1.56 +#define DISP_HISTA2     0x0D // upper addr. of display start of screen #2
    1.57 +#define CURS_LOADDR     0x0E // lower addr. of cursor of screen #1 & #2
    1.58 +#define CURS_HIADDR     0x0F // upper addr. of cursor start of screen #1 & #2
    1.59 +
    1.60 +#define DISP_OR         0x10 // or display of screen #1 & #2
    1.61 +#define DISP_EXOR       0x11 // ex-or display of screen #1 & #2
    1.62 +#define DISP_AND        0x12 // and display of screen #1 & #2
    1.63 +
    1.64 +#define BRIGHT_1        0x18 // luminance level 1 100.0%
    1.65 +#define BRIGHT_2        0x19 // luminance level 2  87.5%
    1.66 +#define BRIGHT_3        0x1A // luminance level 3  75.0%
    1.67 +#define BRIGHT_4        0x1B // luminance level 4  62.5%
    1.68 +
    1.69 +#define WRHI            0x04  // INIT
    1.70 +#define WRLO            0x00
    1.71 +#define CDHI            0x00 // nSEL
    1.72 +#define CDLO            0x08
    1.73 +
    1.74 +
    1.75 +cDriverGU256X64_372::cDriverGU256X64_372(cDriverConfig * config)
    1.76 +:   config(config)
    1.77 +{
    1.78 +    oldConfig = new cDriverConfig(*config);
    1.79 +
    1.80 +    port = new cParallelPort();
    1.81 +
    1.82 +    m_nRefreshCounter = 0;
    1.83 +}
    1.84 +
    1.85 +cDriverGU256X64_372::~cDriverGU256X64_372()
    1.86 +{
    1.87 +    delete oldConfig;
    1.88 +    delete port;
    1.89 +}
    1.90 +
    1.91 +int cDriverGU256X64_372::Init()
    1.92 +{
    1.93 +    int x;
    1.94 +    struct timeval tv1, tv2;
    1.95 +
    1.96 +    width = config->width;
    1.97 +    if (width <= 0)
    1.98 +        width = 256;
    1.99 +    height = config->height;
   1.100 +    if (height <= 0)
   1.101 +        height = 64;
   1.102 +    m_iSizeYb = (height + 7) / 8;
   1.103 +
   1.104 +    for (unsigned int i = 0; i < config->options.size(); i++)
   1.105 +    {
   1.106 +        if (config->options[i].name == "")
   1.107 +        {
   1.108 +        }
   1.109 +    }
   1.110 +
   1.111 +    // setup linear lcd array
   1.112 +    m_pDrawMem = new unsigned char *[width];
   1.113 +    if (m_pDrawMem)
   1.114 +    {
   1.115 +        for (x = 0; x < width; x++)
   1.116 +        {
   1.117 +            m_pDrawMem[x] = new unsigned char[m_iSizeYb];
   1.118 +            memset(m_pDrawMem[x], 0, m_iSizeYb);
   1.119 +        }
   1.120 +    }
   1.121 +    Clear();
   1.122 +
   1.123 +    // setup the lcd array for the "vertical" mem
   1.124 +    m_pVFDMem = new unsigned char *[width];
   1.125 +    if (m_pVFDMem)
   1.126 +    {
   1.127 +        for (x = 0; x < width; x++)
   1.128 +        {
   1.129 +            m_pVFDMem[x] = new unsigned char[m_iSizeYb];
   1.130 +            memset(m_pVFDMem[x], 0, m_iSizeYb);
   1.131 +        }
   1.132 +    }
   1.133 +    ClearVFDMem();
   1.134 +
   1.135 +    if (config->device == "")
   1.136 +    {
   1.137 +        // use DirectIO
   1.138 +        if (port->Open(config->port) != 0)
   1.139 +            return -1;
   1.140 +        uSleep(10);
   1.141 +    }
   1.142 +    else
   1.143 +    {
   1.144 +        // use ppdev
   1.145 +        if (port->Open(config->device.c_str()) != 0)
   1.146 +            return -1;
   1.147 +    }
   1.148 +
   1.149 +    if (nSleepInit() != 0)
   1.150 +    {
   1.151 +        syslog(LOG_ERR, "%s: INFO: cannot change wait parameters  Err: %s (cDriver::Init)\n", config->name.c_str(), strerror(errno));
   1.152 +        m_bSleepIsInit = false;
   1.153 +    }
   1.154 +    else
   1.155 +    {
   1.156 +        m_bSleepIsInit = true;
   1.157 +    }
   1.158 +
   1.159 +    syslog(LOG_DEBUG, "%s: benchmark started.\n", config->name.c_str());
   1.160 +    gettimeofday(&tv1, 0);
   1.161 +    for (x = 0; x < 10000; x++)
   1.162 +    {
   1.163 +        port->WriteData(x % 0x100);
   1.164 +    }
   1.165 +    gettimeofday(&tv2, 0);
   1.166 +    nSleepDeInit();
   1.167 +    m_nTimingAdjustCmd = ((tv2.tv_sec - tv1.tv_sec) * 10000 + (tv2.tv_usec - tv1.tv_usec)) / 1000;
   1.168 +    syslog(LOG_DEBUG, "%s: benchmark stopped. Time for Port Command: %ldns\n", config->name.c_str(), m_nTimingAdjustCmd);
   1.169 +
   1.170 +    GU256X64Cmd(SCREEN1ON);
   1.171 +    GU256X64Cmd(CURS_AUTOINC);
   1.172 +    GU256X64Cmd(SCREEN2CHAR);
   1.173 +
   1.174 +    GU256X64Cmd(DISP_LOSTA1); GU256X64Data(0x00);
   1.175 +    GU256X64Cmd(DISP_HISTA1); GU256X64Data(0x00);
   1.176 +    GU256X64Cmd(DISP_LOSTA2); GU256X64Data(0x00);
   1.177 +    GU256X64Cmd(DISP_HISTA2); GU256X64Data(0x10);
   1.178 +    GU256X64Cmd(CURS_LOADDR); GU256X64Data(0x00);
   1.179 +    GU256X64Cmd(CURS_HIADDR); GU256X64Data(0x00);
   1.180 +
   1.181 +    GU256X64Cmd(DISP_OR);
   1.182 +
   1.183 +    port->Release();
   1.184 +
   1.185 +    *oldConfig = *config;
   1.186 +
   1.187 +    // Set Display SetBrightness
   1.188 +    SetBrightness(config->brightness);
   1.189 +    // clear display
   1.190 +    Clear();
   1.191 +    ClearVFDMem();
   1.192 +
   1.193 +    syslog(LOG_INFO, "%s: gu256x64-372 initialized.\n", config->name.c_str());
   1.194 +    return 0;
   1.195 +}
   1.196 +
   1.197 +int cDriverGU256X64_372::DeInit()
   1.198 +{
   1.199 +    int x;
   1.200 +
   1.201 +    if (m_pVFDMem)
   1.202 +        for (x = 0; x < width; x++)
   1.203 +        {
   1.204 +            delete[] m_pVFDMem[x];
   1.205 +        }
   1.206 +    delete[] m_pVFDMem;
   1.207 +
   1.208 +    if (m_pDrawMem)
   1.209 +        for (x = 0; x < width; x++)
   1.210 +        {
   1.211 +            delete[] m_pDrawMem[x];
   1.212 +        }
   1.213 +    delete[] m_pDrawMem;
   1.214 +
   1.215 +    if (port->Close() != 0)
   1.216 +        return -1;
   1.217 +    return 0;
   1.218 +}
   1.219 +
   1.220 +int cDriverGU256X64_372::CheckSetup()
   1.221 +{
   1.222 +    if (config->device != oldConfig->device ||
   1.223 +        config->port != oldConfig->port ||
   1.224 +        config->width != oldConfig->width ||
   1.225 +        config->height != oldConfig->height)
   1.226 +    {
   1.227 +        DeInit();
   1.228 +        Init();
   1.229 +        return 0;
   1.230 +    }
   1.231 +
   1.232 +    if (config->brightness != oldConfig->brightness)
   1.233 +    {
   1.234 +        oldConfig->brightness = config->brightness;
   1.235 +        SetBrightness(config->brightness);
   1.236 +    }
   1.237 +    if (config->upsideDown != oldConfig->upsideDown ||
   1.238 +        config->invert != oldConfig->invert)
   1.239 +    {
   1.240 +        oldConfig->upsideDown = config->upsideDown;
   1.241 +        oldConfig->invert = config->invert;
   1.242 +        return 1;
   1.243 +    }
   1.244 +    return 0;
   1.245 +}
   1.246 +
   1.247 +void cDriverGU256X64_372::ClearVFDMem()
   1.248 +{
   1.249 +    for (int x = 0; x < width; x++)
   1.250 +        memset(m_pVFDMem[x], 0, m_iSizeYb);
   1.251 +}
   1.252 +
   1.253 +void cDriverGU256X64_372::Clear()
   1.254 +{
   1.255 +    for (int x = 0; x < width; x++)
   1.256 +        memset(m_pDrawMem[x], 0, m_iSizeYb);
   1.257 +}
   1.258 +
   1.259 +void cDriverGU256X64_372::SetBrightness(unsigned int percent)
   1.260 +{
   1.261 +    port->Claim();
   1.262 +
   1.263 +    if (percent > 88) {
   1.264 +        GU256X64Cmd(BRIGHT_1);
   1.265 +    } else if (percent > 75) {
   1.266 +        GU256X64Cmd(BRIGHT_2);
   1.267 +    } else if (percent > 66) {
   1.268 +        GU256X64Cmd(BRIGHT_3);
   1.269 +    } else if (percent > 0 ) {
   1.270 +        GU256X64Cmd(BRIGHT_4);
   1.271 +    } else {
   1.272 +        GU256X64Cmd(SCREENSOFF);
   1.273 +    }
   1.274 +    port->Release();
   1.275 +}
   1.276 +
   1.277 +void cDriverGU256X64_372::GU256X64Cmd(unsigned char data)
   1.278 +{
   1.279 +    if (m_bSleepIsInit)
   1.280 +        nSleepInit();
   1.281 +
   1.282 +    port->WriteControl(CDHI | WRHI);
   1.283 +    port->WriteData(data);
   1.284 +    nSleep(100 + (100 * config->adjustTiming) - m_nTimingAdjustCmd);
   1.285 +    port->WriteControl(CDHI | WRLO);
   1.286 +    nSleep(100 + (100 * config->adjustTiming) - m_nTimingAdjustCmd);
   1.287 +    port->WriteControl(CDHI | WRHI);
   1.288 +    nSleep(100 + (100 * config->adjustTiming) - m_nTimingAdjustCmd);
   1.289 +}
   1.290 +
   1.291 +void cDriverGU256X64_372::GU256X64Data(unsigned char data)
   1.292 +{
   1.293 +    if (m_bSleepIsInit)
   1.294 +        nSleepInit();
   1.295 +
   1.296 +    port->WriteControl(CDLO | WRHI);
   1.297 +    port->WriteData(data);
   1.298 +    nSleep(100 + (100 * config->adjustTiming) - m_nTimingAdjustCmd);
   1.299 +    port->WriteControl(CDLO | WRLO);
   1.300 +    nSleep(100 + (100 * config->adjustTiming) - m_nTimingAdjustCmd);
   1.301 +    port->WriteControl(CDLO | WRHI);
   1.302 +    nSleep(100 + (100 * config->adjustTiming) - m_nTimingAdjustCmd);
   1.303 +}
   1.304 +
   1.305 +void cDriverGU256X64_372::SetPixel(int x, int y)
   1.306 +{
   1.307 +    unsigned char c;
   1.308 +
   1.309 +    if (!m_pDrawMem)
   1.310 +        return;
   1.311 +
   1.312 +    if (x >= width || x < 0)
   1.313 +        return;
   1.314 +    if (y >= height || y < 0)
   1.315 +        return;
   1.316 +
   1.317 +    if (config->upsideDown)
   1.318 +    {
   1.319 +        x = width - 1 - x;
   1.320 +        y = height - 1 - y;
   1.321 +    }
   1.322 +
   1.323 +    c = 0x80 >> (y % 8);
   1.324 +
   1.325 +    m_pDrawMem[x][y/8] = m_pDrawMem[x][y/8] | c;
   1.326 +}
   1.327 +
   1.328 +void cDriverGU256X64_372::Set8Pixels(int x, int y, unsigned char data)
   1.329 +{
   1.330 +    int n;
   1.331 +
   1.332 +    // x - pos is'nt mayby align to 8
   1.333 +    x &= 0xFFF8;
   1.334 +
   1.335 +    for (n = 0; n < 8; ++n)
   1.336 +    {
   1.337 +        if (data & (0x80 >> n)) // if bit is set
   1.338 +            SetPixel(x + n, y);
   1.339 +    }
   1.340 +}
   1.341 +
   1.342 +void cDriverGU256X64_372::Refresh(bool refreshAll)
   1.343 +{
   1.344 +    int xb, yb;
   1.345 +
   1.346 +    if (!m_pVFDMem || !m_pDrawMem)
   1.347 +        return;
   1.348 +
   1.349 +    bool doRefresh = false;
   1.350 +    int minX = width;
   1.351 +    int maxX = 0;
   1.352 +    int minYb = m_iSizeYb;
   1.353 +    int maxYb = 0;
   1.354 +
   1.355 +    if (CheckSetup() > 0)
   1.356 +        refreshAll = true;
   1.357 +
   1.358 +    for (xb = 0; xb < width; ++xb)
   1.359 +    {
   1.360 +        for (yb = 0; yb < m_iSizeYb; ++yb)
   1.361 +        {
   1.362 +            if (m_pVFDMem[xb][yb] != m_pDrawMem[xb][yb])
   1.363 +            {
   1.364 +                m_pVFDMem[xb][yb] = m_pDrawMem[xb][yb];
   1.365 +                minX = std::min(minX, xb);
   1.366 +                maxX = std::max(maxX, xb);
   1.367 +                minYb = std::min(minYb, yb);
   1.368 +                maxYb = std::max(maxYb, yb + 1);
   1.369 +                doRefresh = true;
   1.370 +            }
   1.371 +        }
   1.372 +    }
   1.373 +
   1.374 +    m_nRefreshCounter = (m_nRefreshCounter + 1) % config->refreshDisplay;
   1.375 +    if (!refreshAll && !m_nRefreshCounter)
   1.376 +        refreshAll = true;
   1.377 +
   1.378 +    if (refreshAll || doRefresh)
   1.379 +    {
   1.380 +        if (refreshAll) {
   1.381 +            minX = 0;
   1.382 +            maxX = width;
   1.383 +            minYb = 0;
   1.384 +            maxYb = m_iSizeYb;
   1.385 +            // and reset RefreshCounter
   1.386 +            m_nRefreshCounter = 0;
   1.387 +        }
   1.388 +
   1.389 +        minX = std::max(minX, 0);
   1.390 +        maxX = std::min(maxX, width - 1);
   1.391 +        minYb = std::max(minYb, 0);
   1.392 +        maxYb = std::min(maxYb, m_iSizeYb);
   1.393 +
   1.394 +        port->Claim();
   1.395 +
   1.396 +        GU256X64Cmd(CURS_LOADDR); GU256X64Data(0x00);
   1.397 +        GU256X64Cmd(CURS_HIADDR); GU256X64Data(0x00);
   1.398 +        GU256X64Cmd(DATA_WRITE);
   1.399 +
   1.400 +        for (xb = 0; xb < width; xb++)
   1.401 +        {
   1.402 +            for (yb = 0; yb < m_iSizeYb; yb++)
   1.403 +            {
   1.404 +                GU256X64Data((m_pVFDMem[xb][yb]) ^ (config->invert ? 0xff : 0x00));
   1.405 +            }
   1.406 +        }
   1.407 +        port->Release();
   1.408 +    }
   1.409 +}
   1.410 +
   1.411 +} // end of namespace