#include <stdio.h>
#include <stdlib.h> /* for exit */
#include <unistd.h> /* for open/close .. */
#include <fcntl.h> /* for O_RDWR */
#include <sys/ioctl.h> /* for ioctl */
#include <sys/mman.h> /* for mmap */
#include <linux/fb.h> /* for fb_var_screeninfo, FBIOGET_VSCREENINFO */

#define FBDEVFILE "/dev/fb0"

typedef unsigned char ubyte;

unsigned short makepixel(ubyte r, ubyte g, ubyte b)
{return (unsigned short) (  (r&0x1F) | ((g&0x1F)<<5) | ((b&0x1F)<<10) | (1<<15) );}

int main()
{
        int fbfd;
        int ret;
        struct fb_var_screeninfo fbvar;
        unsigned short pixel;
        int offset;
  unsigned short *pfbdata;

        fbfd = open(FBDEVFILE, O_RDWR);
        if(fbfd < 0)
        {
                perror("fbdev open");
                exit(1);
        }

        ret = ioctl(fbfd, FBIOGET_VSCREENINFO, &fbvar);
        if(ret < 0)
        {
                perror("fbdev ioctl");
                exit(1);
        }
   if(fbvar.bits_per_pixel != 16)
        {
                fbvar.bits_per_pixel=16;
                ioctl(fbfd, FBIOPUT_VSCREENINFO, &fbvar);
        }

        pfbdata = (unsigned short *)  mmap(0, fbvar.xres * fbvar.yres * 16/8, PROT_READ|PROT_WRITE, MAP_SHARED, fbfd, 0);

        if((unsigned)pfbdata == (unsigned)-1)
        {
                perror("fbdev mmap");
                exit(1);
        }

  /*draw screen black*/
        pixel = makepixel(0,0,0);

        for(offset = 0; offset < fbvar.yres * fbvar.xres; offset++)
        {
                *(pfbdata+offset) = pixel;
        }
    /* red pixel @ (50,50) */
        offset = 50*fbvar.xres+50;
        pixel = makepixel(255,0,0); /* red pixel */
        *(pfbdata+offset) = pixel; /* draw pixel */
        //*pfbdata = pixel;

        /* green pixel @ (100,50) */
        offset = 50*fbvar.xres+100;
        pixel = makepixel(0,255,0); /* green pixel */
        *(pfbdata+offset) = pixel; /* draw pixel */


        /* blue pixel @ (50,100) */
        offset = 100*fbvar.xres+50;
        pixel = makepixel(0,0,255); /* blue pixel */
        *(pfbdata+offset) = pixel; /* draw pixel */

        /* white pixel @ (100,100) */
        offset = 100*fbvar.xres+100;
        pixel = makepixel(255,255,255); /* white pixel */
        *(pfbdata+offset) = pixel; /* draw pixel */

     close(fbfd);
        exit(0);
        return 0;
}