21851浏览
查看: 21851|回复: 4

[入门] u8glib自学笔记2

[复制链接]
第三部分,打印字符,字符串

继续上一次的u8glib自学笔记,整理过了有关打印几何图像的函数,下一步就是学习一下如何打印字符。

首先是画出字符。

函数语法:

[mw_shl_code=c,true]u8g_uint_t U8GLIB::drawStr(u8g_uint_t x, u8g_uint_t y, const char *s)
//参数为:(x:字符左下角的横坐标 y:字符左下角的纵坐标 s:要画出的字符)
//注意:使用drawStr函数之前,需要使用setFont函数来设置一下要画出的字符的显示字体。
//同时drawStr函数还有三种变形:
drawStr90();    //字符顺时针旋转响应90°
drawStr180();   //字符顺时针旋转响应180°
drawStr270();   //字符顺时针旋转响应270°[/mw_shl_code]

例子:

[mw_shl_code=c,true]u8g.setFont(u8g_font_osb18);    //设置字体
u8g.drawStr(0, 20, "ABC");      //画出字符在(0,20)的位置[/mw_shl_code]

完整代码:

[mw_shl_code=c,true]//调用u8glib库
#include "U8glib.h"

//创建一个LCD对象
U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

void draw(){
  u8g.setFont(u8g_font_osb18);
  u8g.drawStr(0, 20, "ABC");
}

void setup() {
  // put your setup code here, to run once:
  //旋转屏幕180°
  u8g.setRot180();// rotate screen
}

void loop() {
  // put your main code here, to run repeatedly:
  u8g.firstPage();
  do{
     draw();
  }while(u8g.nextPage());
}[/mw_shl_code]

另一种打印字符,字符串的方法就是用print。

print()函数可以打印字符,字符串,变量值等。但是用以前需要用setPrintPos()来设置位置

函数语法:

[mw_shl_code=c,true]U8GLIB::print(...)
//参数为要打印的内容[/mw_shl_code]

例子:

[mw_shl_code=c,true]u8g.setPrintPos(0,15);          //设置位置
u8g.print("Error Code: ");      //打印内容[/mw_shl_code]

完整代码:

[mw_shl_code=c,true]//调用u8glib库
#include "U8glib.h"

//创建一个LCD对象
U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

void draw(){
  u8g.setPrintPos(0,15);
  u8g.print("Error Code: ");
}

void setup() {
  // put your setup code here, to run once:
  //旋转屏幕180°
  u8g.setRot180();// rotate screen
}

void loop() {
  // put your main code here, to run repeatedly:
  u8g.firstPage();
  do{
     draw();
  }while(u8g.nextPage());
}[/mw_shl_code]

第四部分,画出图像

首先是显示一个位图。

函数语法:

[mw_shl_code=c,true]void U8GLIB::drawXBMP(u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h, const u8g_pgm_uint8_t *bitmap)
//参数为:(x:位图左上角的横坐标 y:位图左上角的纵坐标 w:位图的宽 h:位图的高 *bitmap:位图对象)[/mw_shl_code]

例子:

[mw_shl_code=c,true]static unsigned char u8g_logo_bits[] U8G_PROGMEM = {
   0xff, 0xff, 0xff, 0xff, 0x3f,   0xff, 0xff, 0xff, 0xff, 0x3f,   0xe0, 0xe0, 0xff, 0xff, 0x3f,
   0xe3, 0xe1, 0xff, 0xff, 0x3f,   0xf3, 0xf1, 0xff, 0xff, 0x3f,   0xf3, 0xf1, 0xfe, 0xbf, 0x37,
   0xf3, 0x11, 0x1c, 0x1f, 0x30,   0xf3, 0x01, 0x08, 0x8c, 0x20,   0xf3, 0x01, 0x00, 0xc0, 0x39,
   0xf3, 0x81, 0xc7, 0xc1, 0x39,   0xf3, 0xc1, 0xc7, 0xc9, 0x38,   0xf3, 0xc1, 0xc3, 0x19, 0x3c,
   0xe3, 0x89, 0x01, 0x98, 0x3f,   0xc7, 0x18, 0x00, 0x08, 0x3e,   0x0f, 0x3c, 0x70, 0x1c, 0x30,
   0x3f, 0xff, 0xfc, 0x87, 0x31,   0xff, 0xff, 0xbf, 0xc7, 0x23,   0x01, 0x00, 0x00, 0xc6, 0x23,
   0x03, 0x00, 0x00, 0x0e, 0x30,   0xff, 0xff, 0x3f, 0x1f, 0x3c,   0xff, 0xff, 0x3f, 0xff, 0x3f,
   0xff, 0xff, 0x3f, 0xff, 0x3f,   0xff, 0xff, 0xff, 0xff, 0x3f,   0xff, 0xff, 0xff, 0xff, 0x3f
};
u8g.drawXBMP( 0, 0, 38, 24, u8g_logo_bits);[/mw_shl_code]

