esp8266学习笔记⑨:OLED 屏幕的使用(u8g2图形库模块)

一、使用前的准备

首先先将u8g2的模块烧录到nodemcu中,选择OLED 屏幕所支持的u8g图形库,和所需要的字体,如下图:
esp8266学习笔记⑨:OLED 屏幕的使用(u8g2图形库模块)_第1张图片
构建完成之后,下载烧录到开发板中。

二、u8g2模块的主要函数

使用时,首先初始化u8g2图形库显示。
选择不同的u8g图形库,有不同的初始化函数,但入口参数基本相同
我选择的是ssd1306_i2c_128x64_noname图形库,所以初始化语法如下
I2C显示
语法:u8g2.ssd1306_i2c_128x64_noname(id, address)
参数:
id:i2c接口id
address:显示的OLED的i2c地址
示例:

id  = 0
sda = 5 -- GPIO14
scl = 6 -- GPIO12
sla = 0x3c
i2c.setup(id, sda, scl, i2c.SLOW)
disp = u8g2.ssd1306_i2c_128x64_noname(id, sla)

附:gpio的映射

SPI显示
语法:u8g2.ssd1306_128x64_noname(bus, cs, dc, res)
参数:
bus:SPI的总线,0或1
cs:用于CS的GPIO引脚
dc:用于DC的GPIO引脚
res:用于RES的GPIO引脚,如果省略,则为无
示例:

cs  = 8 -- GPIO15
dc  = 4 -- GPIO2
res = 0 -- GPIO16
bus = 1
spi.setup(bus, spi.MASTER, spi.CPOL_LOW, spi.CPHA_LOW, 8, 8)
gpio.mode(8, gpio.INPUT, gpio.PULLUP)
disp = u8g2.ssd1306_128x64_noname(bus, cs, dc, res)

常用函数

函数名 入口参数 功能
setFont(font) font:u8g2的字体 为字形和字符串绘制函数定义u8g2字体。
setFontMode(num) num:启用(1)或禁用(0)透明模式 定义字体背景颜色模式
setDrawColor(color) color0(显示RAM中的清晰像素值)、1(设置像素值)或2(异或模式) 定义所有绘图函数的位值
DrawBox(x,y,w,h) x,y起点坐标;w框的宽度;h框的高度 绘制一个框(填充框)
drawFrame(x,y,w,h) 同上 绘制一个框架(空框)
drawCircle(x,y,rad,opt) x,y为圆心坐标;rad为圆的半径;opt为选择画的部分,分为U8G2_DRAW_UPPER_RIGHT(右上)U8G2_DRAW_UPPER_LEFT(左上)U8G2_DRAW_LOWER_LEFT(左下)U8G2_DRAW_LOWER_RIGHT(右下)U8G2_DRAW_ALL(全部) 绘制一个实心圆
drawDisc(x,y,rad,opt) 同上 绘制一个实心圆
drawEllipse(x,y,rx,ry,opt) x,y为圆心坐标,rx,ry为与椭圆打下,opt同上 绘制一个椭圆
drawFilledEllipse(x,y,rx,ry,opt) 同上 绘制一个填充的椭圆
drawLine(x0,y0,x1,y1) x0,y0x1,y1是线两端的点坐标 在两点之间画一条线
drawStr(x,y,string) x,y起点坐标;string字符串 绘制一个字符串

三、0.92寸OLED i2c显示例程

sda = 1
scl = 2
function init_OLED(sda,scl)
     sla = 0x3c
     i2c.setup(0, sda, scl, i2c.SLOW)
     disp=u8g2.ssd1306_i2c_128x64_noname(0,sla)
     disp:setFont(u8g2.font_6x10_tf)
     disp:setFontPosTop()
end
function print_OLED()
   disp:drawFrame(0, 0,128,16)
   disp:setFont(u8g2.font_6x10_tf)
   disp:drawStr(30, 5, "Hello Mcu!!")
   disp:drawStr(30, 20,"CSDNzwh")
   disp:drawStr(80, 40, str1)
   disp:drawFrame(0, 16,128,45)
   disp:sendBuffer()
end
str1="by:zwh"
init_OLED(sda,scl)
tmr.create():tmr.alarm(1000, tmr.ALARM_AUTO, function()
    print_OLED()
end)

效果图:
esp8266学习笔记⑨:OLED 屏幕的使用(u8g2图形库模块)_第2张图片
由于本例程的不足对朋友们造成困扰,由于博主前段时间的忙碌,没有登录,也就没有看到大家的评论,在这里非常抱歉,现在例程已经进行修改,同时下面会贴出中文的OLED 屏幕显示例程和取模方式,希望对大家有所帮助

取模方法

esp8266学习笔记⑨:OLED 屏幕的使用(u8g2图形库模块)_第3张图片

sda = 1
scl = 2
i=0

