frambuffer lcd.c

  1 /**
  2 *lcd.c
  3 */
  4 #include <unistd.h>  
  5 #include <stdio.h>  
  6 #include <stdlib.h>  
  7 #include <fcntl.h>  
  8 #include <string.h>  
  9 #include <linux/fb.h>  
 10 #include <sys/mman.h>  
 11 #include <sys/ioctl.h>  
 12 #include <arpa/inet.h>  
 13 #include "type.h"
 14 #include "lcd.h"
 15  
 16 
 17 lcd_t lcd;
 18 
 19 s32 lcd_fb_init(void)
 20 {
 21     //打开显示设备  
 22     lcd.fd = open("/dev/fb0", O_RDWR);  
 23     if (!lcd.fd)  
 24     {  
 25         printf("Error: open /dev/fb0
");  
 26         return -1;
 27     }  
 28 
 29     //get fix information
 30     if (ioctl(lcd.fd, FBIOGET_FSCREENINFO, &lcd.finfo))  
 31     {  
 32         printf("Error: get fix info");  
 33         return -1;
 34     }  
 35 
 36     //get variable information
 37     if (ioctl(lcd.fd, FBIOGET_VSCREENINFO, &lcd.vinfo))  
 38     {  
 39         printf("Error: get var info");  
 40         return -1;
 41     }  
 42 
 43     lcd.vinfo.bits_per_pixel = 16;
 44     //lcd screen byte size
 45     lcd.screensize = lcd.vinfo.xres * lcd.vinfo.bits_per_pixel / 8 + 
 46             lcd.vinfo.yres *lcd.vinfo.xres;
 47 
 48     //对象映射  
 49     lcd.fbp = (u8 *)mmap(0, lcd.screensize, PROT_READ | PROT_WRITE, MAP_SHARED, lcd.fd, 0);  
 50     if ((int)lcd.fbp == -1)  
 51     {  
 52         printf("Error: mmap
");  
 53         return -1;
 54     }  
 55 }
 56 void lcd_fb_release(void)
 57 {
 58     /*释放缓冲区,关闭设备*/
 59     munmap(lcd.fbp, lcd.screensize);  
 60     close(lcd.fd);  
 61     return ;  
 62 }
 63 
 64 void lcd_fb_clear(void)
 65 {
 66     memset(lcd.fbp, 0, lcd.screensize);
 67     return ;  
 68 }
 69 
 70 void lcd_fb_info(void)
 71 {
 72     printf("xres: %d
", lcd.vinfo.xres);
 73     printf("yres: %d
", lcd.vinfo.yres);
 74     printf("screensize: %d
", lcd.screensize);
 75     printf("bits_per_pixel: %d
", lcd.vinfo.bits_per_pixel);
 76     return;
 77 }
 78 
 79 
 80 void lcd_fb_put_pixel(u16 x, u16 y, color_t color)
 81 { 
 82     u32 location;
 83     if (x > lcd.vinfo.xres - 1) x = lcd.vinfo.xres - 1;
 84     if (y > lcd.vinfo.yres - 1) y = lcd.vinfo.yres - 1;
 85     location = x * lcd.vinfo.bits_per_pixel / 8 + y * lcd.vinfo.xres;
 86     if (y%2) location -= (lcd.vinfo.yres + 80);/*if you know why? please tell me*/
 87 
 88     if (32 == lcd.vinfo.bits_per_pixel)
 89     {
 90         *((u8*) (lcd.fbp + location)) = color.r;
 91         *((u8*) (lcd.fbp + location + 1)) = color.g;
 92         *((u8*) (lcd.fbp + location + 2)) = color.b;
 93         *((u8*) (lcd.fbp + location + 3)) = 0;/*transplite, unused*/
 94     }
 95     else if (16 == lcd.vinfo.bits_per_pixel)
 96     {
 97         *((u16*)(lcd.fbp + location)) = (u16)(color.r << 11 | color.g << 5 |color.b );        
 98     }
 99     else 
100     {
101         ;//
102     }
103 }
104 
105 void lcd_fb_draw_line(u16 x1, u16 y1, u16 x2, u16 y2, color_t color)
106 {
107     int i = 0;
108     int d = 0;
109     if (abs(y2 - y1) > abs(x2 - x1))
110     {
111         d = (y2 > y1) ? 1 : -1;
112         for (i = y1; i != y2; i +=d)
113         {
114             lcd_fb_put_pixel(x1 + (i - y1) * (x2 - x1) / (y2 - y1), i, color);
115         }
116     }
117     else 
118     {
119         d = (x2 > x1) ? 1 : -1;
120         for (i = x1; i != x2; i+=d)
121         {
122             lcd_fb_put_pixel(i, y1 + (i - x1) * (y2 - y1) / (x2 - x1), color);
123         }
124     }
125 }
原文地址:https://www.cnblogs.com/ganrui/p/3701649.html