Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
nbgl_obj_keypad_nanos.c
Go to the documentation of this file.
1
8#ifdef NBGL_KEYPAD
9#ifndef HAVE_SE_TOUCH
10
11/*********************
12 * INCLUDES
13 *********************/
14#include "nbgl_debug.h"
15#include "nbgl_front.h"
16#include "nbgl_draw.h"
17#include "nbgl_obj.h"
18#include "nbgl_fonts.h"
19#include "nbgl_screen.h"
20#include "glyphs.h"
21#include "os_io_seproxyhal.h"
22#include "lcx_rng.h"
23
24/*********************
25 * DEFINES
26 *********************/
27#define KEY_WIDTH 9
28#define DIGIT_HEIGHT 12
29#define DIGIT_OFFSET_X 13
30#define DIGIT_OFFSET_Y ((KEYPAD_HEIGHT - DIGIT_HEIGHT) / 2)
31#define INIT_DIGIT_VALUE 5
32
33/**********************
34 * TYPEDEFS
35 **********************/
36
37/**********************
38 * STATIC VARIABLES
39 **********************/
40static const nbgl_icon_details_t *digits_icons[] = {&C_digit_0,
41 &C_digit_1,
42 &C_digit_2,
43 &C_digit_3,
44 &C_digit_4,
45 &C_digit_5,
46 &C_digit_6,
47 &C_digit_7,
48 &C_digit_8,
49 &C_digit_9};
50/**********************
51 * VARIABLES
52 **********************/
53
54/**********************
55 * STATIC FUNCTIONS
56 **********************/
57static char positionToChar(uint8_t pos)
58{
59 if (pos == 0) {
60 return BACKSPACE_KEY;
61 }
62 else if (pos == 11) {
63 return VALIDATE_KEY;
64 }
65 else {
66 return 0x30 + (pos - 1);
67 }
68}
69
70static void keypadDrawDigits(nbgl_keypad_t *keypad)
71{
72 uint8_t i;
73 nbgl_area_t rectArea;
74
75 // clean full area
76 rectArea.backgroundColor = BLACK;
77 rectArea.x0 = keypad->obj.area.x0;
78 rectArea.y0 = keypad->obj.area.y0;
79 rectArea.width = keypad->obj.area.width;
80 rectArea.height = keypad->obj.area.height;
81 nbgl_frontDrawRect(&rectArea);
82
83 rectArea.backgroundColor = BLACK;
84 rectArea.y0 = keypad->obj.area.y0 + DIGIT_OFFSET_Y;
85 rectArea.bpp = NBGL_BPP_1;
86 // row of digits: 0 1 2 3... 9
87 for (i = 0; i < 10; i++) {
88 rectArea.width = digits_icons[i]->width;
89 rectArea.height = digits_icons[i]->height;
90
91 rectArea.x0 = keypad->obj.area.x0 + DIGIT_OFFSET_X + i * KEY_WIDTH;
92 nbgl_frontDrawImage(&rectArea, digits_icons[i]->bitmap, NO_TRANSFORMATION, WHITE);
93 }
94 // draw backspace
95 if (keypad->enableBackspace) {
96 rectArea.width = C_icon_backspace.width;
97 rectArea.height = C_icon_backspace.height;
98 rectArea.x0 = keypad->obj.area.x0;
99 rectArea.y0 = keypad->obj.area.y0 + ((KEYPAD_HEIGHT - C_icon_backspace.height) / 2);
100 nbgl_frontDrawImage(&rectArea, C_icon_backspace.bitmap, NO_TRANSFORMATION, WHITE);
101 }
102
103 // draw validate
104 if (keypad->enableValidate) {
105 rectArea.width = C_digit_validate_bold.width;
106 rectArea.height = C_digit_validate_bold.height;
107 rectArea.x0 = keypad->obj.area.x0 + KEYPAD_WIDTH - C_digit_validate_bold.width;
108 rectArea.y0 = keypad->obj.area.y0 + ((KEYPAD_HEIGHT - C_digit_validate_bold.height) / 2);
109 nbgl_frontDrawImage(&rectArea, C_digit_validate_bold.bitmap, NO_TRANSFORMATION, WHITE);
110 }
111}
112
113static void keypadDrawSelected(nbgl_keypad_t *keypad)
114{
115 nbgl_area_t rectArea;
117 rectArea.backgroundColor = WHITE;
118
119 if (keypad->selectedKey == 0) {
120 rectArea.x0 = keypad->obj.area.x0;
121 }
122 else if (keypad->selectedKey < 11) { // if it's a digit
123 rectArea.x0 = keypad->obj.area.x0 + DIGIT_OFFSET_X + (keypad->selectedKey - 1) * KEY_WIDTH;
124 }
125 else if (keypad->selectedKey == 11) {
126 rectArea.x0 = keypad->obj.area.x0 + KEYPAD_WIDTH - C_digit_validate_bold.width;
127 }
128 else {
129 return;
130 }
131 rectArea.y0 = keypad->obj.area.y0 + keypad->obj.area.height - 2;
132 rectArea.width = 8;
133 rectArea.height = 2;
134 nbgl_frontDrawRect(&rectArea);
135}
136
137static void keypadDrawSelectedTouched(nbgl_keypad_t *keypad)
138{
139 nbgl_area_t rectArea;
141 rectArea.backgroundColor = WHITE;
142
143 if (keypad->selectedKey == 0) {
144 rectArea.x0 = keypad->obj.area.x0;
145 }
146 else if (keypad->selectedKey < 11) { // if it's a digit
147 rectArea.x0 = keypad->obj.area.x0 + DIGIT_OFFSET_X + (keypad->selectedKey - 1) * KEY_WIDTH;
148 }
149 else if (keypad->selectedKey == 11) {
150 rectArea.x0 = keypad->obj.area.x0 + KEYPAD_WIDTH - C_digit_validate_bold.width;
151 }
152 else {
153 return;
154 }
155 rectArea.y0 = keypad->obj.area.y0;
156 rectArea.width = 8;
157 rectArea.height = 2;
158 nbgl_frontDrawRect(&rectArea);
160}
161
162static void keypadInitSelected(nbgl_keypad_t *keypad)
163{
164 if (!keypad->shuffled) {
165 keypad->selectedKey = 1 + INIT_DIGIT_VALUE;
166 }
167 else {
168 // Exclude backspace and validate keys ([0,11]), shuffle only digits
169 keypad->selectedKey = cx_rng_u32_range(1, 11);
170 }
171}
172
173/**********************
174 * GLOBAL FUNCTIONS
175 **********************/
176
184void nbgl_keypadCallback(nbgl_obj_t *obj, nbgl_buttonEvent_t buttonEvent)
185{
186 nbgl_keypad_t *keypad = (nbgl_keypad_t *) obj;
187
188 LOG_DEBUG(OBJ_LOGGER, "nbgl_keypadCallback(): buttonEvent = %d\n", buttonEvent);
189
190 if (buttonEvent == BUTTON_BOTH_TOUCHED) {
191 // draw bar upper selected key
192 keypadDrawSelectedTouched(keypad);
193 }
194 else if (buttonEvent == BUTTON_BOTH_PRESSED) {
195 keypad->callback(positionToChar(keypad->selectedKey));
196 }
197 else if ((buttonEvent == BUTTON_LEFT_PRESSED)
198 || (buttonEvent == BUTTON_LEFT_CONTINUOUS_PRESSED)) {
199 switch (keypad->selectedKey) {
200 case 1:
201 if (keypad->enableBackspace) {
202 keypad->selectedKey = 0;
203 }
204 else {
205 keypad->selectedKey = 10;
206 }
207 break;
208 case 0: // backspace
209 if (keypad->enableValidate) {
210 keypad->selectedKey = 11;
211 }
212 else {
213 keypad->selectedKey = 10;
214 }
215 break;
216 default:
217 keypad->selectedKey--;
218 break;
219 }
221 }
222 else if ((buttonEvent == BUTTON_RIGHT_PRESSED)
223 || (buttonEvent == BUTTON_RIGHT_CONTINUOUS_PRESSED)) {
224 switch (keypad->selectedKey) {
225 case 10: // '9'
226 if (keypad->enableValidate) {
227 keypad->selectedKey = 11;
228 }
229 else if (keypad->enableBackspace) {
230 keypad->selectedKey = 0;
231 }
232 else {
233 keypad->selectedKey = 1;
234 }
235 break;
236 case 11: // validate
237 keypad->selectedKey = 0;
238 break;
239 default:
240 keypad->selectedKey++;
241 break;
242 }
244 }
245}
246
254{
255 LOG_DEBUG(OBJ_LOGGER, "nbgl_objDrawKeypad keypad->shuffled= %d\n", keypad->shuffled);
256 // draw digits content
257 keypadDrawDigits(keypad);
258 if (keypad->selectedKey == 0xFF) {
259 keypadInitSelected(keypad);
260 }
261 keypadDrawSelected(keypad);
262}
263
264#endif // HAVE_SE_TOUCH
265#endif // NBGL_KEYPAD
Random Number Generation.
debug traces management
#define LOG_DEBUG(__logger,...)
Definition nbgl_debug.h:86
@ OBJ_LOGGER
Definition nbgl_debug.h:30
Middle Level API of the new BOLOS Graphical Library.
Font screen low-Level driver API, to draw elementary forms.
void nbgl_frontDrawImage(const nbgl_area_t *area, const uint8_t *buffer, nbgl_transformation_t transformation, nbgl_color_map_t colorMap)
void nbgl_frontRefreshArea(const nbgl_area_t *area, nbgl_refresh_mode_t mode, nbgl_post_refresh_t post_refresh)
void nbgl_frontDrawRect(const nbgl_area_t *area)
API to draw all basic graphic objects.
struct PACKED__ nbgl_keypad_s nbgl_keypad_t
struct to represent a keypad (KEYPAD type)
nbgl_buttonEvent_t
Definition nbgl_obj.h:207
@ BUTTON_BOTH_TOUCHED
Sent when both buttons are touched.
Definition nbgl_obj.h:215
@ BUTTON_LEFT_CONTINUOUS_PRESSED
Definition nbgl_obj.h:210
@ BUTTON_BOTH_PRESSED
Sent when both buttons are released.
Definition nbgl_obj.h:214
@ BUTTON_RIGHT_CONTINUOUS_PRESSED
Definition nbgl_obj.h:212
@ BUTTON_LEFT_PRESSED
Sent when Left button is released.
Definition nbgl_obj.h:208
@ BUTTON_RIGHT_PRESSED
Send when Right button is released.
Definition nbgl_obj.h:209
#define BACKSPACE_KEY
Definition nbgl_obj.h:26
void nbgl_objDrawKeypad(nbgl_keypad_t *kbd)
This function draws a keypad object.
#define VALIDATE_KEY
Definition nbgl_obj.h:27
struct PACKED__ nbgl_obj_s nbgl_obj_t
Common structure for all graphical objects.
#define KEY_WIDTH
API to manage screens.
void nbgl_screenRedraw(void)
This function redraws the whole screen on top of stack and its children.
Definition nbgl_screen.c:66
@ WHITE
Definition nbgl_types.h:126
@ BLACK
Definition nbgl_types.h:123
@ POST_REFRESH_KEEP_POWER_STATE
Keep state after refresh.
Definition nbgl_types.h:338
struct PACKED__ nbgl_icon_details_s nbgl_icon_details_t
Represents all information about an icon.
#define NO_TRANSFORMATION
Definition nbgl_types.h:76
@ NBGL_BPP_1
1 bit per pixel
Definition nbgl_types.h:266
struct PACKED__ nbgl_area_s nbgl_area_t
Represents a rectangle area of the screen.
@ FULL_COLOR_CLEAN_REFRESH
to be used for lock screen display (cleaner but longer refresh)
Definition nbgl_types.h:311
unsigned char uint8_t
Definition usbd_conf.h:53