Arduino學習——u8glib庫資料整理


第一部分,u8glib標准語法格式:


本文使用的是DFRobot出品的LCD12864 Shield V1.0
端口占用情況:
SPI Com: SCK = 13, MOSI = 11, CS = 10, A0 = 9, RST = 8
背光控制占用數字口7

//調用u8glib庫

 

#include "U8glib.h"

//創建一個LCD對象

U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

void

 setup(){

 

}

 

void

 loop(){

 

    u8g.firstPage();

    do{

        //display

    }while(u8g.nextPage());

}

//  還有一個需要注意的地方,u8g有緩存,所以一般顯示要使用下面的方法,將繪圖函數放到draw()

 

//u8g.firstPage() 表示圖像循環的開始

 

//u8g.nextPage() 表示圖像循環的結束

 

 

 

 

第二部分,畫出幾何圖形

首先是畫點。

 

void

 U8GLIB::drawPixel(unit8_t x, unit8_t y)

 

//參數為:(x:點的橫坐標 y:點的縱坐標)

例子: u8g.drawPixel(14, 23);
完整代碼:

 

//調用u8glib庫

 

#include "U8glib.h" 

 

//創建一個LCD對象

 

U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

 

 

void

 draw(){

 

  u8g.drawPixel(14, 23);

 

}

 

 

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::drawLine(u8g_uint_t x1, u8g_uint_t y1, u8g_uint_t x2, u8g_uint_t y2)

 

//參數為: (x1:線段起始點橫坐標 y1:線段起始點縱坐標 x2:線段結束點橫坐標 y2:線段結束點縱坐標)



例子: u8g.drawLine(7, 10, 40, 55);
完整代碼:

 

 

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::drawHLine(u8g_uint_t x, u8g_uint_t y, u8g_uint_t w) //水平線段

 

void

 U8GLIB::drawVLine(u8g_uint_t x, u8g_uint_t y, u8g_uint_t h) //垂直線段

 

//參數為: (x:線段起始點 y:線段結束點 w:水平寬度 h:垂直高度)(高度單位為像素點)


例子: u8g.drawHLine(60,12, 30); u8g.drawVLine(10,20, 20);
完整代碼:

//畫一個方形

 

//調用u8glib庫

 

#include "U8glib.h"

 

//創建一個LCD對象

 

U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

 

void

 draw(){

 

    u8g.drawHLine(40,15, 30);

 

    u8g.drawVLine(70,15, 30);

 

    u8g.drawHLine(40,45, 30);

 

    u8g.drawVLine(40,15, 30);

 

}

 

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::drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2)

 

//參數為:(x0:一個角的橫坐標 y0:一個角的縱坐標 x1:另一個角的橫坐標 y1:另一個角的縱坐標 x2:最后一個角的橫坐標 y2:最后一個角的縱坐標)

 

例子: u8g.drawTriangle(14,9, 45,32, 9,42);

完整代碼:

 

//畫一個實心三角形

 

//調用u8glib庫

 

#include "U8glib.h"

 

 

 

//創建一個LCD對象

 

U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

 

 

 

void

 draw(){

 

    u8g.drawTriangle(14,9, 45,32, 9,42);

 

}

 

 

 

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::drawFrame(u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h)

 

//參數是:(x:矩形左上角橫坐標 y:矩形左上角縱坐標 w:矩形的寬 h:矩形的高)(高和寬的單位都是像素)

 

例子: u8g.drawFrame(10, 12, 30, 20);
完整代碼:

//畫一個空心矩形

 

//調用u8glib庫

 

#include "U8glib.h"

 

 

 

//創建一個LCD對象

 

U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

 

 

 

void

 draw(){

 

    u8g.drawFrame(10, 12, 30, 20);

 

}

 

 

 

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::drawRFrame(u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h, u8g_uint_t r)

 

//參數是: (x:圓角矩形左上角橫坐標 y:圓角矩形左上角縱坐標 w:圓角矩形寬度 h:圓角矩形高度 r:圓角弧度的半徑)

注意:最好要滿足兩個公式,使用時最好加上if來判斷是否符合要求
w > = 2 * x * ( r + 1 )
h > = 2 * x * ( r + 1 )
例子: u8g.drawRFrame(10 ,12 ,30 ,20 ,5);
完整代碼:

 

//創建一個LCD對象

 

U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

 

 

 

void

 draw(){

 

    u8g.drawRFrame(10,12, 30,20, 5);

 

}

 

 

 

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::drawBox(u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h)


例子: u8g.drawBox(10,12,20,30);
然后畫出圓角實心矩形,和畫圓角空心矩形一樣,但注意語法區別!!!
函數語法:

void U8GLIB::drawRBox(u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h, u8g_uint_t r)

 

例子: u8g.drawRBox(10 ,12 ,30 ,20 ,5);
然后畫出空心圓
函數語法:

 

void

 U8GLIB::drawCircle(u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t opt = U8G_DRAW_ALL)

 

