占用资源有点大,一个 dmeo 用掉 uno 60% 的内存
制作图标, 我试了无效
https://icode.best/i/71467042039792
Adafruit_SSD1306* pLcd = nullptr; // 显示器控制器
// https://github.com/adafruit/Adafruit_SSD1306/blob/master/examples/ssd1306_128x64_i2c/ssd1306_128x64_i2c.ino
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
void testscrolltext(Adafruit_SSD1306 &display);
void testdrawbitmap(Adafruit_SSD1306 &display);
void testInvertDisplay(Adafruit_SSD1306 &display);
void testdrawchar(Adafruit_SSD1306 &display);
void testdrawstyles(Adafruit_SSD1306 &display);
Oled12864::Oled12864()
{
// -1 为忽略重置按钮
pLcd = new Adafruit_SSD1306(128, 64, &Wire, -1);
}
Oled12864::~Oled12864()
{
Log.infoln("lcd 类释放");
if (pLcd != nullptr)
{
delete pLcd;
pLcd = nullptr;
}
}
// 初始时调用
void Oled12864::Setup()
{
// 0x3C 是我的显示器的地址,
if (!pLcd->begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Log.infoln("12864 初始化失败");
this->inited = false;
return;
}
Log.infoln("12864 初始化成功");
this->inited = true;
// 显示缓存区内容
pLcd->display();
delay(500);
// 清空缓存区
pLcd->clearDisplay();
// 画一个点
pLcd->drawPixel(10, 10, SSD1306_WHITE);
delay(2000);
testscrolltext(*pLcd);
delay(1000);
testdrawbitmap(*pLcd);
delay(1000);
testInvertDisplay(*pLcd);
delay(1000);
testdrawchar(*pLcd);
delay(1000);
testdrawstyles(*pLcd);
delay(1000);
}
// 循环调用
void Oled12864::Loop()
{
if (!this->inited)
{
return;
}
}
// 滚屏显示
void testscrolltext(Adafruit_SSD1306 &display)
{
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 0);
display.println(F("scroll"));
display.display(); // Show initial text
delay(100);
// Scroll in various directions, pausing in-between:
display.startscrollright(0x00, 0x0F);
delay(1000);
display.stopscroll();
// delay(2000);
// display.stopscroll();
// delay(1000);
// display.startscrollleft(0x00, 0x0F);
// delay(2000);
// display.stopscroll();
// delay(1000);
// display.startscrolldiagright(0x00, 0x07);
// delay(2000);
// display.startscrolldiagleft(0x00, 0x07);
// delay(2000);
// display.stopscroll();
// delay(1000);
}
// 画出点阵图
void testdrawbitmap(Adafruit_SSD1306 &display)
{
display.clearDisplay();
int LOGO_HEIGHT = 16;
int LOGO_WIDTH = 16;
static const unsigned char PROGMEM logo_bmp[] =
{0b00000000, 0b11000000,
0b00000001, 0b11000000,
0b00000001, 0b11000000,
0b00000011, 0b11100000,
0b11110011, 0b11100000,
0b11111110, 0b11111000,
0b01111110, 0b11111111,
0b00110011, 0b10011111,
0b00011111, 0b11111100,
0b00001101, 0b01110000,
0b00011011, 0b10100000,
0b00111111, 0b11100000,
0b00111111, 0b11110000,
0b01111100, 0b11110000,
0b01110000, 0b01110000,
0b00000000, 0b00110000};
display.drawBitmap(
(display.width() - LOGO_WIDTH) / 2,
(display.height() - LOGO_HEIGHT) / 2,
logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1);
display.display();
}
// 反向显示
void testInvertDisplay(Adafruit_SSD1306 &display)
{
for (int i = 0; i < 5; i++)
{
display.invertDisplay(true);
delay(1000);
display.invertDisplay(false);
delay(1000);
}
}
// 输出文字
void testdrawchar(Adafruit_SSD1306 &display)
{
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
// Not all the characters will fit on the display. This is normal.
// Library will draw what it can and the rest will be clipped.
for (int16_t i = 0; i < 256; i++)
{
if (i == '\n')
display.write(' ');
else
display.write(i);
}
display.display();
}
void testdrawstyles(Adafruit_SSD1306 &display)
{
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.println(F("Hello, world!"));
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
display.println(3.141592);
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.print(F("0x"));
display.println(0xDEADBEEF, HEX);
display.display();
delay(2000);
}