-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfbws.c
208 lines (174 loc) · 6.36 KB
/
fbws.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
197
198
199
200
201
202
203
204
205
206
207
208
//WS281X test using Linux framebuffer:
//build: gcc fbws.c -o fbws
//run: [sudo] fbws
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <inttypes.h>
// 'global' variables to store screen info
char* fbp = 0;
struct fb_var_screeninfo vinfo;
struct fb_fix_screeninfo finfo;
void put_pixel_RGB32(int x, int y, int r, int g, int b)
{
// calculate the pixel's byte offset inside the buffer
// note: x * 3 as every pixel is 3 consecutive bytes
unsigned int pix_offset = x * 4 + y * finfo.line_length;
// now this is about the same as 'fbp[pix_offset] = value'
*((char*)(fbp + pix_offset)) = b;
*((char*)(fbp + pix_offset + 1)) = g;
*((char*)(fbp + pix_offset + 2)) = r;
*((char*)(fbp + pix_offset + 3)) = 0xff;
}
void put_pixel_RGB24(int x, int y, int r, int g, int b)
{
// calculate the pixel's byte offset inside the buffer
// note: x * 3 as every pixel is 3 consecutive bytes
unsigned int pix_offset = x * 3 + y * finfo.line_length;
// now this is about the same as 'fbp[pix_offset] = value'
*((char*)(fbp + pix_offset)) = b;
*((char*)(fbp + pix_offset + 1)) = g;
*((char*)(fbp + pix_offset + 2)) = r;
}
void put_pixel_RGB565(int x, int y, int r, int g, int b)
{
// calculate the pixel's byte offset inside the buffer
// note: x * 2 as every pixel is 2 consecutive bytes
unsigned int pix_offset = x * 2 + y * finfo.line_length;
// now this is about the same as 'fbp[pix_offset] = value'
// but a bit more complicated for RGB565
//unsigned short c = ((r / 8) << 11) + ((g / 4) << 5) + (b / 8);
unsigned short c = ((r / 8) * 2048) + ((g / 4) * 32) + (b / 8);
// write 'two bytes at once'
*((unsigned short*)(fbp + pix_offset)) = c;
}
void put_pixel(int x, int y, int r, int g, int b)
{
switch ( vinfo.bits_per_pixel)
{
case 16: put_pixel_RGB565(x, y, r, g, b); return;
case 24: put_pixel_RGB24(x, y, r, g, b); return;
case 32: put_pixel_RGB32(x, y, r, g, b); return;
}
}
#define _0H 16
#define _0L 48
#define _1H 36
#define _1L 28
#define _H(b) ((b)? _1H: _0H)
#define _L(b) ((b)? _1L: _0L)
#define BITW(b) (((b) < 23)? 64: 48) //last bit is partially hidden
#define nel(ary) (sizeof(ary) / sizeof((ary)[0]))
#define RGSWAP(rgb24) ((((rgb24) >> 8) & 0xff00) | (((rgb24) << 8) & 0xff0000) | ((rgb24) & 0xff))
//use low brightness to reduce eye burn during testing:
#define RED 0x1f0000
#define GREEN 0x001f00
#define BLUE 0x00001f
#define YELLOW 0x1f1f00
#define CYAN 0x001f1f
#define MAGENTA 0x1f001f
#define WHITE 0x1f1f1f
void draw()
{
uint32_t colors[] = {RGSWAP(RED), RGSWAP(GREEN), BLUE, YELLOW, RGSWAP(CYAN), RGSWAP(MAGENTA), WHITE};
//for (int i = 0; i < nel(colors); ++i) printf("color[%d/%d]: 0x%x\n", i, nel(colors), colors[i]);
long int scrsize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
memset(fbp, 0, scrsize);
//set first 10 nodes (24-1 bits):
// uint32_t color = 0xff00ff; //R <-> G; //0x00ffff; //cyan (RGB)
for (int loop = 0; loop <= 10; ++loop)
{
for (int y = 0; y < 37; ++y)
for (int b = 0; b < 24; ++b) //NOTE: last bit is partially hidden by hsync
{
uint32_t color = colors[(y + loop) % nel(colors)];
if (loop == 10) color = 0;
//if (!b) printf("node[%d]: 0x%x\n", y, color);
uint32_t bv = color & (0x800000 >> b);
for (int i = 0; i < BITW(b); ++i)
{
int onoff = (i < _H(bv))? 0xff: 0;
put_pixel(BITW(0) * b + i, y, onoff, onoff, onoff);
}
}
sleep(1);
}
}
// helper function for drawing - no more need to go mess with
// the main function when just want to change what to draw...
#define sqrt(x) 0
void old_draw() {
int x, y;
int r, g, b;
int dr;
int cr = vinfo.yres / 3;
int cg = vinfo.yres / 3 + vinfo.yres / 4;
int cb = vinfo.yres / 3 + vinfo.yres / 4 + vinfo.yres / 4;
for (y = 0; y < (vinfo.yres); y++) {
for (x = 0; x < vinfo.xres; x++) {
dr = (int)sqrt((cr - x)*(cr - x)+(cr - y)*(cr - y));
r = 255 - 256 * dr / cr;
r = (r >= 0) ? r : 0;
dr = (int)sqrt((cg - x)*(cg - x)+(cr - y)*(cr - y));
g = 255 - 256 * dr / cr;
g = (g >= 0) ? g : 0;
dr = (int)sqrt((cb - x)*(cb - x)+(cr - y)*(cr - y));
b = 255 - 256 * dr / cr;
b = (b >= 0) ? b : 0;
put_pixel(x, y, r, g, b);
}
}
}
// application entry point
int main(int argc, char* argv[])
{
struct fb_var_screeninfo orig_vinfo;
long int screensize = 0;
int fbfd = 0;
system("cat /dev/fb0 > before.dat");
// Open the file for reading and writing
fbfd = open("/dev/fb0", O_RDWR);
if (!fbfd || ((int)fbfd == -1))
{
printf("Error: cannot open framebuffer device.\n");
return(1);
}
printf("The framebuffer device was opened successfully.\n");
// Get fixed screen information
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo))
printf("Error reading fixed information.\n");
// Get variable screen information
if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo))
printf("Error reading variable information.\n");
if (!vinfo.pixclock) vinfo.pixclock = -1;
printf("Original %dx%d, %d bpp, linelen %d, pxclk %d, lrul marg %d %d %d %d, sync len h %d v %d, fps %f\n",
vinfo.xres, vinfo.yres, vinfo.bits_per_pixel, finfo.line_length, vinfo.pixclock,
vinfo.left_margin, vinfo.right_margin, vinfo.upper_margin, vinfo.lower_margin, vinfo.hsync_len, vinfo.vsync_len,
(double)(vinfo.xres + vinfo.left_margin + vinfo.hsync_len + vinfo.right_margin) * (vinfo.yres + vinfo.upper_margin + vinfo.vsync_len + vinfo.lower_margin ) / vinfo.pixclock);
// Store for reset (copy vinfo to vinfo_orig)
memcpy(&orig_vinfo, &vinfo, sizeof(vinfo)); //struct fb_var_screeninfo));
// map fb to user mem
screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
fbp = (char*)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, fbfd, 0);
if (!fbfd || (fbp == (char*)-1))
printf("Failed to mmap.\n");
else {
// draw...
draw();
sleep(5);
}
// cleanup
munmap(fbp, screensize);
if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &orig_vinfo))
printf("Error re-setting variable information.\n");
close(fbfd);
system("cat /dev/fb0 > after.dat");
printf("done\n");
return 0;
}