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 "lcx_rng.h"
22
23/*********************
24 * DEFINES
25 *********************/
26#define KEY_WIDTH 9
27#define DIGIT_HEIGHT 12
28#define DIGIT_OFFSET_X 13
29#define DIGIT_OFFSET_Y ((KEYPAD_HEIGHT - DIGIT_HEIGHT) / 2)
30#define INIT_DIGIT_VALUE 5
31
32/**********************
33 * TYPEDEFS
34 **********************/
35
36/**********************
37 * STATIC VARIABLES
38 **********************/
39static const nbgl_icon_details_t *digits_icons[] = {&C_digit_0,
40 &C_digit_1,
41 &C_digit_2,
42 &C_digit_3,
43 &C_digit_4,
44 &C_digit_5,
45 &C_digit_6,
46 &C_digit_7,
47 &C_digit_8,
48 &C_digit_9};
49/**********************
50 * VARIABLES
51 **********************/
52
53/**********************
54 * STATIC FUNCTIONS
55 **********************/
56static char positionToChar(uint8_t pos)
57{
58 if (pos == 0) {
59 return BACKSPACE_KEY;
60 }
61 else if (pos == 11) {
62 return VALIDATE_KEY;
63 }
64 else {
65 return 0x30 + (pos - 1);
66 }
67}
68
69static void keypadDrawDigits(nbgl_keypad_t *keypad)
70{
71 uint8_t i;
72 nbgl_area_t rectArea;
73
74 // clean full area
75 rectArea.backgroundColor = BLACK;
76 rectArea.x0 = keypad->obj.area.x0;
77 rectArea.y0 = keypad->obj.area.y0;
78 rectArea.width = keypad->obj.area.width;
79 rectArea.height = keypad->obj.area.height;
80 nbgl_frontDrawRect(&rectArea);
81
82 rectArea.backgroundColor = BLACK;
83 rectArea.y0 = keypad->obj.area.y0 + DIGIT_OFFSET_Y;
84 rectArea.bpp = NBGL_BPP_1;
85 // row of digits: 0 1 2 3... 9
86 for (i = 0; i < 10; i++) {
87 rectArea.width = digits_icons[i]->width;
88 rectArea.height = digits_icons[i]->height;
89
90 rectArea.x0 = keypad->obj.area.x0 + DIGIT_OFFSET_X + i * KEY_WIDTH;
91 nbgl_frontDrawImage(&rectArea, digits_icons[i]->bitmap, NO_TRANSFORMATION, WHITE);
92 }
93 // draw backspace
94 if (keypad->enableBackspace) {
95 rectArea.width = C_icon_backspace.width;
96 rectArea.height = C_icon_backspace.height;
97 rectArea.x0 = keypad->obj.area.x0;
98 rectArea.y0 = keypad->obj.area.y0 + ((KEYPAD_HEIGHT - C_icon_backspace.height) / 2);
99 nbgl_frontDrawImage(&rectArea, C_icon_backspace.bitmap, NO_TRANSFORMATION, WHITE);
100 }
101
102 // draw validate
103 if (keypad->enableValidate) {
104 rectArea.width = C_digit_validate_bold.width;
105 rectArea.height = C_digit_validate_bold.height;
106 rectArea.x0 = keypad->obj.area.x0 + KEYPAD_WIDTH - C_digit_validate_bold.width;
107 rectArea.y0 = keypad->obj.area.y0 + ((KEYPAD_HEIGHT - C_digit_validate_bold.height) / 2);
108 nbgl_frontDrawImage(&rectArea, C_digit_validate_bold.bitmap, NO_TRANSFORMATION, WHITE);
109 }
110}
111
112static void keypadDrawSelected(nbgl_keypad_t *keypad)
113{
114 nbgl_area_t rectArea;
116 rectArea.backgroundColor = WHITE;
117
118 if (keypad->selectedKey == 0) {
119 rectArea.x0 = keypad->obj.area.x0;
120 }
121 else if (keypad->selectedKey < 11) { // if it's a digit
122 rectArea.x0 = keypad->obj.area.x0 + DIGIT_OFFSET_X + (keypad->selectedKey - 1) * KEY_WIDTH;
123 }
124 else if (keypad->selectedKey == 11) {
125 rectArea.x0 = keypad->obj.area.x0 + KEYPAD_WIDTH - C_digit_validate_bold.width;
126 }
127 else {
128 return;
129 }
130 rectArea.y0 = keypad->obj.area.y0 + keypad->obj.area.height - 2;
131 rectArea.width = 8;
132 rectArea.height = 2;
133 nbgl_frontDrawRect(&rectArea);
134}
135
136static void keypadDrawSelectedTouched(nbgl_keypad_t *keypad)
137{
138 nbgl_area_t rectArea;
140 rectArea.backgroundColor = WHITE;
141
142 if (keypad->selectedKey == 0) {
143 rectArea.x0 = keypad->obj.area.x0;
144 }
145 else if (keypad->selectedKey < 11) { // if it's a digit
146 rectArea.x0 = keypad->obj.area.x0 + DIGIT_OFFSET_X + (keypad->selectedKey - 1) * KEY_WIDTH;
147 }
148 else if (keypad->selectedKey == 11) {
149 rectArea.x0 = keypad->obj.area.x0 + KEYPAD_WIDTH - C_digit_validate_bold.width;
150 }
151 else {
152 return;
153 }
154 rectArea.y0 = keypad->obj.area.y0;
155 rectArea.width = 8;
156 rectArea.height = 2;
157 nbgl_frontDrawRect(&rectArea);
159}
160
161static void keypadInitSelected(nbgl_keypad_t *keypad)
162{
163 if (!keypad->shuffled) {
164 keypad->selectedKey = 1 + INIT_DIGIT_VALUE;
165 }
166 else {
167 // Exclude backspace and validate keys ([0,11]), shuffle only digits
168 keypad->selectedKey = cx_rng_u32_range(1, 11);
169 }
170}
171
172/**********************
173 * GLOBAL FUNCTIONS
174 **********************/
175
183void nbgl_keypadCallback(nbgl_obj_t *obj, nbgl_buttonEvent_t buttonEvent)
184{
185 nbgl_keypad_t *keypad = (nbgl_keypad_t *) obj;
186
187 LOG_DEBUG(OBJ_LOGGER, "nbgl_keypadCallback(): buttonEvent = %d\n", buttonEvent);
188
189 if (buttonEvent == BUTTON_BOTH_TOUCHED) {
190 // draw bar upper selected key
191 keypadDrawSelectedTouched(keypad);
192 }
193 else if (buttonEvent == BUTTON_BOTH_PRESSED) {
194 keypad->callback(positionToChar(keypad->selectedKey));
195 }
196 else if ((buttonEvent == BUTTON_LEFT_PRESSED)
197 || (buttonEvent == BUTTON_LEFT_CONTINUOUS_PRESSED)) {
198 switch (keypad->selectedKey) {
199 case 1:
200 if (keypad->enableBackspace) {
201 keypad->selectedKey = 0;
202 }
203 else {
204 keypad->selectedKey = 10;
205 }
206 break;
207 case 0: // backspace
208 if (keypad->enableValidate) {
209 keypad->selectedKey = 11;
210 }
211 else {
212 keypad->selectedKey = 10;
213 }
214 break;
215 default:
216 keypad->selectedKey--;
217 break;
218 }
220 }
221 else if ((buttonEvent == BUTTON_RIGHT_PRESSED)
222 || (buttonEvent == BUTTON_RIGHT_CONTINUOUS_PRESSED)) {
223 switch (keypad->selectedKey) {
224 case 10: // '9'
225 if (keypad->enableValidate) {
226 keypad->selectedKey = 11;
227 }
228 else if (keypad->enableBackspace) {
229 keypad->selectedKey = 0;
230 }
231 else {
232 keypad->selectedKey = 1;
233 }
234 break;
235 case 11: // validate
236 keypad->selectedKey = 0;
237 break;
238 default:
239 keypad->selectedKey++;
240 break;
241 }
243 }
244}
245
253{
254 LOG_DEBUG(OBJ_LOGGER, "nbgl_objDrawKeypad keypad->shuffled= %d\n", keypad->shuffled);
255 // draw digits content
256 keypadDrawDigits(keypad);
257 if (keypad->selectedKey == 0xFF) {
258 keypadInitSelected(keypad);
259 }
260 keypadDrawSelected(keypad);
261}
262
263#endif // HAVE_SE_TOUCH
264#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