玩转 ESP32 + Arduino (二十五) SSD1306库驱动OLED

本次我们使用了如下库:

相对于U8G2库, 此库功能少了很多, 相对的RAM ROM占用也都少,那个绘制进度条是很有亮点的

依然接硬件IIC SCL: 22 SDA: 21

一. 如何使用库

  1. 引入库(我的是IIC接口)
#include "SSD1306Wire.h"
  1. 实例化一个SSD1306Wire对象
SSD1306Wire display(0x3c, 21, 22);
  1. 初始化屏幕
display.init();
  1. 显示和清除
display.clear();
display.display();

二. 相关API

1. 清屏, 清除显示buf区, display.clear

void OLEDDisplay::clear()
display.clear();

2. 清除某个点 display.clearPixel

void OLEDDisplay::clearPixel(int16_t x, int16_t y)
display.clearPixel(0,0);

3. 显示, 显示buf区的内容 display.display

void SSD1306Wire::display()
display.display();

4. 把显示屏关掉 display.displayOff();

5. 把显示屏打开 display.displayOn();

6. 深度睡眠后恢复 display.allocateBuffer();

//使用它可以在深度睡眠后恢复而不重置显示(init()会做什么)。
//如果已建立与显示器的连接并分配了缓冲区,则返回true,否则返回false。

display.allocateBuffer();

7. 关闭OLED,清除对象和缓存 display.end();

void OLEDDisplay::end()

8. 屏幕垂直翻转 display.flipScreenVertically();

display.flipScreenVertically();

9. 屏幕镜像显示 display.mirrorScreen();

display.mirrorScreen();

10. 反色显示 display.invertDisplay();

display.invertDisplay();

11. 回归正常显示 display.normalDisplay();

display.normalDisplay();

12. 重新初始化 display.resetDisplay();

display.resetDisplay();

13. 重置显示方向 display.resetOrientation();

display.resetOrientation();

14. 设置显示亮度 display.setBrightness();

void OLEDDisplay::setBrightness(uint8_t)

15. 设置对比度 display.setContrast()

void OLEDDisplay::setContrast(uint8_t contrast, uint8_t precharge = (uint8_t)'�', uint8_t comdetect = (uint8_t)'@')

设置显示对比度
例如: 极低的亮度和对比度:对比度= 10,预充电precharge= 5,comdetect = 0
正常亮度和对比度:对比度= 100

三. 绘制相关API

1. 设置一个点 display.setPixel

这是一下所有绘制方法的基础

void OLEDDisplay::setPixel(int16_t x, int16_t y)

2. 画空心圆 display.drawCircle

void OLEDDisplay::drawCircle(int16_t x, int16_t y, int16_t radius)
    display.drawCircle(64,32,20);

3. 画实心圆 display.fillCircle

void OLEDDisplay::fillCircle(int16_t x, int16_t y, int16_t radius)

4. 画1/4圆弧 display.drawCircleQuads

void OLEDDisplay::drawCircleQuads(int16_t x0, int16_t y0, int16_t radius, uint8_t quads)
display.drawCircleQuads(32,32,20,1);

其中: quads是角度

quads 左上 右上 左下 右下
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

5. 画水平线 display.drawHorizontalLine

void OLEDDisplay::drawHorizontalLine(int16_t x, int16_t y, int16_t length)
display.drawHorizontalLine(0,20,100);

6. 画垂直线 display.drawVerticalLine

void OLEDDisplay::drawVerticalLine(int16_t x, int16_t y, int16_t length)

7. 画线 display.drawLine

void OLEDDisplay::drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1)

8. 画空心矩形 display.drawRect

void OLEDDisplay::drawRect(int16_t x, int16_t y, int16_t width, int16_t height)

9. 画实心矩形 display.fillRect

void OLEDDisplay::fillRect(int16_t x, int16_t y, int16_t width, int16_t height)

10. 画进度条 display.drawProgressBar

void OLEDDisplay::drawProgressBar(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t progress)

进度取值0~100

四. 文本相关API

1. 设置字体 display.setFont

void OLEDDisplay::setFont(const uint8_t *fontData)
内建字体 字高 字宽 包含字符
ArialMT_Plain_10 13 10 224个字符
ArialMT_Plain_16 19 16 224个字符
ArialMT_Plain_24 28 24 224个字符

2. 设置文本对齐方法 display.setTextAlignment()

void OLEDDisplay::setTextAlignment(OLEDDISPLAY_TEXT_ALIGNMENT textAlignment)

对齐方法有:

对齐方法 描述
TEXT_ALIGN_LEFT 左对齐
TEXT_ALIGN_RIGHT 右对齐
TEXT_ALIGN_CENTER 居中对齐
TEXT_ALIGN_CENTER_BOTH 上下左右对齐

3. 绘制字符串 display.drawString

用默认或设置好的字体绘制字符串

void OLEDDisplay::drawString(int16_t x, int16_t y, String text)
  display.setFont(ArialMT_Plain_16);
  display.clear();
  display.drawString(0,0,"hello");
  display.display();

4. 绘制字符串(带最大宽度) display.drawStringMaxWidth

到达最大宽度回换行显示

五. 图像相关API

1. 显示16*16的图标 display.drawIco16x16

void OLEDDisplay::drawIco16x16(int16_t x, int16_t y, const char *ico, bool inverse = false)

我使用的绘图方法: 使用PCtoLCD

然后灵魂绘图

然后设置输出格式:

最后生成字模

最后写在程序中:

#include "SSD1306Wire.h"

SSD1306Wire display(0x3c, 21, 22);

const char image[] = {
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x01, 0x70, 0x03, 0x18, 0x06, 0x08, 0x04, 0x08, 0x04,
    0x00, 0x06, 0x80, 0x03, 0xE0, 0x00, 0xC0, 0x01, 0x00, 0x01, 0x00, 0x0C, 0x00, 0x0C, 0x00, 0x00, /*"未命名文件",0*/
};

void setup()
{
  Serial.begin(115200);
  display.init();
  display.flipScreenVertically();
  display.clear();
  display.drawIco16x16(0, 0, image, 0);
  display.display();
}

void loop()
{
}

2. 显示XBM图像 display.drawXbm

void OLEDDisplay::drawXbm(int16_t x, int16_t y, int16_t width, int16_t height, const uint8_t *xbm)

xbm图像我使用了在线转换器: https://convertio.co/zh/

3. 显示BMP位图图像 display.drawFastImage (未实验)

void OLEDDisplay::drawFastImage(int16_t x, int16_t y, int16_t width, int16_t height, const uint8_t *image)
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,165评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,720评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,849评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,245评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,596评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,747评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,977评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,708评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,448评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,657评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,141评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,493评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,153评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,890评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,799评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,685评论 2 272