Quantcast
Channel: CAVEDU教育團隊技術部落格
Viewing all articles
Browse latest Browse all 678

《Arduino首次接觸就上手》的進階教學 追憶鳥山明:OLED螢幕上的經典重現

$
0
0

前言

已逝的鳥山明畫家,曾畫出許多知名的漫畫作品,如《怪博士與機器娃娃》和《七龍珠》…等,而這些作品深深著影響著我們,並陪伴著我們長大,變成我們的童年回憶。

本篇文章,要在Arduino 首次接觸就上手的OLED螢幕中,重現鳥山明的經典作品。從基礎的圖像處理到數據轉換,到最終的OLED顯示技術,我們將一步步引導你完成這個有趣的項目。

如果是新手或初學者想要知道有關《Arduino首次接觸就上手》基本操作的相關文章,可以移駕到

撰寫/攝影  許鈺莨
時間  30分鐘   材料表

(購買)

難度  2(滿分5)

本文

第一步:網路上下載七龍珠經典圖片

1.圖片搜尋

筆者是以Google搜尋的圖片,請輸入:七龍珠孫悟空黑白圖片。圖片不一定是七龍珠啦~讀者也可以找到喜歡的圖。但請注意,關鍵字一定要有”黑白圖片”,因為《Arduino首次接觸就上手》的OLED是單色的模組,顯示會較清楚。

 

2.開啟圖像畫面及截圖

另外要注意的一點是,OLED模組像素為128×64,顯示為長方形,所以盡量找橫向顯示的圖,如果找直向顯示的圖,如:悟空的站立圖,之後顯示會變很扁,不過可以用圖片軟體來旋轉角度。

基於很多都是悟空的站立圖,所以筆者找到顯示DRAGONBALL圖片(下方紅框處),打算用截圖出來,但在這邊截圖會變成很小。

 

所以要顯示正常大小的圖形畫面,可對圖形按右鍵>複製圖片網址

再把網址貼上網址列,就可以還原圖片大小。

 

再來截圖,可以使用Windows內建的截圖功能,Home+Shift+S三鍵一起按就可以截圖。

如下圖所示。

 

第二步:改變圖像尺寸

1.使用小畫家

檔案>開啟舊檔>開啟圖片

 

2.改變圖片尺寸

因OLED的尺寸為128×64個像素,所以必須要更改圖片像素的尺寸。

常用>調整大小

 

然後選像素,水平欄位寫128垂直欄位寫入64。另外,不要勾選維持外觀比例。

3.另存新檔

另存新檔成單色點陣圖

 

第三步:下載並安裝Image2Lcd軟體

1.下載並安裝Image2Lcd軟體

Image2Lcd是一款可將圖像轉換為LCD 顯示器格式的軟體,當然有很多種作法,只要最後可以轉換成BMP(Bitmap)格式即可。

下載Image2Lcd軟體  (請按我)

2.預覽圖像畫面

開啟圖片後,右側可以預覽圖片,有圖片旋轉及顏色反轉之功能。

3.保存為BMP檔

【注意】這裡必須要把位元組內象素數據反序勾選起來,否則OLED的圖會很破碎,再選擇保存

之後就會產生圖片的BMP格式了,再將當中的像素陣列程式寫入到Arduino程式碼當中即可。

 

 

第四步:使用Arduino IDE燒錄

要在OLED模組顯示圖形,必須要使用到U8g2lib.h的函式庫。所以只能用Arduino IDE撰寫,而HanGeekduino圖形化的OLED積木程式中,採用的是U8x8lib.h的函式庫,此函式庫只能顯示文字,而不行繪製圖形。

1.開啟程式

路徑位置在HanGeekduino\BlocklyDuino3\arduino-1.8.5

再開啟arduino.exe即可。

開啟名為Arduino_OLED.ino程式,程式連結(請按我)

 

完整程式如下

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,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1F,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);
}

 

 

2.上傳程式

取代像素陣列程式,即下方黃色部份,並燒錄上傳至《Arduino首次接觸就上手》

 

 

實體效果

程式上傳完成後,就會看見圖示顯示在OLED上面。

 

結語

恭喜完成了在《Arduino首次接觸就上手》的OLED模組完成進階實作,這當中建議找黑白色的圖,成功完整顯示機率才比較高。其實,OLED模組不僅可以單純的顯示文字,透過這些步驟,還可以在OLED模組中顯示想要的圖案,是不是很棒呢?

未來還有更多《Arduino首次接觸就上手》的主題喔,敬請各位讀者期待,我們下次見!

 

相關文章與資源連結

《Arduino首次接觸就上手》的進階教學 追憶鳥山明:OLED螢幕上的經典重現〉這篇文章最早發佈於《CAVEDU教育團隊技術部落格》。


Viewing all articles
Browse latest Browse all 678

Trending Articles