3535
3636namespace target = klib::target;
3737
38+ constexpr static klib::time::ms screen_timeout = 60'000 ;
3839constexpr static uint32_t fps_frametime = (1'000'000 ) / 60 ;
3940
4041// implement the throw_bad_function_call so we can use std::function
@@ -186,8 +187,11 @@ int main() {
186187 // current active framebuffer
187188 uint8_t current_framebuffer = 0 ;
188189
190+ // last time the user pressed a button for the screen timeout
191+ auto last_pressed_time = klib::io::systick<>::template get_runtime ();
192+
189193 // get the previous time for the delta
190- auto previous_time = klib::io::systick<>::template get_runtime<klib::time::us>();
194+ auto previous_time = klib::io::systick<>::get_runtime<klib::time::us>();
191195
192196 // timing for the buttons. To keep track on how long a
193197 // button is pressed. We mark everything with the pressed
@@ -206,11 +210,33 @@ int main() {
206210 const auto current_time = klib::io::systick<>::template get_runtime<klib::time::us>();
207211
208212 // get the buttons
209- const input::buttons buttons = input::get_state (
213+ input::buttons buttons = input::get_state (
210214 current_time - previous_time, flipped, button_timing,
211215 {button0::get (), button1::get (), button2::get ()}
212216 );
213217
218+ // flag if we have reached the screen timeout
219+ const bool timeout = klib::io::systick<>::get_runtime () > (last_pressed_time + screen_timeout);
220+
221+ // check if we have pressed any button
222+ if (is_pressed (buttons.up ) || is_pressed (buttons.enter ) || is_pressed (buttons.down )) {
223+ // check if we need to enable the backlight again
224+ if (timeout) {
225+ // turn on the backlight
226+ blk::set<false >();
227+
228+ // prevent any of the buttons from triggering when we turn on the backlight
229+ buttons = {input::state::no_change, input::state::no_change, input::state::no_change};
230+ }
231+
232+ // update the last time we pressed the buttons
233+ last_pressed_time = klib::io::systick<>::get_runtime ();
234+ }
235+ else if (timeout) {
236+ // turn off the backlight
237+ blk::set<true >();
238+ }
239+
214240 // check if we have switched screens
215241 if (current_screen != previous_screen) {
216242 // deactivate the previous screen
@@ -233,9 +259,6 @@ int main() {
233259 // run the correct screen
234260 screens[current_screen]->main (current_time - previous_time, buttons);
235261
236- // update the previous time
237- previous_time = current_time;
238-
239262 // draw the screen alternating the framebuffers
240263 for (uint32_t i = 0 ; i < display::height; i += move_height) {
241264 // call the on_draw
@@ -266,6 +289,9 @@ int main() {
266289 current_framebuffer ^= 1 ;
267290 }
268291
292+ // update the previous time
293+ previous_time = current_time;
294+
269295 const auto end_time = klib::io::systick<>::template get_runtime<klib::time::us>();
270296
271297 // we try to target around 60 fps
0 commit comments