本篇文章,要在Arduino 首次接觸就上手的OLED螢幕中,重現鳥山明的經典作品。從基礎的圖像處理到數據轉換,到最終的OLED顯示技術,我們將一步步引導你完成這個有趣的項目。
另外要注意的一點是,OLED模組像素為128×64,顯示為長方形,所以盡量找橫向顯示的圖,如果找直向顯示的圖,如:悟空的站立圖,之後顯示會變很扁,不過可以用圖片軟體來旋轉角度。
要在OLED模組顯示圖形,必須要使用到U8g2lib.h的函式庫。所以只能用Arduino IDE撰寫,而HanGeekduino圖形化的OLED積木程式中,採用的是U8x8lib.h的函式庫,此函式庫只能顯示文字,而不行繪製圖形。
Arduino_OLED.ino
#include <Arduino.h>
#include <U8g2lib.h>
// 初始化顯示器
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R2, /* reset=*/ U8X8_PIN_NONE);
#define imgWidth 128
#define imgHeight 64
//可以替換第13行~第76行的陣列圖案
static const unsigned char logo_bmp[] PROGMEM =
{
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,0X20,0X30,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X50,0X50,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X10,0X50,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X00,0X94,0X02,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0XA0,0X8A,0X02,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X00,0X20,0X2A,0X02,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X40,0X40,0X0B,0X02,0X06,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0XC0,0X0D,0X42,0X02,0X0C,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X00,0X00,0X00,0X70,0XF7,0X90,0X0C,0X18,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0X80,0XFF,0X03,0XCC,0XAA,0XE3,0X11,0X30,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,
0XF0,0XFF,0X0F,0X92,0X48,0X05,0XF3,0X63,0X00,0X00,0X00,0X02,0X00,0X00,0X00,0X00,
0XF8,0X01,0XFE,0XF3,0XFF,0X39,0X09,0XC4,0X00,0X00,0X00,0X1F,0X00,0X00,0X00,0X00,
0X38,0X00,0X78,0X5C,0XD5,0XDF,0X55,0X84,0XFF,0X13,0X00,0X7B,0X00,0X0C,0X00,0X00,
0X18,0X00,0XF0,0XBB,0XFE,0XEF,0XBF,0X07,0X7E,0X27,0X00,0XC1,0X03,0X3C,0X00,0X00,
0X18,0X00,0X88,0XD7,0XFB,0XFF,0X3F,0X02,0X04,0X5E,0X01,0X01,0X07,0XF4,0X01,0X00,
0X18,0X00,0XE8,0X2D,0XB5,0XDF,0X2B,0X0A,0X1B,0XBA,0X03,0X01,0X0C,0X86,0X01,0X00,
0X18,0X00,0XA2,0XFB,0XFF,0X7F,0X02,0XEF,0X11,0X0C,0X81,0X01,0X18,0X86,0X01,0X00,
0X10,0X00,0X76,0X3F,0XF2,0XFF,0X03,0X21,0X35,0X5E,0X82,0X01,0X10,0X82,0X01,0X00,
0X10,0X00,0XDE,0X43,0X00,0XED,0X93,0X61,0X60,0X1A,0X9A,0X01,0XB0,0X83,0X00,0X00,
0X30,0X10,0XFD,0XC8,0X2D,0X06,0X63,0X3E,0X60,0X8C,0XE8,0X00,0X60,0X83,0X00,0X00,
0X30,0XF0,0XFF,0X70,0X80,0X07,0XE3,0XEF,0XE1,0X38,0X83,0X60,0X60,0X83,0X03,0X00,
0X30,0XF0,0X3F,0X0E,0X00,0X06,0X17,0XB0,0XFF,0X18,0XDE,0XE0,0XE1,0X83,0X07,0X00,
0X30,0X90,0X1F,0X18,0X00,0X02,0XD7,0XFF,0XE3,0XB8,0XFD,0XE0,0XC1,0XC1,0X7F,0X00,
0X20,0XB0,0X2B,0X19,0X00,0X03,0XC7,0X3F,0X80,0X8C,0XFA,0XF0,0XC1,0XC1,0X43,0X00,
0X20,0XB0,0XC7,0X18,0X00,0X03,0X87,0X27,0X80,0X07,0XDD,0XB0,0XC1,0XC1,0X41,0X00,
0X60,0XF0,0X47,0X10,0X00,0X01,0X1F,0X27,0X00,0X07,0X40,0XF0,0XC1,0XC1,0XC1,0X00,
0X60,0XE0,0X2F,0X90,0X01,0X01,0XAF,0X23,0X00,0X83,0X40,0XE0,0XE1,0XC1,0XC1,0X03,
0X60,0X60,0X21,0X70,0X87,0X11,0X86,0X23,0X02,0XBB,0X58,0X00,0XE0,0XE0,0X41,0X02,
0X60,0XE0,0X21,0X30,0X88,0X10,0X7E,0X33,0X02,0X96,0X53,0X00,0XA0,0XE0,0XC1,0X06,
0X60,0XE0,0X31,0X30,0X80,0XF0,0X7F,0XD3,0X0F,0X52,0X70,0X00,0XF0,0X20,0XC1,0X05,
0XE0,0XE1,0X3C,0X70,0XCF,0XF8,0X7F,0X9B,0X0F,0XFE,0X87,0X00,0XF0,0X21,0XC1,0X05,
0X20,0X61,0X30,0X70,0X59,0X3C,0XFA,0X8F,0X07,0XFE,0XDF,0X00,0XF8,0X21,0XC1,0X07,
0XA0,0XC1,0X38,0X70,0X73,0X3A,0XC2,0X03,0X07,0X3E,0X3C,0X01,0X18,0XF1,0XC1,0X0B,
0X40,0XC3,0X3F,0XF0,0X62,0X01,0X02,0X83,0X07,0X3E,0X6C,0X19,0X18,0XE3,0XC3,0X0B,
0X20,0XC3,0X38,0X30,0XA1,0X00,0X02,0X83,0X04,0X3E,0XFC,0X79,0X18,0X03,0XCF,0X0F,
0XA0,0XC1,0X18,0X30,0XA0,0X00,0X12,0X02,0X08,0X3A,0XFC,0X6C,0X14,0X02,0XCC,0X07,
0XA0,0XC3,0X1C,0XD0,0X31,0X00,0X7E,0X02,0X00,0X33,0XFC,0X4D,0X1A,0X02,0XCC,0X0F,
0X40,0XC2,0X1B,0X70,0X13,0X00,0XCE,0X06,0X00,0X31,0X6C,0XCD,0X19,0X02,0XCC,0X0F,
0X00,0X81,0X0C,0X58,0X12,0X02,0X86,0X05,0X80,0X21,0X7C,0X3F,0X14,0X06,0XC4,0X0F,
0X80,0X81,0X0F,0XD8,0X1B,0X3E,0X7E,0X0F,0X80,0X20,0XAC,0X3B,0X12,0X06,0XC4,0X1B,
0XA0,0X81,0X07,0XD8,0X09,0X3F,0XF6,0X1F,0XC0,0X00,0XFC,0X23,0X1C,0X0C,0XC4,0X3F,
0XA0,0X81,0X03,0X0C,0X38,0X3D,0XF6,0X3F,0X70,0X00,0XFC,0X76,0X0A,0X3C,0XC4,0X3F,
0XA0,0X01,0X00,0X0C,0XF8,0X2D,0X9E,0XFF,0X3F,0X00,0XEC,0XF5,0X2E,0XFC,0XC6,0X3F,
0XE0,0X03,0X00,0X06,0XE0,0X2D,0X1E,0XFF,0X1F,0X00,0XEC,0XE7,0X2C,0XCC,0XC7,0X37,
0XE0,0X03,0X00,0X06,0X30,0XEC,0X0F,0X0E,0X19,0X06,0XCC,0X6F,0X67,0X88,0XE3,0X37,
0XE0,0X03,0X00,0X03,0X38,0XCC,0X1F,0X38,0X19,0X04,0XCC,0X7F,0X63,0X08,0XF1,0X36,
0XE0,0X03,0X00,0X63,0X18,0X0F,0X3F,0X30,0X19,0X0C,0XD6,0XDF,0X23,0X08,0XB9,0X33,
0XE0,0X03,0X80,0X61,0X58,0X0F,0X7F,0X60,0X15,0X1C,0XF6,0XDF,0X01,0X18,0XB9,0X33,
0XC0,0X03,0XC0,0XE1,0XF0,0X1B,0XFE,0X20,0X13,0X1C,0X27,0XEF,0X00,0X98,0XFD,0X31,
0XC0,0X03,0XE0,0XF1,0XF0,0X1F,0XFC,0XA0,0X30,0XB4,0XA8,0XFF,0X00,0X90,0XF7,0X31,
0X00,0X07,0XF0,0XB1,0XF0,0X1F,0X00,0X30,0X30,0XE4,0XA3,0XFF,0X00,0XF0,0XF3,0X35,
0X00,0X06,0XBC,0XF0,0XB0,0X3F,0X00,0X10,0X30,0XE4,0X65,0XF7,0X00,0XB0,0XEE,0X3F,
0X00,0X06,0XCE,0XF0,0XF0,0X61,0X00,0X18,0X30,0X44,0XE6,0XFE,0X00,0X70,0X7F,0X1E,
0X00,0X06,0XE7,0XFF,0X61,0XE9,0X00,0X0C,0X30,0XCC,0XE1,0XFF,0XF0,0X60,0X3F,0X00,
0X00,0XC6,0X9F,0X9F,0X3F,0XC9,0X01,0X06,0XF0,0X8F,0XC7,0XFF,0XF0,0XE1,0X1F,0X00,
0X00,0XFC,0X01,0X99,0X7F,0XA9,0X83,0X03,0XF0,0X0F,0XC5,0XFB,0XF0,0XE1,0X1D,0X00,
0X00,0X7C,0X88,0X08,0X64,0X09,0XFF,0X01,0X00,0X00,0XF8,0XFF,0XDF,0XFF,0X0F,0X00,
0X00,0X1C,0X70,0X44,0X24,0X05,0X7C,0X00,0X00,0X00,0XE0,0XEB,0XBF,0XFF,0X07,0X00,
0X00,0X04,0X40,0X06,0X80,0X01,0X00,0X00,0X00,0X00,0XC0,0XB3,0X5F,0XF7,0X01,0X00,
0X00,0X00,0X80,0X23,0X42,0X00,0X00,0X00,0X00,0X00,0X80,0X43,0XFE,0X7F,0X00,0X00,
0X00,0X00,0X00,0X1C,0X11,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X21,0X0D,0X00,0X00,
};
void setup() {
u8g2.begin();
}
void loop() {
u8g2.clearBuffer(); // 清除內部記憶體
u8g2.setFont(u8g2_font_ncenB08_tr);// 設定字型
u8g2.firstPage();
do {
u8g2.drawXBMP(0, 0, imgWidth, imgHeight, logo_bmp); //繪圖
//u8g2.drawStr(0, 15, "DRAGON BALL !"); //輸出文字
} while ( u8g2.nextPage() );
u8g2.sendBuffer();
delay(1000);
}
恭喜完成了在《Arduino首次接觸就上手》的OLED模組完成進階實作,這當中建議找黑白色的圖,成功完整顯示機率才比較高。其實,OLED模組不僅可以單純的顯示文字,透過這些步驟,還可以在OLED模組中顯示想要的圖案,是不是很棒呢?