玩转 ESP32 + Arduino 25.SSD1306库驱动OLED

本次我们使用了如下库:


相对于U8G2库, 此库功能少了很多, 相对的RAM ROM占用也都少,那个绘制进度条是很有亮点的
依然接硬件IIC SCL: 22 SDA: 21

一. 如何使用库

引入库(我的是IIC接口)
#include "SSD1306Wire.h"
实例化一个SSD1306Wire对象
SSD1306Wire display(0x3c, 21, 22);
初始化屏幕
display.init();
显示和清除
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是角度


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)

————————————————
链接:https://blog.csdn.net/finedayforu/article/details/108769900
- 本文内容来自网络,如有侵权,请联系本站处理。

2023-09   阅读(70)   评论(0)
 标签: 创客 ESP32 Arduino

涨知识
伺服电机

伺服电机(servo motor )是指在伺服系统中控制机械元件运转的发动机,是一种补助马达间接变速装置。

评论:
相关文章
ESP-Hosted 入门介绍 &使用指南

ESP-Hosted 解决方案提供了将 ESP 板用作 Wi-Fi 和 Bluetooth/BLE 连接的通信处理器的方法。


设备上云太麻烦?ESP-Hosted一站触达!

ESP-Hosted 提供了一种将ESP芯片和模组用作通信协处理器的解决方案,该解决方案为主机微处理器或微控制器提供无线连接,使主机能够与其他设备通信。简单来说为网卡方案。


ESP32 + Arduino使用TFT_eSPI库

Arduino+ESP32上使用TFT_eSPI库快速点亮这个屏幕,驱动芯片ST7789


ESP32 利用 SPI 连通 TFT 彩屏

本文给出了一个ESP32与SPI 接口TFT显示屏接线的详细说明,供大家参考。


在Micropython下使用ESPNow功能进行数据传输

本文讲解如何在Micropython环境下使用ESP32的ESPNow功能进行数据传输。


用 ESP32-S3 打造多功能 USB Dongle

ESP-Dongle 是一款基于 ESP32-S3 芯片开发的多功能 USB Device 解决方案。它不仅外形小巧,功能齐全,更集成了无线 U 盘、SD 卡读取以及 USB 无线网卡等多项功能。


利用 ESP32-S3 和 CSI 技术打造智能家居

ESP32 系列芯片可以利用 CSI 数据实现动作检测和存在检测。无论是自动调节灯光、风扇,还是节能控制,CSI 技术为智能家居带来了新的可能性。随着 CSI 技术的发展,未来的智能家居将能够更精确地感知和响应我们的行为,实现更高效、更人性化的控制。


ESP32-FreeRTOS:大量FreeRTOS实例,供参考学习

ESP32-FreeRTOS项目提供了丰富的示例,帮助开发者快速掌握ESP32的硬件功能和FreeRTOS实时操作系统。


物联网项目开发实战-第3章-自动浇花项目迭代3

本节我们在迭代二的基础上使用四位数码管和OLED显示屏显示相关交互信息。


物联网项目开发实战-第3章-自动浇花项目迭代2

本节我们在迭代一的基础上增加采集土壤湿度数据,并根据湿度数据来决定是否自动进行浇水动作。