//參數是:(x0:圓心的橫坐標 y0:圓心的縱坐標 rad:圓的半徑 opt:見下表)

 

  • U8G_DRAW_UPPER_RIGHT        上部右側 1/4 圓弧
  • U8G_DRAW_UPPER_LEFT         上部左側 1/4 圓弧
  • U8G_DRAW_LOWER_LEFT         下部左側 1/4 圓弧
  • U8G_DRAW_LOWER_RIGHT        下部右側 1/4 圓弧
  • U8G_DRAW_ALL                整圓(默認)


例子: u8g.drawCircle(20,20, 14); //整圓 
          u8g.drawCircle(20,20, 14, U8G_DRAW_UPPER_RIGHT); //1/4圓

 

//畫一個空心圓

 

//調用u8glib庫

 

#include "U8glib.h"

 

 

 

//創建一個LCD對象

 

U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

 

 

 

void

 draw(){

 

    u8g.drawCircle(20,20, 14);

 

}

 

 

 

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::drawDisc(u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rad, uint8_t opt = U8G_DRAW_ALL)

//參數是:(x0:圓心的橫坐標 y0:圓心的縱坐標 rad:圓的半徑 opt:見下表)

 

  • U8G_DRAW_UPPER_RIGHT        上部右側 1/4 扇形
  • U8G_DRAW_UPPER_LEFT         上部左側 1/4 扇形
  • U8G_DRAW_LOWER_LEFT         下部左側 1/4 扇形
  • U8G_DRAW_LOWER_RIGHT        下部右側 1/4 扇形
  • U8G_DRAW_ALL                整圓(默認)


例子: u8g.drawDisc(20,20, 14); //整圓 
          u8g.drawDisc(20,20, 14, U8G_DRAW_UPPER_RIGHT); //1/4圓

完整代碼:
//畫一個實心圓

 

//調用u8glib庫

 

#include "U8glib.h"

 

 

 

//創建一個LCD對象

 

U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

 

 

 

void

 draw(){

 

    u8g.drawDisc(20,20, 14);

 

}

 

 

 

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

 drawEllipse(u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rx, u8g_uint_t ry, uint8_t opt)

 

//參數是:(x0:橢圓圓心的橫坐標 y0:橢圓圓心的縱坐標 rx:水平方向半徑 ry:垂直方向半徑 rad:圓的半徑 opt:見下表)

 

  • U8G_DRAW_UPPER_RIGHT        上部右側 1/4 橢圓弧
  • U8G_DRAW_UPPER_LEFT         上部左側 1/4 橢圓弧
  • U8G_DRAW_LOWER_LEFT         下部左側 1/4 橢圓弧
  • U8G_DRAW_LOWER_RIGHT        下部右側 1/4 橢圓弧
  • U8G_DRAW_ALL                整圓(默認)

例子: u8g.drawEllipse(20,20, 14,17);

完整代碼:

 

//畫一個橢圓

 

//調用u8glib庫

 

#include "U8glib.h"

 

 

 

//創建一個LCD對象

 

U8GLIB_NHD_C12864 u8g(13, 11, 10, 9, 8);

 

 

 

void

 draw(){

 

    u8g.drawEllipse(20,20, 14,17);

 

}

 

 

 

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

 drawFilledEllipse(u8g_uint_t x0, u8g_uint_t y0, u8g_uint_t rx, u8g_uint_t ry, uint8_t opt)

 

//參數是:(x0:橢圓圓心的橫坐標 y0:橢圓圓心的縱坐標 rx:水平方向半徑 ry:垂直方向半徑 rad:圓的半徑 opt:見下表)

  • U8G_DRAW_UPPER_RIGHT        上部右側 1/4 橢圓弧
  • U8G_DRAW_UPPER_LEFT         上部左側 1/4 橢圓弧
  • U8G_DRAW_LOWER_LEFT         下部左側 1/4 橢圓弧
  • U8G_DRAW_LOWER_RIGHT        下部右側 1/4 橢圓弧
  • U8G_DRAW_ALL                整圓(默認)


例子: u8g.drawFilledEllipse(20,20, 14,17);

以上就是使用u8glib庫畫出幾何圖形。 整理一下:

 

u8g.drawPixel(14, 23);                     //畫點

 

u8g.drawLine(7, 10, 40, 55);            //畫線

 

u8g.drawHLine(60,12, 30);                 //畫水平線段

 

u8g.drawVLine(10,20, 20);                //畫垂直線段

 

u8g.drawTriangle(14,9, 45,32, 9,42);    //畫實心三角形

 

u8g.drawFrame(10, 12, 30, 20);            //畫空心矩形

 

u8g.drawRFrame(10 ,12 ,30 ,20 ,5);        //畫空心圓角矩形

 

u8g.drawBox(10,12,20,30);                //畫實心矩形

 

u8g.drawRBox(10 ,12 ,30 ,20 ,5);        //畫實心圓角矩形

 

u8g.drawCircle(20,20, 14);                 //整空心圓

 

u8g.drawCircle(20,20, 14, U8G_DRAW_UPPER_RIGHT); //1/4空心圓

 

u8g.drawDisc(20,20, 14);                 //整實心圓

 

u8g.drawDisc(20,20, 14, U8G_DRAW_UPPER_RIGHT); //1/4扇形

 

