Embedded SDK
Embedded SDK
nbgl_buttons.c
Go to the documentation of this file.
1 
6 #ifndef HAVE_SE_TOUCH
7 /*********************
8  * INCLUDES
9  *********************/
10 #include <string.h>
11 #include "nbgl_obj.h"
12 #include "nbgl_debug.h"
13 #include "nbgl_buttons.h"
14 #include "nbgl_screen.h"
15 #include "os_pic.h"
16 
17 /*********************
18  * DEFINES
19  *********************/
20 
21 /**********************
22  * TYPEDEFS
23  **********************/
24 
25 /**********************
26  * STATIC PROTOTYPES
27  **********************/
28 
29 /**********************
30  * STATIC VARIABLES
31  **********************/
32 static uint8_t gButtonMask = 0;
33 static uint32_t gButtonSameMaskCounter = 0;
34 
35 /**********************
36  * VARIABLES
37  **********************/
38 
39 /**********************
40  * STATIC PROTOTYPES
41  **********************/
42 static nbgl_buttonEvent_t maskToEvent(uint8_t mask)
43 {
45  switch (mask) {
47  event = BUTTON_BOTH_PRESSED;
48  break;
49 
51  event = BUTTON_BOTH_TOUCHED;
52  break;
53 
56  break;
57 
59  event = BUTTON_LEFT_PRESSED;
60  break;
61 
64  break;
65 
67  event = BUTTON_RIGHT_PRESSED;
68  break;
69  }
70 
71  return event;
72 }
73 /**********************
74  * GLOBAL FUNCTIONS
75  **********************/
76 
83 void nbgl_buttonsHandler(uint8_t buttonState, uint32_t currentTimeMs)
84 {
85  uint8_t button_mask;
86  uint32_t button_same_mask_counter;
87 
88  (void) currentTimeMs;
89  // enable speeded up long push (continuous)
90  if (buttonState == gButtonMask) {
91  if (buttonState == 0) {
92  // nothing to be done when both buttons released twice in a row
93  return;
94  }
95  // each 100ms ~
96  gButtonSameMaskCounter++;
97  }
98 
99  // append the button mask
100  button_mask = gButtonMask | buttonState;
101 
102  // pre reset variable due to os_sched_exit
103  button_same_mask_counter = gButtonSameMaskCounter;
104 
105  if (buttonState == 0) {
106  // reset next state when both buttons are released
107  gButtonMask = 0;
108  gButtonSameMaskCounter = 0;
109 
110  // notify button released event
111  button_mask |= RELEASED_MASK;
112  }
113  else {
114  gButtonMask = button_mask;
115  }
116 
117  // reset counter when button mask changes
118  if (buttonState != gButtonMask) {
119  gButtonSameMaskCounter = 0;
120  }
121 
122  // if the same button(s) is pressed more than 800 ms
123  if (button_same_mask_counter >= CONTINOUS_PRESS_THRESHOLD) {
124  // fast bit when pressing and timing is right (tag the event every 300ms)
125  if ((button_same_mask_counter % CONTINUOUS_PRESS_PERIOD) == 0) {
126  button_mask |= CONTINUOUS_MASK;
127  }
128 
129  // discard the release event after a fastskip has been detected, to avoid strange at release
130  // behavior and also to enable user to cancel an operation by starting triggering the fast
131  // skip
132  button_mask &= ~RELEASED_MASK;
133  }
134 
135  nbgl_screen_t *topScreen = (nbgl_screen_t *) nbgl_screenGetTop();
136  if ((topScreen != NULL) && (topScreen->buttonCallback != NULL)) {
137  nbgl_buttonEvent_t event = maskToEvent(button_mask);
138  if (event != INVALID_BUTTON_EVENT) {
139  topScreen->buttonCallback(topScreen, event);
140  }
141  }
142 }
143 
144 void nbgl_buttonsReset(void)
145 {
146  // no button push so far
147  gButtonMask = 0;
148  gButtonSameMaskCounter = 0;
149 }
150 #endif // HAVE_SE_TOUCH
#define CONTINUOUS_PRESS_PERIOD
Definition: nbgl_buttons.h:27
#define CONTINOUS_PRESS_THRESHOLD
Periodicity of "continuous press" events (in 100ms)
Definition: nbgl_buttons.h:25
void nbgl_buttonsHandler(uint8_t buttonState, uint32_t currentTimeMs)
void nbgl_buttonsReset(void)
debug traces management
API to draw all basic graphic objects.
#define CONTINUOUS_MASK
300ms after the first 800ms)
Definition: nbgl_obj.h:178
nbgl_buttonEvent_t
Definition: nbgl_obj.h:182
@ BUTTON_BOTH_TOUCHED
Sent when both buttons are touched.
Definition: nbgl_obj.h:190
@ BUTTON_LEFT_CONTINUOUS_PRESSED
Definition: nbgl_obj.h:185
@ BUTTON_BOTH_PRESSED
Sent when both buttons are released.
Definition: nbgl_obj.h:189
@ BUTTON_RIGHT_CONTINUOUS_PRESSED
Definition: nbgl_obj.h:187
@ BUTTON_LEFT_PRESSED
Sent when Left button is released.
Definition: nbgl_obj.h:183
@ BUTTON_RIGHT_PRESSED
Send when Right button is released.
Definition: nbgl_obj.h:184
@ INVALID_BUTTON_EVENT
Definition: nbgl_obj.h:191
#define RELEASED_MASK
released (see LSB bits to know what buttons are released)
Definition: nbgl_obj.h:177
#define LEFT_BUTTON
The different pressed buttons.
Definition: nbgl_obj.h:174
#define RIGHT_BUTTON
Right button event.
Definition: nbgl_obj.h:175
API to manage screens.
nbgl_obj_t * nbgl_screenGetTop(void)
Returns the screen on top layer, as a generic object.
Definition: nbgl_screen.c:102
struct PACKED__ nbgl_screen_s nbgl_screen_t
struct to represent a screen (SCREEN type)
unsigned char uint8_t
Definition: usbd_conf.h:53