一文详解TI的grlib图形库移植到Atmel SAM4套件

电子说

1.2w人已加入

描述

图形库其实都是都是由底层的画点,画线等这些基本函数组成。大家以前上数学都有学过点成线,线成面这些的,而图形库也是基于此的。

TI的图形库分3层,如下图:

ti

我们只需要修改显示驱动层即可。上面两层都是在调用最低层的驱动层。

显示驱动层函数要我们重写,不是很多,就只有几个函数而已:

如下截图:

ti

分别是:

画点函数:void PixelDraw(void *pvDisplayData, long x, long y, unsigned longcolor);

画多点的函数:void PixelDrawMultiple(void *pvDisplayData,

longx,

longy,

longx0,

longlCount,

longBPP,

constunsigned char *pucData,

constunsigned char *pucPalette)

{

画横线的函数:void LineDrawH (void *pvDisplayData,long x1,long x2, long y,unsigned long color);

画竖线的函数:void LineDrawH (void *pvDisplayData,long x1,long x2, long y,unsigned long color);

矩形填充函数:void RectFill (void *pvDisplayData, const tRectangle *pRect,unsigned long ulValue);

以下是我重写的源代码,我用的屏幕是2.4寸的TFT屏,主控是ILI9341,SPI通信的。

画点函数:

void PixelDraw(void *pvDisplayData, long x,long y, unsigned long color)

{

TFT_setXY(x,y);

TFT_sendData16(color);

}

画线横线的函数:

void LineDrawH (void *pvDisplayData,longx1,long x2, long y, unsigned long color)

{

int32_ti = 0;

int32_tlength = x2 - x1;

TFT_setCol(x1,x2);

TFT_setPage(y,y);

TFT_sendCMD(0x2c);

TFT_DC_HIGH;

//TFT_CS_LOW;

for(; i 《 length; i++)

{

SPI_transfer8(color》》 8);

SPI_transfer8(color& 0xff);

}

//TFT_CS_HIGH;

}

画竖线的函数:

void LineDrawV (void *pvDisplayData,longx,long y1, long y2, unsigned long color)

{

int32_t i = 0;

int32_tlength = y2 - y1;

TFT_setCol(x,x);

TFT_setPage(y1,y2);

TFT_sendCMD(0x2c);

TFT_DC_HIGH;

//TFT_CS_LOW;

for(; i 《 length; i++)

{

SPI_transfer8(color》》 8);

SPI_transfer8(color& 0xff);

}

//TFT_CS_HIGH;

}

填充矩形的函数:

void RectFill (void *pvDisplayData, consttRectangle *pRect, unsigned long ulValue)

{

uint32_tuY;

for(uY = pRect-》sYMin; uY 《= pRect-》sYMax; uY++)

{

LineDrawH(0,pRect-》sXMin, pRect-》sXMax, uY, ulValue);

}

}

最后的函数有些长的画多点的函数:

void PixelDrawMultiple(void *pvDisplayData,

longx,

longy,

longx0,

longlCount,

longBPP,

constunsigned char *pucData,

constunsigned char *pucPalette)

{

uint32_tulPixel = 0;

uint32_tulColor = 0;

TFT_setCol(x,DISPLAY_WIDTH);

TFT_setPage(y,DISPLAY_HEIGHT);

TFT_sendCMD(0x2c);

if(BPP == 1)

{

//1 bit per pixel in pucData

//lX0 is the index of the bit processed within a byte

//pucPalette holds the pre-translated 32bit display color

while(lCount)

{

ulPixel= *pucData++;

while(lCount && x0 《 8) // whilethere are pixels in this byte

{

ulColor= ((uint32_t *) pucPalette)[ulPixel & 1];// retrieve already translatedcolor

TFT_sendData16(ulColor);

lCount--; // processed another pixel

x0++; // done with this bit

ulPixel》》= 1; // prepare next bit

}

x0= 0; // process next byte, reset bitcounter

}

}

elseif (BPP == 4)

{

//4 bits per pixel in pucData

//lX0 holds 0/1 to indicate 4-bit nibble within byte

//pucPalette holds untranslated 24 bit color

while(lCount)

{

if(x0 == 0) // read first nibble

{

ulPixel= *pucData 》》 4;

x0= 1; // set index to second nibble

}

else

{ // readsecond nibble

ulPixel= *pucData & 0x0f;

pucData++;//increase byte pointer as we‘re done reading this byte

x0= 0; // set index to first nibble

}

ulColor= *(uint32_t *) (pucPalette + (ulPixel * 3)) & 0x00ffffff;// retrieve 24bit color

TFT_sendData16(COLOR24TO16BIT(ulColor));//translate and write to display

lCount--; // processed another pixel

}

}

elseif (BPP == 8)

{

//8 bits per pixel in pucData

//pucPalette holds untranslated 24 bit color

while(lCount)

{

ulPixel= *pucData++; // read pixel

ulColor= *(uint32_t *) (pucPalette + (ulPixel * 3)) & 0x00ffffff;// retrieve 24bit color

TFT_sendData16(COLOR24TO16BIT(ulColor));//translate and write to display

lCount--; // processed another pixel

}

}

elseif (BPP == 16)

{

//16 bits per pixel

//Pixel is in 16bit color, 5R 6G 5B format

//No color translation needed for this display

while(lCount)

{

ulPixel= *((uint16_t *) pucData);

TFT_sendData16(ulPixel);

pucData+= 2;

lCount--;

}

}

}

如果你的屏幕是控制芯片是其他的,可以拿厂家提供的底层驱动文件整合到TI的图形库的底层驱动中。

这次使用了TI图形库的库文件grlib.a,我加上grlib.h的头文件即可。工程结构如下图:

ti

ti的图像库可以支持多种文件格式的输出到屏幕显示,还有画图像的函数。我也是参考坛友的帖子进行操作的。TI的图形库有个工具可以把图像转换成c的代码。只不过只支持pnm后缀的图片格式。我们可以用图像编辑软件转换格式。我使用了GIMP的软件进行转换。

具体在目录是StellarisWare oolsin;

首先我们打开GMIP软件,然后可以直接把图片拖到编辑区,然后设置导出的参数,步奏如下:

ti

ti

ti

ti

ti

软件默认转化后的文件放值得位置是在图片所在的目录:

ti

软件默认是第一个选项,但这样生成的图片数据太大,转换软件会爆出:颜色太多的信息而导致转换失败。所以我就选择了网页优化版。

我们启动cmd,把目录切换到我们工具所在的位置,这些windows的命令自行百度去。我这里就不多说了,都是常用的命令。

ti

ti

然后打上pnmtoc -c image.pnm 》 image.c,image是你图片的文件名,回车后就会生成一个c的文本。

ti

以下是我的测试图片:

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分