Arduino OLED Adafruit_SSD1306
本文发布于 123 天前。

Adafruit_SSD1306 是针对 SSD1306这款OLED屏幕的显示图形库。此文主要记录其对应库的调用方式。

0.91寸OLED模块接法


注:SCL实际印刷的是SCK

基础代码

接线

SDAA4SCKA5VCC3.5VGND接地。

代码

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(128, 32, &Wire, OLED_RESET);

void setup()
{
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
    display.setTextColor(WHITE); //开像素点发光
}

void loop()
{
    display.clearDisplay();  //清屏
    display.setTextSize(1);   //设置字体大小
    display.setCursor(35, 5); //设置显示位置
    display.println("Yoshida Yuki");
    display.setTextSize(1);    //设置字体大小
    display.setCursor(40, 20); //设置显示位置
    display.println("Time:");
    display.setTextSize(1);
    display.setCursor(75, 20);
    display.println(millis() / 1000);
    display.drawLine(32, 16, 108, 16, WHITE);
    display.display(); // 开显示
}

库方法(IIC版本)

初始化

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//屏幕分辨率
#define SCREEN_WIDTH 128 // 屏幕宽
#define SCREEN_HEIGHT 64 // 屏幕高
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // 重置PIN口
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

OLED初始化方法

display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

仅仅支持 128X32 128X64 96X16 这三款SSD1306;
此方法必须成功,否则表示整个模块无法使用,无法使用可能就需要检测硬件模块。

显示类方法

display显示内容

display.display();

立即将所有内容绘制到屏幕上。

clearDisplay清除显示

display.clearDisplay();

此方法仅仅是清除Arduino缓存,不会立即显示在display上,可以通过调用display来立即清除;

绘制类方法

绘制类方法分为父类绘制方法(Adafruit_GFX内部实现,子类不需要重写,直接调用)以及子类绘制方法(Adafruit_SSD1306实现)。

Adafruit_GFX 绘制类方法包括:
drawCircle——绘制空心圆
fillCircle——绘制实心圆
drawTriangle——绘制空心三角形
fillTriangle——绘制实心三角形
drawRoundRect——绘制空心圆角方形
fillRoundRect——绘制实心圆角方形
drawBitmap——绘制图片
drawXBitmap
drawGrayscaleBitmap
drawRGBBitmap
drawChar——绘制字符
getTextBounds
setTextSize
setFont
setCursor
setTextColor
setTextWrap

Adafruit_SSD1306 绘制类方法包括:
drawPixel——绘制像素点
drawFastHLine——绘制水平线
drawFastVLine——绘制垂直线
startscrollright
startscrollleft
startscrolldiagright
startscrolldiagleft
stopscroll

drawPixel绘制像素点

drawPixel(int16_t x, int16_t y, uint16_t color);

(x,y)上绘制颜色为color的像素点。
Sample[1]

void loop()
{
    display.clearDisplay();
    for (int i = 1; i < 20; i++){
        display.drawPixel(i*5, 20, WHITE);
    }
    display.display(); // 开显示
}

drawFastHLine绘制水平线

void drawFastHLine(int16_t x, int16_t y, int16_t w, uint16_t color);

(x,y)为起点,w为长度,向右绘制颜色为color的水平线。

w不能为负数。

Sample

void loop()
{
    display.clearDisplay();
    for (int i = 1; i < 20; i++){
        display.drawFastHLine(0, i*5, i*5, WHITE);
    }
    display.display(); // 开显示
}

drawFastVLine绘制垂直线

void drawFastVLine(int16_t x, int16_t y, int16_t h, uint16_t color);

(x,y)为起点,h为高度,向下绘制颜色为color的竖直线。

h不能为负数。

Sample

void loop()
{
    display.clearDisplay();
    for (int i = 1; i < 20; i++){
        display.drawFastVLine(i*5, 5, i*5, WHITE);
    }
    display.display(); // 开显示
}

drawLine绘制线

void drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)

(x0,y0)(x1,y1)绘制颜色为color的线段。
Sample

void loop()
{
    display.clearDisplay();
    for (float i = 1; i < 34; i++){
        float x1 = i/20;
        float y1 = i/20;
        display.drawLine(0,0, 100*sin(x1), 50*cos(y1) ,WHITE);
    }
    display.display(); // 开显示
}

drawRect绘制空心方形

void drawRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);

(x,y)向右下绘制宽高为w,hcolor颜色的空心方形。

源代码主要应用了绘制线方法 writeFastHLinewriteFastVLine

Sample

void loop()
{
    display.clearDisplay();
    for (float i = 1; i < 10; i++){
        display.drawRect(i*5 + 1, i*5 + 1, 5, 5, WHITE);
    }
    display.display(); // 开显示
}

fillRect绘制实心方形

void fillRect(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t color);

(x,y)向右下绘制宽高为w,hcolor颜色的实心方形。

绘制实心方形,代码非常巧妙,一行行调用writeFastVLine即可.

Sample

void loop()
{
    display.clearDisplay();
    for (float i = 1; i < 10; i++){
        display.fillRect(i*5 + 1, i*5 + 1, 5, 5, WHITE);
    }
    display.display(); // 开显示
}

fillScreen绘制填充整个屏幕

fillScreen(uint16_t color);

color填充满整屏。
Sample

void loop()
{
    display.clearDisplay();
    display.fillScreen(WHITE); //白色填充全屏
    for (float i = 1; i < 10; i++){
        display.fillRect(i*5 + 1, i*5 + 1, 5, 5, BLACK); //绘制黑色方形
    }
    display.display(); // 开显示
}

drawCircle绘制空心圆

drawCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);

(x0, y0)为圆心,绘制半径为r的空心圆。
Sample

void loop()
{
    display.clearDisplay();
    display.drawCircle(30,30,20,WHITE);
    display.drawCircle(50,30,20,WHITE);
    display.display(); // 开显示
}

fillCircle绘制实心圆

fillCircle(int16_t x0, int16_t y0, int16_t r, uint16_t color);

(x0, y0)为圆心,绘制半径为r的实心圆。
Sample

void loop()
{
    display.clearDisplay();
    display.drawCircle(30,30,20,WHITE);
    display.fillCircle(50,30,20,WHITE);
    display.display(); // 开显示
}

drawTriangle绘制空心三角形

drawTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);

绘制顶点在(x0,y0) (x1,y1) (x2,y2)三点的颜色为color的空心三角形。
Sample

void loop()
{
    display.clearDisplay();
    display.drawTriangle(10, 10, 40, 10, 25, 40, WHITE);
    display.drawTriangle(30, 40, 45, 10, 60, 40, WHITE);
    display.display(); // 开显示
}

fillTriangle绘制实心三角形

fillTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint16_t color);

绘制顶点在(x0,y0) (x1,y1) (x2,y2)三点的颜色为color的实心三角形。
Sample

void loop()
{
    display.clearDisplay();
    display.drawTriangle(10, 10, 40, 10, 25, 40, WHITE);
    display.fillTriangle(30, 40, 45, 10, 60, 40, WHITE);
    display.display(); // 开显示
}

drawRoundRect绘制空心圆角矩形

fillRoundRect绘制实心圆角矩形

drawRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color);
fillRoundRect(int16_t x, int16_t y, int16_t w, int16_t h, int16_t r, uint16_t color);

(x,y)绘制宽高为w,h,倒角半径rcolor色矩形。
Sample

void loop()
{
    display.clearDisplay();
    display.drawRoundRect(10, 10, 30, 30, 7, WHITE);
    display.fillRoundRect(20, 20, 30, 30, 7, WHITE);
    display.display(); // 开显示
}

drawBitmap绘制Bitmap图形

// 图形内容从 PROGMEM区 来
drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color);
drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color, uint16_t bg);
// 图形内容从 RAM区 来
drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color);
drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg);

(x,y)位置,绘制宽高为x,y的位图bitmap,颜色为color,*背景颜色为bg
Sample

获取位图数据

使用Photoshop等工具绘制好单色位图,并使用工具转换为bitmap代码。
推荐在线工具:Marlin – Bitmap Converter

代码

/*
 * OLED显示
 */
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define OLED_RESET 4
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);

/**
 * Made with Marlin Bitmap Converter
 * https://marlinfw.org/tools/u8glib/converter.html
 *
 * This bitmap from the file '未标题-1.bmp'
 */
#pragma once

#define CUSTOM_BOOTSCREEN_BMPWIDTH  128

const unsigned char img[] PROGMEM = {
  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xF8,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  0xFF,0xFF,0xFF,0xFF,0xFF,0xF0,0x00,0x00,0x00,0x00,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,
  0xFF,0xFF,0xFF,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,
  0xFF,0xFF,0xFF,0xFF,0xC0,0x00,0x0F,0xFF,0xFF,0x80,0x00,0x1F,0xFF,0xFF,0xFF,0xFF,
  0xFF,0xFF,0xFF,0xFC,0x00,0x0F,0xFF,0xFF,0xFF,0xFF,0x80,0x01,0xFF,0xFF,0xFF,0xFF,
  0xFF,0xFF,0xFF,0xE0,0x01,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x00,0x3F,0xFF,0xFF,0xFF,
  0xFF,0xFF,0xFF,0x00,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xE0,0x07,0xFF,0xFF,0xFF,
  0xFF,0xFF,0xFC,0x03,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0x01,0xFF,0xFF,0xFF,
  0xFF,0xFF,0xF0,0x0F,0xFF,0xFF,0xFF,0xF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,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};


void setup()
{
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //初始化
    display.setTextColor(WHITE); //开像素点发光
}

void loop()
{
    display.clearDisplay();
    display.drawBitmap(0, 0, img, 128, 64, WHITE); //创建位图
    display.display(); // 开显示
}

drawXBitmap绘制XBitmap图形

DawXBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color);

类似Bitmap,使用较少,不作例子。

文字类方法

drawChar绘制单个字母

drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size);
drawChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, uint8_t size_x, uint8_t size_y);

从字母左上角/左下角坐标(x,y)绘制一个字母c,颜色为color,底色bg,放大倍数size,或x,y上独立放大倍数size_x, size_y
Sample

void loop()
{
    display.clearDisplay();
    display.drawChar(20,20,'Y',WHITE,BLACK,2);
    display.drawChar(33,20,'A',BLACK,WHITE,1,2);
    display.display(); // 开显示
}

setTextSize设置字体大小

setTextSize(uint8_t s);

设置文字大小为s

setFont设置字体

setFont(const GFXfont *f); // 使用一个字体
setFont(); // 恢复默认字体

Sample

#include <Fonts/FreeMonoBoldOblique12pt7b.h>

void loop()
{
    display.clearDisplay();
    display.setFont(&FreeMonoBoldOblique12pt7b); // 设置自定义字体
    display.drawChar(40,40,'Y',WHITE,BLACK,1);
    display.setFont(); // 重置字体
    display.drawChar(54,34,'e',WHITE,BLACK,1);
    display.drawChar(60,34,'s',WHITE,BLACK,1);
    display.display(); // 开显示
}

getTextBounds获取文字边界

getTextBounds(const String &str, int16_t x, int16_t y, int16_t *x1, int16_t *y1, uint16_t *w, uint16_t *h);

不知道怎么用

setCursor设置打印光标位置

setCursor(int16_t x, int16_t y);

将光标移动到(x,y)

setTextColor设置字体颜色

setTextColor(uint16_t c);

设置字体颜色为c

通常颜色均为WHITEBLACK

setTextWrap设置是否自动换行

setTextWrap(boolean w);

当字符串太长以致一行无法显示,设置是否自动换行。这个函数是全局生效的。

println按行打印文字序列

println(const String &str);

Sample

void loop()
{
    display.clearDisplay();
    display.setTextSize(1);
    display.setCursor(30,20);
    display.println("Yoshida Yuki 0625");
    display.setCursor(45,40);
    display.setTextWrap(false);
    display.println("Yoshida Yuki 0625");
    display.display(); // 开显示
}

滚动类方法

startscrollright向右滚动

startscrollright(uint8_t start, uint8_t stop);

start行到stop行向右滚动。

此处的行数既不是像素数,也不是第几行。
Sample

void loop()
{
    display.clearDisplay();
    display.setTextSize(1);
    display.setCursor(5,1);
    display.println("Yoshida Yuki 0625");
    display.setCursor(5,11);
    display.println("Yoshida Yuki 0625");
    display.setCursor(5,21);
    display.println("Yoshida Yuki 0625");
    display.setCursor(5,31);
    display.println("Yoshida Yuki 0625");
    display.display(); // 开显示
    display.startscrollright(0, 5);
    delay(5000);
    display.stopscroll();

startscrollleft向左滚动

startscrollleft(uint8_t start, uint8_t stop);

start行到stop行向左滚动。

此处的行数既不是像素数,也不是第几行。
Sample

void loop()
{
    display.clearDisplay();
    display.setTextSize(1);
    display.setCursor(5,1);
    display.println("Yoshida Yuki 0625");
    display.setCursor(5,11);
    display.println("Yoshida Yuki 0625");
    display.setCursor(5,21);
    display.println("Yoshida Yuki 0625");
    display.setCursor(5,31);
    display.println("Yoshida Yuki 0625");
    display.display(); // 开显示
    display.startscrollleft(0, 5);
    delay(5000);
    display.stopscroll();
}

startscrolldiagright向斜对角滚动

startscrolldiagright(uint8_t start, uint8_t stop);
startscrolldiagleft(uint8_t start, uint8_t stop);

Sample

void loop()
{
    display.clearDisplay();
    display.setTextSize(1);
    display.setCursor(5,1);
    display.println("Yoshida Yuki 0625");
    display.setCursor(5,11);
    display.println("Yoshida Yuki 0625");
    display.setCursor(5,21);
    display.println("Yoshida Yuki 0625");
    display.setCursor(5,31);
    display.println("Yoshida Yuki 0625");
    display.display(); // 开显示
    display.startscrolldiagright(0, 5);
    delay(2000);
    display.startscrolldiagleft(0, 5);
    delay(2000);
    display.stopscroll();
}

stopscroll停止滚动

stopscroll(void);

屏幕参数类方法

width获取屏幕宽度

height获取屏幕高度

width(void);
height(void);

getRotation获取屏幕旋转角度

getCursorX获取坐标x

getCursorY获取坐标Y

参考

  1. ^所有的示例图片使用wokwi仿真绘制,屏幕尺寸128×64
标题:Arduino OLED Adafruit_SSD1306
作者:IKK
除转载和特殊声明外,所有文章采用 CC BY-NC-SA 4.0协议
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