LVGL MicroPython

Somsak Lima
4 min readOct 31, 2021

หาวิธีติดตั้งใช้งานง่ายๆ LVGL Library สำหรับ MicroPython อยู่
MicroPython มี LVGL เรียกว่า lv_micropython ดูรายละเอียดได้จาก https://github.com/lvgl/lv_micropython

หรือดูได้จาก
https://github.com/Mair/learn-lvgl/tree/master/esp32

ตัวอย่างที่เอาไปใช้ได้ คลิก

คลิก Simulator

Copy code จากจอซ้ายไปใส่ใน Thonny

#import display_driver
from ili9XXX import ili9341
disp = ili9341()

ใส่ #หน้าบรรทัด import display_driver

เพิ่มสองบรรทัด

หาก เจอ Error ตอน Run ว่า RuntimeError: Not enough DMA-able memory to allocate display buffer ให้ใช้คำสั่ง
import machine
machine.reset()

วิธีเชื่อมขา

  • ILI9341: miso=5, mosi=18, clk=19, cs=13, dc=12, rst=4, power=14, backlight=15, spihost=esp.HSPI_HOST, mhz=40, factor=4, hybrid=True
  • XPT2046: cs=25, spihost=esp.HSPI_HOST, mhz=5, max_cmds=16, cal_x0 = 3783, cal_y0 = 3948, cal_x1 = 242, cal_y1 = 423, transpose = True, samples = 3

MS5Stack

from ili9XXX import ili9341
lcd = ili9341(spihost=1, mosi=23, miso=38, clk=18, dc=15, cs=5, invert=True, rot=0xC0, width=320, height=240, rst=-1, power=-1, backlight=-1)

คู่มือ LVGL Document V.8.1 คลิก

LittlevGL+MicroPython simulator คลิก คลิก

อาจจะ run ตัวอย่าง เช่น

import lvgl as lv
import display_driver
##### main script #####
def btn_event_cb(e):
print("Clicked")
# Create a Button and a Label
btn = lv.btn(lv.scr_act())
btn.set_pos(10, 10)
btn.set_size(100, 50)
btn.add_event_cb(btn_event_cb, lv.EVENT.CLICKED, None)
label = lv.label(btn)
label.set_text("Button")
label.center()

ตัวอย่างอื่น เช่น ที่ https://github.com/lvgl/lv_binding_micropython/tree/master/examples

onion ก็มีวิธีสร้าง

LVGL MicroPython ST7789 Driver คลิก

มี Firmware สำหรับ
LILYGO® TTGO T-Display or GENERIC ESP32
LILYGO® T-Watch 2020 or 16MB GENERIC ESP32

ใน Dir Firmware

Example
แสดง อุณหภูมิจาก Sensor BME280

##### startup script #####
import bme280
import lvgl as lv
import utime as time
from machine import Pin, SoftI2C
from ili9XXX import ili9341
disp = ili9341()
addrOled = 60 # 0x3c
addrBME280 = 118 # 0x76
hSize = 64 # Hauteur ecran en pixels | display heigh in pixels
wSize = 128 # Largeur ecran en pixels | display width in pixels
oledIsConnected = False
bmeIsConnected = False
temp = 0
pa = 0
hum = 0
i2c = SoftI2C(scl=Pin(22), sda=Pin(21), freq=10000) # ESP32 Dev Board /myown
##### main script #####
print(“Scan i2c bus…”)
devices = i2c.scan()
if len(devices) == 0:
print(“No i2c device !”)
else:
print(“i2c devices found:”, len(devices))
for device in devices:
if device == addrOled:
oledIsConnected = True
if device == addrBME280:
bmeIsConnected = True
print(device)
def set_value(indic, v):
meter.set_indicator_value(indic, v)
lv.init()
scr = lv.obj()
btn = lv.btn(scr)
btn.align(lv.ALIGN.CENTER, -5, 120)
label = lv.label(btn)
label.set_text(“Temp”)
lv.scr_load(scr)
meter = lv.meter(lv.scr_act())
meter.center()
meter.set_size(200, 200)
# Add a scale first
scale = meter.add_scale()
meter.set_scale_ticks(scale, 51, 2, 10, lv.palette_main(lv.PALETTE.GREY))
meter.set_scale_major_ticks(scale, 10, 4, 15, lv.color_black(), 10)
indic = lv.meter_indicator_t()# Add a blue arc to the start
indic = meter.add_arc(scale, 3, lv.palette_main(lv.PALETTE.BLUE), 0)
meter.set_indicator_start_value(indic, 0)
meter.set_indicator_end_value(indic, 20)
# Make the tick lines blue at the start of the scale
indic = meter.add_scale_lines(
scale, lv.palette_main(lv.PALETTE.BLUE), lv.palette_main(lv.PALETTE.BLUE), False, 0
)
meter.set_indicator_start_value(indic, 0)
meter.set_indicator_end_value(indic, 20)
# Add a red arc to the end
indic = meter.add_arc(scale, 3, lv.palette_main(lv.PALETTE.RED), 0)
meter.set_indicator_start_value(indic, 55)
meter.set_indicator_end_value(indic, 100)
# Make the tick lines red at the end of the scale
indic = meter.add_scale_lines(
scale, lv.palette_main(lv.PALETTE.RED), lv.palette_main(lv.PALETTE.RED), False, 0
)
meter.set_indicator_start_value(indic, 55)
meter.set_indicator_end_value(indic, 100)
# Add a needle line indicator
indic = meter.add_needle_line(scale, 4, lv.palette_main(lv.PALETTE.GREY), -10)
# Create an animation to set the value
a = lv.anim_t()
a.init()
a.set_var(indic)
a.set_values(0, 100)
a.set_time(2000)
while True:
if bmeIsConnected:
bme = bme280.BME280(i2c=i2c, address=addrBME280)
print(“BME280 values:”)
print(bme.values)
temp, pa, hum = bme.values
print(temp)
print(pa)
print(hum)
a.set_custom_exec_cb(lambda a, val: set_value(indic, int(float(temp))))
lv.anim_t.start(a)
time.sleep_ms(5000)

วิธ๊สร้าง Firmware คลิก1 คลิก2

วิธีสร้าง Firwmare MicroPython ทั่วไป คลิก

LVGL Demo คลิก

LVGL example คลิก

--

--

Somsak Lima

สนับสนุนและส่งเสริมให้ผู้สนใจสามารถใช้งานเทคโนโลยี LoRa และ LoRaWAN ได้ โดยนำความรู้ที่ได้ไปต่อยอดเพื่อใช้งาน