1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00
lvgl/examples/others/msg/lv_example_msg_1.py
Amir Gonnen cd11476592
fix(msg): make it work with Micropython (#3488)
* fix(msg): fill callback prototype

lv_msg_subscribe_cb_t does not follow the callback conventions.
Remove unused void* argument to fix that.

* fix(msg):  move subs_ll to gc roots

* fix(msg): rename subsribe to subscribe

* fix(msg): update docs

* fix(msg): Add example_1

* fix(msg): fix include paths

* fix(mgs): Fix python example typo and comments

* fix(msg):  LV_EVENT_MSG_RECEIVED event code
2022-07-27 09:42:48 +02:00

40 lines
1.2 KiB
Python

# Define a message ID
MSG_NEW_TEMPERATURE = const(1)
# Define the object that will be sent as msg payload
class Temperature:
def __init__(self, value):
self.value = value
def __repr__(self):
return f"{self.value} °C"
def slider_event_cb(e):
slider = e.get_target()
v = slider.get_value()
# Notify all subscribers (only the label now) that the slider value has been changed
lv.msg_send(MSG_NEW_TEMPERATURE, Temperature(v))
def label_event_cb(e):
label = e.get_target()
msg = e.get_msg()
# Respond only to MSG_NEW_TEMPERATURE message
if msg.get_id() == MSG_NEW_TEMPERATURE:
payload = msg.get_payload()
temprature = payload.__cast__()
label.set_text(str(temprature))
# Create a slider in the center of the display
slider = lv.slider(lv.scr_act())
slider.center()
slider.add_event_cb(slider_event_cb, lv.EVENT.VALUE_CHANGED, None)
# Create a label below the slider
label = lv.label(lv.scr_act())
label.add_event_cb(label_event_cb, lv.EVENT.MSG_RECEIVED, None)
label.set_text("0%")
label.align(lv.ALIGN.CENTER, 0, 30)
# Subscribe the label to a message
lv.msg_subscribe_obj(MSG_NEW_TEMPERATURE, label, None)