第三部分,打印字符,字符串
继续上一次的u8glib自学笔记,整理过了有关打印几何图像的函数,下一步就是学习一下如何打印字符。
首先是画出字符。
函数语法:
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°
例子:
u8g.setFont(u8g_font_osb18); //设置字体
u8g.drawStr(0, 20, "ABC"); //画出字符在(0,20)的位置
完整代码:
//调用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());
}
另一种打印字符,字符串的方法就是用print。
print()函数可以打印字符,字符串,变量值等。但是用以前需要用setPrintPos()来设置位置
函数语法:
U8GLIB::print(...)
//参数为要打印的内容
例子:
u8g.setPrintPos(0,15); //设置位置
u8g.print("Error Code: "); //打印内容
完整代码:
//调用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());
}
第四部分,画出图像
首先是显示一个位图。
函数语法:
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:位图对象)
例子:
static unsigned char u8g_logo_bits[] U8G_PROGMEM = {
0xf