graphlcd-base/glcddrivers/simlcd.c
author root@rika
Wed, 06 Feb 2008 17:32:55 +0000
changeset 4 df6a40031aa5
permissions -rw-r--r--
added graphlcd-base
root@4
     1
/*
root@4
     2
 * GraphLCD driver library
root@4
     3
 *
root@4
     4
 * simlcd.c  -  SimLCD driver class
root@4
     5
 *              Output goes to a file instead of lcd.
root@4
     6
 *              Use SimLCD tool to view this file.
root@4
     7
 *
root@4
     8
 * This file is released under the GNU General Public License. Refer
root@4
     9
 * to the COPYING file distributed with this package.
root@4
    10
 *
root@4
    11
 * (c) 2001-2004 Carsten Siebholz <c.siebholz AT t-online.de>
root@4
    12
 */
root@4
    13
root@4
    14
#include <stdio.h>
root@4
    15
#include <syslog.h>
root@4
    16
root@4
    17
#include "common.h"
root@4
    18
#include "config.h"
root@4
    19
#include "simlcd.h"
root@4
    20
root@4
    21
root@4
    22
namespace GLCD
root@4
    23
{
root@4
    24
root@4
    25
cDriverSimLCD::cDriverSimLCD(cDriverConfig * config)
root@4
    26
:   config(config)
root@4
    27
{
root@4
    28
    oldConfig = new cDriverConfig(*config);
root@4
    29
}
root@4
    30
root@4
    31
cDriverSimLCD::~cDriverSimLCD()
root@4
    32
{
root@4
    33
    delete oldConfig;
root@4
    34
}
root@4
    35
root@4
    36
int cDriverSimLCD::Init()
root@4
    37
{
root@4
    38
    width = config->width;
root@4
    39
    if (width <= 0)
root@4
    40
        width = 240;
root@4
    41
    height = config->height;
root@4
    42
    if (height <= 0)
root@4
    43
        height = 128;
root@4
    44
root@4
    45
    for (unsigned int i = 0; i < config->options.size(); i++)
root@4
    46
    {
root@4
    47
        if (config->options[i].name == "")
root@4
    48
        {
root@4
    49
        }
root@4
    50
    }
root@4
    51
root@4
    52
    // setup lcd array
root@4
    53
    LCD = new unsigned char *[(width + 7) / 8];
root@4
    54
    if (LCD)
root@4
    55
    {
root@4
    56
        for (int x = 0; x < (width + 7) / 8; x++)
root@4
    57
        {
root@4
    58
            LCD[x] = new unsigned char[height];
root@4
    59
            memset(LCD[x], 0, height);
root@4
    60
        }
root@4
    61
    }
root@4
    62
root@4
    63
    *oldConfig = *config;
root@4
    64
root@4
    65
    // clear display
root@4
    66
    Clear();
root@4
    67
root@4
    68
    syslog(LOG_INFO, "%s: SIMLCD initialized.\n", config->name.c_str());
root@4
    69
    return 0;
root@4
    70
}
root@4
    71
root@4
    72
int cDriverSimLCD::DeInit()
root@4
    73
{
root@4
    74
    // free lcd array
root@4
    75
    if (LCD)
root@4
    76
    {
root@4
    77
        for (int x = 0; x < (width + 7) / 8; x++)
root@4
    78
        {
root@4
    79
            delete[] LCD[x];
root@4
    80
        }
root@4
    81
        delete[] LCD;
root@4
    82
    }
root@4
    83
root@4
    84
    return 0;
root@4
    85
}
root@4
    86
root@4
    87
int cDriverSimLCD::CheckSetup()
root@4
    88
{
root@4
    89
    if (config->width != oldConfig->width ||
root@4
    90
        config->height != oldConfig->height)
root@4
    91
    {
root@4
    92
        DeInit();
root@4
    93
        Init();
root@4
    94
        return 0;
root@4
    95
    }
root@4
    96
root@4
    97
    if (config->upsideDown != oldConfig->upsideDown ||
root@4
    98
        config->invert != oldConfig->invert)
root@4
    99
    {
root@4
   100
        oldConfig->upsideDown = config->upsideDown;
root@4
   101
        oldConfig->invert = config->invert;
root@4
   102
        return 1;
root@4
   103
    }
root@4
   104
    return 0;
root@4
   105
}
root@4
   106
root@4
   107
void cDriverSimLCD::Clear()
root@4
   108
{
root@4
   109
    for (int x = 0; x < (width + 7) / 8; x++)
root@4
   110
        memset(LCD[x], 0, height);
root@4
   111
}
root@4
   112
root@4
   113
void cDriverSimLCD::Set8Pixels(int x, int y, unsigned char data)
root@4
   114
{
root@4
   115
    if (x >= width || y >= height)
root@4
   116
        return;
root@4
   117
root@4
   118
    if (!config->upsideDown)
root@4
   119
    {
root@4
   120
        // normal orientation
root@4
   121
        LCD[x / 8][y] = LCD[x / 8][y] | data;
root@4
   122
    }
root@4
   123
    else
root@4
   124
    {
root@4
   125
        // upside down orientation
root@4
   126
        x = width - 1 - x;
root@4
   127
        y = height - 1 - y;
root@4
   128
        LCD[x / 8][y] = LCD[x / 8][y] | ReverseBits(data);
root@4
   129
    }
root@4
   130
}
root@4
   131
root@4
   132
void cDriverSimLCD::Refresh(bool refreshAll)
root@4
   133
{
root@4
   134
    FILE * fp = NULL;
root@4
   135
    int x;
root@4
   136
    int y;
root@4
   137
    int i;
root@4
   138
    unsigned char c;
root@4
   139
root@4
   140
    if (CheckSetup() > 0)
root@4
   141
        refreshAll = true;
root@4
   142
root@4
   143
    fp = fopen("/tmp/simlcd.sem", "r");
root@4
   144
    if (!fp || refreshAll)
root@4
   145
    {
root@4
   146
        if (fp)
root@4
   147
            fclose(fp);
root@4
   148
        fp = fopen("/tmp/simlcd.dat", "w");
root@4
   149
        if (fp)
root@4
   150
        {
root@4
   151
            for (y = 0; y < height; y++)
root@4
   152
            {
root@4
   153
                for (x = 0; x < (width + 7) / 8; x++)
root@4
   154
                {
root@4
   155
                    c = LCD[x][y] ^ (config->invert ? 0xff : 0x00);
root@4
   156
                    for (i = 0; i < 8; i++)
root@4
   157
                    {
root@4
   158
                        if (c & 0x80)
root@4
   159
                        {
root@4
   160
                            fprintf(fp,"#");
root@4
   161
                        }
root@4
   162
                        else
root@4
   163
                        {
root@4
   164
                            fprintf(fp,".");
root@4
   165
                        }
root@4
   166
                        c = c << 1;
root@4
   167
                    }
root@4
   168
                }
root@4
   169
                fprintf(fp,"\n");
root@4
   170
            }
root@4
   171
            fclose(fp);
root@4
   172
        }
root@4
   173
root@4
   174
        fp = fopen("/tmp/simlcd.sem", "w");
root@4
   175
        fclose(fp);
root@4
   176
    }
root@4
   177
    else
root@4
   178
    {
root@4
   179
        fclose(fp);
root@4
   180
    }
root@4
   181
}
root@4
   182
root@4
   183
} // end of namespace