u8g.drawEllipse(20,20, 14,17);            //空心橢圓

 

u8g.drawFilledEllipse(20,20, 14,17);    //實心橢圓

 

第三部分,打印字符,字符串

繼續上一次的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)的位置12

 

 

 

完整代碼:

 

 

 

//調用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());

}123456789101112131415161718192021222324

 

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

 

print()函數可以打印字符,字符串,變量值等。但是用以前需要用setPrintPos()來設置位置

 

函數語法:

U8GLIB::print(...)

//參數為要打印的內容12

 

例子: 

u8g.setPrintPos(0,15);          //設置位置

u8g.print("Error Code: ");      //打印內容12

 

完整代碼:

 

 

//調用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());

}

 

 

u8g.setRot90();     //旋轉90°u8g.setRot180();    //旋轉180°u8g.setRot270();    //旋轉270°u8g.undoRotation(); //取消旋轉u8g.setFont(u8g_font_osb18);    //設置字體u8g.drawStr(0, 20, "ABC");      //畫出字符在(0,20)的位置u8g.setPrintPos(0,15);          //設置位置u8g.print("Error Code: ");      //打印內容u8g.drawBitmapP( 0, 0, 1, 9, bitmap);     //畫位圖,每行一個字節(8個點),總共9行,數據定義在bitmap[]u8g.drawXBMP( 0, 0, 38, 24, bitmap);      //畫位圖,38×24,數據定義在bitmap[]

第四部分,畫出圖像

 

首先是顯示一個位圖。

 

函數語法:

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:位圖對象)12

 

例子:

 

 

 

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);1234567891011

 

 

 

完整代碼:

 

 

 

//調用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());

}

}

 

 

 

另一種打印位圖的方法是:drawBitmapP()函數   

 

函數語法:

 

 

 

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位。12

 

例子:

 

 

 

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)

完整代碼:

//調用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());

}

第五部分,獲取內容信息

首先是獲取屏幕的寬和高。

函數語法:

u8g_uint_t U8GLIB::getHeight(void)      //返回屏幕的高度

u8g_uint_t U8GLIB::getWidth(void)       //返回屏幕的寬度

LCD12864的高度64,寬度是128

例子:

int w;

int h;

w = u8g.getWidth();

h = u8g.getHeight();

返回:w=128, h=64

然后是獲得所顯示字符串的寬度

函數語法:

u8g_uint_t U8GLIB::getStrWidth(const char *s)

//參數是:(*s: 字符串的內容)

例子:

int w; 

 

u8g.setFont(u8g_font_osb18);    //設置字體

u8g.drawStr(0,20, "ABC");

w = u8g.getStrWidth("ABC");     //獲得顯示的字符串寬度

u8g.drawFrame(0,10, w,11);      //畫一個以獲得的字符串寬度為寬度的方框

 

 

第六部分,設置字體,位置等

首先是設置字體

函數語法:

U8GLIB::setFont(const u8g_fntpgm_uint8_t *font)

//參數為:(*font: 要顯示字符的字體)

 

 

有關u8glib提供的字體樣式,請移步至Click Me

因為之前已經使用過此函數,此處就不做過多描述,

例子:

u8g.setFont(u8g_font_osb18);

有關u8glib提供的字體樣式

因為之前已經使用過此函數,此處就不做過多描述,

例子:

u8g.setFont(u8g_font_osb18);

然后是設置print()函數的顯示位置。

函數語法:

void U8GLIB::setPrintPos(u8g_uint_t x, u8g_uint_t y)

//參數為:(x:顯示內容的橫坐標 y:顯示內容的縱坐標)

因為之前已經使用過此函數,此處就不做過多描述,

例子:

u8g.setPrintPos(10,20);

然后是設置是否顯示對象,透明還是不透明。對於單色OLED來說,此函數就是設置對象是否為透明。對於有灰度值的屏幕來說則是一個設置灰度值。

void U8GLIB::setColorIndex(uint8_t color_index)

//參數為:(color_index:0和1)

//0為不顯示,透明

//1為顯示,不透明

例子:

u8g.setColorIndex(1);           //不透明,顯示內容

u8g.drawBox(10, 12, 20, 30); 

u8g.setColorIndex(0);           //透明,不顯示內容

u8g.drawPixel(28, 14);

然后是將顯示結果旋轉90°,180°或者270°

函數語法:

void U8GLIB::setRot90()

void U8GLIB::setRot180()

void U8GLIB::setRot270()

例子:

//正常顯示

u8g.setFont(u8g_font_osb18);

u8g.drawStr(0,20, "ABC");

 

//旋轉顯示

u8g.setRot90();     //旋轉90°

u8g.setFont(u8g_font_osb18);

u8g.drawStr(0,20, "ABC");

最后一個是修改顯示字符的坐標標准。默認標准是字符的左下角坐標,可以通過函數setFontPosTop()函數來修改為字符的左上角坐標。

函數語法:

void U8GLIB::setFontPosTop(void)

例子:

u8g.setFont(u8g_font_osb18);

u8g.setFontPosTop();

u8g.drawStr(0, 20, "ABC");

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM