บทความประกอบการเรียนรู้ => IOT : Internet of Thing (ESP8266, NodeMCU, WeMos D1 mini) => ข้อความที่เริ่มโดย: admin ที่ พฤษภาคม 15, 2016, 04:27:23 PM

หัวข้อ: งานครั้งที่ 9 การติดตั้งไลบารี่ OLED(SSD1306) i2c และการใช้งานเบื้องต้น
เริ่มหัวข้อโดย: admin ที่ พฤษภาคม 15, 2016, 04:27:23 PM
การติดตั้งไลบารี่ OLED i2c และการใช้งานเบื้องต้น
การติดตั้งไลบารี่ OLED i2c  เนื่องจากโปรแกรม Arduino IDE ไม่ได้มีการติดตั้งมาให้ตั้งแต่เริ่มต้นจึงจำเป็นต้องมีการติดตั้งเพิ่มเติ่มเข้าไปในโปรแกรม ซึ่งจะต้องใช้ไลบรารี่ 2 ตัวคือ
-Adafruit GFX
-Adafruit SSD1306

โดยมีขั้นตอนดังนี้

1. เข้าไปที่เมนู Manage Libraries...
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/1.png)

2. พิมพ์คำว่า Adafruit GFX ในช่องค้นหา ซึ่งจะขึ้นรายการไลบรารี่ดังกล่าวขึ้น ทำการเลือกเวอร์ชั่นล่าสุดเพื่อทำการติดตั้ง แล้วคลิก install
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/2.png)

3. พิมพ์คำว่า Adafruit SSD1306 ในช่องค้นหา ซึ่งจะขึ้นรายการไลบรารี่ดังกล่าวขึ้น ทำการเลือกเวอร์ชั่นล่าสุดเพื่อทำการติดตั้ง แล้วคลิก install
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/3.png)

หมายเหตุ เนื่องจากค่าเริ่มต้นของไลบรารี่กำหนดขนาด OLED เป็น 128x32 ดังนั้นเมื่อใช้งานกับ OLED ขนาด 128x64 จะทำให้ตัวอักษรยืดในแนวตั้ง ซึ่งจะต้องแก้ไขไฟล์ไลบารี่ Adafruit_SSD1306.h โดยเข้าไปยังโฟลเดอร์ที่เก็บไลบรารี่ แล้วทำการแก้ไขค่าในไฟล์ดังกล่าว ซึ่งมีขั้นตอนดังนี้

1. เข้าโฟลเดอร์ Documents ของเครื่องแล้วเข้าโฟลเดอร์ Arduino (คลิก 1, 2, 3)
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/4.png)

2. เข้าไปในโฟลเดอร์ libraries ซึ่งอยู่ภายในโฟลเดอร์ Arduino
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/5.png)

3. เข้าไปในโฟลเดอร์ Adafruit_SSD1306 ซึ่งอยู่ภายในโฟลเดอร์  libraries
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/6.png)

4. เข้าไปแก้ไขไฟล์ Adafruit_SSD1306.h
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/7.png)

5. ค้นหาค่าเดิมที่ตั้งค่าไว้ดังรูป
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/8.png)

ุ6. ทำการแก้ไขให้เป็นการกำหนดค่าใช้งาน OLED ขนาด 128x64
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/9.png)

ผลการแก้ไขไลบารี่ (ก่อนแก้และหลังแก้)
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/Before-After.png)

ฟังก์ชั่นที่มีให้ใช้งาน
.begin(SSD1306_SWITCHCAPVCC,addr);
.clearDisplay();
.dim(dim) // Dim the display, dim = true: display is dimmed ,dim = false: display is normal
.display();
.drawBitmap( x,y,*bitmap,w,h,color)
.drawChar(x,y,c,color,background,size);
.drawCircle(x0,y0,radius,color);
.drawFastHLine(x0,y0,length,color);
.drawFastHLineInternal(x0,y0,w,color)
.drawFastVLine(x0,y0,length,color);
.drawFastVLineInternal(x,y,h,color)
.drawLine(x0,y0,x1,y1,color);
.drawPixel(x, y,color);
.drawRect(x0,y0, w,h,color);
.drawRoundRect(x0,y0,w,h,radius,color);
.drawTriangle(x0,y0,x1,y1,x2,y2,color);
.fillCircle(x0,y0,radius,color);
.fillRect(x0,y0,w,h,color);
.fillRoundRect(x0,y0,w,h,radius,color);
.fillScreen(color);
.fillTriangle(x0,y0,x1,y1,x2,y2,color);
.invertDisplay(i) //i=1,0
.print();
.println();
.setCursor(x0,y0);
.setRotation(rotation); //The rotation screen parameter can be 0, 1, 2 or 3.
.setTextColor(color);
.setTextColor(color,background)
.setTextSize(sizes);      // printable sizes from 1 to 8; typical use is 1, 2 or 3
.startscrolldiagleft(start,stop)
.startscrolldiagright(start,stop)
.startscrollleft(start,stop)
.startscrollright(start,stop)
.stopscroll()

วงจรที่ใช้ทดลอง
กรณีใช้บอร์ดรุ่น NodeMCU
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/sch1.png)
กรณีใช้บอร์ดรุ่น WeMos D1 mini
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/sch2.png)

โปรแกรมตัวอย่างที่ 1
โค๊ด: [Select]
#include <Wire.h> 
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 OLED(-1);
int x;
void setup() {
  OLED.begin(SSD1306_SWITCHCAPVCC,0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
}
void loop(){
  OLED.clearDisplay();
  OLED.setTextColor(WHITE,BLACK);   //Text is white ,background is black
  OLED.setCursor(0, 0);
  OLED.setTextSize(2);
  OLED.println("IOT");
  OLED.setTextSize(1);   
  OLED.print("Laboratory");
  OLED.println(" 2016");
  OLED.setTextColor(BLACK,WHITE); // 'inverted' text
  OLED.print("www.praphas.com");
  OLED.setTextColor(WHITE,BLACK); // 'inverted' text again
  OLED.setCursor(60,0);
  OLED.setTextSize(2); 
  OLED.println(x,DEC);
  OLED.display();
  x++;
  delay(250);
}
https://www.youtube.com/v/Q6eSYwEQwwg


สำหรับผู้ที่ลงไลบรารี่เวอร์ชั่นใหม่ GFX 1.3.1 และ SSD 1.2.4 (หลังเดือน พ.ย. 2018)
สามารถกำหนดขนาดจอได้ในตัวโปรแกรมโดยไม่ต้องเข้าไปแก้ในไฟล์ไลบรารี่ การกำหนดทำดังนี้
(เมื่อใช้ใช้ชื่อออปเจคจอว่า OLED)

โค๊ด: [Select]
#define SCREEN_WIDTH    128           // OLED display width, in pixels
#define SCREEN_HEIGHT   64            // OLED display height, in pixels
#define OLED_RESET      -1            // -1 sharing Arduino reset pin)
Adafruit_SSD1306 OLED(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

การกำหนดพิกัดตำแหน่งของคำสั่งเขียนภาพ
ในแต่ละคำสั่งจะมีการกำหนดค่าพิกัดต่าง ๆ ในแต่ละคำสั่งตัวอย่างเช่น

คำสั่งพล๊อตจุด .drawPixel(x, y,color);
(https://cdn-learn.adafruit.com/assets/assets/000/001/264/original/lcds___displays_coordsys.png?1396770439)

คำสั่งลากเส้น เช่น .drawLine(x0,y0,x1,y1,color);
(https://cdn-learn.adafruit.com/assets/assets/000/001/268/original/lcds___displays_line.png?1396770476)

คำสั่งวาดรูปสี่เหลี่ยม เช่น .drawRect(x0,y0, w,h,color);
(https://cdn-learn.adafruit.com/assets/assets/000/001/270/original/lcds___displays_rect.png?1396770497)

คำสั่งวาดรูปวงกลม เช่น .drawCircle(x0,y0,radius,color);
(https://cdn-learn.adafruit.com/assets/assets/000/001/272/original/lcds___displays_circle.png?1396770516)

คำสั่งวาดรูปสี่เหลี่ยมหัวมน เช่น .drawRoundRect(x0,y0,w,h,radius,color);
(https://cdn-learn.adafruit.com/assets/assets/000/001/274/original/lcds___displays_roundrect.png?1396770535)

คำสั่งวาดรูปสามเหลี่ยม เช่น .drawTriangle(x0,y0,x1,y1,x2,y2,color);
(https://cdn-learn.adafruit.com/assets/assets/000/001/275/original/lcds___displays_triangle.png?1396770547)


ตัวอย่างโปรแกรมที่ 2
โค๊ด: [Select]
#include <Wire.h> 
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 OLED(4);
int x;
void setup() {
  OLED.begin(SSD1306_SWITCHCAPVCC,0x3C); //initialize I2C addr 0x3c
}
void loop(){
  OLED.clearDisplay();
  OLED.setTextColor(WHITE,BLACK);
  OLED.drawRect(10,10, 100,40,WHITE);
  OLED.drawLine(0, 63, 127, 0, WHITE);
  OLED.drawCircle(110, 50, 12, WHITE);
  OLED.fillCircle(45, 50, 8, WHITE);
  OLED.drawTriangle(70, 60, 90, 60, 80, 46, WHITE);
  OLED.fillRect(15,15,20,20,WHITE);
  OLED.fillRoundRect(50,15,50,20,10,WHITE);
  OLED.setCursor(5,30);
  OLED.setTextSize(1);
  OLED.println("www.praphas.com");
  OLED.display(); //
  delay(250);
}
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/grapic.png)

การเปลี่ยนฟอนต์ในการแสดงผล
ไฟล์ไลบรารี่ Adafruit GFX มีฟอนต์มาให้จำนวนมากดังนี้
โค๊ด: [Select]
FreeMono12pt7b.h		FreeSansBoldOblique12pt7b.h
FreeMono18pt7b.h FreeSansBoldOblique18pt7b.h
FreeMono24pt7b.h FreeSansBoldOblique24pt7b.h
FreeMono9pt7b.h FreeSansBoldOblique9pt7b.h
FreeMonoBold12pt7b.h FreeSansOblique12pt7b.h
FreeMonoBold18pt7b.h FreeSansOblique18pt7b.h
FreeMonoBold24pt7b.h FreeSansOblique24pt7b.h
FreeMonoBold9pt7b.h FreeSansOblique9pt7b.h
FreeMonoBoldOblique12pt7b.h FreeSerif12pt7b.h
FreeMonoBoldOblique18pt7b.h FreeSerif18pt7b.h
FreeMonoBoldOblique24pt7b.h FreeSerif24pt7b.h
FreeMonoBoldOblique9pt7b.h FreeSerif9pt7b.h
FreeMonoOblique12pt7b.h FreeSerifBold12pt7b.h
FreeMonoOblique18pt7b.h FreeSerifBold18pt7b.h
FreeMonoOblique24pt7b.h FreeSerifBold24pt7b.h
FreeMonoOblique9pt7b.h FreeSerifBold9pt7b.h
FreeSans12pt7b.h FreeSerifBoldItalic12pt7b.h
FreeSans18pt7b.h FreeSerifBoldItalic18pt7b.h
FreeSans24pt7b.h FreeSerifBoldItalic24pt7b.h
FreeSans9pt7b.h FreeSerifBoldItalic9pt7b.h
FreeSansBold12pt7b.h FreeSerifItalic12pt7b.h
FreeSansBold18pt7b.h FreeSerifItalic18pt7b.h
FreeSansBold24pt7b.h FreeSerifItalic24pt7b.h
FreeSansBold9pt7b.h FreeSerifItalic9pt7b.h
ผู้ใช้งานสามารถเรียกใช้ได้โดยการ include ไฟล์ฟอนต์ดังกล่าวเข้าในโปรแกรมแล้วทำการเรียกใช้ เพียงแต่ตำแหน่งของตัวอักษรที่ใช้กำหนดตำแหน่งจะแตกต่างกันกับที่ไม่ได้มีการใช้ฟอนต์ดังรูป
(https://cdn-learn.adafruit.com/assets/assets/000/029/277/original/lcds___displays_NewChar.png?1450831047)
ตัวอย่างโปรแกรมที่มีการเปลี่ยนฟอนต์เพื่อแสดงผล

โค๊ด: [Select]
#include <Wire.h> 
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeMonoBold9pt7b.h>  //include font MonoBold size 9pt7b
Adafruit_SSD1306 OLED(-1);
void setup() {
  OLED.setFont(&FreeMonoBold9pt7b); // Use font from file include   
  OLED.begin(SSD1306_SWITCHCAPVCC,0x3C); // initialize with the I2C addr 0x3C (for the 128x64)
}
void loop(){
  OLED.clearDisplay();
  OLED.setTextColor(WHITE,BLACK);   //Text is white ,background is black
  OLED.setCursor(0,20);
  OLED.setTextSize(1);
  OLED.println("Change font");
  OLED.setCursor(0,50);   
  OLED.print("OLED 128x64");
  OLED.display(); //
  delay(250);
}
(http://www.praphas.com/PhotoForum/iot/Lab-09-OLED/changeFont.png)