zhengzai_width = 32
zhengzai_height = 16
zhengzai_bits = string.char(
0x00,0x00,0x40,0x00,0xFE,0x3F,0x40,0x00,
0x80,0x00,0x20,0x00,0x80,0x00,0xFF,0x7F,
0x80,0x00,0x10,0x00,0x80,0x00,0x10,0x02,
0x88,0x00,0x08,0x02,0x88,0x1F,0x0C,0x02,
0x88,0x00,0xEA,0x3F,0x88,0x00,0x09,0x02,
0x88,0x00,0x08,0x02,0x88,0x00,0x08,0x02,
0x88,0x00,0x08,0x02,0x88,0x00,0x08,0x02,
0xFF,0x7F,0xF8,0x7F,0x00,0x00,0x08,0x00
)
qidong_width = 32
qidong_height = 16
qidong_bits = string.char(
0x80,0x00,0x00,0x02,0x00,0x01,0x00,0x02,
0xF8,0x3F,0x3E,0x02,0x08,0x20,0x00,0x02,
0x08,0x20,0x80,0x3F,0x08,0x20,0x00,0x22,
0xF8,0x3F,0x7F,0x22,0x08,0x00,0x04,0x22,
0x08,0x00,0x04,0x22,0x08,0x00,0x04,0x21,
0xE8,0x3F,0x12,0x21,0x24,0x20,0x22,0x21,
0x24,0x20,0xBF,0x20,0x22,0x20,0xA2,0x20,
0xE1,0x3F,0x40,0x14,0x20,0x20,0x20,0x08
)
feichang_width = 32
feichang_height = 16
feichang_bits = string.char(
0x20,0x02,0x80,0x00,0x20,0x02,0x88,0x08,
0x20,0x02,0x90,0x04,0x3F,0x7E,0xFE,0x7F,
0x20,0x02,0x02,0x40,0x20,0x02,0xF9,0x2F,
0x20,0x02,0x08,0x08,0x3E,0x3E,0xF8,0x0F,
0x20,0x02,0x80,0x00,0x20,0x02,0xFC,0x1F,
0x20,0x02,0x84,0x10,0x20,0x02,0x84,0x10,
0x3F,0x7E,0x84,0x14,0x20,0x02,0x84,0x08,
0x20,0x02,0x80,0x00,0x20,0x02,0x80,0x00
)
baoqian_width = 32
baoqian_height = 16
baoqian_bits = string.char(
0x08,0x01,0x44,0x04,0x08,0x01,0x28,0x04,
0x88,0x3F,0xFF,0x05,0x88,0x20,0x28,0x7E,
0x5F,0x20,0x28,0x42,0xA8,0x2F,0xFE,0x29,
0x88,0x28,0xA8,0x08,0x98,0x28,0xFF,0x09,
0x8C,0x28,0xA8,0x08,0x8B,0x2F,0xFE,0x08,
0x88,0x20,0x28,0x14,0x88,0x14,0x6C,0x14,
0x88,0x48,0xAA,0x14,0x88,0x40,0x29,0x23,
0x0A,0x7F,0x28,0x22,0x04,0x00,0x28,0x41
)
function init_OLED(sda,scl)
     sla = 0x3c
     i2c.setup(0, sda, scl, i2c.SLOW)
     disp=u8g2.ssd1306_i2c_128x64_noname(0,sla)
     disp:setFont(u8g2.font_6x10_tf)
     disp:setFontPosTop()
end
function print_OLED()
      if mode==1 then
       disp:setDrawColor(1)
       disp:setBitmapMode(0)
       disp:setFont(u8g2.font_6x10_tf)
       disp:drawXBM(26,0,zhengzai_width,zhengzai_height, zhengzai_bits)
       disp:drawXBM(59, 0, qidong_width, qidong_height, qidong_bits)
       disp:drawRFrame(0,22,101,10,2)
       disp:drawRBox(0,23,i,8,2)
       disp:drawStr(102,23,i.."%")
       disp:sendBuffer()
      elseif i>96 then
         disp:drawStr(10,50,"Everything is OK!")
      end   
      if mode == 2 then
        disp:clearBuffer()
        disp:setDrawColor(1)
        disp:setBitmapMode(0)
        disp:drawXBM(26, 20, feichang_width, feichang_height, feichang_bits)
        disp:drawXBM(59, 20, baoqian_width, baoqian_height, baoqian_bits)
        disp:drawStr(24, 50, "I'm Sorry.")
      end
      disp:sendBuffer()
end
mode = 1
init_OLED(sda,scl)
tmr.create():alarm(100,tmr.ALARM_AUTO, function()
    tmr.wdclr()
    i=i+4
    if i>100 then
        i=100
        mode=2
    end
    print_OLED()
end)

esp8266学习笔记⑨:OLED 屏幕的使用(u8g2图形库模块)_第4张图片
esp8266学习笔记⑨:OLED 屏幕的使用(u8g2图形库模块)_第5张图片

你可能感兴趣的:(nodemcu)