summaryrefslogtreecommitdiff
path: root/glcddrivers/image.c
blob: a3d123f99b3eee47ebb6ff216b9260551651814f (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * GraphLCD driver library
 *
 * image.c  -  Image output device
 *             Output goes to a image file instead of LCD.
 *
 * This file is released under the GNU General Public License. Refer
 * to the COPYING file distributed with this package.
 *
 * (c) 2004-2010 Andreas Regel <andreas.regel AT powarman.de>
 * (c) 2010-2011 Wolfgang Astleitner <mrwastl AT users sourceforge net>
                 Andreas 'randy' Weinberger 
 */

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

#include "common.h"
#include "config.h"
#include "image.h"


namespace GLCD
{

cDriverImage::cDriverImage(cDriverConfig * config)
:   cDriver(config)
{
}

cDriverImage::~cDriverImage()
{
    DeInit();
}

int cDriverImage::Init()
{
    width = config->width;
    if (width <= 0)
        width = 240;
    height = config->height;
    if (height <= 0)
        height = 128;
    lineSize = (width + 7) / 8;

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

//    newLCD = new unsigned char[lineSize * height];
    newLCD = new uint32_t[width * height];
    if (newLCD)
        memset(newLCD, 0, width * height);
    oldLCD = new uint32_t[width * height];
    if (oldLCD)
        memset(oldLCD, 0, width * height);

    counter = 0;

    *oldConfig = *config;

    // clear display
    Clear();

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

int cDriverImage::DeInit()
{
    if (newLCD)
    {
        delete[] newLCD;
        newLCD = 0;
    }
    if (oldLCD)
    {
        delete[] oldLCD;
        oldLCD = 0;
    }
    return 0;
}

int cDriverImage::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 cDriverImage::Clear()
{
    memset(newLCD, 0, lineSize * height);
}

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

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

void cDriverImage::SetPixel(int x, int y, uint32_t data)
{
    if (x >= width || y >= height)
        return;

    int cols = (width + 7 ) >> 3;
    int pos = x % 8;
    if (config->upsideDown)
    {
        x = width - 1 - x;
        y = height - 1 - y;
    } else {
        pos = 7 - pos; // reverse bit position
    }

    if (data == GRAPHLCD_White)
        newLCD[y * cols + (x >> 3)] |= ( 1 << pos );
    else
        newLCD[y * cols + (x >> 3)] &= ( 0xFF ^ ( 1 << pos) );
}

void cDriverImage::Refresh(bool refreshAll)
{
    int i;
    bool refresh;
    char fileName[256];
    char str[32];
    FILE * fp;
    unsigned char c;

    refresh = false;
    if (CheckSetup() > 0)
        refresh = true;

    for (i = 0; i < lineSize * height; i++)
    {
        if (newLCD[i] != oldLCD[i])
        {
            refresh = true;
            break;
        }
    }

    if (refresh)
    {
        sprintf(fileName, "%s/%s%05d.%s", "/tmp", "lcd", counter, "pbm");
        fp = fopen(fileName, "wb");
        if (fp)
        {
            sprintf(str, "P4\n%d %d\n", width, height);
            fwrite(str, strlen(str), 1, fp);
            for (i = 0; i < lineSize * height; i++)
            {
                c = newLCD[i] ^ (config->invert ? 0xff : 0x00);
                fwrite(&c, 1, 1, fp);
                oldLCD[i] = newLCD[i];
            }
            fclose(fp);
        }
        counter++;
        if (counter > 99999)
            counter = 0;
    }
}

} // end of namespace