1
1
use super :: * ;
2
2
3
+ pub const SETTINGS_SIZE : usize = 10 ;
4
+ static mut NVM_REF : Option < & mut AtomicStorage < [ u8 ; SETTINGS_SIZE ] > > = None ;
5
+ static mut SWITCH_ARRAY : [ nbgl_contentSwitch_t ; SETTINGS_SIZE ] =
6
+ [ unsafe { const_zero ! ( nbgl_contentSwitch_t) } ; SETTINGS_SIZE ] ;
7
+
3
8
/// Callback triggered by the NBGL API when a setting switch is toggled.
4
9
unsafe extern "C" fn settings_callback ( token : c_int , _index : u8 , _page : c_int ) {
5
10
let idx = token - FIRST_USER_TOKEN as i32 ;
@@ -16,7 +21,7 @@ unsafe extern "C" fn settings_callback(token: c_int, _index: u8, _page: c_int) {
16
21
}
17
22
18
23
if let Some ( data) = NVM_REF . as_mut ( ) {
19
- let mut switch_values: [ u8 ; SETTINGS_SIZE ] = data. get_ref ( ) . clone ( ) ;
24
+ let mut switch_values: [ u8 ; SETTINGS_SIZE ] = * data. get_ref ( ) ;
20
25
if switch_values[ setting_idx] == OFF_STATE {
21
26
switch_values[ setting_idx] = ON_STATE ;
22
27
} else {
@@ -49,6 +54,16 @@ pub struct NbglHomeAndSettings {
49
54
50
55
impl SyncNBGL for NbglHomeAndSettings { }
51
56
57
+ unsafe extern "C" fn quit_cb ( ) {
58
+ exit_app ( 0 ) ;
59
+ }
60
+
61
+ impl < ' a > Default for NbglHomeAndSettings {
62
+ fn default ( ) -> Self {
63
+ Self :: new ( )
64
+ }
65
+ }
66
+
52
67
impl < ' a > NbglHomeAndSettings {
53
68
pub fn new ( ) -> NbglHomeAndSettings {
54
69
NbglHomeAndSettings {
@@ -66,7 +81,7 @@ impl<'a> NbglHomeAndSettings {
66
81
67
82
pub fn glyph ( self , glyph : & ' a NbglGlyph ) -> NbglHomeAndSettings {
68
83
let icon = glyph. into ( ) ;
69
- NbglHomeAndSettings { icon : icon , ..self }
84
+ NbglHomeAndSettings { icon, ..self }
70
85
}
71
86
72
87
pub fn infos (
@@ -75,9 +90,10 @@ impl<'a> NbglHomeAndSettings {
75
90
version : & ' a str ,
76
91
author : & ' a str ,
77
92
) -> NbglHomeAndSettings {
78
- let mut v: Vec < CString > = Vec :: new ( ) ;
79
- v. push ( CString :: new ( version) . unwrap ( ) ) ;
80
- v. push ( CString :: new ( author) . unwrap ( ) ) ;
93
+ let v: Vec < CString > = vec ! [
94
+ CString :: new( version) . unwrap( ) ,
95
+ CString :: new( author) . unwrap( ) ,
96
+ ] ;
81
97
82
98
NbglHomeAndSettings {
83
99
app_name : CString :: new ( app_name) . unwrap ( ) ,
@@ -111,6 +127,10 @@ impl<'a> NbglHomeAndSettings {
111
127
}
112
128
}
113
129
130
+ /// Show the home screen and settings page.
131
+ /// This function will block until an APDU is received or the user quits the app.
132
+ /// DEPRECATED as it constraints to refresh screen for every received APDU.
133
+ /// Use `show_and_return` instead.
114
134
pub fn show < T : TryFrom < ApduHeader > > ( & mut self ) -> Event < T >
115
135
where
116
136
Reply : From < <T as TryFrom < ApduHeader > >:: Error > ,
@@ -124,8 +144,8 @@ impl<'a> NbglHomeAndSettings {
124
144
. collect :: < Vec < _ > > ( ) ;
125
145
126
146
self . info_list = nbgl_contentInfoList_t {
127
- infoTypes : INFO_FIELDS . as_ptr ( ) as * const * const c_char ,
128
- infoContents : self . info_contents_ptr [ ..] . as_ptr ( ) as * const * const c_char ,
147
+ infoTypes : INFO_FIELDS . as_ptr ( ) ,
148
+ infoContents : self . info_contents_ptr [ ..] . as_ptr ( ) ,
129
149
nbInfos : INFO_FIELDS . len ( ) as u8 ,
130
150
} ;
131
151
@@ -190,4 +210,65 @@ impl<'a> NbglHomeAndSettings {
190
210
}
191
211
}
192
212
}
213
+
214
+ /// Show the home screen and settings page.
215
+ /// This function returns immediately after the screen is displayed.
216
+ pub fn show_and_return ( & mut self ) {
217
+ unsafe {
218
+ self . info_contents_ptr = self
219
+ . info_contents
220
+ . iter ( )
221
+ . map ( |s| s. as_ptr ( ) )
222
+ . collect :: < Vec < _ > > ( ) ;
223
+
224
+ self . info_list = nbgl_contentInfoList_t {
225
+ infoTypes : INFO_FIELDS . as_ptr ( ) ,
226
+ infoContents : self . info_contents_ptr [ ..] . as_ptr ( ) ,
227
+ nbInfos : INFO_FIELDS . len ( ) as u8 ,
228
+ } ;
229
+
230
+ for ( i, setting) in self . setting_contents . iter ( ) . enumerate ( ) {
231
+ SWITCH_ARRAY [ i] . text = setting[ 0 ] . as_ptr ( ) ;
232
+ SWITCH_ARRAY [ i] . subText = setting[ 1 ] . as_ptr ( ) ;
233
+ let state = if let Some ( data) = NVM_REF . as_mut ( ) {
234
+ data. get_ref ( ) [ i]
235
+ } else {
236
+ OFF_STATE
237
+ } ;
238
+ SWITCH_ARRAY [ i] . initState = state;
239
+ SWITCH_ARRAY [ i] . token = ( FIRST_USER_TOKEN + i as u32 ) as u8 ;
240
+ SWITCH_ARRAY [ i] . tuneId = TuneIndex :: TapCasual as u8 ;
241
+ }
242
+
243
+ self . content = nbgl_content_t {
244
+ content : nbgl_content_u {
245
+ switchesList : nbgl_pageSwitchesList_s {
246
+ switches : & SWITCH_ARRAY as * const nbgl_contentSwitch_t ,
247
+ nbSwitches : self . nb_settings ,
248
+ } ,
249
+ } ,
250
+ contentActionCallback : Some ( settings_callback) ,
251
+ type_ : SWITCHES_LIST ,
252
+ } ;
253
+
254
+ self . generic_contents = nbgl_genericContents_t {
255
+ callbackCallNeeded : false ,
256
+ __bindgen_anon_1 : nbgl_genericContents_t__bindgen_ty_1 {
257
+ contentsList : & self . content as * const nbgl_content_t ,
258
+ } ,
259
+ nbContents : if self . nb_settings > 0 { 1 } else { 0 } ,
260
+ } ;
261
+
262
+ nbgl_useCaseHomeAndSettings (
263
+ self . app_name . as_ptr ( ) as * const c_char ,
264
+ & self . icon as * const nbgl_icon_details_t ,
265
+ core:: ptr:: null ( ) ,
266
+ INIT_HOME_PAGE as u8 ,
267
+ & self . generic_contents as * const nbgl_genericContents_t ,
268
+ & self . info_list as * const nbgl_contentInfoList_t ,
269
+ core:: ptr:: null ( ) ,
270
+ Some ( quit_cb) ,
271
+ ) ;
272
+ }
273
+ }
193
274
}
0 commit comments