完整代码:

[mw_shl_code=c,true]//调用u8glib库
#include "U8glib.h"

//创建一个LCD对象
U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

static unsigned char u8g_logo_bits[] U8G_PROGMEM = {
   0xff, 0xff, 0xff, 0xff, 0x3f,   0xff, 0xff, 0xff, 0xff, 0x3f,   0xe0, 0xe0, 0xff, 0xff, 0x3f,
   0xe3, 0xe1, 0xff, 0xff, 0x3f,   0xf3, 0xf1, 0xff, 0xff, 0x3f,   0xf3, 0xf1, 0xfe, 0xbf, 0x37,
   0xf3, 0x11, 0x1c, 0x1f, 0x30,   0xf3, 0x01, 0x08, 0x8c, 0x20,   0xf3, 0x01, 0x00, 0xc0, 0x39,
   0xf3, 0x81, 0xc7, 0xc1, 0x39,   0xf3, 0xc1, 0xc7, 0xc9, 0x38,   0xf3, 0xc1, 0xc3, 0x19, 0x3c,
   0xe3, 0x89, 0x01, 0x98, 0x3f,   0xc7, 0x18, 0x00, 0x08, 0x3e,   0x0f, 0x3c, 0x70, 0x1c, 0x30,
   0x3f, 0xff, 0xfc, 0x87, 0x31,   0xff, 0xff, 0xbf, 0xc7, 0x23,   0x01, 0x00, 0x00, 0xc6, 0x23,
   0x03, 0x00, 0x00, 0x0e, 0x30,   0xff, 0xff, 0x3f, 0x1f, 0x3c,   0xff, 0xff, 0x3f, 0xff, 0x3f,
   0xff, 0xff, 0x3f, 0xff, 0x3f,   0xff, 0xff, 0xff, 0xff, 0x3f,   0xff, 0xff, 0xff, 0xff, 0x3f
};

void draw(){
  u8g.drawXBMP( 0, 0, 38, 24, u8g_logo_bits);
}

void setup() {
  // put your setup code here, to run once:
  //旋转屏幕180°
  u8g.setRot180();// rotate screen
}

void loop() {
  // put your main code here, to run repeatedly:
  u8g.firstPage();
  do{
     draw();
  }while(u8g.nextPage());
}[/mw_shl_code]

另一种打印位图的方法是:drawBitmapP()函数

函数语法:

[mw_shl_code=c,true]void U8GLIB::drawBitmapP(u8g_uint_t x, u8g_uint_t y, u8g_uint_t cnt, u8g_uint_t h, const u8g_pgm_uint8_t *bitmap)
//参数为:(x:位图左上角的横坐标 y:位图左上角的纵坐标 cnt:在水平方向上位图的字节数 h:位图的高 *bitmap:位图对象),所以位图的宽,就是(cnt * 8),1字节=8位。[/mw_shl_code]

例子:

[mw_shl_code=c,true]const uint8_t rook_bitmap[] U8G_PROGMEM = {
  0x00,         // 00000000
  0x55,         // 01010101
  0x7f,         // 01111111
  0x3e,         // 00111110
  0x3e,         // 00111110
  0x3e,         // 00111110
  0x3e,         // 00111110
  0x7f          // 01111111
};
u8g.drawBitmapP(0,0, 1, 8, rook_bitmap);[/mw_shl_code]

完整代码:

[mw_shl_code=c,true]//调用u8glib库
#include "U8glib.h"

//创建一个LCD对象
U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

const uint8_t rook_bitmap[] U8G_PROGMEM = {
  0x00,         // 00000000
  0x55,         // 01010101
  0x7f,         // 01111111
  0x3e,         // 00111110
  0x3e,         // 00111110
  0x3e,         // 00111110
  0x3e,         // 00111110
  0x7f          // 01111111
};

void draw(){
    u8g.drawBitmapP(0,0, 1, 8, rook_bitmap);
}

void setup() {
  // put your setup code here, to run once:
  //旋转屏幕180°
  u8g.setRot180();// rotate screen
}

void loop() {
  // put your main code here, to run repeatedly:
  u8g.firstPage();
  do{
     draw();
  }while(u8g.nextPage());
}[/mw_shl_code]

