diff --git a/C/Server/Makefile b/C/Server/Makefile index d383723..4f0b384 100644 --- a/C/Server/Makefile +++ b/C/Server/Makefile @@ -8,7 +8,7 @@ HFILES := $(foreach dir,$(HDIR),$(wildcard $(dir)/*.h)) build: @echo @echo Compiling and linking files - $(CC) $(CFILES) -I $(IDIR) -o pixelvloed -Wall + $(CC) $(CFILES) -I $(IDIR) -Os -o pixelvloed -Wall @echo Done @echo diff --git a/C/Server/main.c b/C/Server/main.c index 8a6eaa1..a240af3 100644 --- a/C/Server/main.c +++ b/C/Server/main.c @@ -53,10 +53,12 @@ static ssize_t get_udp_packet(int sock_fd, char *packet, size_t size) // application entry point int main(int argc, char* argv[]) { - - - char packet[1120]; + uint8_t packet[1120 + 32]; ssize_t packet_size; + uint16_t x, y; + uint8_t r, g, b, a; + uint8_t pixellength = 0, protocol, version; + uint16_t i; int sock_fd = get_udp_socket(UDP_PORT); @@ -66,36 +68,101 @@ int main(int argc, char* argv[]) } // draw... - int x, y; - uint8_t r, g, b, a; - int pixeloffset = 1; - int pixellength = 0; - - while( (packet_size = get_udp_packet(sock_fd, packet, sizeof(packet))) >= 0 ) { - if((uint8_t)packet[0] == 1){ - pixellength = 8; - } else { - pixellength = 7; - } - - // how many pixels - //pixelcount = (packet_size-1)/pixellength; - //printf("%d pixels in packet of size: %d using a pixel size of: %d \n", pixelcount, packet_size, pixellength); - // fetch a pixel from the udp socket - int i = 0; - for(i=pixeloffset; // skip pixeloffset - i= 0 ) { + + + + // Get protocol from the packet + version = packet[1]; // + protocol = packet[0]; // Alpha in old + + //printf("V:%d, P:%d\n", version, protocol); + + switch (protocol) { + // Protocol 0: xyrgb 16:16:8:8:8 specified for each pixel + case 0: + pixellength = 7; + for (i=2; i @@ -16,8 +23,7 @@ long int screensize = 0; char *fbp = 0; -int8_t init_frame_buffer(void) -{ +int8_t init_frame_buffer(void) { // Open the file for reading and writing fbfd = open("/dev/fb0", O_RDWR); if (!fbfd) { @@ -31,16 +37,27 @@ 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)) { printf("Error reading fixed information.\n"); return 0; } + + // Check if the framebuffer mode is supported + if ((vinfo.bits_per_pixel == 24 || vinfo.bits_per_pixel == 32) && + vinfo.red.length == 8 && vinfo.red.offset == 16 && + vinfo.green.length == 8 && vinfo.green.offset == 8 && + vinfo.blue.length == 8 && vinfo.blue.offset == 0 && + vinfo.transp.length == 0 && vinfo.transp.offset == 0) { + printf("Screen buffer mode is supported.\n"); + } else { + printf("ERROR: Screen buffer mode is not supported.\n"); + return 0; + } - // map fb to user mem + // Map fb to user mem screensize = finfo.smem_len; fbp = (char*)mmap(0, screensize, @@ -66,22 +83,63 @@ void deinit_frame_buffer(void) 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; - uint8_t bpp = vinfo.bits_per_pixel / 8; - union { - uint32_t pixel32; - uint8_t pixel8[4]; - } pixel; - uint8_t i; + uint16_t temp_r, temp_g, temp_b; - if(x < vinfo.xres && y < vinfo.yres){ - pixel.pixel32 = 0; - pixel.pixel32 |= ((uint32_t)r >> (8 - vinfo.red.length)) << vinfo.red.offset; - pixel.pixel32 |= ((uint32_t)g >> (8 - vinfo.green.length)) << vinfo.green.offset; - pixel.pixel32 |= ((uint32_t)b >> (8 - vinfo.blue.length)) << vinfo.blue.offset; - - pix_offset = bpp * x + y * finfo.line_length; - for (i=0; i= vinfo.xres || y >= vinfo.yres) { + return; + } + + //pix_offset = byte_pp * x + y * finfo.line_length; + + // 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 + 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 + pix_offset = 4 * x + y * finfo.line_length; + temp_r = fbp[pix_offset+0]; + temp_g = fbp[pix_offset+1]; + temp_b = fbp[pix_offset+2]; + } else { + temp_r = 0; + temp_g = 0; + temp_b = 0; } + //printf("%08x : %04x %04x %04x\n", pixel.pixel32, temp_r, temp_g, temp_b); + // Aply alpha + temp_r = (temp_r * (256-a))>>8; + temp_r = temp_r + ((r * (a))>>8); + temp_g = (temp_g * (256-a))>>8; + temp_g = temp_g + ((g * (a))>>8); + temp_b = (temp_b * (256-a))>>8; + temp_b = temp_b + ((b * (a))>>8); + } else { + temp_r = r; + temp_g = g; + temp_b = b; + } + + // Write new pixels to buffer + 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 + 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; } }