Arduino网络编程实战-OLED显示图片

OLED显示图片

1、OLED介绍

OLED显示屏是指有机电激发光二极管(OrganicLight-EmittingDiode,OLED)由于同时具备自发光,不需背光源、对比度高、厚度薄、视角广、反应速度快、可用于挠曲性面板、使用温度范围广、构造及制程较简单等优异之特性,被认为是下一代的平面显示器新兴应用技术。

在前面的文章中,对OLED做了详细的介绍,请参考:

本实例将详细介绍如果在OLED中显示图片。

1、硬件准备

  • Arduino Mego2560开发板一块
  • SSD1306 I2C OLED显示屏一块
  • 数据线一条
  • 杜邦线若干

OLED与Arduino连线如下:

  • OLED:VCC <–> Arduino Mega2560 3.3V
  • OLED:GND <–> Arduino Mega2560 GND
  • OLED:SDA <–> Arduino Mega2560 SDA(20引脚)
  • OLED:SCL <–> Arduino Mega2560 SCL(21引脚)

2、软件准备

3、图像数据制作

Arduino单片机不支持直接解码图像,因此需要将图像文件转换成OLED驱动支持形式。网络上有许多Image转换LCD格式工具,比如:Image2Lcd。在本次实例中使用了一款在线转换工具:image2cpp

打开网页后,将图像上传,即可得到转换的数据。步骤如下:

3.1 上传图像

在这里插入图片描述

3.2 生成设置

在这里插入图片描述

可根据需要进行设置。

3.3 输出设置

在这里插入图片描述

3.4 代码生成

在这里插入图片描述

4、代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <Wire.h>
#include <Adafruit\_GFX.h>
#include <Adafruit\_SSD1306.h>

// 图像数据
// '105-sun-shower', 32x32px
// '105-sun-shower', 32x32px
const unsigned char epd_bitmap_105_sun_shower [] PROGMEM = {
0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x00, 0x18, 0x00, 0x00, 0x08, 0x18, 0x18,
0x00, 0x0c, 0x00, 0x38, 0x00, 0x06, 0x00, 0x70, 0x00, 0x02, 0x7e, 0x20, 0x00, 0x00, 0xe7, 0x00,
0x00, 0x01, 0xc1, 0x80, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x1c, 0x00, 0xc0, 0x00, 0xff, 0x80, 0x4f,
0x01, 0xc1, 0xc0, 0x4f, 0x03, 0x80, 0x60, 0xc0, 0x07, 0x00, 0x31, 0xc0, 0x1e, 0x00, 0x31, 0x80,
0x3c, 0x00, 0x18, 0x00, 0x60, 0x00, 0x1c, 0x60, 0xc0, 0x00, 0x06, 0x30, 0xc0, 0x00, 0x03, 0x18,
0xc0, 0x00, 0x03, 0x00, 0xc2, 0x02, 0x03, 0x00, 0xc6, 0x06, 0x03, 0x00, 0x66, 0x66, 0x66, 0x00,
0x64, 0x44, 0x4e, 0x00, 0x0c, 0xcc, 0xcc, 0x00, 0x0c, 0xcc, 0xc0, 0x00, 0x08, 0x88, 0x80, 0x00,
0x19, 0x99, 0x80, 0x00, 0x11, 0x91, 0x80, 0x00, 0x03, 0x03, 0x00, 0x00, 0x03, 0x03, 0x00, 0x00
};

// Array of all bitmaps for convenience. (Total bytes used to store images in PROGMEM = 144)
const int epd_bitmap_allArray_LEN = 1;
const unsigned char\* epd_bitmap_allArray[1] = {
epd_bitmap_105_sun_shower
};

// 屏幕大小
#define SCREEN\_WIDTH 128 // OLED display width, in pixels
#define SCREEN\_HEIGHT 64 // OLED display height, in pixels

// 图片大小
#define IMG\_HEIGHT 32
#define IMG\_WIDTH 32

// OLED重置引脚,I2C时和与板载重置引脚共用时,设置-1
#define OLED\_RESET -1
// OLED地址
#define SCREEN\_ADDRESS 0x3C
// OLED对象
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
Serial.begin(9600);

// 初始化OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}

// 显示欢迎画面
// Clear the buffer Adafruit\_SSD1306.h文件中将 #define SSD1306\_NO\_SPLASH 打开屏蔽
display.display();
delay(1000);
// 清屏
display.clearDisplay();

testdrawbitmap((const uint8\_t\*)epd_bitmap_allArray[0],32,32);
}

void loop() {

}

void testdrawbitmap(const uint8\_t\* datas,int width,int height) {
display.clearDisplay();
// 绘制图片
display.drawBitmap(
(display.width() - width ) / 2,
(display.height() - height) / 2,
datas, width, height, 1);
display.display();
delay(1000);
}

文章来源: https://iotsmart.blog.csdn.net/article/details/122979852