acid-drop

- Hacking the planet from a LilyGo T-Deck using custom firmware
git clone git://git.acid.vegas/acid-drop.git
Log | Files | Refs | Archive | README | LICENSE

lv_example_msg_2.c (5707B)

      1 #include "../../lv_examples.h"
      2 #if LV_USE_MSG && LV_USE_SLIDER && LV_USE_LABEL && LV_BUILD_EXAMPLES
      3 
      4 /*Define a message ID*/
      5 #define MSG_LOGIN_ATTEMPT   1
      6 #define MSG_LOG_OUT         2
      7 #define MSG_LOGIN_ERROR     3
      8 #define MSG_LOGIN_OK        4
      9 
     10 static void auth_manager(void * s, lv_msg_t * m);
     11 static void textarea_event_cb(lv_event_t * e);
     12 static void log_out_event_cb(lv_event_t * e);
     13 static void start_engine_msg_event_cb(lv_event_t * e);
     14 static void info_label_msg_event_cb(lv_event_t * e);
     15 
     16 /**
     17  * Simple PIN login screen.
     18  * No global variables are used, all state changes are communicated via messages.
     19  */
     20 void lv_example_msg_2(void)
     21 {
     22     lv_msg_subsribe(MSG_LOGIN_ATTEMPT, auth_manager, "hello");
     23 
     24     /*Create a slider in the center of the display*/
     25     lv_obj_t * ta = lv_textarea_create(lv_scr_act());
     26     lv_obj_set_pos(ta, 10, 10);
     27     lv_obj_set_width(ta, 200);
     28     lv_textarea_set_one_line(ta, true);
     29     lv_textarea_set_password_mode(ta, true);
     30     lv_textarea_set_placeholder_text(ta, "The password is: hello");
     31     lv_obj_add_event_cb(ta, textarea_event_cb, LV_EVENT_ALL, NULL);
     32     lv_msg_subsribe_obj(MSG_LOGIN_ERROR, ta, NULL);
     33     lv_msg_subsribe_obj(MSG_LOGIN_OK, ta, NULL);
     34     lv_msg_subsribe_obj(MSG_LOG_OUT, ta, NULL);
     35 
     36     lv_obj_t * kb = lv_keyboard_create(lv_scr_act());
     37     lv_keyboard_set_textarea(kb, ta);
     38 
     39     lv_obj_t * btn;
     40     lv_obj_t * label;
     41 
     42     /*Create a log out button which will be active only when logged in*/
     43     btn = lv_btn_create(lv_scr_act());
     44     lv_obj_set_pos(btn, 240, 10);
     45     lv_obj_add_event_cb(btn, log_out_event_cb, LV_EVENT_ALL, NULL);
     46     lv_msg_subsribe_obj(MSG_LOGIN_OK, btn, NULL);
     47     lv_msg_subsribe_obj(MSG_LOG_OUT, btn, NULL);
     48 
     49     label = lv_label_create(btn);
     50     lv_label_set_text(label, "LOG OUT");
     51 
     52     /*Create a label to show info*/
     53     label = lv_label_create(lv_scr_act());
     54     lv_label_set_text(label, "");
     55     lv_obj_add_event_cb(label, info_label_msg_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
     56     lv_obj_set_pos(label, 10, 60);
     57     lv_msg_subsribe_obj(MSG_LOGIN_ERROR, label, NULL);
     58     lv_msg_subsribe_obj(MSG_LOGIN_OK, label, NULL);
     59     lv_msg_subsribe_obj(MSG_LOG_OUT, label, NULL);
     60 
     61     /*Create button which will be active only when logged in*/
     62     btn = lv_btn_create(lv_scr_act());
     63     lv_obj_set_pos(btn, 10, 80);
     64     lv_obj_add_event_cb(btn, start_engine_msg_event_cb, LV_EVENT_MSG_RECEIVED, NULL);
     65     lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
     66     lv_msg_subsribe_obj(MSG_LOGIN_OK, btn, NULL);
     67     lv_msg_subsribe_obj(MSG_LOG_OUT, btn, NULL);
     68 
     69     label = lv_label_create(btn);
     70     lv_label_set_text(label, "START ENGINE");
     71 
     72     lv_msg_send(MSG_LOG_OUT, NULL);
     73 }
     74 
     75 static void auth_manager(void * s, lv_msg_t * m)
     76 {
     77     LV_UNUSED(s);
     78     const char * pin_act = lv_msg_get_payload(m);
     79     const char * pin_expexted = lv_msg_get_user_data(m);
     80     if(strcmp(pin_act, pin_expexted) == 0) {
     81         lv_msg_send(MSG_LOGIN_OK, NULL);
     82     }
     83     else {
     84         lv_msg_send(MSG_LOGIN_ERROR, "Incorrect PIN");
     85     }
     86 
     87 }
     88 
     89 static void textarea_event_cb(lv_event_t * e)
     90 {
     91     lv_obj_t * ta = lv_event_get_target(e);
     92     lv_event_code_t code = lv_event_get_code(e);
     93     if(code == LV_EVENT_READY) {
     94         lv_msg_send(MSG_LOGIN_ATTEMPT, lv_textarea_get_text(ta));
     95     }
     96     else if(code == LV_EVENT_MSG_RECEIVED) {
     97         lv_msg_t * m = lv_event_get_msg(e);
     98         switch(lv_msg_get_id(m)) {
     99             case MSG_LOGIN_ERROR:
    100                 /*If there was an error, clean the text area*/
    101                 if(strlen(lv_msg_get_payload(m))) lv_textarea_set_text(ta, "");
    102                 break;
    103             case MSG_LOGIN_OK:
    104                 lv_obj_add_state(ta, LV_STATE_DISABLED);
    105                 lv_obj_clear_state(ta, LV_STATE_FOCUSED | LV_STATE_FOCUS_KEY);
    106                 break;
    107             case MSG_LOG_OUT:
    108                 lv_textarea_set_text(ta, "");
    109                 lv_obj_clear_state(ta, LV_STATE_DISABLED);
    110                 break;
    111         }
    112     }
    113 }
    114 
    115 static void log_out_event_cb(lv_event_t * e)
    116 {
    117     lv_event_code_t code = lv_event_get_code(e);
    118     if(code == LV_EVENT_CLICKED) {
    119         lv_msg_send(MSG_LOG_OUT, NULL);
    120     }
    121     else if(code == LV_EVENT_MSG_RECEIVED) {
    122         lv_msg_t * m = lv_event_get_msg(e);
    123         lv_obj_t * btn = lv_event_get_target(e);
    124         switch(lv_msg_get_id(m)) {
    125             case MSG_LOGIN_OK:
    126                 lv_obj_clear_state(btn, LV_STATE_DISABLED);
    127                 break;
    128             case MSG_LOG_OUT:
    129                 lv_obj_add_state(btn, LV_STATE_DISABLED);
    130                 break;
    131         }
    132     }
    133 }
    134 
    135 static void start_engine_msg_event_cb(lv_event_t * e)
    136 {
    137     lv_msg_t * m = lv_event_get_msg(e);
    138     lv_obj_t * btn = lv_event_get_target(e);
    139     switch(lv_msg_get_id(m)) {
    140         case MSG_LOGIN_OK:
    141             lv_obj_clear_state(btn, LV_STATE_DISABLED);
    142             break;
    143         case MSG_LOG_OUT:
    144             lv_obj_add_state(btn, LV_STATE_DISABLED);
    145             break;
    146     }
    147 }
    148 
    149 static void info_label_msg_event_cb(lv_event_t * e)
    150 {
    151     lv_obj_t * label = lv_event_get_target(e);
    152     lv_msg_t * m = lv_event_get_msg(e);
    153     switch(lv_msg_get_id(m)) {
    154         case MSG_LOGIN_ERROR:
    155             lv_label_set_text(label, lv_msg_get_payload(m));
    156             lv_obj_set_style_text_color(label, lv_palette_main(LV_PALETTE_RED), 0);
    157             break;
    158         case MSG_LOGIN_OK:
    159             lv_label_set_text(label, "Login successful");
    160             lv_obj_set_style_text_color(label, lv_palette_main(LV_PALETTE_GREEN), 0);
    161             break;
    162         case MSG_LOG_OUT:
    163             lv_label_set_text(label, "Logged out");
    164             lv_obj_set_style_text_color(label, lv_palette_main(LV_PALETTE_GREY), 0);
    165             break;
    166         default:
    167             break;
    168     }
    169 }
    170 
    171 #endif