文章目录
- 1 前言
- 2 PCtoLCD2002使用
- 3 代码
- 4 结果
1 前言
前一篇介绍了sh1106的显示,本文尝试绘制图像并显示。
工具:PCtoLCD
参考文章:
Ardunio + I2C OLED显示文字和小图案
教你如何用PCtoLCD2002生成字模以及软件的下载
安装路径参见该篇文章
2 PCtoLCD2002使用
1 选择图形模式
2 设置
取模方式为逐行式
自定义格式为C51
行前缀为空格
行后缀为,(注意是英文逗号)
PS:注意点击左下角的“确定”
3 选择“文件”–“新建”
输入显示器件的分辨率,长宽像素点数。
比如我的sh1106时128*64.
4 绘图
画完图后点击“生成字模”和“保存字模”
5 获取数据
打开保存的txt文件
选择数据存到一个.h文件中
3 代码
代码来源于示例SSD1306SimpleDemo
注意修改内容
1 添加对应的头文件,如SH1106
2 修改参数类型,如SH1106Wire
3 修改SDA SCL对应IO号
4 修改drawImageDemo中的drawXbm前两个参数,这个是图片显示的起始点坐标,示例中非0
5 可以尝试先注销掉一部分显示:Demo demos中仅保留最后一个图像显示
6 将字模生成的数据存到images.h中
7 注意images.h中两个宏定义对应的是长宽要修改
//test1_add_sh1106.ino
/**The MIT License (MIT)Copyright (c) 2018 by ThingPulse, Daniel EichhornCopyright (c) 2018 by Fabrice WeinbergPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.ThingPulse invests considerable time and money to develop these open source libraries.Please support us by buying our products (and not the clones) fromhttps://thingpulse.com*/// Include the correct display library// For a connection via I2C using the Arduino Wire include:
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include "SH1106Wire.h" // legacy: #include "SSD1306.h"// Optionally include custom images
#include "images_bmp.h"// Initialize the OLED display using Arduino Wire:
SH1106Wire display(0x3c, 23, 22); #define DEMO_DURATION 3000
typedef void (*Demo)(void);int demoMode = 0;
int counter = 1;void setup() {
Serial.begin(115200);Serial.println();Serial.println();// Initialising the UI will init the display too.display.init();display.flipScreenVertically();display.setFont(ArialMT_Plain_10);}void drawFontFaceDemo() {
// Font Demo1// create more fonts at http://oleddisplay.squix.ch/display.setTextAlignment(TEXT_ALIGN_LEFT);display.setFont(ArialMT_Plain_10);display.drawString(0, 0, "Hello world");display.setFont(ArialMT_Plain_16);display.drawString(0, 10, "Hello world");display.setFont(ArialMT_Plain_24);display.drawString(0, 26, "Hello world");
}void drawTextFlowDemo() {
display.setFont(ArialMT_Plain_10);display.setTextAlignment(TEXT_ALIGN_LEFT);display.drawStringMaxWidth(0, 0, 128,"Lorem ipsum\n dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore." );
}void drawTextAlignmentDemo() {
// Text alignment demodisplay.setFont(ArialMT_Plain_10);// The coordinates define the left starting point of the textdisplay.setTextAlignment(TEXT_ALIGN_LEFT);display.drawString(0, 10, "Left aligned (0,10)");// The coordinates define the center of the textdisplay.setTextAlignment(TEXT_ALIGN_CENTER);display.drawString(64, 22, "Center aligned (64,22)");// The coordinates define the right end of the textdisplay.setTextAlignment(TEXT_ALIGN_RIGHT);display.drawString(128, 33, "Right aligned (128,33)");
}void drawRectDemo() {
// Draw a pixel at given positionfor (int i = 0; i < 10; i++) {
display.setPixel(i, i);display.setPixel(10 - i, i);}display.drawRect(12, 12, 20, 20);// Fill the rectangledisplay.fillRect(14, 14, 17, 17);// Draw a line horizontallydisplay.drawHorizontalLine(0, 40, 20);// Draw a line horizontallydisplay.drawVerticalLine(40, 0, 20);
}void drawCircleDemo() {
for (int i = 1; i < 8; i++) {
display.setColor(WHITE);display.drawCircle(32, 32, i * 3);if (i % 2 == 0) {
display.setColor(BLACK);}display.fillCircle(96, 32, 32 - i * 3);}
}void drawProgressBarDemo() {
int progress = (counter / 5) % 100;// draw the progress bardisplay.drawProgressBar(0, 32, 120, 10, progress);// draw the percentage as Stringdisplay.setTextAlignment(TEXT_ALIGN_CENTER);display.drawString(64, 15, String(progress) + "%");
}void drawImageDemo() {
// see http://blog.squix.org/2015/05/esp8266-nodemcu-how-to-create-xbm.html// on how to create xbm filesdisplay.drawXbm(0, 0, WiFi_Logo_width, WiFi_Logo_height, WiFi_Logo_bits);
}// Demo demos[] = {drawFontFaceDemo, drawTextFlowDemo, drawTextAlignmentDemo, drawRectDemo, drawCircleDemo, drawProgressBarDemo, drawImageDemo};
Demo demos[] = {
drawImageDemo};
int demoLength = (sizeof(demos) / sizeof(Demo));
long timeSinceLastModeSwitch = 0;void loop() {
// clear the displaydisplay.clear();// draw the current demo methoddemos[demoMode]();display.setFont(ArialMT_Plain_10);display.setTextAlignment(TEXT_ALIGN_RIGHT);//display.drawString(128, 54, String(millis()));// write the buffer to the displaydisplay.display();if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) {
demoMode = (demoMode + 1) % demoLength;timeSinceLastModeSwitch = millis();}counter++;delay(10);
}
//images_bmp.h
#define WiFi_Logo_width 128
#define WiFi_Logo_height 64
const uint8_t WiFi_Logo_bits[] PROGMEM = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x2B,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x50,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xDF,0xF6,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xB8,0xB6,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0x7F,0x00,0x00,0x00,0x01,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x40,0x00,0x00,0x80,0x01,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x80,0x00,0x00,0x80,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x80,0x01,0x00,0x80,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x03,0x00,0x80,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x02,0x00,0x80,0x01,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x01,0x00,0x04,0x00,0x00,0x01,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x08,0x00,0x00,0x01,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x10,0x00,0x00,0x03,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x30,0x00,0x00,0x03,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0xC0,0x00,0x00,0x07,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xBC,0x01,0x00,0x80,0x01,0x00,0x06,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xB4,0x06,0xFC,0x01,0x00,0x0C,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x03,0x00,0x00,0x38,0x60,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x0B,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xD8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x01,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xBE,0x01,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x94,0xFD,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};