Skip to content

Instantly share code, notes, and snippets.

@danielealbano
Last active April 5, 2020 19:24
Show Gist options
  • Save danielealbano/911c9607c959105f4c93fd66e52ce59b to your computer and use it in GitHub Desktop.
Save danielealbano/911c9607c959105f4c93fd66e52ce59b to your computer and use it in GitHub Desktop.
import ntptime
import network
import time
import machine
import utime
import gc
import urequests
import lvgl as lv
import lvesp32
import ILI9341 as ili
wlan = None
label_datetime = None
def wlan_do_connect():
wlan = network.WLAN(network.STA_IF)
if not wlan.isconnected():
print('connecting to network...')
wlan.active(False)
wlan.active(True)
wlan.disconnect()
wlan.connect('SSID', 'PASSWORD')
while not wlan.isconnected():
print(".", end="")
time.sleep(1)
print('network config:', wlan.ifconfig())
return wlan
def build_timestamp(datetime):
return '{0:04d}-{1:02d}-{2:02d} {3:02d}:{4:02d}:{5:02d}'.format(*datetime)
def update_clock(user_data=None):
global label_datetime
if (label_datetime):
timestamp = "Now: {0}\nFree Memory: {1:0.2f}kb".format(build_timestamp(utime.localtime()), (gc.mem_free() / 1024))
label_datetime.set_text(timestamp)
gc.collect()
def update_status(text, show_preload=False):
global preload, label_status
label_status.set_text(text)
if show_preload:
label_status.set_x(25)
else:
label_status.set_x(5)
preload.set_hidden(not show_preload)
time.sleep(0.05)
def update_feed_list_ui(user_data=None):
global label_datetime, label_status, feed_scr, feed_list
update_status("Loading feed...", True)
feed_response = urequests.get('https://newsapi.org/v2/top-headlines?country=ie&category=business&apiKey=__KEY__')
update_status("Parsing feed...", True)
feed = feed_response.json()
update_status("Updating UI...", True)
feed_list_new = lv.list(feed_scr, feed_list)
feed_list_new.set_hidden(True)
start_time = utime.ticks_ms()
articles = feed['articles']
articles_numbers = [str(i) for i in range(1, len(articles) + 1)]
counter = 0
for article in articles:
title = article['title']
text = ''.join([articles_numbers[counter], ') ', title])
pos_y = label_datetime.get_y() + label_datetime.get_height() + 5 + (counter * 16)
label = lv.label(feed_list_new)
label.set_text(text)
label.set_long_mode(lv.label.LONG.DOT)
label.set_width(230)
label.set_align(lv.label.ALIGN.LEFT)
label.align(None, lv.ALIGN.IN_TOP_LEFT, 5, 5)
counter += 1
end_time = utime.ticks_ms()
preload.set_hidden(True)
feed_list.clean()
feed_list.delete()
feed_list_new.set_hidden(False)
feed_list = feed_list_new
update_status("Updated at {}".format(build_timestamp(utime.localtime())), False)
disp = ili.display(miso=22, mosi=23, clk=18, cs=27, dc=26, rst=5, backlight=2)
disp.init()
disp_buf1 = lv.disp_buf_t()
buf1_1 = bytearray(240*10)
lv.disp_buf_init(disp_buf1,buf1_1, None, len(buf1_1)//4)
disp_drv = lv.disp_drv_t()
lv.disp_drv_init(disp_drv)
disp_drv.buffer = disp_buf1
disp_drv.flush_cb = disp.flush
disp_drv.hor_res = 240
disp_drv.ver_res = 320
lv.disp_drv_register(disp_drv)
feed_scr = lv.obj()
label_datetime = lv.label(feed_scr)
label_datetime.set_size(240, 20)
label_datetime.align(None, lv.ALIGN.IN_TOP_LEFT, 5, 5)
label_datetime.set_align(lv.label.ALIGN.LEFT)
update_clock()
label_status = lv.label(feed_scr)
label_status.set_size(240, 10)
label_status.align(None, lv.ALIGN.IN_BOTTOM_LEFT, 5, 0)
label_status.set_align(lv.label.ALIGN.LEFT)
feed_list = lv.list(feed_scr)
feed_list.set_hidden(True)
feed_list.set_size(230, 240)
feed_list.align(label_datetime, lv.ALIGN.OUT_BOTTOM_LEFT, 0, 10)
feed_list.set_opa_scale_enable(True)
feed_list.set_opa_scale(255)
style_preloader = lv.style_t(lv.style_plain)
style_preloader.line.width = 4
style_preloader.line.color = lv.color_hex3(0x258)
style_preloader.body.border.color = lv.color_hex3(0xBBB)
style_preloader.body.border.width = 4
style_preloader.body.padding.left = 0
preload = lv.preload(feed_scr)
preload.set_hidden(False)
preload.set_size(15, 15)
preload.align(None, lv.ALIGN.IN_BOTTOM_LEFT, 5, -3)
preload.set_style(lv.preload.STYLE.MAIN, style_preloader)
update_status("")
lv.scr_load(feed_scr)
clock_timer = machine.Timer(0)
clock_timer.init(period=1000, mode=machine.Timer.PERIODIC, callback=update_clock)
update_status("Connecting to WiFi...", True)
wlan = wlan_do_connect()
update_status("Fetching current time", True)
ntptime.settime()
update_feed_list_ui()
update_feed_timer = machine.Timer(1)
update_feed_timer.init(period=60000, mode=machine.Timer.PERIODIC, callback=update_feed_list_ui)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment