玩轉(zhuǎn) ESP32 + Arduino (二十五) SSD1306庫驅(qū)動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();

二. 相關(guān)API

1. 清屏, 清除顯示buf區(qū), 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區(qū)的內(nèi)容 display.display

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

4. 把顯示屏關(guān)掉 display.displayOff();

5. 把顯示屏打開 display.displayOn();

6. 深度睡眠后恢復(fù) display.allocateBuffer();

//使用它可以在深度睡眠后恢復(fù)而不重置顯示(init()會做什么)。
//如果已建立與顯示器的連接并分配了緩沖區(qū)乡翅,則返回true鳞疲,否則返回false。

display.allocateBuffer();

7. 關(guān)閉OLED,清除對象和緩存 display.end();

void OLEDDisplay::end()

8. 屏幕垂直翻轉(zhuǎn) 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. 設(shè)置顯示亮度 display.setBrightness();

void OLEDDisplay::setBrightness(uint8_t)

15. 設(shè)置對比度 display.setContrast()

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

設(shè)置顯示對比度
例如: 極低的亮度和對比度:對比度= 10蠕蚜,預(yù)充電precharge= 5尚洽,comdetect = 0
正常亮度和對比度:對比度= 100

三. 繪制相關(guān)API

1. 設(shè)置一個點 display.setPixel

這是一下所有繪制方法的基礎(chǔ)

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

四. 文本相關(guān)API

1. 設(shè)置字體 display.setFont

void OLEDDisplay::setFont(const uint8_t *fontData)
內(nèi)建字體 字高 字寬 包含字符
ArialMT_Plain_10 13 10 224個字符
ArialMT_Plain_16 19 16 224個字符
ArialMT_Plain_24 28 24 224個字符

2. 設(shè)置文本對齊方法 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

用默認(rèn)或設(shè)置好的字體繪制字符串

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

到達最大寬度回換行顯示

五. 圖像相關(guān)API

1. 顯示16*16的圖標(biāo) display.drawIco16x16

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

我使用的繪圖方法: 使用PCtoLCD

然后靈魂繪圖

然后設(shè)置輸出格式:

最后生成字模

最后寫在程序中:

#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圖像我使用了在線轉(zhuǎn)換器: 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)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市靶累,隨后出現(xiàn)的幾起案子腺毫,更是在濱河造成了極大的恐慌,老刑警劉巖挣柬,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件潮酒,死亡現(xiàn)場離奇詭異,居然都是意外死亡邪蛔,警方通過查閱死者的電腦和手機急黎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來侧到,“玉大人勃教,你說我怎么就攤上這事〈材粒” “怎么了荣回?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長戈咳。 經(jīng)常有香客問我心软,道長,這世上最難降的妖魔是什么著蛙? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任删铃,我火速辦了婚禮,結(jié)果婚禮上踏堡,老公的妹妹穿的比我還像新娘猎唁。我一直安慰自己,他們只是感情好顷蟆,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布诫隅。 她就那樣靜靜地躺著,像睡著了一般帐偎。 火紅的嫁衣襯著肌膚如雪逐纬。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天削樊,我揣著相機與錄音豁生,去河邊找鬼兔毒。 笑死,一個胖子當(dāng)著我的面吹牛甸箱,可吹牛的內(nèi)容都是我干的育叁。 我是一名探鬼主播,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼芍殖,長吁一口氣:“原來是場噩夢啊……” “哼豪嗽!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起围小,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤昵骤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后肯适,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體变秦,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年框舔,在試婚紗的時候發(fā)現(xiàn)自己被綠了蹦玫。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡刘绣,死狀恐怖樱溉,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布我注,位于F島的核電站,受9級特大地震影響挖帘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜恋技,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一拇舀、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蜻底,春花似錦骄崩、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至站楚,卻和暖如春宇弛,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背源请。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人谁尸。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓舅踪,卻偏偏與公主長得像,于是被迫代替她去往敵國和親良蛮。 傳聞我的和親對象是個殘疾皇子抽碌,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355