第五部分,获取内容信息

首先是获取屏幕的宽和高。

函数语法:

[mw_shl_code=c,true]u8g_uint_t U8GLIB::getHeight(void)      //返回屏幕的高度
u8g_uint_t U8GLIB::getWidth(void)       //返回屏幕的宽度[/mw_shl_code]

LCD12864的高度64,宽度是128

例子:

[mw_shl_code=c,true]int w;
int h;
w = u8g.getWidth();
h = u8g.getHeight();[/mw_shl_code]

返回:w=128, h=64

然后是获得所显示字符串的宽度

函数语法:

[mw_shl_code=c,true]u8g_uint_t U8GLIB::getStrWidth(const char *s)
//参数是:(*s: 字符串的内容)[/mw_shl_code]

例子:

[mw_shl_code=c,true]int w;  

u8g.setFont(u8g_font_osb18);    //设置字体
u8g.drawStr(0,20, "ABC");
w = u8g.getStrWidth("ABC");     //获得显示的字符串宽度
u8g.drawFrame(0,10, w,11);      //画一个以获得的字符串宽度为宽度的方框[/mw_shl_code]

w=54

第六部分,设置字体,位置等

首先是设置字体

函数语法:

[mw_shl_code=c,true]U8GLIB::setFont(const u8g_fntpgm_uint8_t *font)
//参数为:(*font: 要显示字符的字体)[/mw_shl_code]

有关u8glib提供的字体样式,请移步至 点我 (可能需要翻墙)

因为之前已经使用过此函数,此处就不做过多描述,

例子:

[mw_shl_code=c,true]u8g.setFont(u8g_font_osb18);[/mw_shl_code]

然后是设置print()函数的显示位置。

函数语法:

[mw_shl_code=c,true]void U8GLIB::setPrintPos(u8g_uint_t x, u8g_uint_t y)
//参数为:(x:显示内容的横坐标 y:显示内容的纵坐标)[/mw_shl_code]

因为之前已经使用过此函数,此处就不做过多描述,

例子:

[mw_shl_code=c,true]u8g.setPrintPos(10,20);[/mw_shl_code]

然后是设置是否显示对象,透明还是不透明。对于单色OLED来说,此函数就是设置对象是否为透明。对于有灰度值的屏幕来说则是一个设置灰度值。

函数语法:

[mw_shl_code=c,true]void U8GLIB::setColorIndex(uint8_t color_index)
//参数为:(color_index:0和1)
//0为不显示,透明
//1为显示,不透明[/mw_shl_code]

例子:

[mw_shl_code=c,true]u8g.setColorIndex(1);           //不透明,显示内容
u8g.drawBox(10, 12, 20, 30);  
u8g.setColorIndex(0);           //透明,不显示内容
u8g.drawPixel(28, 14);[/mw_shl_code]

然后是将显示结果旋转90°,180°或者270°

函数语法:

[mw_shl_code=c,true]void U8GLIB::setRot90()
void U8GLIB::setRot180()
void U8GLIB::setRot270()[/mw_shl_code]

例子:

[mw_shl_code=c,true]//正常显示
u8g.setFont(u8g_font_osb18);
u8g.drawStr(0,20, "ABC");

//旋转显示
u8g.setRot90();     //旋转90°
u8g.setFont(u8g_font_osb18);
u8g.drawStr(0,20, "ABC");[/mw_shl_code]

最后一个是修改显示字符的坐标标准。默认标准是字符的左下角坐标,可以通过函数setFontPosTop()函数来修改为字符的左上角坐标。

函数语法:

[mw_shl_code=c,true]void U8GLIB::setFontPosTop(void)[/mw_shl_code]

例子:

[mw_shl_code=c,true]u8g.setFont(u8g_font_osb18);
u8g.setFontPosTop();
u8g.drawStr(0, 20, "ABC");[/mw_shl_code]

通过这两篇笔记,记录了u8glib库的使用方法。配合u8glib库里面的各个函数,可以在LCD12864屏幕上打印出来各种内容。

dsweiliang  初级技神

发表于 2016-12-22 04:05:23

又学到东西了
回复

使用道具 举报

gray6666  初级技神

发表于 2016-12-24 20:30:12

路过,帮顶
回复

使用道具 举报

极速未来PHANTOM  见习技师

发表于 2017-7-16 22:09:44

我的字符取模后,不能够正常显示,是怎么回事?
回复

使用道具 举报

极速未来PHANTOM  见习技师

发表于 2017-7-16 22:09:47

我的字符取模后,不能够正常显示,是怎么回事?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail