Some fixes, cleanups, and less magic numbers
This commit is contained in:
parent
c9c13b4271
commit
922b090b8e
2 changed files with 47 additions and 51 deletions
|
|
@ -14,14 +14,16 @@
|
|||
#define UDP_PORT 5005 // the port users will be connecting to
|
||||
#define DISCOVER_PORT 5006 // the port on which the server will broadcast its details
|
||||
|
||||
static void die(char *msg)
|
||||
{
|
||||
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
|
||||
|
||||
#define MAX_PIXELS 140
|
||||
|
||||
static void die(char *msg) {
|
||||
fprintf(stderr, "%s\n", msg);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
static int get_udp_socket(int port)
|
||||
{
|
||||
static int get_udp_socket(int port) {
|
||||
int sock_fd;
|
||||
struct sockaddr_in6 addr;
|
||||
|
||||
|
|
@ -41,8 +43,7 @@ static int get_udp_socket(int port)
|
|||
}
|
||||
|
||||
|
||||
static ssize_t get_udp_packet(int sock_fd, char *packet, size_t size)
|
||||
{
|
||||
static ssize_t get_udp_packet(int sock_fd, char *packet, size_t size) {
|
||||
ssize_t n_read;
|
||||
while ((n_read = recv(sock_fd, packet, size, 0)) < 0 );
|
||||
return n_read;
|
||||
|
|
@ -51,30 +52,27 @@ static ssize_t get_udp_packet(int sock_fd, char *packet, size_t size)
|
|||
// PixelVloed C version
|
||||
|
||||
// application entry point
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
uint8_t packet[1120 + 32];
|
||||
int main(int argc, char* argv[]) {
|
||||
uint8_t packet[ (8 * MAX_PIXELS) + 32]; // 8 bytes per pixel is the maximum size
|
||||
ssize_t packet_size;
|
||||
uint16_t x, y;
|
||||
uint8_t r, g, b, a;
|
||||
uint8_t pixellength = 0, protocol, version;
|
||||
uint16_t i;
|
||||
uint8_t offset = 2;
|
||||
|
||||
int sock_fd = get_udp_socket(UDP_PORT);
|
||||
|
||||
|
||||
// Init screen buffer
|
||||
if (!init_frame_buffer()) {
|
||||
die("Failed to init screen buffer.\n");
|
||||
}
|
||||
|
||||
// draw...
|
||||
// draw each udp packet
|
||||
while( (packet_size = get_udp_packet(sock_fd, (char*)&packet, sizeof(packet))) >= 0 ) {
|
||||
|
||||
|
||||
|
||||
// Get protocol from the packet
|
||||
version = packet[1]; //
|
||||
protocol = packet[0]; // Alpha in old
|
||||
version = packet[1]; // the version
|
||||
protocol = packet[0]; // protocol switch
|
||||
|
||||
//printf("V:%d, P:%d\n", version, protocol);
|
||||
|
||||
|
|
@ -82,7 +80,8 @@ int main(int argc, char* argv[])
|
|||
// Protocol 0: xyrgb 16:16:8:8:8 specified for each pixel
|
||||
case 0:
|
||||
pixellength = 7;
|
||||
for (i=2; i<packet_size; i+=pixellength) {
|
||||
packet_size = MIN(offset + (pixellength * MAX_PIXELS), packet_size);
|
||||
for (i=offset; i<packet_size; i+=pixellength) {
|
||||
x = packet[i ] | packet[i+1]<<8;
|
||||
y = packet[i+2] | packet[i+3]<<8;
|
||||
r = packet[i+4];
|
||||
|
|
@ -92,10 +91,12 @@ int main(int argc, char* argv[])
|
|||
write_pixel_to_screen(x, y, r, g, b, a);
|
||||
}
|
||||
break;
|
||||
|
||||
// Protocol 1: xyrgba 16:16:8:8:8:8 specified for each pixel
|
||||
case 1:
|
||||
pixellength = 8;
|
||||
for (i=2; i<packet_size; i+=pixellength) {
|
||||
packet_size = MIN(offset + (pixellength * MAX_PIXELS), packet_size);
|
||||
for (i=offset; i<packet_size; i+=pixellength) {
|
||||
x = packet[i ] | packet[i+1]<<8;
|
||||
y = packet[i+2] | packet[i+3]<<8;
|
||||
r = packet[i+4];
|
||||
|
|
@ -105,10 +106,12 @@ int main(int argc, char* argv[])
|
|||
write_pixel_to_screen(x, y, r, g, b, a);
|
||||
}
|
||||
break;
|
||||
|
||||
// Protocol 2: xyrgb 12:12:8:8:8 specified for each pixel
|
||||
case 2:
|
||||
pixellength = 6;
|
||||
for (i=2; i<packet_size; i+=pixellength) {
|
||||
packet_size = MIN(offset + (pixellength * MAX_PIXELS), packet_size);
|
||||
for (i=offset; i<packet_size; i+=pixellength) {
|
||||
x = packet[i ] | (packet[i+1]&0xF0)<<4;
|
||||
y = (packet[i+1]&0x0F) | packet[i+2]<<4;
|
||||
r = packet[i+3];
|
||||
|
|
@ -118,10 +121,12 @@ int main(int argc, char* argv[])
|
|||
write_pixel_to_screen(x, y, r, g, b, a);
|
||||
}
|
||||
break;
|
||||
|
||||
// Protocol 3: xyrgba 12:12:8:8:8:8 specified for each pixel
|
||||
case 3:
|
||||
pixellength = 7;
|
||||
for (i=2; i<packet_size; i+=pixellength) {
|
||||
packet_size = MIN(offset + (pixellength * MAX_PIXELS), packet_size);
|
||||
for (i=offset; i<packet_size; i+=pixellength) {
|
||||
x = packet[i ] | (packet[i+1]&0xF0)<<4;
|
||||
y = (packet[i+1]&0x0F) | packet[i+2]<<4;
|
||||
r = packet[i+3];
|
||||
|
|
@ -131,10 +136,12 @@ int main(int argc, char* argv[])
|
|||
write_pixel_to_screen(x, y, r, g, b, a);
|
||||
}
|
||||
break;
|
||||
|
||||
// Protocol 4: xyrgb 12:12:3:3:2 specified for each pixel
|
||||
case 4:
|
||||
pixellength = 4;
|
||||
for (i=2; i<packet_size; i+=pixellength) {
|
||||
packet_size = MIN(offset + (pixellength * MAX_PIXELS), packet_size);
|
||||
for (i=offset; i<packet_size; i+=pixellength) {
|
||||
x = packet[i ] | (packet[i+1]&0xF0)<<4;
|
||||
y = (packet[i+1]&0x0F) | packet[i+2]<<4;
|
||||
r = packet[i+3] & 0xE0;
|
||||
|
|
@ -144,10 +151,12 @@ int main(int argc, char* argv[])
|
|||
write_pixel_to_screen(x, y, r, g, b, a);
|
||||
}
|
||||
break;
|
||||
|
||||
// Protocol 5: xyrgba 12:12:2:2:2:2 specified for each pixel
|
||||
case 5:
|
||||
pixellength = 4;
|
||||
for (i=2; i<packet_size; i+=pixellength) {
|
||||
packet_size = MIN(offset + (pixellength * MAX_PIXELS), packet_size);
|
||||
for (i=offset; i<packet_size; i+=pixellength) {
|
||||
x = packet[i ] | (packet[i+1]&0xF0)<<4;
|
||||
y = (packet[i+1]&0x0F) | packet[i+2]<<4;
|
||||
r = packet[i+3] & 0xC0;
|
||||
|
|
@ -157,16 +166,14 @@ int main(int argc, char* argv[])
|
|||
write_pixel_to_screen(x, y, r, g, b, a);
|
||||
}
|
||||
break;
|
||||
// Error no protocol defined
|
||||
|
||||
// Error no recognied protocol defined
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// De init screen buffer
|
||||
|
||||
// Deinitialize screen buffer
|
||||
deinit_frame_buffer();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,9 +22,8 @@ struct fb_fix_screeninfo finfo;
|
|||
long int screensize = 0;
|
||||
char *fbp = 0;
|
||||
|
||||
|
||||
int8_t init_frame_buffer(void) {
|
||||
// Open the file for reading and writing
|
||||
// Open the framebuffer file for reading and writing
|
||||
fbfd = open("/dev/fb0", O_RDWR);
|
||||
if (!fbfd) {
|
||||
printf("Error: cannot open framebuffer device.\n");
|
||||
|
|
@ -37,7 +36,7 @@ int8_t init_frame_buffer(void) {
|
|||
printf("Error reading variable information.\n");
|
||||
return 0;
|
||||
}
|
||||
printf("Screen info %dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel );
|
||||
printf("Screen info %dx%d, %dbpp\n", vinfo.xres, vinfo.yres, vinfo.bits_per_pixel);
|
||||
|
||||
// Get fixed screen information
|
||||
if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
|
||||
|
|
@ -74,14 +73,13 @@ int8_t init_frame_buffer(void) {
|
|||
}
|
||||
|
||||
|
||||
void deinit_frame_buffer(void)
|
||||
{
|
||||
void deinit_frame_buffer(void) {
|
||||
// cleanup
|
||||
munmap((void *)fbp, screensize);
|
||||
close(fbfd);
|
||||
}
|
||||
|
||||
void write_pixel_to_screen(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a){
|
||||
void write_pixel_to_screen(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
||||
uint32_t pix_offset;
|
||||
uint16_t temp_r, temp_g, temp_b;
|
||||
|
||||
|
|
@ -94,16 +92,13 @@ void write_pixel_to_screen(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t
|
|||
|
||||
// If alpha is used calculate it, otherwise just paint the colors
|
||||
if (a != 0xFF) {
|
||||
|
||||
// Get the old pixel
|
||||
if (vinfo.bits_per_pixel == 24) {
|
||||
// 24bpp format
|
||||
if (vinfo.bits_per_pixel == 24) { // 24bpp format
|
||||
pix_offset = 3 * x + y * finfo.line_length;
|
||||
temp_r = fbp[pix_offset+0];
|
||||
temp_g = fbp[pix_offset+1];
|
||||
temp_b = fbp[pix_offset+2];
|
||||
} else if (vinfo.bits_per_pixel == 32) {
|
||||
// 32bpp format
|
||||
} else if (vinfo.bits_per_pixel == 32) { // 32bpp format
|
||||
pix_offset = 4 * x + y * finfo.line_length;
|
||||
temp_r = fbp[pix_offset+0];
|
||||
temp_g = fbp[pix_offset+1];
|
||||
|
|
@ -114,7 +109,7 @@ void write_pixel_to_screen(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t
|
|||
temp_b = 0;
|
||||
}
|
||||
//printf("%08x : %04x %04x %04x\n", pixel.pixel32, temp_r, temp_g, temp_b);
|
||||
// Aply alpha
|
||||
// Apply alpha
|
||||
temp_r = (temp_r * (256-a))>>8;
|
||||
temp_r = temp_r + ((r * (a))>>8);
|
||||
temp_g = (temp_g * (256-a))>>8;
|
||||
|
|
@ -128,18 +123,12 @@ void write_pixel_to_screen(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t
|
|||
}
|
||||
|
||||
// Write new pixels to buffer
|
||||
if (vinfo.bits_per_pixel == 24) {
|
||||
// 24bpp format
|
||||
if (vinfo.bits_per_pixel == 24) { // 24bpp format
|
||||
pix_offset = 3 * x + y * finfo.line_length;
|
||||
fbp[pix_offset+2] = temp_r;
|
||||
fbp[pix_offset+1] = temp_g;
|
||||
fbp[pix_offset+0] = temp_b;
|
||||
}
|
||||
if (vinfo.bits_per_pixel == 32) {
|
||||
// 32bpp format
|
||||
} else if (vinfo.bits_per_pixel == 32) { // 32bpp format
|
||||
pix_offset = 4 * x + y * finfo.line_length;
|
||||
fbp[pix_offset+2] = temp_r;
|
||||
fbp[pix_offset+1] = temp_g;
|
||||
fbp[pix_offset+0] = temp_b;
|
||||
}
|
||||
fbp[pix_offset+2] = temp_r;
|
||||
fbp[pix_offset+1] = temp_g;
|
||||
fbp[pix_offset+0] = temp_b;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue