基于Lua框架下Air103使用U8g2库驱动I2C OLED图像显示

Posted perseverance52

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于Lua框架下Air103使用U8g2库驱动I2C OLED图像显示相关的知识,希望对你有一定的参考价值。

基于Lua框架下Air103使用U8g2库驱动I2C OLED屏幕图像演示


  • 📍本案例来源于:https://gitee.com/openLuat/LuatOS/tree/master/demo/u8g2
  • 📌关于Lua u8g2图形处理库介绍:https://wiki.luatos.com/api/u8g2.html

🌻支持I2C 0.96寸屏幕以及1.3寸屏幕

  • 🌿0.96寸屏幕为ssd1306
u8g2.begin(ic = "ssd1106",direction = 0,mode="i2c_hw",i2c_id=0,i2c_speed = i2c.FAST) -- direction 可选0 90 180 270
-- 初始化软件i2c的ssd1306
-- u8g2.begin(ic = "ssd1306",direction = 0,mode="i2c_sw", i2c_scl=1, i2c_sda=4) -- 通过PA1 SCL / PA4 SDA模拟
  • 🌿1.3寸屏幕为sh1106
u8g2.begin(ic = "sh1106",direction = 0,mode="i2c_hw",i2c_id=0,i2c_speed = i2c.FAST) -- direction 可选0 90 180 270
-- 初始化软件i2c的sh1106
-- u8g2.begin(ic = "sh1106",direction = 0,mode="i2c_sw", i2c_scl=1, i2c_sda=4) -- 通过PA1 SCL / PA4 SDA模拟

  • ⚡注意:如果1.3寸sh1106 OLED屏幕刷ssd1306屏幕的程序,会有白边。注意ic变量参数都是小写,大写会识别不到默认为是ssd1306.

🌼汉字取模显示内容说明

  • 由于U8g2库本身就集成了中文汉字库,所以你也可以直接使用汉字字体,汉字取模,调用图像函数显示。
  • 🔧取模软件:PCtoLCD2002
  • 🔨取模参数设置:
  • 📢在生成字模前,设置好,避免拷贝到程序中再进行二次加工。
  • ⚡需要注意的是:字模点阵必须是8的整倍:16 x16 、 24x24、32x32 。
-- 以下是汉字取模显示内容:
    u8g2.ClearBuffer()
    u8g2.SetBitmapMode(1)   --定义位图函数是否将写入背景色,mode字体模式,启用(1)或禁用(0)透明模式
 
    t=

       --(0)(1)(2)(3)(4)(5)(6)(7)(8)(9)(10)(11)(12)(13)(14)(15)

        string.char(0x40,0x04,0x40,0x08,0x40,0x10,0xFC,0x07,0x40,0x04,0x40,0x04,0x40,0x04,0x40,0x04),
        string.char(0xFF,0x3F,0x40,0x20,0x40,0x20,0x40,0x20,0x40,0x20,0x40,0x14,0x40,0x08,0x40,0x00),--/*"书",0*/
        
        string.char(0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x84,0x10,0x84,0x10,0x84,0x10,0x84,0x10),
        string.char(0x84,0x10,0x84,0x10,0x84,0x10,0x84,0x10,0x84,0x10,0xFC,0x1F,0x00,0x10,0x00,0x00),--/*"山",1*/
        
        string.char(0x40,0x00,0x40,0x00,0xFF,0x7F,0x20,0x00,0x20,0x00,0xF0,0x0F,0x10,0x08,0x18,0x08),
        string.char(0xF4,0x0F,0x12,0x08,0x11,0x08,0xF0,0x0F,0x10,0x08,0x10,0x08,0x10,0x0A,0x10,0x04),--/*"有",2*/
        
        string.char(0x00,0x02,0x3E,0x02,0x22,0x1E,0x22,0x11,0xA2,0x0A,0x3E,0x04,0x08,0x0A,0x08,0x11),
        string.char(0x88,0x60,0x3A,0x1F,0x0A,0x11,0x0A,0x11,0x0A,0x11,0x3A,0x11,0x07,0x1F,0x00,0x11),--/*"路",3*/
        
        string.char(0x44,0x04,0x44,0x04,0xFF,0x05,0x44,0x04,0x7C,0x3F,0x10,0x24,0xFE,0x24,0x92,0x24),
        string.char(0xFE,0x24,0x10,0x24,0xFE,0x24,0x10,0x22,0xFE,0x22,0x10,0x21,0xF0,0x14,0x4F,0x08),--/*"勤",4*/
        
        string.char(0x80,0x00,0x84,0x00,0x88,0x00,0x88,0x00,0x80,0x00,0xFE,0x1F,0x40,0x10,0x40,0x10),
        string.char(0x40,0x11,0x20,0x12,0x20,0x12,0x10,0x10,0x08,0x10,0x04,0x10,0x02,0x0A,0x01,0x04),--/*"为",5*/
        
        string.char(0x10,0x00,0xD0,0x1F,0x08,0x08,0x04,0x04,0x12,0x06,0x10,0x19,0x88,0x20,0x6C,0x40),
        string.char(0x0A,0x00,0xC9,0x3F,0x08,0x02,0x08,0x02,0x08,0x02,0x08,0x02,0xE8,0x7F,0x08,0x00),--/*"径",6*/
        
        string.char(0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00),
        string.char(0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x0C,0x00,0x08,0x00,0x04,0x00,0x00,0x00),--/*",",7*/
        
        string.char(0x44,0x10,0x88,0x10,0x88,0x08,0x00,0x04,0xFE,0x7F,0x02,0x40,0x01,0x20,0xF8,0x07),
        string.char(0x00,0x02,0x80,0x01,0xFF,0x7F,0x80,0x00,0x80,0x00,0x80,0x00,0xA0,0x00,0x40,0x00),--/*"学",8*/
        
        string.char(0x80,0x00,0x84,0x00,0x88,0x3F,0x48,0x00,0xA1,0x1F,0x82,0x10,0x92,0x12,0x90,0x14),
        string.char(0xE8,0x7F,0x88,0x10,0x47,0x12,0x44,0x14,0xC4,0x3F,0x04,0x10,0x04,0x0A,0x00,0x04),--/*"海",9*/
        
        string.char(0x00,0x00,0xFC,0x0F,0x40,0x00,0x40,0x00,0x40,0x00,0x40,0x00,0xFE,0x3F,0x20,0x01),
        string.char(0x20,0x01,0x20,0x01,0x10,0x01,0x10,0x01,0x08,0x21,0x04,0x21,0x02,0x3E,0x01,0x00),--/*"无",10*/
        
        string.char(0x00,0x00,0xE4,0x7F,0x28,0x04,0x28,0x04,0xA1,0x3F,0x22,0x04,0x22,0x04,0xE8,0x7F),
        string.char(0x28,0x00,0x24,0x04,0x27,0x04,0xA4,0x3F,0x24,0x04,0x14,0x04,0xD4,0x7F,0x08,0x00),--/*"涯",11*/
        
        string.char(0x10,0x04,0x10,0x04,0xFF,0x7F,0x10,0x04,0x90,0x04,0x80,0x00,0x80,0x00,0xFF,0x7F),
        string.char(0x80,0x00,0x80,0x00,0xF8,0x0F,0x08,0x08,0x08,0x08,0x08,0x08,0xF8,0x0F,0x08,0x08),--/*"苦",12*/
        
        string.char(0x90,0x00,0x90,0x00,0x90,0x00,0x88,0x7F,0x48,0x01,0x4C,0x01,0x2C,0x01,0x0A,0x1F),
        string.char(0x09,0x01,0x08,0x01,0x08,0x01,0x08,0x3F,0x08,0x01,0x08,0x01,0x08,0x01,0x08,0x01),--/*"作",13*/
        
        string.char(0x40,0x00,0x20,0x00,0xF8,0x0F,0x08,0x08,0x48,0x08,0x88,0x08,0x88,0x08,0xFF,0x7F),
        string.char(0x08,0x08,0x48,0x08,0x88,0x08,0x88,0x08,0x04,0x08,0x04,0x08,0x02,0x0A,0x01,0x04),--/*"舟",14*/
        
        string.char(0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00),
        string.char(0x00,0x00,0x00,0x00,0x18,0x00,0x24,0x00,0x24,0x00,0x18,0x00,0x00,0x00,0x00,0x00) --/*"。",15*/
 
 
    
    for i = 1, #t/2 do
        row=(math.ceil(2*i/#t[1])-1)   --取整数部分,得到需显示的行数
        col=i%(128/#t[1])
        --col= (col==0) and 8 or col --等同于 if col==0 then col=8 end
        col=(col==0 and 8 or col)[1]  --通用三目运算符
        u8g2.DrawXBM((col-1)*#t[1],row*#t[2],#t[1],#t[2], t[i*2-1]..t[i*2])   --行数增加后,从0,0重新输出
        -- u8g2.SendBuffer()  --逐字输出,间隔100ms
        -- sys.wait(100)
    end
    -- u8g2.DrawXBM(0, 0, 16,16, t[1]..t[2])
    -- u8g2.DrawXBM(16, 0, 16,16, t[3]..t[4])
    u8g2.SendBuffer()   --一次性输出
    sys.wait(2000)

📝Demo代码

✨文件位置:/demo/u8g2/Air101_Air103,在原来代码基础上正价了一个while循环显示,原代码只执行一遍就不显示了,注释掉了后面的qrcode测试内容。

--- 模块功能:u8g2demo
-- @module u8g2
-- @author Dozingfiretruck
-- @release 2021.01.25

-- LuaTools需要PROJECT和VERSION这两个信息
PROJECT = "u8g2demo"
VERSION = "1.0.1"

log.info("main", PROJECT, VERSION)

-- sys库是标配
_G.sys = require("sys")

--[[
I2C0
I2C0_SCL               (PA1)
I2C0_SDA               (PA4)
]]

--添加硬狗防止程序卡死
wdt.init(900

以上是关于基于Lua框架下Air103使用U8g2库驱动I2C OLED图像显示的主要内容,如果未能解决你的问题,请参考以下文章

基于Lua框架下的合宙ESP32C3+1.5‘’Eink墨水屏天气时钟+OLED开源项目分享

使用U8g2库驱动I2C 0.96“or1.3“OLED屏幕相关接口函数选择

AIR32F103 在无系统环境和FreeRTOS环境集成LVGL

开源项目介绍STC32基于u8g2库DMA驱动IIC or SPI OLED屏幕显示

STM32F103VE基于标准库下DHT11数据I2C oled显示

ESP8266驱动SSD1306 ESP8266 for Arduino(NodeMCU U8G2库)