summaryrefslogtreecommitdiff
path: root/glcddrivers/framebuffer.c
blob: 440af8965034559e9f3d22a1ecad4eeffced980e (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * GraphLCD driver library
 *
 * framebuffer.h  -  framebuffer device
 *                   Output goes to a framebuffer device
 *
 * This file is released under the GNU General Public License. Refer
 * to the COPYING file distributed with this package.
 *
 * (c) 2004      Stephan Skrodzki
 * (c) 2011      Wolfgang Astleitner <mrwastl AT users.sourceforge.net>
 */

#include <fcntl.h>
#include <stdio.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <cstdlib>
#include <cstring>

#include "common.h"
#include "config.h"
#include "framebuffer.h"


namespace GLCD
{

cDriverFramebuffer::cDriverFramebuffer(cDriverConfig * config)
:   config(config),
    offbuff(0),
    fbfd(-1)
{
    oldConfig = new cDriverConfig(*config);
}

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

int cDriverFramebuffer::Init()
{
    // default values
    width = config->width;
    if (width <= 0)
        width = 320;
    height = config->height;
    if (height <= 0)
        height = 240;
    zoom = 1;

    for (unsigned int i = 0; i < config->options.size(); i++)
    {
        if (config->options[i].name == "Zoom")
        {
            int z = atoi(config->options[i].value.c_str());
            if (z == 0 || z == 1)
                zoom = z;
            else
                syslog(LOG_ERR, "%s error: zoom %d not supported, using default (%d)!\n",
                       config->name.c_str(), z, zoom);
        }
    }

    // Open the file for reading and writing
    fbfd = open("/dev/fb0", O_RDWR);
    if (1 == fbfd)
    {
        syslog(LOG_ERR, "%s: cannot open framebuffer device.\n", config->name.c_str());
        return -1;
    }
    syslog(LOG_INFO, "%s: The framebuffer device was opened successfully.\n", config->name.c_str());

    // Get fixed screen information
    if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo))
    {
        syslog(LOG_ERR, "%s: Error reading fixed information.\n", config->name.c_str());
        return -1;
    }

    // Get variable screen information
    if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo))
    {
        syslog(LOG_ERR, "%s: Error reading variable information.\n", config->name.c_str());
        return -1;
    }

    // Figure out the size of the screen in bytes
    screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;

    syslog(LOG_INFO, "%s: V01: xres: %d, yres %d, vyres: %d, bpp: %d, linelenght: %d\n", config->name.c_str(),vinfo.xres,vinfo.yres,vinfo.yres_virtual,vinfo.bits_per_pixel,finfo.line_length);

    // reserve another memory to draw into
    offbuff = new char[screensize];
    if (!offbuff)
    {
        syslog(LOG_ERR, "%s: failed to alloc memory for framebuffer device.\n", config->name.c_str());
        return -1;
    }

    // Map the device to memory
    fbp = mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
    if (fbp == MAP_FAILED)
    {
        syslog(LOG_ERR, "%s: failed to map framebuffer device to memory.\n", config->name.c_str());
        return -1;
    }
    syslog(LOG_INFO, "%s: The framebuffer device was mapped to memory successfully.\n", config->name.c_str());

    *oldConfig = *config;

    // clear display
    Refresh(true);

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

int cDriverFramebuffer::DeInit()
{
    if (offbuff)
        delete[] offbuff;
    munmap(fbp, screensize);
    if (-1 != fbfd)
        close(fbfd);
    return 0;
}

int cDriverFramebuffer::CheckSetup()
{
    if (config->device != oldConfig->device ||
        config->port != oldConfig->port ||
        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 cDriverFramebuffer::SetPixel(int x, int y, uint32_t data)
{
    int location;
    int outcol;

    if (x >= width || y >= height)
        return;

    if (config->upsideDown)
    {
        x = width - 1 - x;
        y = height - 1 - y;
    }


    // Figure out where in memory to put the pixel
    location = (x*(1+zoom)+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
               (y*(1+zoom)+vinfo.yoffset) * finfo.line_length;

    if (data == GLCD::cColor::White) {
        if (vinfo.bits_per_pixel <= 8) {
            outcol = 15;
        } else {
            outcol = 255;
        }
    } else {
        outcol = 0;
    }

    if (vinfo.bits_per_pixel <= 8)
    {
        *(offbuff + location) = outcol;
        if (zoom == 1)
        {
            *(offbuff + location + 1) = outcol;
            *(offbuff + location + finfo.line_length) = outcol;
            *(offbuff + location + finfo.line_length + 1) = outcol;
        }
    }
    else if (vinfo.bits_per_pixel <= 16)
    {
        *(offbuff + location) = outcol;
        *(offbuff + location + 1) = outcol;
        if (zoom == 1)
        {
            *(offbuff + location + 2) = outcol;
            *(offbuff + location + 3) = outcol;
            *(offbuff + location + finfo.line_length) = outcol;
            *(offbuff + location + finfo.line_length + 1) = outcol;
            *(offbuff + location + finfo.line_length + 2) = outcol;
            *(offbuff + location + finfo.line_length + 3) = outcol;
        }
    }
    else
    {
        *(offbuff + location) = outcol;
        *(offbuff + location + 1) = outcol;
        *(offbuff + location + 2) = outcol;
        *(offbuff + location + 3) = 0;     /* should be transparency */
        if (zoom == 1)
        {
            *(offbuff + location + 4) = outcol;
            *(offbuff + location + 5) = outcol;
            *(offbuff + location + 6) = outcol;
            *(offbuff + location + 7) = 0;
            *(offbuff + location + finfo.line_length) = outcol;
            *(offbuff + location + finfo.line_length + 1) = outcol;
            *(offbuff + location + finfo.line_length + 2) = outcol;
            *(offbuff + location + finfo.line_length + 3) = 0;
            *(offbuff + location + finfo.line_length + 4) = outcol;
            *(offbuff + location + finfo.line_length + 5) = outcol;
            *(offbuff + location + finfo.line_length + 6) = outcol;
            *(offbuff + location + finfo.line_length + 7) = 0;
        }
    }
}

void cDriverFramebuffer::Clear()
{
    memset(offbuff, 0, screensize);
}

#if 0
void cDriverFramebuffer::Set8Pixels(int x, int y, unsigned char data)
{
    int n;

    x &= 0xFFF8;

    for (n = 0; n < 8; ++n)
    {
        if (data & (0x80 >> n))      // if bit is set
            SetPixel(x + n, y, GLCD::cColor::White);
    }
}
#endif

void cDriverFramebuffer::Refresh(bool refreshAll)
{
    memcpy(fbp, offbuff, screensize);
}

} // end of namespace