溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

嵌入式Linux Framebuffer怎么描點畫線

發(fā)布時間:2021-11-23 18:19:54 來源:億速云 閱讀:633 作者:iii 欄目:互聯(lián)網(wǎng)科技

這篇文章主要講解了“嵌入式Linux Framebuffer怎么描點畫線”,文中的講解內(nèi)容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“嵌入式Linux Framebuffer怎么描點畫線”吧!

Framebuffer顧名思義,Frame是幀的意思,buffer是緩沖區(qū)的。Framebuffer中保存著每一幀圖像的每一個像素的顏色值。

LCD操作原理

  • 驅(qū)動程序設置好LCD控制器

    • 根據(jù)LCD參數(shù)設置LCD控制器的時序,信號極性

    • 根據(jù)LCD分辨率,BPP分配Framebuffer

  • APP通過ioctl獲取LCD的分辨率,BPP等參數(shù)

  • APP通過mmap映射Framebuffer,在Framebuffer中寫入數(shù)據(jù)。 嵌入式Linux Framebuffer怎么描點畫線 從上圖可以看到Framebuffer和LCD的可顯示區(qū)域是一一對應的。使用Framebuffer顯示實際就是向Framebuffer寫入每個像素點的顏色數(shù)據(jù)。LCD的像素點的坐標與Framebuffer中的位置關系如下圖 嵌入式Linux Framebuffer怎么描點畫線

給出任意屏幕坐標(x,y),以及Framebuffer的基地址fb_base.另外LCD的分辨率是Xres x Yres,表示單個像素顏色的二進制位數(shù)bpp。則其顏色數(shù)據(jù)在framebuffer中的地址是:

(x,y)像素起始地址=fb_base + (y*Xres+x)*bpp/8

像素顏色表示

嵌入式Linux Framebuffer怎么描點畫線

示例代碼

#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <linux/fb.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <math.h>
static int fd_fb;
static struct fb_var_screeninfo var;	/* Current var */
static int screen_size;					/* 一幀數(shù)據(jù)所占用的字節(jié)數(shù)*/
static unsigned char *fb_base;			/* Framebuffer首地址*/
static unsigned int line_width;			/* 一行數(shù)據(jù)所占用的字節(jié)數(shù)*/
static unsigned int pixel_width;		/* 單個像素所占用的字節(jié)數(shù)*/

/**********************************************************************
 * 函數(shù)名稱: lcd_put_pixel
 * 功能描述: 在LCD指定位置上輸出指定顏色(描點)
 * 輸入?yún)?shù): x坐標,y坐標,顏色
 * 輸出參數(shù): 無
 * 返 回 值: 會
 * 修改日期        版本號     修改人	      修改內(nèi)容
 * -----------------------------------------------
 * 2020/05/12	     V1.0	  zh(angenao)	      創(chuàng)建
 ***********************************************************************/ 
void lcd_put_pixel(int x, int y, unsigned int color){
	unsigned char *pen_8 = fb_base+y*line_width+x*pixel_width;
	unsigned short *pen_16;	
	unsigned int *pen_32;	

	unsigned int red, green, blue;	

	pen_16 = (unsigned short *)pen_8;
	pen_32 = (unsigned int *)pen_8;

	switch (var.bits_per_pixel){
		case 8:{
			*pen_8 = color;
			break;
		}
		case 16:{
			/* 565 */
			red   = (color >> 16) & 0xff;
			green = (color >> 8) & 0xff;
			blue  = (color >> 0) & 0xff;
			color = ((red >> 3) << 11) | ((green >> 2) << 5) | (blue >> 3);
			*pen_16 = color;
			break;
		}
		case 32:{
			*pen_32 = color;
			break;
		}
		default:{
			printf("can't surport %dbpp\n", var.bits_per_pixel);
			break;
		}
	}
}
int main(int argc, char **argv){
	int i;
	fd_fb = open("/dev/fb0", O_RDWR);/** 打開fb設備*/
	if (fd_fb < 0){
		printf("can't open /dev/fb0\n");
		return -1;
	}
	if (ioctl(fd_fb, FBIOGET_VSCREENINFO, &var)){/** 獲取屏幕可變信息*/
		printf("can't get var\n");
		return -1;
	}
	printf("RES:%d x %d\n",var.xres,var.yres);
	printf("one pixel bits:%d\n",var.bits_per_pixel);
	line_width  = var.xres * var.bits_per_pixel / 8;// 一行數(shù)據(jù) 占據(jù)字節(jié)數(shù)
	printf("line_width:%d byte\n",line_width);
	pixel_width = var.bits_per_pixel / 8;///單個像素占用的字節(jié)數(shù)
	printf("pixel_width:%d byte\n",pixel_width);
	screen_size = var.xres * var.yres * var.bits_per_pixel / 8;//一幀畫面占用的字節(jié)數(shù)
	printf("screen_size:%d byte\n",screen_size);
	fb_base = (unsigned char *)mmap(NULL , screen_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd_fb, 0);/** 映射framebuffer的首地址*/
	if (fb_base == (unsigned char *)-1){
		printf("can't mmap\n");
		return -1;
	}
	memset(fb_base, 0x00, screen_size);/* 清屏: 全部設為黑色 */
	/* 隨便設置出100個為紅點 */
	const double T=2*3.14;//周期
	const int A=100;//振幅
	double x,sin_y,cos_y;
	int start_x,start_y;//繪圖的起始點像素坐標
	start_x=0;
	start_y = var.yres/2;
	
	while(1){
		for(i=0;i<var.xres;i++){
			x= (i*2*T)/(var.xres);
			sin_y = A*sin(x);
			cos_y = A*cos(x);
			int w=0;
			for(w=0;w<5;w++){
				
				lcd_put_pixel(start_x+i+w,start_y+sin_y, 0x00FF00);	
				lcd_put_pixel(start_x+i+w,start_y+cos_y, 0xFF0000);
			}
			usleep(1000);
		}
		memset(fb_base, 0x00, screen_size);/* 清屏: 全部設為黑色 */
	}
	munmap(fb_base , screen_size);/** 解除內(nèi)存映射*/
	close(fd_fb);
	return 0;	
}

上述代碼示例效果(手機拍出來效果略微偏色) 嵌入式Linux Framebuffer怎么描點畫線

感謝各位的閱讀,以上就是“嵌入式Linux Framebuffer怎么描點畫線”的內(nèi)容了,經(jīng)過本文的學習后,相信大家對嵌入式Linux Framebuffer怎么描點畫線這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI