风筝
发表于: 2022-11-4 21:15:16 | 显示全部楼层

是否想在您的ESP8266物联网项目中添加小图形?或者,也许您想显示ESP8266的IP地址,而无需通过串口输出。这些超酷的OLED显示屏可能是完美的选择!它们具有超级薄的尺寸,并生成更明亮清晰的图片。


OLED显示模块概述

OLED(Organic Light-Emitting Diode)显示模块实际上是一个小的单色OLED显示屏。宽128像素,高64像素,0.96英寸。 由于对比度很高,OLED显示屏可读性特别强,并且您可以在其中放置大量的图形。


由于显示器可以自身发光,因此不需要背光。这大大降低了运行OLED所需的功耗,这就是为什么显示屏具有如此高的对比度,非常宽的视角并可以显示深黑级别的原因。


该模块的核心是功能强大的单芯片CMOS OLED驱动器控制器SSD1306,它可以处理所有RAM缓冲,因此您的ESP8266几乎不需要完成工作。SSD1306控制器的工作电压从1.65V到3.3V,非常适合与ESP8266连接。


OLED存储器内存图

为了绝对控制您的OLED显示模块,重要的是要了解其内存图。


不管OLED模块的大小如何,SSD1306驱动程序的内置1KB图形显示数据RAM(GDDRAM),可容纳要显示的位模式。这个1K内存区域分为8页(从0到7)。每个页面包含128列/段(0至127个块)。每列可以存储8位数据(0到7)。


下面突出显示了整个1K内存的页面、段和数据位。

1KB-128x64-OLED-Display-RAM-Memory-Map.jpg


每个位代表屏幕上特定的OLED像素,可以通过编程打开或关闭。


OLED显示模块与ESP8266 NodeMCU的硬件连接

理论上足够了,让我们去实际操作!将显示屏连接到ESP8266 NodeMCU。


连接非常简单。首先将VCC引脚连接到NodeMCU上的3.3V输出,然后将GND连接到地。接下来,将SCL引脚连接到NodeMCU上的I2C时钟D1引脚,然后将SDA引脚连接到NodeMCU上的I2C数据D2引脚。


下图显示了如何连接所有内容。

Fritzing-Wiring-OLED-Display-with-ESP8266-NodeMCU.jpg

这样,您现在就可以上传一些代码并可以在显示屏打印输出。


为OLED显示模块安装库

OLED显示器的SSD1306控制器具有灵活但复杂的驱动程序。为了使用SSD1306控制器,需要大量有关存储器地址的知识。幸运的是,Adafruit的SSD1306库被编写为隐藏SSD1306控制器的复杂性,以便我们可以发布简单的命令来控制显示屏。


要安装库导航到Sketch > Include Library > Manage Libraries…等待Library Manager下载库索引和更新已安装的库的列表。

输入Adafruit SSD1306筛选搜索结果。应该有几个条目。寻找Adafruit的Adafruit SSD1306。单击该条目,然后选择Install。

Installing-Adafruit-SSD1306-Monochrome-OLED-Display-Library.jpg


此AdaFruit SSD1306库是一个特定于硬件的库,可处理较低级别的功能。它需要与Adafruit GFX库配对,以显示图形原始图,例如点、线、圆、矩形等。

Installing-Adafruit-GFX-Graphics-Core-Library.jpg


修改Adafruit SSD1306库

Adafruit的SSD1306库未设置为128×64 OLED显示屏。必须在Adafruit_ssd1306.h头文件中更改显示大小,以使其对我们有用。如果没有更改,则尝试验证Arduino IDE中的示例草图时可能会出现:一条错误消息“#error(“Height incorrect, please fix Adafruit_SSD1306.h!)”;

Height-incorrect-please-fix-Adafruit_SSD1306.h-Error.jpg


为了更改ADAFRUIT_SSD1306.H头文件,请打开库文件位置。通常是我的文档> Arduino。现在转到libraries > Adafruit_SSD1306

Adafruit-SSD1306-Library-Location.jpg


在文本编辑器中打开adafruit_ssd1306.h文件。向下滚动文件以找到SSD1306 Displays的部分,或直接转到第73行。注掉释#Define SSD1306_128_32和取消注释 #DEFINE SSD1306_128_64,本节中的代码看起来像这样:

Remove-Height-Incorrect-Error-By-Changing-Adafruit_SSD1306.h-File.jpg

现在保存文件并重新启动Arduino IDE。

跳转到指定楼层
风筝
发表于: 2022-11-4 21:48:28 | 显示全部楼层

ESP8266代码 - 显示文本

以下草图将在显示屏上打印“ Hello World!”。 它还包括

●    显示反白文本

●    显示数字

●    以进制显示数字(十六进制,十进制)

●    显示ASCII符号

●    水平和垂直滚动文本

●    滚动显示屏的部分文本


这将使您完全了解如何使用OLED显示屏,并可以作为更实际的实验和项目的基础。

  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SSD1306.h>

  5. Adafruit_SSD1306 display(-1);

  6. void setup()   
  7. {               
  8.         // initialize with the I2C addr 0x3C
  9.         display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  

  10.         // Clear the buffer.
  11.         display.clearDisplay();

  12.         // Display Text
  13.         display.setTextSize(1);
  14.         display.setTextColor(WHITE);
  15.         display.setCursor(0,28);
  16.         display.println("Hello world!");
  17.         display.display();
  18.         delay(2000);
  19.         display.clearDisplay();

  20.         // Display Inverted Text
  21.         display.setTextColor(BLACK, WHITE); // 'inverted' text
  22.         display.setCursor(0,28);
  23.         display.println("Hello world!");
  24.         display.display();
  25.         delay(2000);
  26.         display.clearDisplay();

  27.         // Changing Font Size
  28.         display.setTextColor(WHITE);
  29.         display.setCursor(0,24);
  30.         display.setTextSize(2);
  31.         display.println("Hello!");
  32.         display.display();
  33.         delay(2000);
  34.         display.clearDisplay();

  35.         // Display Numbers
  36.         display.setTextSize(1);
  37.         display.setCursor(0,28);
  38.         display.println(123456789);
  39.         display.display();
  40.         delay(2000);
  41.         display.clearDisplay();

  42.         // Specifying Base For Numbers
  43.         display.setCursor(0,28);
  44.         display.print("0x"); display.print(0xFF, HEX);
  45.         display.print("(HEX) = ");
  46.         display.print(0xFF, DEC);
  47.         display.println("(DEC)");
  48.         display.display();
  49.         delay(2000);
  50.         display.clearDisplay();

  51.         // Display ASCII Characters
  52.         display.setCursor(0,24);
  53.         display.setTextSize(2);
  54.         display.write(3);
  55.         display.display();
  56.         delay(2000);
  57.         display.clearDisplay();

  58.         // Scroll full screen
  59.         display.setCursor(0,0);
  60.         display.setTextSize(1);
  61.         display.println("Full");
  62.         display.println("screen");
  63.         display.println("scrolling!");
  64.         display.display();
  65.         display.startscrollright(0x00, 0x07);
  66.         delay(2000);
  67.         display.stopscroll();
  68.         delay(1000);
  69.         display.startscrollleft(0x00, 0x07);
  70.         delay(2000);
  71.         display.stopscroll();
  72.         delay(1000);   
  73.         display.startscrolldiagright(0x00, 0x07);
  74.         delay(2000);
  75.         display.startscrolldiagleft(0x00, 0x07);
  76.         delay(2000);
  77.         display.stopscroll();
  78.         display.clearDisplay();

  79.         // Scroll part of the screen
  80.         display.setCursor(0,0);
  81.         display.setTextSize(1);
  82.         display.println("Scroll");
  83.         display.println("some part");
  84.         display.println("of the screen.");
  85.         display.display();
  86.         display.startscrollright(0x00, 0x00);
  87. }

  88. void loop() {}
复制代码

草图首先包括四个库文件:spi.h、wire.h、adafruit_gfx.h和adafruit_ssd1306.h。尽管I2C OLED显示器不需要SPI.H库,但我们需要为了编译程序而添加它。

  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SSD1306.h>
复制代码

接下来,我们需要创建一个adafruit_ssd1306.h的对象。ADAFRUIT_SSD1306构造函数接受ESP8266引脚编号,该引脚连接了显示器的复位引脚。由于我们使用的OLED显示器没有复位引脚,因此我们将发送–1到构造函数,因此ESP8266引脚都没有用作显示器的复位。

  1. Adafruit_SSD1306 display(-1);
复制代码

在setup()函数中,我们需要使用begin()函数初始化OLED对象。该函数需要两个参数。第一个参数SSD1306_SWITCHCAPVCC打开内部电荷泵电路,而第二个参数则提供OLED显示屏的I2C地址。此类OLED显示模块的I2C地址通常为0x3c。它是固定的,不能更改。


接下来,我们在屏幕上打印第一条消息之前清除缓冲区。

  1. // initialize with the I2C addr 0x3C
  2. display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  3. // Clear the buffer.
  4. display.clearDisplay();
复制代码

显示简单文本(Hello World)

  1.         
  2. // Display Text
  3. display.clearDisplay();
  4. display.setTextSize(1);
  5. display.setTextColor(WHITE);
  6. display.setCursor(0,28);
  7. display.println("Hello world!");
  8. display.display();
  9. delay(2000);
复制代码

要在屏幕上显示文本,我们需要设置字体大小。这可以通过调用setTextSize(font-size)和传递字体大小(从1开始)作为参数来完成。


接下来,我们需要通过调用setTextColor(color)来设置字体颜色。将参数WHITE用于黑暗背景,并将BLACK传递给明亮的背景。现在,在打印消息之前,我们需要通过调用函数setCursor(X,Y)来设置光标位置。屏幕上的像素通过其水平(x)和垂直(y)坐标来解决。坐标系将原点(0,0)放在左上角,正x向右增加,正y向下增加。


我们可以使用简单的print(“ ”)或println(“ ”)函数在屏幕上打印消息,就像我们在串口监视器上打印数据一样。请记住,println()将将光标移至新行。


为了使库在屏幕缓冲区上执行极快的数学操作(每秒超过100帧),对print函数的调用不会立即将屏幕缓冲区的内容传输到SSD1306控制器。需要一个display()命令来指示库执行从ESP8266中的屏幕缓冲区进行批量传输到SSD1306控制器的内部内存。一旦传输内存,与屏幕缓冲区相对应的像素将出现在OLED显示屏上。


显示反白文字

  1. // Display Inverted Text
  2. display.clearDisplay();
  3. display.setTextColor(BLACK, WHITE); // 'inverted' text
  4. display.setCursor(0,28);
  5. display.println("Hello world!");
  6. display.display();
  7. delay(2000);
复制代码

要显示反白文本,我们将再次调用setTextColor(FontColor,BackgroundColor) 函数。如果您要注意,您知道我们只将一个参数传递给了此函数,但是现在我们传递了两个参数。这是可能的,因为所谓的函数重载。函数重载是能够创建同名的多个函数,但具有不同的参数集。对重载函数的调用将根据传递的参数运行该函数的特定实现。


示例中通过setTextColor(BLACK, WHITE)将在填充的背景上呈现黑色文本。


缩放字体尺寸

  1.         
  2. // Changing Font Size
  3. display.clearDisplay();
  4. display.setTextColor(WHITE);
  5. display.setCursor(0,24);
  6. display.setTextSize(2);
  7. display.println("Hello!");
  8. display.display();
  9. delay(2000);
复制代码

我们调用setTextSize(font-size)函数设置字体大小,并将1作为参数传递。您可以使用此函数通过传递任何非负整数来缩放字体。


字符以7:10的比率渲染。含义,传递字体大小1将以每个字符的7×10像素为单位,传递2将以每个字符的14×20像素等形式渲染文本。


ADAFRUIT_GFX库负责渲染字体。默认情况下,选择了单间字体。但是,Adafruit GFX库的最新版本提供了使用替代字体的功能。库附带了几种备用字体,此外还有添加新字体。


显示数字

  1.         
  2. // Display Numbers
  3. display.clearDisplay();
  4. display.setTextSize(1);
  5. display.setCursor(0,28);
  6. display.println(123456789);
  7. display.display();
  8. delay(2000);
复制代码

数字可以通过调用print()或println()函数在OLED显示器上显示。这些函数的重载实现接受了32位无符号整型数,因此您只能显示0到4,294,967,295。


指定数字的进制

  1. // Specifying Base For Numbers
  2. display.clearDisplay();
  3. display.setCursor(0,28);
  4. display.print("0x"); display.print(0xFF, HEX);
  5. display.print("(HEX) = ");
  6. display.print(0xFF, DEC);
  7. display.println("(DEC)");
  8. display.display();
  9. delay(2000);
复制代码

print()println()函数具有可选的第二个参数,该参数指定要使用的进制(格式);允许的值是BIN(二进制)、OCT(八进制)、DEC(十进制)、HEX(十六进制)。对于浮点数,此参数指定使用的小数位数。例如:

●    print(78, BIN)显示“ 1001110”

●    print(78, OCT)显示“ 116”

●    print(78, DEC) 显示“ 78”

●    print(78, HEX) 显示“ 4E”

●    println(1.23456, 0) 显示“ 1”

●    println(1.23456, 2) 显示“ 1.23”

●    println(1.23456, 4) 显示“ 1.2346”


显示ASCII符号

  1.         
  2. // Display ASCII Characters
  3. display.clearDisplay();
  4. display.setCursor(0,24);
  5. display.setTextSize(2);
  6. display.write(3);
  7. display.display();
  8. delay(2000);
复制代码

print()和println()函数以可读的ASCII文本形式将数据发送到显示屏,而write()函数将二进制数据发送到显示屏。因此,您可以使用此函数显示ASCII符号。在我们的示例中,发送编号3将显示心脏符号。


全屏滚动

  1. // Scroll full screen
  2. display.clearDisplay();
  3. display.setCursor(0,0);
  4. display.setTextSize(1);
  5. display.println("Full");
  6. display.println("screen");
  7. display.println("scrolling!");
  8. display.display();
  9. display.startscrollright(0x00, 0x07);
  10. delay(2000);
  11. display.stopscroll();
  12. delay(1000);
  13. display.startscrollleft(0x00, 0x07);
  14. delay(2000);
  15. display.stopscroll();
  16. delay(1000);   
  17. display.startscrolldiagright(0x00, 0x07);
  18. delay(2000);
  19. display.startscrolldiagleft(0x00, 0x07);
  20. delay(2000);
  21. display.stopscroll();
复制代码

您可以通过调用startscrollright(start page, stop page)startscrollleft(start page, stop page)函数来水平滚动显示,调用startscrolldiagright(start page, stop page)startscrolldiagleft(start page, stop page)垂直滚动。所有这些函数都接受两个参数,即启动页面和停止页面。由于显示屏中从0到7的八页,因此您可以通过滚动所有页面(即传递参数0x00和0x07)滚动整个屏幕。


要停止显示滚动,您可以使用stopscroll()函数。


滚动特定部分

  1.         
  2. // Scroll part of the screen
  3. display.setCursor(0,0);
  4. display.setTextSize(1);
  5. display.println("Scroll");
  6. display.println("some part");
  7. display.println("of the screen.");
  8. display.display();
  9. display.startscrollright(0x00, 0x00);
复制代码

有时我们不想滚动整个显示屏。您可以通过传递适当的启动页面并停止页面信息来滚动函数来做到这一点。由于显示屏中从0到7的八页,因此您可以通过将特定的页码作为参数传递到显示屏的某些部分。


在示例中,我们将0x00作为两个参数传递。这将仅滚动显示显示的第一页(前8行)。

回复

使用道具 举报

风筝
发表于: 2022-11-4 21:58:15 | 显示全部楼层

ESP8266代码 - 基本图形

在此示例中,我们将尝试一些基础图形。 该草图展示了许多图形函数,包括矩形、圆形矩形、圆圈和三角形。 尝试草图,然后我们将其详细介绍。

  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SSD1306.h>

  5. Adafruit_SSD1306 display(-1);

  6. void setup()   
  7. {               
  8.         // initialize with the I2C addr 0x3C
  9.         display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  

  10.         // Clear the buffer.
  11.         display.clearDisplay();

  12.         display.setTextSize(1);
  13.         display.setTextColor(WHITE);
  14.         display.setCursor(0,0);
  15.         display.println("Rectangle");
  16.         display.drawRect(0, 15, 60, 40, WHITE);
  17.         display.display();
  18.         delay(2000);
  19.         display.clearDisplay();

  20.         display.setTextSize(1);
  21.         display.setTextColor(WHITE);
  22.         display.setCursor(0,0);
  23.         display.println("Filled Rectangle");
  24.         display.fillRect(0, 15, 60, 40, WHITE);
  25.         display.display();
  26.         delay(2000);
  27.         display.clearDisplay();

  28.         display.setTextSize(1);
  29.         display.setTextColor(WHITE);
  30.         display.setCursor(0,0);
  31.         display.println("Round Rectangle");
  32.         display.drawRoundRect(0, 15, 60, 40, 8, WHITE);
  33.         display.display();
  34.         delay(2000);
  35.         display.clearDisplay();

  36.         display.setTextSize(1);
  37.         display.setTextColor(WHITE);
  38.         display.setCursor(0,0);
  39.         display.println("Filled Round Rectangl");
  40.         display.fillRoundRect(0, 15, 60, 40, 8, WHITE);
  41.         display.display();
  42.         delay(2000);
  43.         display.clearDisplay();

  44.         display.setTextSize(1);
  45.         display.setTextColor(WHITE);
  46.         display.setCursor(0,0);
  47.         display.println("Circle");
  48.         display.drawCircle(20, 35, 20, WHITE);
  49.         display.display();
  50.         delay(2000);
  51.         display.clearDisplay();

  52.         display.setTextSize(1);
  53.         display.setTextColor(WHITE);
  54.         display.setCursor(0,0);
  55.         display.println("Filled Circle");
  56.         display.fillCircle(20, 35, 20, WHITE);
  57.         display.display();
  58.         delay(2000);
  59.         display.clearDisplay();

  60.         display.setTextSize(1);
  61.         display.setTextColor(WHITE);
  62.         display.setCursor(0,0);
  63.         display.println("Triangle");
  64.         display.drawTriangle(30, 15, 0, 60, 60, 60, WHITE);
  65.         display.display();
  66.         delay(2000);
  67.         display.clearDisplay();

  68.         display.setTextSize(1);
  69.         display.setTextColor(WHITE);
  70.         display.setCursor(0,0);
  71.         display.println("Filled Triangle");
  72.         display.fillTriangle(30, 15, 0, 60, 60, 60, WHITE);
  73.         display.display();
  74.         delay(2000);
  75.         display.clearDisplay();
  76. }

  77. void loop() {}
复制代码

大多数代码(包括库和初始化显示)与上述代码示例相同,除了以下代码段绘制基本图纸。


绘制矩形

  1. display.clearDisplay();
  2. display.setTextSize(1);
  3. display.setTextColor(WHITE);
  4. display.setCursor(0,0);
  5. display.println("Rectangle");
  6. display.drawRect(0, 15, 60, 40, WHITE);
  7. display.display();
  8. delay(2000);

  9. display.clearDisplay();  
  10. display.setTextSize(1);
  11. display.setTextColor(WHITE);
  12. display.setCursor(0,0);
  13. display.println("Filled Rectangle");
  14. display.fillRect(0, 15, 60, 40, WHITE);
  15. display.display();
  16. delay(2000);
复制代码

您可以使用drawRect(X-coordinate, Y-coordinate, Width, Height, color)函数在显示屏上绘制矩形。实际上,此函数绘制了具有1像素边框的空心矩形。您可以使用fillRect()函数绘制填充的矩形。


绘制圆形矩形

  1. display.clearDisplay();
  2. display.setTextSize(1);
  3. display.setTextColor(WHITE);
  4. display.setCursor(0,0);
  5. display.println("Round Rectangle");
  6. display.drawRoundRect(0, 15, 60, 40, 8, WHITE);
  7. display.display();
  8. delay(2000);

  9. display.clearDisplay();  
  10. display.setTextSize(1);
  11. display.setTextColor(WHITE);
  12. display.setCursor(0,0);
  13. display.println("Filled Round Rectangl");
  14. display.fillRoundRect(0, 15, 60, 40, 8, WHITE);
  15. display.display();
  16. delay(2000);
复制代码

您可以使用drawRoundRect(X-coordinate, Y-coordinate, Width, Height, color)函数在显示屏上绘制圆形矩形。此函数采用与drawRect()函数相同的参数,除了一个附加参数 - 转角舍入半径。实际上,此函数绘制了具有1像素边框的空心圆形矩形。您可以使用fillRoundRect()函数绘制填充的圆形矩形。


绘制圆形

  1. display.clearDisplay();
  2. display.setTextSize(1);
  3. display.setTextColor(WHITE);
  4. display.setCursor(0,0);
  5. display.println("Circle");
  6. display.drawCircle(20, 35, 20, WHITE);
  7. display.display();
  8. delay(2000);

  9. display.clearDisplay();
  10. display.setTextSize(1);
  11. display.setTextColor(WHITE);
  12. display.setCursor(0,0);
  13. display.println("Filled Circle");
  14. display.fillCircle(20, 35, 20, WHITE);
  15. display.display();
  16. delay(2000);
复制代码

您可以使用drawCircle(X-coordinate of center, Y-coordinate of center, radius, color)函数在显示屏上绘制一个圆形。该函数用1像素边框绘制空心圆圈。您可以使用fillCircle()函数绘制填充的圆圈。


绘制三角形

  1. display.clearDisplay();
  2. display.setTextSize(1);
  3. display.setTextColor(WHITE);
  4. display.setCursor(0,0);
  5. display.println("Triangle");
  6. display.drawTriangle(30, 15, 0, 60, 60, 60, WHITE);
  7. display.display();
  8. delay(2000);

  9. display.clearDisplay();
  10. display.setTextSize(1);
  11. display.setTextColor(WHITE);
  12. display.setCursor(0,0);
  13. display.println("Filled Triangle");
  14. display.fillTriangle(30, 15, 0, 60, 60, 60, WHITE);
  15. display.display();
  16. delay(2000);
复制代码

您可以使用drawTriangle(x0, y0, x1, y1, x2, y2, color)函数在显示屏上绘制三角形。该函数采用七个参数,即3个X和y三角形和颜色顶点的坐标。 (x0,y0)代表顶顶点,(x1,y1)表示左顶点,(x2,y2)代表右顶点。


此函数用1像素边框绘制空心三角形。您可以使用fillTriangle()函数绘制填充的三角形。

回复

使用道具 举报

风筝
发表于: 2022-11-4 22:04:28 | 显示全部楼层

ESP8266 NODEMCU代码 - 显示位图图像

