summaryrefslogtreecommitdiff
path: root/glcddrivers/simlcd.c
blob: 6dc1dfee882efee82a19694164901b1d66c792b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * GraphLCD driver library
 *
 * simlcd.c  -  SimLCD driver class
 *              Output goes to a file instead of lcd.
 *              Use SimLCD tool to view this file.
 *
 * This file is released under the GNU General Public License. Refer
 * to the COPYING file distributed with this package.
 *
 * (c) 2001-2004 Carsten Siebholz <c.siebholz AT t-online.de>
 */

#include <stdio.h>
#include <syslog.h>
#include <cstring>

#include "common.h"
#include "config.h"
#include "simlcd.h"


namespace GLCD
{

cDriverSimLCD::cDriverSimLCD(cDriverConfig * config)
:   config(config)
{
    oldConfig = new cDriverConfig(*config);
}

cDriverSimLCD::~cDriverSimLCD()
{
    delete oldConfig;
}

int cDriverSimLCD::Init()
{
    width = config->width;
    if (width <= 0)
        width = 240;
    height = config->height;
    if (height <= 0)
        height = 128;

    for (unsigned int i = 0; i < config->options.size(); i++)
    {
        if (config->options[i].name == "")
        {
        }
    }

    // setup lcd array
    LCD = new unsigned char *[(width + 7) / 8];
    if (LCD)
    {
        for (int x = 0; x < (width + 7) / 8; x++)
        {
            LCD[x] = new unsigned char[height];
            memset(LCD[x], 0, height);
        }
    }

    *oldConfig = *config;

    // clear display
    Clear();

    syslog(LOG_INFO, "%s: SIMLCD initialized.\n", config->name.c_str());
    return 0;
}

int cDriverSimLCD::DeInit()
{
    // free lcd array
    if (LCD)
    {
        for (int x = 0; x < (width + 7) / 8; x++)
        {
            delete[] LCD[x];
        }
        delete[] LCD;
    }

    return 0;
}

int cDriverSimLCD::CheckSetup()
{
    if (config->width != oldConfig->width ||
        config->height != oldConfig->height)
    {
        DeInit();
        Init();
        return 0;
    }

    if (config->upsideDown != oldConfig->upsideDown ||
        config->invert != oldConfig->invert)
    {
        oldConfig->upsideDown = config->upsideDown;
        oldConfig->invert = config->invert;
        return 1;
    }
    return 0;
}

void cDriverSimLCD::Clear()
{
    for (int x = 0; x < (width + 7) / 8; x++)
        memset(LCD[x], 0, height);
}

void cDriverSimLCD::Set8Pixels(int x, int y, unsigned char data)
{
    if (x >= width || y >= height)
        return;

    if (!config->upsideDown)
    {
        // normal orientation
        LCD[x / 8][y] = LCD[x / 8][y] | data;
    }
    else
    {
        // upside down orientation
        x = width - 1 - x;
        y = height - 1 - y;
        LCD[x / 8][y] = LCD[x / 8][y] | ReverseBits(data);
    }
}

void cDriverSimLCD::Refresh(bool refreshAll)
{
    FILE * fp = NULL;
    int x;
    int y;
    int i;
    unsigned char c;

    if (CheckSetup() > 0)
        refreshAll = true;

    fp = fopen("/tmp/simlcd.sem", "r");
    if (!fp || refreshAll)
    {
        if (fp)
            fclose(fp);
        fp = fopen("/tmp/simlcd.dat", "w");
        if (fp)
        {
            for (y = 0; y < height; y++)
            {
                for (x = 0; x < (width + 7) / 8; x++)
                {
                    c = LCD[x][y] ^ (config->invert ? 0xff : 0x00);
                    for (i = 0; i < 8; i++)
                    {
                        if (c & 0x80)
                        {
                            fprintf(fp,"#");
                        }
                        else
                        {
                            fprintf(fp,".");
                        }
                        c = c << 1;
                    }
                }
                fprintf(fp,"\n");
            }
            fclose(fp);
        }

        fp = fopen("/tmp/simlcd.sem", "w");
        fclose(fp);
    }
    else
    {
        fclose(fp);
    }
}

} // end of namespace