本次我們使用了如下庫:
相對于U8G2庫, 此庫功能少了很多, 相對的RAM ROM占用也都少,那個繪制進度條是很有亮點的
依然接硬件IIC SCL: 22 SDA: 21
一. 如何使用庫
- 引入庫(我的是IIC接口)
#include "SSD1306Wire.h"
- 實例化一個SSD1306Wire對象
SSD1306Wire display(0x3c, 21, 22);
- 初始化屏幕
display.init();
- 顯示和清除
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)