最后一个示例显示了如何将位图图像绘制到OLED显示屏。这对于创建公司图标的启动界面很有用,制作精灵或仅创建有趣的图形来显示信息。 复制以下代码,将其粘贴到Arduino IDE中,然后单击上传。

  1. #include <SPI.h>
  2. #include <Wire.h>
  3. #include <Adafruit_GFX.h>
  4. #include <Adafruit_SSD1306.h>

  5. Adafruit_SSD1306 display(-1);

  6. // Bitmap of MarilynMonroe Image
  7. const unsigned char MarilynMonroe [] PROGMEM = {
  8.         0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  9.         0xff, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x1f, 0xff, 0xff, 0xf0, 0x41, 0xff, 0xff, 0xff, 0xff, 0xff,
  10.         0xff, 0xff, 0xff, 0xff, 0xff, 0x80, 0x7f, 0xff, 0xff, 0xf8, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff,
  11.         0xff, 0xff, 0xff, 0xff, 0xff, 0xf9, 0xff, 0xff, 0xff, 0xe0, 0x07, 0xff, 0xff, 0xff, 0xff, 0xff,
  12.         0xff, 0xff, 0xff, 0xff, 0xff, 0x87, 0xff, 0xff, 0xff, 0xf8, 0x03, 0xff, 0xff, 0xff, 0xff, 0xff,
  13.         0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0xff, 0xff, 0xff, 0xf8, 0x01, 0xf1, 0xff, 0xff, 0xff, 0xff,
  14.         0xff, 0xff, 0xff, 0xff, 0xff, 0x9f, 0xff, 0xff, 0xff, 0xf8, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff,
  15.         0xff, 0xff, 0xff, 0xff, 0xff, 0xbf, 0xff, 0xff, 0xff, 0xfc, 0x02, 0x78, 0x7f, 0xff, 0xff, 0xff,
  16.         0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x3f, 0xff, 0xff, 0xfe, 0x03, 0x7c, 0x1f, 0xff, 0xff, 0xff,
  17.         0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x07, 0xff, 0xff, 0xfe, 0x01, 0xfe, 0x1f, 0xff, 0xff, 0xff,
  18.         0xff, 0xff, 0xff, 0xff, 0xfd, 0xe0, 0x03, 0xff, 0xff, 0xfc, 0x00, 0xfe, 0x0f, 0xff, 0xff, 0xff,
  19.         0xff, 0xff, 0xff, 0xff, 0xfe, 0x87, 0xe0, 0xff, 0xff, 0xfc, 0x00, 0x06, 0x07, 0xff, 0xff, 0xff,
  20.         0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xf9, 0xff, 0xff, 0xfc, 0x00, 0x02, 0x07, 0xff, 0xff, 0xff,
  21.         0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xff, 0xfc, 0x00, 0xc3, 0xc3, 0xff, 0xff, 0xff,
  22.         0xff, 0xff, 0xff, 0xff, 0xf0, 0x3f, 0xff, 0xff, 0xe0, 0x0c, 0x00, 0xe7, 0x81, 0xff, 0xff, 0xff,
  23.         0xff, 0xff, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0xff, 0xe0, 0x02, 0x00, 0x02, 0x00, 0xff, 0xff, 0xff,
  24.         0xff, 0xff, 0xff, 0xff, 0xf0, 0x0f, 0xff, 0xff, 0xe0, 0x01, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff,
  25.         0xff, 0xff, 0xff, 0xff, 0x80, 0x00, 0x3f, 0xff, 0xff, 0xe0, 0x00, 0x00, 0x1e, 0x3f, 0xff, 0xff,
  26.         0xff, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x0f, 0xff, 0x3f, 0xf8, 0x00, 0x18, 0x7f, 0x1f, 0xff, 0xff,
  27.         0xff, 0xff, 0xff, 0xf8, 0x01, 0x80, 0x03, 0xfc, 0x3f, 0xfc, 0x00, 0x70, 0xfe, 0x1f, 0xff, 0xff,
  28.         0xff, 0xff, 0xff, 0xf0, 0x43, 0xff, 0xff, 0xf8, 0x7f, 0xf8, 0x00, 0x00, 0x7e, 0x1f, 0xff, 0xff,
  29.         0xff, 0xff, 0xff, 0xe0, 0x07, 0xff, 0xff, 0xf0, 0xff, 0xfc, 0x00, 0x00, 0x7c, 0x3f, 0xff, 0xff,
  30.         0xff, 0xff, 0xff, 0xe0, 0x0f, 0xff, 0xff, 0xf1, 0xef, 0xf8, 0x00, 0x01, 0xfc, 0x3f, 0xff, 0xff,
  31.         0xff, 0xff, 0xff, 0xe4, 0xff, 0xff, 0xff, 0xf3, 0x80, 0xa0, 0x00, 0x07, 0xfc, 0xaf, 0xff, 0xff,
  32.         0xff, 0xff, 0xff, 0xec, 0x5f, 0xff, 0xff, 0xe7, 0xf0, 0x00, 0x00, 0x03, 0xfe, 0xdf, 0xff, 0xff,
  33.         0xff, 0xff, 0xff, 0xee, 0x7f, 0xff, 0xff, 0xc7, 0xf8, 0x00, 0x00, 0x03, 0xff, 0xdf, 0xff, 0xff,
  34.         0xff, 0xff, 0xff, 0xfe, 0x7f, 0xff, 0xf7, 0xc7, 0xff, 0x06, 0x00, 0x03, 0xff, 0xbf, 0xff, 0xff,
  35.         0xff, 0xff, 0xff, 0xfe, 0x5f, 0xff, 0xc7, 0x07, 0xff, 0x80, 0x00, 0x07, 0xdb, 0xbf, 0xff, 0xff,
  36.         0xff, 0xff, 0xff, 0xee, 0xff, 0xff, 0x80, 0x03, 0xff, 0xc0, 0x00, 0x03, 0xc3, 0x0f, 0xff, 0xff,
  37.         0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0x98, 0x03, 0xff, 0xf8, 0x00, 0x07, 0xe0, 0x0f, 0xff, 0xff,
  38.         0xff, 0xff, 0xff, 0xef, 0xff, 0xff, 0xf8, 0x01, 0xff, 0xfc, 0x01, 0x07, 0xfc, 0x1f, 0xff, 0xff,
  39.         0xff, 0xff, 0xff, 0xcf, 0xef, 0xff, 0xff, 0xe1, 0xff, 0xfc, 0x01, 0x07, 0xf8, 0x1f, 0xff, 0xff,
  40.         0xff, 0xff, 0xff, 0x9f, 0xff, 0xff, 0x7f, 0xf1, 0xff, 0xf8, 0x02, 0x07, 0x88, 0x3f, 0xff, 0xff,
  41.         0xff, 0xff, 0xff, 0xcf, 0xef, 0xf8, 0x0f, 0xff, 0xff, 0xe0, 0x00, 0x07, 0x84, 0x3f, 0xff, 0xff,
  42.         0xff, 0xff, 0xff, 0xe7, 0xef, 0xf0, 0x04, 0x7f, 0xff, 0xc0, 0x00, 0x07, 0x84, 0x7f, 0xff, 0xff,
  43.         0xff, 0xff, 0xff, 0x3f, 0xff, 0xe0, 0x00, 0x1f, 0xff, 0x80, 0x00, 0x06, 0x04, 0xff, 0xff, 0xff,
  44.         0xff, 0xff, 0xff, 0x3f, 0x7f, 0xe1, 0xf0, 0x07, 0xff, 0x80, 0x00, 0x07, 0x06, 0xff, 0xff, 0xff,
  45.         0xff, 0xff, 0xff, 0xff, 0xff, 0xc3, 0xfe, 0x03, 0xff, 0x00, 0x00, 0x03, 0x80, 0xff, 0xff, 0xff,
  46.         0xff, 0xff, 0xff, 0xf2, 0x3f, 0xc6, 0x7f, 0x81, 0xce, 0x00, 0x00, 0x01, 0xc1, 0xff, 0xff, 0xff,
  47.         0xff, 0xff, 0xff, 0xe0, 0x3f, 0xc0, 0x07, 0xc1, 0xfe, 0x00, 0x00, 0x0d, 0xc0, 0x7f, 0xff, 0xff,
  48.         0xff, 0xff, 0xff, 0xe0, 0x3f, 0xc0, 0x01, 0xe0, 0xfc, 0x00, 0x00, 0x0f, 0xc0, 0x7f, 0xff, 0xff,
  49.         0xff, 0xff, 0xff, 0xc0, 0x3f, 0xc0, 0x00, 0x50, 0xfc, 0x00, 0x00, 0x0e, 0xc0, 0xff, 0xff, 0xff,
  50.         0xff, 0xff, 0xff, 0xc0, 0x3f, 0xc0, 0x00, 0x18, 0xf8, 0x00, 0x00, 0x0e, 0xc1, 0xff, 0xff, 0xff,
  51.         0xff, 0xff, 0xff, 0xc0, 0x3f, 0xc0, 0x00, 0x00, 0xf8, 0x00, 0x00, 0x66, 0x81, 0xff, 0xff, 0xff,
  52.         0xff, 0xff, 0xff, 0xc0, 0x1f, 0xc7, 0x80, 0x00, 0xf8, 0x00, 0x01, 0xe0, 0x00, 0xff, 0xff, 0xff,
  53.         0xff, 0xff, 0xff, 0xc0, 0x1f, 0xc1, 0xe0, 0x01, 0xf8, 0x00, 0x03, 0xf0, 0x01, 0xff, 0xff, 0xff,
  54.         0xff, 0xff, 0xff, 0x80, 0x1f, 0xc0, 0x3e, 0x03, 0xf0, 0x00, 0x00, 0xe0, 0x03, 0xff, 0xff, 0xff,
  55.         0xff, 0xff, 0xff, 0x00, 0x1f, 0xe0, 0xe0, 0x03, 0xf2, 0x00, 0x00, 0xc0, 0x03, 0xff, 0xff, 0xff,
  56.         0xff, 0xff, 0xff, 0x80, 0x1f, 0xf0, 0x00, 0x07, 0xe6, 0x00, 0x00, 0xc0, 0x03, 0xff, 0xff, 0xff,
  57.         0xff, 0xff, 0xff, 0x80, 0x1f, 0xff, 0x00, 0x1f, 0xee, 0x00, 0x00, 0x80, 0x07, 0xff, 0xff, 0xff,
  58.         0xff, 0xff, 0xff, 0xb8, 0x0f, 0xff, 0xf0, 0x3f, 0xdc, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff,
  59.         0xff, 0xff, 0xff, 0xbc, 0x0f, 0xff, 0xff, 0xff, 0xdc, 0x00, 0x00, 0x00, 0x0f, 0xff, 0xff, 0xff,
  60.         0xff, 0xff, 0xff, 0x9e, 0x0f, 0xff, 0xff, 0xff, 0xf8, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff,
  61.         0xff, 0xff, 0xff, 0x08, 0x0f, 0xff, 0xff, 0xff, 0x70, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff,
  62.         0xff, 0xff, 0xff, 0x00, 0x0b, 0xff, 0xff, 0xfe, 0xe0, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff,
  63.         0xff, 0xff, 0xff, 0x00, 0x0b, 0xff, 0xff, 0xf9, 0xc0, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff,
  64.         0xff, 0xff, 0xff, 0x3c, 0x09, 0xff, 0xff, 0xf1, 0x80, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff,
  65.         0xff, 0xff, 0xff, 0x1e, 0x08, 0x3f, 0xff, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff,
  66.         0xff, 0xff, 0xff, 0x1f, 0x08, 0x03, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff,
  67.         0xff, 0xff, 0xff, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff,
  68.         0xff, 0xff, 0xff, 0x80, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff,
  69.         0xff, 0xff, 0xff, 0xce, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xff,
  70.         0xff, 0xff, 0xff, 0xfe, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff,
  71.         0xff, 0xff, 0xff, 0xff, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff
  72. };

  73. void setup()   
  74. {               
  75.         // initialize with the I2C addr 0x3C
  76.         display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  

  77.         // Clear the buffer.
  78.         display.clearDisplay();

  79.         // Display bitmap
  80.         display.drawBitmap(0, 0,  MarilynMonroe, 128, 64, WHITE);
  81.         display.display();

  82.         // Invert Display
  83.         //display.invertDisplay(1);
  84. }

  85. void loop() {}
