Skip to content

Commit

Permalink
feat: add screen where user can type pin number
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanOricil committed Apr 22, 2024
1 parent 48d442c commit e5a921e
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 15 deletions.
46 changes: 46 additions & 0 deletions src/ui/screens/ui_pin_screen.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file was generated by SquareLine Studio
// SquareLine Studio version: SquareLine Studio 1.3.4
// LVGL version: 8.3.6
// Project name: SquareLine_Project

#include "../ui.h"

void ui_pin_screen_screen_init(void) {
ui_pin_screen = lv_obj_create(NULL);
lv_obj_add_flag( ui_pin_screen, LV_OBJ_FLAG_SCROLLABLE);
lv_obj_set_scrollbar_mode(ui_pin_screen, LV_SCROLLBAR_MODE_OFF);
lv_obj_set_style_bg_color(ui_pin_screen, lv_color_make(12, 18, 30), LV_PART_MAIN | LV_STATE_DEFAULT );
lv_obj_set_style_bg_opa(ui_pin_screen, 255, LV_PART_MAIN| LV_STATE_DEFAULT);
lv_obj_set_style_pad_all(ui_pin_screen, 5, LV_PART_MAIN | LV_STATE_DEFAULT);
lv_obj_set_flex_flow(ui_pin_screen, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(ui_pin_screen, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
lv_obj_set_layout(ui_pin_screen, LV_LAYOUT_FLEX);


lv_obj_t* ui_pin_textarea = lv_textarea_create(ui_pin_screen);
lv_textarea_set_text(ui_pin_textarea, "");
lv_textarea_set_max_length(ui_pin_textarea, PIN_LENGTH);
lv_textarea_set_one_line(ui_pin_textarea, true);
lv_textarea_set_password_mode(ui_pin_textarea, true);
lv_obj_align(ui_pin_textarea, LV_ALIGN_TOP_LEFT, 0, 0);
lv_obj_set_pos(ui_pin_textarea, 0, 0);
lv_obj_set_width(ui_pin_textarea, LV_HOR_RES - 10);
lv_obj_set_height(ui_pin_textarea, 40);


static const char* btnm_map[] = {
"1", "2", "3", "4", "5", "\n",
"6", "7", "8", "9", "0", "\n",
"OK", "Delete", ""
};
lv_obj_t* keyboard = lv_btnmatrix_create(ui_pin_screen);
lv_btnmatrix_set_map(keyboard, btnm_map);
lv_obj_set_width(keyboard, LV_HOR_RES - 10);
lv_obj_set_flex_grow(keyboard, 1);
lv_obj_align(keyboard, LV_ALIGN_CENTER, 0, 0);

lv_obj_add_event_cb(keyboard, ui_event_keyboard_button, LV_EVENT_ALL, NULL);
}



1 change: 0 additions & 1 deletion src/ui/screens/ui_totp_screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ lv_obj_t* create_totp_component(
){
lv_obj_t* container = lv_obj_create(parent);
lv_obj_set_width(container, 150);
lv_obj_set_height(container, 70);
lv_obj_set_height(container, LV_SIZE_CONTENT);
lv_obj_set_flex_flow(container, LV_FLEX_FLOW_COLUMN);
lv_obj_set_flex_align(container, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
Expand Down
27 changes: 19 additions & 8 deletions src/ui/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@

// CUSTOM EVENTS
uint32_t LV_EVENT_SETUP_COMPLETE;

#define PIN_LENGTH 4

///////////////////// VARIABLES ////////////////////

// SCREEN: ui_totp_screen
lv_obj_t *ui_totp_screen;
lv_obj_t *ui_pin_screen;
lv_obj_t *ui_pin_textarea;
lv_obj_t *ui____initial_actions0;
void ui_totp_screen_screen_init(void);
void ui_event_totp_component_label(lv_event_t * e);
void ui_event_totp_component_bar(lv_event_t * e);
void ui_pin_screen_screen_init(void);
void ui_event_totp_component_label(lv_event_t *e);
void ui_event_totp_component_bar(lv_event_t *e);
void ui_event_keyboard_button(lv_event_t *e);

///////////////////// TEST LVGL SETTINGS ////////////////////
#if LV_COLOR_DEPTH != 16
Expand All @@ -27,22 +31,28 @@ void ui_event_totp_component_bar(lv_event_t * e);
///////////////////// ANIMATIONS ////////////////////

///////////////////// FUNCTIONS ////////////////////
void ui_event_totp_component_label(lv_event_t * e) {
void ui_event_totp_component_label(lv_event_t *e) {
lv_event_code_t event_code = lv_event_get_code(e);
lv_obj_t * target = lv_event_get_target(e);
if (event_code == LV_EVENT_VALUE_CHANGED) {
on_totp_component_label_value_changed(e);
}
}

void ui_event_totp_component_bar(lv_event_t * e) {
void ui_event_totp_component_bar(lv_event_t *e) {
lv_event_code_t event_code = lv_event_get_code(e);
lv_obj_t * target = lv_event_get_target(e);
if (event_code == LV_EVENT_VALUE_CHANGED) {
on_totp_component_bar_value_changed(e);
}
}

void ui_event_keyboard_button(lv_event_t *e) {
lv_event_code_t event_code = lv_event_get_code(e);
if (event_code == LV_EVENT_VALUE_CHANGED) {
on_keyboard_button_clicked(e);
}
}



///////////////////// SCREENS ////////////////////

Expand All @@ -58,8 +68,9 @@ void ui_init( void ){
LV_EVENT_SETUP_COMPLETE = lv_event_register_id();
lv_disp_set_theme(disp, theme);
ui_totp_screen_screen_init();
ui_pin_screen_screen_init();
ui____initial_actions0 = lv_obj_create(NULL);
lv_disp_load_scr(ui_totp_screen);
lv_disp_load_scr(ui_pin_screen);
}


Expand Down
12 changes: 9 additions & 3 deletions src/ui/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@
extern "C" {
#endif

#include "lvgl.h"

#include "lvgl.h"
#include "ui_helpers.h"
#include "ui_events.h"

#define PIN_LENGTH 4

// SCREEN: ui_TotpScreen
extern lv_obj_t *ui_totp_screen;
extern lv_obj_t *ui_pin_screen;
extern lv_obj_t *ui_pin_textarea;
extern lv_obj_t *ui____initial_actions0;
void ui_event_totp_component_label(lv_event_t * e);
void ui_event_totp_component_bar(lv_event_t * e);
void ui_event_totp_component_label(lv_event_t *e);
void ui_event_totp_component_bar(lv_event_t *e);
void ui_event_keyboard_button(lv_event_t *e);
void ui_init(void);
void refresh_totp_labels();
void refresh_counter_bars();
Expand Down
35 changes: 34 additions & 1 deletion src/ui/ui_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,42 @@ void on_totp_component_label_value_changed(lv_event_t *e) {
}
}

void on_totp_component_bar_value_changed(lv_event_t *e){
void on_totp_component_bar_value_changed(lv_event_t *e) {
LV_LOG_TRACE("on_totp_component_bar_value_changed");
lv_obj_t *bar = lv_event_get_target(e);
int val = calculate_new_bar_value();
lv_bar_set_value(bar, val, LV_ANIM_OFF);
}

void on_keyboard_button_clicked(lv_event_t *e){
lv_obj_t *keyboard = lv_event_get_target(e);
uint16_t btn_id = lv_btnmatrix_get_selected_btn(keyboard);
printf("Debug message: %u\n", btn_id);

if(btn_id != LV_BTNMATRIX_BTN_NONE){
// const char* btn_text = lv_btnmatrix_get_btn_text(keyboard, btn_id);
// const char* current_text_const = lv_textarea_get_text(ui_pin_textarea);

// char* current_text = lv_mem_alloc(strlen(current_text_const) + 1);
// strcpy(current_text, current_text_const);

// if (btn_text != NULL && strlen(btn_text) == 1) {
// if (strlen(current_text) < PIN_LENGTH) {
// strcat(current_text, btn_text);
// lv_textarea_set_text(ui_pin_textarea, current_text);
// }
// } else if (strcmp(btn_text, "Delete") == 0) {
// size_t len = strlen(current_text);
// if (len > 0) {
// current_text[len - 1] = '\0';
// lv_textarea_set_text(ui_pin_textarea, current_text);
// }
// } else if (strcmp(btn_text, "OK") == 0) {
// if (strlen(current_text) == PIN_LENGTH) {
// lv_disp_load_scr(ui_totp_screen);
// }
// }

// lv_mem_free(current_text);
}
}
5 changes: 3 additions & 2 deletions src/ui/ui_events.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ typedef struct {
int index;
} TotpValueChangeEvent;

void on_totp_component_label_value_changed(lv_event_t * e);
void on_totp_component_bar_value_changed(lv_event_t * e);
void on_totp_component_label_value_changed(lv_event_t *e);
void on_totp_component_bar_value_changed(lv_event_t *e);
void on_keyboard_button_clicked(lv_event_t *e);

#ifdef __cplusplus
} /*extern "C"*/
Expand Down

0 comments on commit e5a921e

Please sign in to comment.