11// SPDX-License-Identifier: MIT
2+ // Dice Roller — EoS LVGL Application
23#include "dice.h"
34#include <stdbool.h>
4- static bool dice_init (lv_obj_t * parent ) { (void )parent ; return true; }
5- static void dice_deinit (void ) { }
5+ #include <string.h>
6+
7+ typedef struct {
8+ lv_obj_t * lbl_result ;
9+ lv_obj_t * lbl_history ;
10+ int history [10 ];
11+ int hist_len ;
12+ uint64_t seed ;
13+ lv_timer_t * timer ;
14+ } dice_ctx_t ;
15+ static dice_ctx_t ctx ;
16+
17+
18+ static void dice_roll_cb (lv_event_t * e ) {
19+ (void )e ;
20+ ctx .seed = ctx .seed * 6364136223846793005ULL + 1442695040888963407ULL ;
21+ int val = (int )((ctx .seed >> 33 ) % 6 ) + 1 ;
22+ lv_label_set_text_fmt (ctx .lbl_result , "%d" , val );
23+ if (ctx .hist_len < 10 ) ctx .history [ctx .hist_len ++ ] = val ;
24+ else { for (int i = 0 ;i < 9 ;i ++ ) ctx .history [i ]= ctx .history [i + 1 ]; ctx .history [9 ]= val ; }
25+ char buf [64 ] = "History: " ;
26+ for (int i = 0 ;i < ctx .hist_len ;i ++ ) { char tmp [4 ]; lv_snprintf (tmp ,4 ,"%d " ,ctx .history [i ]); lv_strncat (buf ,tmp ,sizeof (buf )- strlen (buf )- 1 ); }
27+ lv_label_set_text (ctx .lbl_history , buf );
28+ }
29+
30+ static bool dice_init (lv_obj_t * parent ) {
31+
32+ ctx .hist_len = 0 ; ctx .seed = 42 ;
33+ lv_obj_t * btn = lv_btn_create (parent );
34+ lv_obj_align (btn , LV_ALIGN_CENTER , 0 , -40 );
35+ lv_obj_set_size (btn , 140 , 56 );
36+ lv_obj_t * lbl = lv_label_create (btn );
37+ lv_label_set_text (lbl , "Roll D6" );
38+ lv_obj_add_event_cb (btn , dice_roll_cb , LV_EVENT_CLICKED , NULL );
39+ ctx .lbl_result = lv_label_create (parent );
40+ lv_obj_align (ctx .lbl_result , LV_ALIGN_CENTER , 0 , 30 );
41+ lv_obj_set_style_text_font (ctx .lbl_result , & lv_font_montserrat_48 , 0 );
42+ lv_label_set_text (ctx .lbl_result , "-" );
43+ ctx .lbl_history = lv_label_create (parent );
44+ lv_obj_align (ctx .lbl_history , LV_ALIGN_CENTER , 0 , 90 );
45+ lv_label_set_text (ctx .lbl_history , "History: —" );
46+ return true;
47+
48+ }
49+ static void dice_deinit (void ) {
50+ if (ctx .timer ) { lv_timer_del (ctx .timer ); ctx .timer = NULL ; }
51+ }
652static void dice_on_show (void ) { }
753static void dice_on_hide (void ) { }
854const eapps_app_info_t dice_info = {
9- .id = "dice" , .name = "Dice" , .icon = "die " ,
10- .description = "Dice roller with animation " , .category = EAPPS_CAT_GAMES , .version = "2.0.0" ,
55+ .id = "dice" , .name = "Dice Roller " , .icon = "dic " ,
56+ .description = "Multi-dice roller with history " , .category = EAPPS_CAT_GAMES , .version = "2.0.0" ,
1157};
1258const eapps_app_lifecycle_t dice_lifecycle = {
1359 .init = dice_init , .deinit = dice_deinit ,
1460 .on_show = dice_on_show , .on_hide = dice_on_hide ,
15- };
61+ };
0 commit comments