复制代码

以下这就是输出的样子。

Displaying-Bitmap-Image-On-OLED-Dsiplay-Module.jpg


要在OLED显示屏上显示位图图像,我们需要调用drawBitmap(X-coordinate, Y-coordinate, bitmap array, width, height, color)函数。它需要六个参数:左上角X坐标、左上角Y坐标、单色位图的字节数组、像素中位图的宽度、像素和颜色的位图高度。


在我们的示例中,位图图像的大小为128×64。因此,X和y坐标设置为0,而宽度和高度设置为128和64。

  1. // Display bitmap
  2. display.drawBitmap(0, 0,  MarilynMonroe, 128, 64, WHITE);
  3. display.display();
复制代码

但是,在我们调用drawbitmap()函数之前,首先需要绘制图像。请记住,OLED显示屏的屏幕分辨率为128×64像素,因此超过显示的图像将不会正确显示。要获得正确尺寸的图像,您可以使用自己喜欢的绘图程序,将画布大小设置为128×64像素。


我们以玛丽莲·梦露的图像为例,并使用画图工具将其转换为128×64像素,并保存为.bmp。一旦有了位图图像,就可以将其转换为SSD1306 OLED控制器可以理解的数组了。这可以使用两种方法:使用image2CPP在线方法和使用LCD助手的离线方法完成。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 54 | 回复: 107



手机版|

GMT+8, 2024-5-9 22:29 , Processed in 0.059463 second(s), 7 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

快速回复 返回顶部 返回列表