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
This commit is contained in:
Amir Gonnen
2022-07-27 10:42:48 +03:00
committed by GitHub
parent ca8f0ab549
commit cd11476592
9 changed files with 91 additions and 55 deletions
+1 -1
View File
@@ -24,7 +24,7 @@ void lv_example_msg_1(void)
lv_obj_align(label, LV_ALIGN_CENTER, 0, 30);
/*Subscribe the label to a message. Also use the user_data to set a format string here.*/
lv_msg_subsribe_obj(MSG_NEW_TEMPERATURE, label, "%d °C");
lv_msg_subscribe_obj(MSG_NEW_TEMPERATURE, label, "%d °C");
}
static void slider_event_cb(lv_event_t * e)
+39
View File
@@ -0,0 +1,39 @@
# 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)
+13 -14
View File
@@ -7,7 +7,7 @@
#define MSG_LOGIN_ERROR 3
#define MSG_LOGIN_OK 4
static void auth_manager(void * s, lv_msg_t * m);
static void auth_manager(lv_msg_t * m);
static void textarea_event_cb(lv_event_t * e);
static void log_out_event_cb(lv_event_t * e);
static void start_engine_msg_event_cb(lv_event_t * e);
@@ -19,7 +19,7 @@ static void info_label_msg_event_cb(lv_event_t * e);
*/
void lv_example_msg_2(void)
{
lv_msg_subsribe(MSG_LOGIN_ATTEMPT, auth_manager, "hello");
lv_msg_subscribe(MSG_LOGIN_ATTEMPT, auth_manager, "hello");
/*Create a slider in the center of the display*/
lv_obj_t * ta = lv_textarea_create(lv_scr_act());
@@ -29,9 +29,9 @@ void lv_example_msg_2(void)
lv_textarea_set_password_mode(ta, true);
lv_textarea_set_placeholder_text(ta, "The password is: hello");
lv_obj_add_event_cb(ta, textarea_event_cb, LV_EVENT_ALL, NULL);
lv_msg_subsribe_obj(MSG_LOGIN_ERROR, ta, NULL);
lv_msg_subsribe_obj(MSG_LOGIN_OK, ta, NULL);
lv_msg_subsribe_obj(MSG_LOG_OUT, ta, NULL);
lv_msg_subscribe_obj(MSG_LOGIN_ERROR, ta, NULL);
lv_msg_subscribe_obj(MSG_LOGIN_OK, ta, NULL);
lv_msg_subscribe_obj(MSG_LOG_OUT, ta, NULL);
lv_obj_t * kb = lv_keyboard_create(lv_scr_act());
lv_keyboard_set_textarea(kb, ta);
@@ -43,8 +43,8 @@ void lv_example_msg_2(void)
btn = lv_btn_create(lv_scr_act());
lv_obj_set_pos(btn, 240, 10);
lv_obj_add_event_cb(btn, log_out_event_cb, LV_EVENT_ALL, NULL);
lv_msg_subsribe_obj(MSG_LOGIN_OK, btn, NULL);
lv_msg_subsribe_obj(MSG_LOG_OUT, btn, NULL);
lv_msg_subscribe_obj(MSG_LOGIN_OK, btn, NULL);
lv_msg_subscribe_obj(MSG_LOG_OUT, btn, NULL);
label = lv_label_create(btn);
lv_label_set_text(label, "LOG OUT");
@@ -54,17 +54,17 @@ void lv_example_msg_2(void)
lv_label_set_text(label, "");
lv_obj_add_event_cb(label, info_label_msg_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
lv_obj_set_pos(label, 10, 60);
lv_msg_subsribe_obj(MSG_LOGIN_ERROR, label, NULL);
lv_msg_subsribe_obj(MSG_LOGIN_OK, label, NULL);
lv_msg_subsribe_obj(MSG_LOG_OUT, label, NULL);
lv_msg_subscribe_obj(MSG_LOGIN_ERROR, label, NULL);
lv_msg_subscribe_obj(MSG_LOGIN_OK, label, NULL);
lv_msg_subscribe_obj(MSG_LOG_OUT, label, NULL);
/*Create button which will be active only when logged in*/
btn = lv_btn_create(lv_scr_act());
lv_obj_set_pos(btn, 10, 80);
lv_obj_add_event_cb(btn, start_engine_msg_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
lv_msg_subsribe_obj(MSG_LOGIN_OK, btn, NULL);
lv_msg_subsribe_obj(MSG_LOG_OUT, btn, NULL);
lv_msg_subscribe_obj(MSG_LOGIN_OK, btn, NULL);
lv_msg_subscribe_obj(MSG_LOG_OUT, btn, NULL);
label = lv_label_create(btn);
lv_label_set_text(label, "START ENGINE");
@@ -72,9 +72,8 @@ void lv_example_msg_2(void)
lv_msg_send(MSG_LOG_OUT, NULL);
}
static void auth_manager(void * s, lv_msg_t * m)
static void auth_manager(lv_msg_t * m)
{
LV_UNUSED(s);
const char * pin_act = lv_msg_get_payload(m);
const char * pin_expexted = lv_msg_get_user_data(m);
if(strcmp(pin_act, pin_expexted) == 0) {
+9 -12
View File
@@ -8,8 +8,7 @@
#define MSG_UPDATE 4
#define MSG_UPDATE_REQUEST 5
static void value_handler(void * s, lv_msg_t * m);
static void value_handler(void * s, lv_msg_t * m);
static void value_handler(lv_msg_t * m);
static void btn_event_cb(lv_event_t * e);
static void label_event_cb(lv_event_t * e);
static void slider_event_cb(lv_event_t * e);
@@ -23,11 +22,11 @@ static void slider_event_cb(lv_event_t * e);
void lv_example_msg_3(void)
{
lv_msg_subsribe(MSG_INC, value_handler, NULL);
lv_msg_subsribe(MSG_DEC, value_handler, NULL);
lv_msg_subsribe(MSG_SET, value_handler, NULL);
lv_msg_subsribe(MSG_UPDATE, value_handler, NULL);
lv_msg_subsribe(MSG_UPDATE_REQUEST, value_handler, NULL);
lv_msg_subscribe(MSG_INC, value_handler, NULL);
lv_msg_subscribe(MSG_DEC, value_handler, NULL);
lv_msg_subscribe(MSG_SET, value_handler, NULL);
lv_msg_subscribe(MSG_UPDATE, value_handler, NULL);
lv_msg_subscribe(MSG_UPDATE_REQUEST, value_handler, NULL);
lv_obj_t * panel = lv_obj_create(lv_scr_act());
lv_obj_set_size(panel, 250, LV_SIZE_CONTENT);
@@ -51,7 +50,7 @@ void lv_example_msg_3(void)
lv_obj_set_flex_grow(label, 2);
lv_obj_set_style_text_align(label, LV_TEXT_ALIGN_CENTER, 0);
lv_label_set_text(label, "?");
lv_msg_subsribe_obj(MSG_UPDATE, label, NULL);
lv_msg_subscribe_obj(MSG_UPDATE, label, NULL);
lv_obj_add_event_cb(label, label_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
/*Down button*/
@@ -67,7 +66,7 @@ void lv_example_msg_3(void)
lv_obj_set_flex_grow(slider, 1);
lv_obj_add_flag(slider, LV_OBJ_FLAG_FLEX_IN_NEW_TRACK);
lv_obj_add_event_cb(slider, slider_event_cb, LV_EVENT_ALL, NULL);
lv_msg_subsribe_obj(MSG_UPDATE, slider, NULL);
lv_msg_subscribe_obj(MSG_UPDATE, slider, NULL);
/* As there are new UI elements that don't know the system's state
@@ -76,10 +75,8 @@ void lv_example_msg_3(void)
}
static void value_handler(void * s, lv_msg_t * m)
static void value_handler(lv_msg_t * m)
{
LV_UNUSED(s);
static int32_t value = 10;
int32_t old_value = value;
switch(lv_msg_get_id(m)) {