-
Notifications
You must be signed in to change notification settings - Fork 18
/
framebuffer.c
196 lines (157 loc) · 5.09 KB
/
framebuffer.c
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
/*
A simple program that demonstrates how to program for a
touch screen. Specifically on the Raspberry Pi.
This prgram can be used for most Linux based systems.
For more details: www.marks-space.com
Copyright (C) 2013 Mark Williams
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
#include <stdlib.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include "font_8x8.c"
#define KNRM "\x1B[0m"
#define KRED "\x1B[31m"
#define KGRN "\x1B[32m"
#define KYEL "\x1B[33m"
#define KBLU "\x1B[34m"
#define KMAG "\x1B[35m"
#define KCYN "\x1B[36m"
#define KWHT "\x1B[37m"
static struct fb_fix_screeninfo fix;
static struct fb_var_screeninfo orig_var;
static struct fb_var_screeninfo var;
char *fbp = 0;
int fb=0;
long int screensize = 0;
// default framebuffer palette
typedef enum {
BLACK = 0, /* 0, 0, 0 */
BLUE = 1, /* 0, 0, 172 */
GREEN = 2, /* 0, 172, 0 */
CYAN = 3, /* 0, 172, 172 */
RED = 4, /* 172, 0, 0 */
PURPLE = 5, /* 172, 0, 172 */
ORANGE = 6, /* 172, 84, 0 */
LTGREY = 7, /* 172, 172, 172 */
GREY = 8, /* 84, 84, 84 */
LIGHT_BLUE = 9, /* 84, 84, 255 */
LIGHT_GREEN = 10, /* 84, 255, 84 */
LIGHT_CYAN = 11, /* 84, 255, 255 */
LIGHT_RED = 12, /* 255, 84, 84 */
LIGHT_PURPLE = 13, /* 255, 84, 255 */
YELLOW = 14, /* 255, 255, 84 */
WHITE = 15 /* 255, 255, 255 */
} COLOR_INDEX_T;
static unsigned short def_r[] =
{ 0, 0, 0, 0, 172, 172, 172, 168,
84, 84, 84, 84, 255, 255, 255, 255};
static unsigned short def_g[] =
{ 0, 0, 168, 168, 0, 0, 84, 168,
84, 84, 255, 255, 84, 84, 255, 255};
static unsigned short def_b[] =
{ 0, 172, 0, 168, 0, 172, 0, 168,
84, 255, 84, 255, 84, 255, 84, 255};
void put_pixel_16bpp(int x, int y, int r, int g, int b)
{
unsigned int pix_offset;
unsigned short c;
// calculate the pixel's byte offset inside the buffer
pix_offset = x*2 + y * fix.line_length;
//some magic to work out the color
c = ((r / 8) << 11) + ((g / 4) << 5) + (b / 8);
// write 'two bytes at once'
*((unsigned short*)(fbp + pix_offset)) = c;
}
void drawSquare(int x, int y,int height, int width, int c)
//void drawSquare(int x, int y)
{
// int height = 20;
// int width = 20;
int h = 0;
int w = 0;
for ( h = 0; h< height;h++)
for ( w = 0; w< width;w++)
put_pixel_16bpp( h+(x-2), w+(y-2) , def_r[c],def_g[c],def_b[c]);
}
int framebufferInitialize(int *xres, int *yres)
{
char *fbdevice = "/dev/fb1" ;
fb = open(fbdevice, O_RDWR);
if (fb == -1) {
perror("open fbdevice");
return -1;
}
if (ioctl(fb, FBIOGET_FSCREENINFO, &fix) < 0) {
perror("ioctl FBIOGET_FSCREENINFO");
close(fb);
return -1;
}
if (ioctl(fb, FBIOGET_VSCREENINFO, &var) < 0) {
perror("ioctl FBIOGET_VSCREENINFO");
close(fb);
return -1;
}
printf("Original %dx%d, %dbpp\n", var.xres, var.yres,
var.bits_per_pixel );
memcpy(&orig_var, &var, sizeof(struct fb_var_screeninfo));
printf("Framebuffer %s%s%s resolution;\n",KYEL, fbdevice, KWHT);
printf("%dx%d, %d bpp\n\n\n", var.xres, var.yres, var.bits_per_pixel );
// map framebuffer to user memory
screensize = fix.smem_len;
fbp = (char*)mmap(0,
screensize,
PROT_READ | PROT_WRITE,
MAP_SHARED,
fb, 0);
if ((int)fbp == -1) {
printf("Failed to mmap.\n");
}
*xres = var.xres;
*yres = var.yres;
//clear framebuffer
int x, y;
for (x = 0; x<var.xres;x++)
for (y = 0; y < var.yres;y++)
put_pixel_16bpp(x,y, 0, 0, 0);
}
void closeFramebuffer()
{
int x, y;
for (x = 0; x < var.xres; x++)
for (y = 0; y < var.yres;y++)
put_pixel_16bpp(x,y, 0, 0, 0);
munmap(fbp, screensize);
if (ioctl(fb, FBIOPUT_VSCREENINFO, &orig_var)) {
printf("Error re-setting variable information.\n");
}
close(fb);
}
void put_char(int x, int y, int c, int colidx)
{
int i,j,bits;
for (i = 0; i < font_vga_8x8.height; i++) {
bits = font_vga_8x8.data [font_vga_8x8.height * c + i];
for (j = 0; j < font_vga_8x8.width; j++, bits <<= 1)
if (bits & 0x80){
put_pixel_16bpp(x+j, y+i, 255,255,255);
}
}
}
void put_string(int x, int y, char *s, unsigned colidx)
{
int i;
for (i = 0; *s; i++, x += font_vga_8x8.width, s++)
put_char (x, y, *s, colidx);
}