今天,世界末日的大年初三.一年到头就这么几天最宝贵的时间,在家琢磨着怎样整一个5×8的ASCII字符库,用于我后面要设计的温度记录仪.本人比较喜欢LCD12864(ST920)这个模块,它本身的字符库给我们带来了很多方便,可是也有些缺陷,就是个子太大,一个ASCII字符都占用了8×16的空间,可见如果我们用于绘图时当需要显示某些提示内容的时候可想而知,那文字都比图像还要大.于是,笔者经过多番的修改和更正后,完成了以下函数库,在今后的使用中,字符库可以任意更改,字体也能任意宽高,便于移植到更大的LCD模块上使用,接下来我就以一个简单的C语言程序让它显示到LCD模块上.经过使用自定义的字符库,单个12864能够显示7行字符. 效果如图:
![](http://c.51hei.com/a/old/up/0/491618571048791.jpg)
![](http://c.51hei.com/a/old/up/0/491618571039892.jpg)
本程序使用IAR AVR编译器,芯片使用M128.
先来看看我的主程序吧,很简单,就几行内容,基于之前我设计的FrameAVR,多任务调用.使用LCD12864的class类和BMP的class类组合实现.
#include "mydef.h"
#include "d:\inc\io.h"
#include "mydevice.h"
#include "myglobar.h"
#include "myfunction.h"
#include "myvect.h"
void FrameAVR::init()
{
lcd= LCD12864_SPI;// 使用SPI串行模式
bmp.create(128,64);// 创建一个128×64像素的BMP类
}
void FrameAVR::Run()
{
DDRB = 0xFF;
PORTB ^= 0x0F// 测试信号,可用示波器观察判断程序的运行状况;
}
void FrameAVR::Timer()
{
bmp.clear();// 清除缓冲区
bmp= tabAscii5_8;// 设定字符库
bmp.drawText("#include <stdio.h>");// 开始写入C程序
bmp.drawText("int main(void)",0,9);
bmp.drawText("{",0,18);
bmp.drawText(" for(chari=0;i<128;i++)",0,27);
bmp.drawText(" sprintf(\"i=%3d\",i);",0,36);
bmp.drawText(" return0;",0,45);
bmp.drawText("}",0,54);
lcd= bmp;// 向LCD模块发送数据
}
首先我建立的是一个BMP类,内容如下:(经过数天设计的可怕的程序,功能强大!慢慢来就会看得懂,重点看看下面这两个)
bool drawText(char* pStr,int dx = 0,int dy = 0)// 使用指定的字体绘字
{
while(*pStr != '\0')
{
if(dx + font.Width() >= width)// 判断当前行能否容纳下一个要填充的字符
{
dx = 0;
dy += font.Height() + 1;
if(dy > height)
return false;
}
for(int i = 0 ; i < font.Height(); i++)
{
fillLine(tabAscii5_8 + *pStr * 8 + 16 + i * font.WidthBytes(),1,dx,dy +i);
}
pStr++;
dx += font.Width();
}
return true;
}
bool fillLine(char *pSource,int nbytes,int dx,int dy)// 从pSource指向的地址向dx,dy指定的地址横向填充(自动换行)
{
char i = 0;
while(nbytes--)
{
if(dx % 8 == 0)// 当前字节尚能容纳nPixs像素
{
sbuffer[dy * nWidths + dx / 8] |= pSource;
}
else// 当前字节不能容纳nPixs像素
{
sbuffer[dy * nWidths + dx / 8] |= pSource >> (dx % 8) ;
sbuffer[dy * nWidths + dx / 8 + 1] |= pSource << (8 - dx % 8);
}
dx += 8;
i++;
}
return true;
}
以下为ASCII_5_8.h
横向取模,未定义的字符全部为空(0x00)
数组开始的几个字节具有某些特殊的用途,可以灵活调配,为了不误人子弟,本人已亲自测试
#ifndef _ASCII_5_8_H_
#define _ASCII_5_8_H_
__flash char tabAscii5_8[] =
{
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
// 每个字符
// 起始 占用的
// 宽度 高度 地址 字节数 保留 保留 保留 名称
5, 8, 0x10, 8, 0, 0, 0, 'A', 'S', 'C', 'I', 'I', '5', '*', '8', '\0',
// 0x00
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x01
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x02
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x03
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x04
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x05
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x06
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x07
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x08
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x09
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x0A
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x0B
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x0C
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x0D
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x0E
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x0F
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x10
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x11
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x13
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x14
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x15
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x16
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x17
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x18
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x19
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x1A
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x1B
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x1C
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x1D
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x1E
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x1F
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x20
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
// 0x21 !
0x00,0x60,0x60,0x60,0x60,0x00,0x60,0x60,
// 0x22 "
0xA0,0xA0,0xA0,0x00,0x00,0x00,0x00,0x00,
// 0x23 #
0x00,0x50,0x50,0xF8,0x50,0xF8,0x50,0x50,
// 0x24 $
0x00,0x20,0xF0,0x80,0xF0,0x10,0xF0,0x40,
// 0x25 %
0x90,0x10,0x20,0x20,0x40,0x40,0x80,0x90,
// 0x26 &
0x60,0x90,0x90,0x60,0x48,0xB8,0x90,0x68,
// 0x27 '
0x20,0x20,0x20,0x00,0x00,0x00,0x00,0x00,
// 0x28(
0x00,0x20,0x40,0x80,0x80,0x80,0x40,0x20,
// 0x29 )
0x00,0x40,0x20,0x10,0x10,0x10,0x20,0x40,
// 0x2A *
0x50,0x20,0x50,0x00,0x00,0x00,0x00,0x00,
// 0x2B +
0x00,0x00,0x20,0x20,0xF8,0x20,0x20,0x00,
// 0x2C ,
0x00,0x00,0x00,0x00,0x60,0x60,0x20,0x40,
// 0x2D -
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
// 0x2E .
0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,
// 0x2F /
0x10,0x10,0x20,0x20,0x40,0x40,0x80,0x80,
// 0x30 0
0x00,0xF0,0x90,0x90,0xB0,0xD0,0x90,0xF0,
// 0x31 1
0x00,0x20,0x60,0x20,0x20,0x20,0x20,0x70,
// 0x32 2
0x00,0xF0,0x10,0x10,0xF0,0x80,0x80,0xF0,
// 0x33 3
0x00,0xF0,0x10,0x10,0xF0,0x10,0x10,0xF0,
// 0x34 4
0x00,0x90,0x90,0x90,0xF0,0x10,0x10,0x10,
// 0x35 5
0x00,0xF0,0x80,0x80,0xF0,0x10,0x10,0xF0,
// 0x36 6
0x00,0xF0,0x80,0x80,0xF0,0x90,0x90,0xF0,
// 0x37 7
0x00,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,
// 0x38 8
0x00,0xF0,0x90,0x90,0xF0,0x90,0x90,0xF0,
// 0x39 9
0x00,0xF0,0x90,0x90,0xF0,0x10,0x10,0xF0,
// 0x3A :
0x00,0x00,0x00,0x60,0x60,0x00,0x60,0x60,
// 0x3B ;
0x00,0x00,0x00,0x60,0x60,0x00,0x20,0x60,
// 0x3C <
0x00,0x10,0x20,0x40,0x80,0x40,0x20,0x10,
// 0x3D =
0x00,0x00,0xF0,0x00,0x00,0xF0,0x00,0x00,
// 0x3E >
0x00,0x80,0x40,0x20,0x10,0x20,0x40,0x80,
// 0x3F ?
0x60,0x90,0x10,0x10,0x60,0x00,0x60,0x60,
// 0x40 @
0x60,0x90,0x90,0xB0,0xA0,0x80,0x90,0x60,
// 0x41 A
0x60,0x90,0x90,0x90,0xF0,0x90,0x90,0x90,
// 0x42 B
0xE0,0x90,0x90,0x90,0xE0,0x90,0x90,0xE0,
// 0x43 C
0x60,0x90,0x80,0x80,0x80,0x80,0x90,0x60,
// 0x44 D
0xE0,0xA0,0x90,0x90,0x90,0x90,0xA0,0xE0,
// 0x45 E
0xF0,0x80,0x80,0x80,0xF0,0x80,0x80,0xF0,
// 0x46 F
0xF0,0x80,0x80,0xF0,0x80,0x80,0x80,0x80,
// 0x47 G
0x60,0x90,0x80,0x80,0xB0,0x90,0x90,0x70,
// 0x48 H
0x90,0x90,0x90,0x90,0xF0,0x90,0x90,0x90,
// 0x49 I
0xE0,0x40,0x40,0x40,0x40,0x40,0x40,0xE0,
// 0x4A J
0xF0,0x20,0x20,0x20,0x20,0x20,0xA0,0x40,
// 0x4B K
0x90,0x90,0xA0,0xC0,0xC0,0xA0,0x90,0x90,
// 0x4C L
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xF0,
// 0x4D M
0xD8,0xF8,0xA8,0xA8,0x88,0x88,0x88,0x88,
// 0x4E N
0x90,0xD0,0xD0,0xD0,0xB0,0xB0,0xB0,0x90,
// 0x4F O
0x60,0x90,0x90,0x90,0x90,0x90,0x90,0x60,
// 0x50 P
0xE0,0x90,0x90,0xE0,0x80,0x80,0x80,0x80,
// 0x51 Q
0x60,0x90,0x90,0x90,0x90,0xB0,0xE0,0x70,
// 0x52 R
0x60,0x90,0x90,0x90,0xE0,0xA0,0x90,0x90,
// 0x53 S
0x60,0x90,0x80,0x40,0x20,0x10,0x90,0x60,
// 0x54 T
0xF8,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
// 0x55 U
0x90,0x90,0x90,0x90,0x90,0x90,0x90,0x60,
// 0x56 V
0x90,0x90,0x90,0x90,0x90,0xA0,0xC0,0x80,
// 0x57 W
0x88,0x88,0x88,0x88,0xA8,0xA8,0xD8,0xD8,
// 0x58 X
0x90,0x90,0x90,0x60,0x60,0x90,0x90,0x90,
// 0x59 Y
0x88,0x88,0x50,0x70,0x20,0x20,0x20,0x20,
// 0x5A Z
0xF0,0x10,0x10,0x20,0x60,0x40,0x80,0xF0,
// 0x5B [
0xE0,0x80,0x80,0x80,0x80,0x80,0x80,0xE0,
// 0x5C 反斜杠
0x80,0x80,0x40,0x40,0x20,0x20,0x10,0x10,
// 0x5D ]
0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x70,
// 0x5E ^
0x40,0x40,0xA0,0xA0,0x00,0x00,0x00,0x00,
// 0x5F _
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,
// 0x60 '
0x70,0x70,0x20,0x00,0x00,0x00,0x00,0x00,
// 0x61 a
0x00,0x00,0x60,0x10,0x70,0x90,0xB0,0x50,
// 0x62 b
0x00,0x00,0x80,0x80,0xE0,0x90,0x90,0xE0,
// 0x63 c
0x00,0x00,0x00,0x70,0x80,0x80,0x80,0x70,
// 0x64 d
0x00,0x10,0x10,0x10,0x70,0x90,0x90,0x70,
// 0x65 e
0x00,0x00,0x00,0x60,0x90,0xF0,0x80,0x70,
// 0x66 f
0x00,0x20,0x50,0x40,0xF0,0x40,0x40,0x40,
// 0x67 g
0x00,0x00,0x70,0x90,0x90,0x70,0x10,0x60,
// 0x68 h
0x00,0x80,0x80,0x80,0xE0,0x90,0x90,0x90,
// 0x69 i
0x00,0x00,0x40,0x00,0xC0,0x40,0x40,0xE0,
// 0x6A j
0x00,0x00,0x20,0x00,0x60,0x20,0x20,0xE0,
// 0x6B k
0x00,0x00,0x80,0x90,0xA0,0xC0,0xA0,0x90,
// 0x6C l
0x00,0x00,0x40,0x40,0x40,0x40,0x50,0x60,
// 0x6D m
0x00,0x00,0x00,0x70,0xA8,0xA8,0xA8,0xA8,
// 0x6E n
0x00,0x00,0x00,0xE0,0x90,0x90,0x90,0x90,
// 0x6F o
0x00,0x00,0x00,0x60,0x90,0x90,0x90,0x60,
// 0x70 p
0x00,0x00,0xE0,0x90,0x90,0xE0,0x80,0x80,
// 0x71 q
0x00,0x00,0x70,0x90,0x90,0x70,0x10,0x10,
// 0x72 r
0x00,0x00,0x00,0xD0,0x50,0x60,0x40,0x40,
// 0x73 s
0x00,0x00,0x00,0x70,0x80,0x60,0x10,0xE0,
// 0x74 t
0x00,0x00,0x40,0x40,0xF0,0x40,0x50,0x60,
// 0x75 u
0x00,0x00,0x00,0x90,0x90,0x90,0x90,0x60,
// 0x76 v
0x00,0x00,0x00,0x90,0x90,0xB0,0xE0,0x80,
// 0x77 w
0x00,0x00,0x00,0x00,0x88,0xA8,0xA8,0xD8,
// 0x78 x
0x00,0x00,0x00,0x90,0x90,0x60,0x90,0x90,
// 0x79 y
0x00,0x00,0x00,0x90,0x90,0x70,0x10,0x70,
// 0x7A z
0x00,0x00,0x00,0xF0,0x10,0x60,0x80,0xF0,
// 0x7B {
0x00,0x30,0x40,0x40,0xC0,0x40,0x40,0x30,
// 0x7C |
0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
// 0x7D }
0x00,0xC0,0x20,0x20,0x30,0x20,0x20,0xC0,
// 0x7E ~
0x00,0x00,0x00,0x40,0xA8,0x10,0x00,0x00,
// 0x7F
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
#endif
以下为bmp.h
#ifndef _BMP_H_
#define _BMP_H_
#include "d:\inc\array.h"
#include "d:\inc\font.h"
__flash char tab_pixel[8] ={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
class BMP
{
protected:
Array sbuffer;
private:
intx;
inty;
protected:
UINT nWidths; // 横向数组宽度
intwidth;// 横向像素点数
intheight;// 竖向像素点数
Font font;// 内部字符库
public:
BMP(int width = 0,int height = 0)
{
create(width,height);
}
bool create(int width,int height)// 创建一个实例
{
this->width = width;
this->height = height;
nWidths = width / 8 + (width % 8 ? 1 : 0);
if(sbuffer.create(nWidths * height))
return true;
this->width = 0;
this->height= 0;
nWidths = 0;
return false;
}
intfabs(int i)
{
return i >= 0 ? i : -i;
}
void clear()// 清除缓冲区为0x00
{
sbuffer.format(0);
}
bool drawText(char* pStr,int dx = 0,int dy = 0)// 使用指定的字体绘字
{
while(*pStr != '\0')
{
if(dx + font.Width() >= width)// 判断当前行能否容纳下一个要填充的字符
{
dx = 0;
dy += font.Height() + 1;
if(dy > height)
return false;
}
for(int i = 0 ; i < font.Height(); i++)
{
fillLine(tabAscii5_8 + *pStr * 8 + 16 + i * font.WidthBytes(),1,dx,dy +i);
}
pStr++;
dx += font.Width();
}
return true;
}
bool fillLine(char *pSource,int nbytes,int dx,int dy)// 从pSource指向的地址向dx,dy指定的地址横向填充(自动换行)
{
char i = 0;
while(nbytes--)
{
if(dx % 8 == 0)// 当前字节尚能容纳nPixs像素
{
sbuffer[dy * nWidths + dx / 8] |= pSource;
}
else// 当前字节不能容纳nPixs像素
{
sbuffer[dy * nWidths + dx / 8] |= pSource >> (dx % 8) ;
sbuffer[dy * nWidths + dx / 8 + 1] |= pSource << (8 - dx % 8);
}
dx += 8;
i++;
}
return true;
}
bool fillLine(char __flash *pSource,int nbytes,int dx,int dy)// 从pSource指向的地址向dx,dy指定的地址横向填充(自动换行)
{
char i = 0;
while(nbytes--)
{
if(dx % 8 == 0)// 当前字节尚能容纳nPixs像素
{
sbuffer[dy * nWidths + dx / 8] |= pSource;
}
else// 当前字节不能容纳nPixs像素
{
sbuffer[dy * nWidths + dx / 8] |= pSource >> (dx % 8) ;
sbuffer[dy * nWidths + dx / 8 + 1] |= pSource << (8 - dx % 8);
}
dx += 8;
i++;
}
return true;
}
void format(char fmt)// 格式化
{
sbuffer.format(fmt);
}
void drawpixel(int x,int y)// 画一个点
{
sbuffer[y * nWidths + x / 8] |= tab_pixel[x % 8];
}
void lineX(int x,const int y,int length)// 画横线
{
if(length > 0)
{
while(length != 0)
{
if(x % 8)
{
drawpixel(x++,y);
length--;
}
else
{
if(length >= 8)
{
sbuffer[y * nWidths + x / 8] = 0xFF;
length -= 8;
x += 8;
}
else
{
drawpixel(x++,y);
length--;
}
}
}
}
else if(length < 0)
{
length = fabs(length);
while(length != 0)
{
if(x % 8)
{
drawpixel(x--,y);
length--;
}
else
{
if(length >= 8)
{
sbuffer[y * nWidths + x / 8 -1] = 0xFF;
length -= 8;
x -= 8;
}
else
{
drawpixel(x--,y);
length--;
}
}
}
}
}
void lineY(const int x,int y,int length)// 画竖线
{
char pixel = tab_pixel[x % 8];
if(length > 0)
{
while(length != 0)
{
sbuffer[y++ * nWidths + x / 8] |= pixel;
length--;
}
}
else if(length < 0)
{
length = fabs(length);
while(length != 0)
{
sbuffer[y-- * nWidths + x / 8] |= pixel;
length--;
}
}
}
void moveLeft(int line,UINT size)// 整行向左移动
{
int i,source,des;
size = fabs(size);
des = nWidths * line;
while(size >= 8)
{
source = des + 1;
for( i = 0; i != nWidths - 1 ; i++)
{
sbuffer[des] = sbuffer[source];
sbuffer[source] = 0x00;
des++;
source++;
}
size -= 8;
}
des = nWidths * line;
x= 8 - size;
for(i = 0 ; i < nWidths - 1 ; i++)
{
sbuffer[des] = (sbuffer[des] << size) | (sbuffer[des + 1] >>x);
des++;
}
sbuffer[des] <<= size;
}
void moveRight(int line,UINT size)// 整行向右移动
{
int i,source,des;
size = fabs(size);
des = nWidths * (line + 1) -1;
while(size >= 8)
{
source = des - 1;
for( i = 0; i != nWidths - 1 ; i++)
{
sbuffer[des] = sbuffer[source];
sbuffer[source] = 0x00;
des--;
source--;
}
size -= 8;
}
des = nWidths * line;
x= 8 - size;
for(i = 0 ; i < nWidths - 1 ; i++)
{
sbuffer[des] = (sbuffer[des] >> size) | (sbuffer[des - 1] <<x);
des--;
}
sbuffer[des] >>= size;
}
void moveUp(UINT line)// 整体向上移动
{
int des,source;
des = 0x00;
source = line * nWidths;
while(source != sbuffer.Size())
{
sbuffer[des++] = sbuffer[source++];
}
while(des != sbuffer.Size())
{
sbuffer[des++] = 0x00;
}
}
void moveDown(UINT line)// 整体向下移动
{
int des,source;
des = sbuffer.Size() - 1;
source = sbuffer.Size() - line * nWidths - 1;
while(source != 0)
{
sbuffer[des--] = sbuffer[source--];
}
while(des != 0)
{
sbuffer[des++] = 0x00;
}
}
void move(int rx,int ry)// 移动图像
{
if(rx > 0)
{
for(int i = 0; i < height ; i++)
{
moveRight(i,rx);
}
}
else if(rx < 0)
{
for(int i = 0; i < height ; i++)
{
moveLeft(i,rx);
}
}
else if(ry > 0)
{
moveDown(ry);
}
else if(ry < 0)
{
moveUp(fabs(ry));
}
}
void MoveTo(int X,int Y)// 移动起始点
{
x= X;
y= Y;
}
void LineTo(int X,int Y)// 按MoveTo指定的起始点画线,自动保存起始点到dx,dy
{
int relx = X - x;
int rely = Y - y;
float k;
if(X < 0 || X > width || Y < 0 || Y > height)// 超过极限取消画线
return ;
drawpixel(x,y);
if(relx == 0)// 画竖线
{
lineY(x,y,rely);
}
else if(rely == 0)// 画横线
{
lineX(x,y,relx);
}
else
{
if(fabs(relx) > fabs(rely))
{
float y1 = y + 0.5;
k = (float)rely / (float)fabs(relx);
if(relx > 0)
{
while(x != X && x < 128 && x >= 0)
{
y1 += k;
drawpixel(++x,(int)(y1));
}
}
else
{
while(x != X && x < 128 && x >= 0)
{
y1 += k;
drawpixel(--x,(int)(y1));
}
}
}
else
{
float x1 = x + 0.5;
k = (float)relx / (float)fabs(rely);
if(rely > 0)
{
while(y != Y && y < 64 && y >= 0)
{
x1 += k;
drawpixel((int)(x1) , ++y);
}
}
else
{
while(y != Y && y < 64 && y >= 0)
{
x1 += k;
drawpixel((int)(x1) , --y);
}
}
}
}
x= X;
y= Y;
}
void DashTo(int X,int Y,char dash = 1)// 按MoveTo指定的起始点画虚线,自动保存起始点到dx,dy,虚线间隔为dsah设定的值
{
int relx = X - x;
int rely = Y - y;
int i = 0;
drawpixel(x & 127,y & 63);
if(relx == 0)// 画竖线
{
while(y!=Y&&y<64&&y>=0)
{
i++;
rely>=0?++y:--y;
if(i%dash==0)
drawpixel(x,y);
}
}
else if(rely==0)// 画横线
{
while(x!=X&&x<128&&x>=0)
{
i++;
relx>=0?++x:--x;
if(i%dash==0)
drawpixel(x,y);
}
}
x=X;
y=Y;
}
void Rectangle(int x,int y,int width,int height,bool filler =false)// 画一个矩形
{
if(filler)// 画填充实心的矩形
{
while(height)
{
MoveTo(x,y);
LineTo(x+width,y);
if(height > 0)
y++,height--;
else
y--,height++;
}
}
else// 画空心的矩形
{
MoveTo(x,y);
LineTo(x + width,y);
LineTo(x + width,y + height);
LineTo(x,y + height);
LineTo(x,y);
}
}
void loadBmp(char __flash *bmp,char width,char height,char stX = 0,charstY = 0)// 从flash数组里提取图像
{
char x,y;
for(y = 0 ; y < height ; y++)
{
for(x = 0 ; x < width / 8 ; x++)
{
sbuffer[(stY + y) * nWidths + x + stX / 8] = bmp[y * width / 8 + x];
}
}
}
char getByte(int x,int y)// 获取缓冲区里的数据
{
return sbuffer[y * nWidths + x];
}
UINT getWidths()// 获取横向字节数
{
return nWidths;
}
char* get(int x,int y)// 获取缓冲区地址
{
return sbuffer.Sbuffer() + y * nWidths + x;
}
intWidth()// 获取宽度(像素)
{
return width;
}
intHeight()// 获取高度(像素)
{
return height;
}
void operator = (char* p)
{
}
void operator = (char __flash *pFont)
{
font = pFont;
}
char& operator [] (UINT index)
{
return sbuffer[index];
}
Array& Sbuffer()
{
return sbuffer;
}
UINT Length()// 返回总字节数
{
return width * height;
}
};
BMP bmp;
#endif
结束语 : 经过一番努力,我终于可以在液晶模块任何地方显示文字和图形了.
|