Kategoriler
IoT

Wemos D1 Mini ve Oled Ekrana Çizdirme

Merhaba,

Wemos D1 Mini’in shieldlerinden oled ekran üzerinde micropython ile çizim işlemlerini inceleyeceğiz.

Text yazdırma

text fonksiyonunu kullanarak bir satır yazı yazdırabiliriz. Bu fonksiyon aşağıdaki parametreleri alır.

  • String tipinde metin
  • Yazı X pozisyonu
  • Yazı Y pozisyonu
  • Opsiyonel olarak metin rengi (0 = siyah, 1 = beyaz,  varsayılan beyaz’dır)
import ssd1306
from machine import I2C, Pin
 
i2c = I2C(sda=Pin(4), scl=Pin(5))
display = ssd1306.SSD1306_I2C(64, 48, i2c)
 
display.fill(0)
display.text('Merhaba',5,5)
display.text('Yavuz',5,15)
display.show()

Aşağıdaki invert komutu ile de renkleri tersine çevirebiliriz.

display.invert(True)

Çizgi Çizdirme

Önce iki nokta arasına çizdi çizme fonksiyonunu tanımlayalım.

def draw_line(display, x0, y0, x1, y1):
    deltax = x1 - x0
    deltay = y1 - y0
    error = -1.0
    deltaerr = abs(deltay / deltax)  
    y = y0
    for x in range(int(x0), int(x1)-1):
        # plot(x,y)
        display.pixel(x, y, 1)
        # print(x, y)
        error = error + deltaerr
        if error >= 0.0:
            y = y + 1
            error = error - 1.0

Daha sonra tanımladığımız fonksiyon yazdımıyla belirttiğimiz koordinatlar arasına çizgi çizdirelim.

display.fill(0)
draw_line(display,0,0,30,60)
display.show()

Resim Çizdirme

Çizdirmek istediğiniz resmi önce buradan 48×48 şeklinde boyutlandırarak hex formatına çevirelim ve logo değişkenine atalayım.

Resmi ekrana ortalamak için soldan x değerine 10 piksellik bir boşluk ekleyerek aşağıdaki şekilde çizdirebiliriz.

logo = (
0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0x00,0x3f,0xff,0xff
,0xff,0xfc,0x00,0x0f,0xff,0xff
,0xff,0xf8,0x03,0xe7,0xff,0xff
,0xff,0xf0,0x1f,0xff,0xff,0xff
,0xff,0xe0,0x3f,0xff,0xff,0xff
,0xff,0xc0,0x7f,0xff,0xff,0xff
,0xff,0xc0,0xff,0xff,0xff,0xff
,0xff,0x81,0xff,0xff,0xf7,0xff
,0xff,0x81,0xff,0xff,0xf3,0xff
,0xff,0x03,0xff,0xff,0xf3,0x9f
,0xff,0x03,0xff,0xff,0xf0,0x3f
,0xff,0x03,0xff,0xff,0xe0,0x7f
,0xff,0x03,0xff,0xff,0x00,0x7f
,0xff,0x03,0xff,0xff,0xc0,0x7f
,0xff,0x03,0xff,0xff,0xf0,0x3f
,0xff,0x03,0xff,0xff,0xf3,0x1f
,0xff,0x81,0xff,0xff,0xf3,0xff
,0xff,0x81,0xff,0xff,0xf7,0xff
,0xff,0x80,0xff,0xff,0xff,0xff
,0xff,0xc0,0x7f,0xff,0xff,0xff
,0xff,0xe0,0x3f,0xff,0xff,0xff
,0xff,0xf0,0x1f,0xfd,0xff,0xff
,0xff,0xf8,0x07,0xe3,0xff,0xff
,0xff,0xfc,0x00,0x0f,0xff,0xff
,0xff,0xff,0x00,0x3f,0xff,0xff
,0xff,0xff,0xf1,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
,0xff,0xff,0xff,0xff,0xff,0xff
)
 
i2c = I2C(sda=Pin(4), scl=Pin(5))
display = ssd1306.SSD1306_I2C(64, 48, i2c)
 
display.fill(0)
for y in range(48):
    for x in range(48):
        display.pixel(10 + x, y, not logo[y * 6 + x // 8] & (1<<(7 - x % 8)))
display.show()

Diğer resim örnekleri

Bir cevap yazın

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir