1 /* 2 * Display the physical keypad matrix layout for the Ben NanoNote. 3 * 4 * Copyright (C) 2018, 2023 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/c/rm.h> 23 #include <l4/re/env.h> 24 25 #include <l4/re/c/util/video/goos_fb.h> 26 #include <l4/re/c/video/view.h> 27 28 #include <unistd.h> 29 #include <stdint.h> 30 #include <stdlib.h> 31 32 #include "keypad_client.h" 33 34 35 36 enum Jz4740_keypad_gpio 37 { 38 Jz4740_keypad_gpio_inputs_count = 8, 39 Jz4740_keypad_gpio_outputs_count = 8, 40 }; 41 42 /* Video abstractions. */ 43 44 static l4re_util_video_goos_fb_t gfb; 45 static l4re_video_view_info_t fbi; 46 static void *fb; 47 48 /* Keypad status and dimensions. */ 49 50 uint32_t *keymem = 0; 51 int columns = Jz4740_keypad_gpio_outputs_count, rows = Jz4740_keypad_gpio_inputs_count; 52 53 /* Position units. */ 54 55 enum { 56 KEY = 12, DIRKEY = 8, 57 WIDTH = 120, HEIGHT = 72, ROWS = 6, 58 }; 59 60 /* 61 F1 F2 F3 F4 F5 F6 F7 F8 62 Q W E R T Y U I O P 63 A S D F G H J K L Bsp 64 Esc Z X C V B N M = Enter 65 Tab Cap \ ' , . / Up V-Up 66 LSh LAl Fn Sym Spc Qi Ctr Lt Dn Rt V-Dn 67 */ 68 69 enum { 70 SIZE_KEY_F1 = KEY, SIZE_KEY_F2 = KEY, SIZE_KEY_F3 = KEY, SIZE_KEY_F4 = KEY, 71 SIZE_KEY_F5 = KEY, SIZE_KEY_F6 = KEY, SIZE_KEY_F7 = KEY, SIZE_KEY_F8 = KEY, 72 73 SIZE_KEY_Q = KEY, SIZE_KEY_W = KEY, SIZE_KEY_E = KEY, SIZE_KEY_R = KEY, 74 SIZE_KEY_T = KEY, SIZE_KEY_Y = KEY, SIZE_KEY_U = KEY, SIZE_KEY_I = KEY, 75 SIZE_KEY_O = KEY, SIZE_KEY_P = KEY, 76 77 SIZE_KEY_A = KEY, SIZE_KEY_S = KEY, SIZE_KEY_D = KEY, SIZE_KEY_F = KEY, 78 SIZE_KEY_G = KEY, SIZE_KEY_H = KEY, SIZE_KEY_J = KEY, SIZE_KEY_K = KEY, 79 SIZE_KEY_L = KEY, SIZE_KEY_BACKSPACE = KEY, 80 81 SIZE_KEY_ESCAPE = KEY, SIZE_KEY_Z = KEY, SIZE_KEY_X = KEY, SIZE_KEY_C = KEY, 82 SIZE_KEY_V = KEY, SIZE_KEY_B = KEY, SIZE_KEY_N = KEY, SIZE_KEY_M = KEY, 83 SIZE_KEY_EQUAL = KEY, SIZE_KEY_ENTER = KEY, 84 85 SIZE_KEY_TAB = KEY, SIZE_KEY_DOWNSHIFT = KEY, SIZE_KEY_BACKSLASH = KEY, SIZE_KEY_APOSTROPHE = KEY, 86 SIZE_KEY_COMMA = KEY, SIZE_KEY_DOT = KEY, SIZE_KEY_SLASH = KEY, 87 SIZE_KEY_UP = DIRKEY, SIZE_KEY_VOLUMEUP = KEY, 88 89 SIZE_KEY_LEFTSHIFT = KEY, SIZE_KEY_LEFTALT = KEY, SIZE_KEY_FN = KEY, SIZE_KEY_SYM = KEY, 90 SIZE_KEY_SPACE = KEY, SIZE_KEY_QI = KEY, SIZE_KEY_RIGHTCTRL = KEY, SIZE_KEY_LEFT = DIRKEY, 91 SIZE_KEY_DOWN = DIRKEY, SIZE_KEY_RIGHT = DIRKEY, SIZE_KEY_VOLUMEDOWN = KEY, 92 }; 93 94 enum { 95 XPOS_KEY_F1 = 2*KEY, XPOS_KEY_F2 = 3*KEY, XPOS_KEY_F3 = 4*KEY, XPOS_KEY_F4 = 5*KEY, 96 XPOS_KEY_F5 = 6*KEY, XPOS_KEY_F6 = 7*KEY, XPOS_KEY_F7 = 8*KEY, XPOS_KEY_F8 = 9*KEY, 97 98 XPOS_KEY_Q = 0, XPOS_KEY_W = KEY, XPOS_KEY_E = 2*KEY, XPOS_KEY_R = 3*KEY, 99 XPOS_KEY_T = 4*KEY, XPOS_KEY_Y = 5*KEY, XPOS_KEY_U = 6*KEY, XPOS_KEY_I = 7*KEY, 100 XPOS_KEY_O = 8*KEY, XPOS_KEY_P = 9*KEY, 101 102 XPOS_KEY_A = 0, XPOS_KEY_S = KEY, XPOS_KEY_D = 2*KEY, XPOS_KEY_F = 3*KEY, 103 XPOS_KEY_G = 4*KEY, XPOS_KEY_H = 5*KEY, XPOS_KEY_J = 6*KEY, XPOS_KEY_K = 7*KEY, 104 XPOS_KEY_L = 8*KEY, XPOS_KEY_BACKSPACE = 9*KEY, 105 106 XPOS_KEY_ESCAPE = 0, XPOS_KEY_Z = KEY, XPOS_KEY_X = 2*KEY, XPOS_KEY_C = 3*KEY, 107 XPOS_KEY_V = 4*KEY, XPOS_KEY_B = 5*KEY, XPOS_KEY_N = 6*KEY, XPOS_KEY_M = 7*KEY, 108 XPOS_KEY_EQUAL = 8*KEY, XPOS_KEY_ENTER = 9*KEY, 109 110 XPOS_KEY_TAB = 0, XPOS_KEY_DOWNSHIFT = KEY, XPOS_KEY_BACKSLASH = 2*KEY, XPOS_KEY_APOSTROPHE = 3*KEY, 111 XPOS_KEY_COMMA = 4*KEY, XPOS_KEY_DOT = 5*KEY, XPOS_KEY_SLASH = 6*KEY, 112 XPOS_KEY_UP = 7*KEY+DIRKEY, XPOS_KEY_VOLUMEUP = 9*KEY, 113 114 XPOS_KEY_LEFTSHIFT = 0, XPOS_KEY_LEFTALT = KEY, XPOS_KEY_FN = 2*KEY, XPOS_KEY_SYM = 3*KEY, 115 XPOS_KEY_SPACE = 4*KEY, XPOS_KEY_QI = 5*KEY, XPOS_KEY_RIGHTCTRL = 6*KEY, XPOS_KEY_LEFT = 7*KEY, 116 XPOS_KEY_DOWN = 7*KEY+DIRKEY, XPOS_KEY_RIGHT = 7*KEY+2*DIRKEY, XPOS_KEY_VOLUMEDOWN = 9*KEY, 117 }; 118 119 enum { 120 YPOS_KEY_F1 = 0, YPOS_KEY_F2 = 0, YPOS_KEY_F3 = 0, YPOS_KEY_F4 = 0, 121 YPOS_KEY_F5 = 0, YPOS_KEY_F6 = 0, YPOS_KEY_F7 = 0, YPOS_KEY_F8 = 0, 122 123 YPOS_KEY_Q = 1, YPOS_KEY_W = 1, YPOS_KEY_E = 1, YPOS_KEY_R = 1, 124 YPOS_KEY_T = 1, YPOS_KEY_Y = 1, YPOS_KEY_U = 1, YPOS_KEY_I = 1, 125 YPOS_KEY_O = 1, YPOS_KEY_P = 1, 126 127 YPOS_KEY_A = 2, YPOS_KEY_S = 2, YPOS_KEY_D = 2, YPOS_KEY_F = 2, 128 YPOS_KEY_G = 2, YPOS_KEY_H = 2, YPOS_KEY_J = 2, YPOS_KEY_K = 2, 129 YPOS_KEY_L = 2, YPOS_KEY_BACKSPACE = 2, 130 131 YPOS_KEY_ESCAPE = 3, YPOS_KEY_Z = 3, YPOS_KEY_X = 3, YPOS_KEY_C = 3, 132 YPOS_KEY_V = 3, YPOS_KEY_B = 3, YPOS_KEY_N = 3, YPOS_KEY_M = 3, 133 YPOS_KEY_EQUAL = 3, YPOS_KEY_ENTER = 3, 134 135 YPOS_KEY_TAB = 4, YPOS_KEY_DOWNSHIFT = 4, YPOS_KEY_BACKSLASH = 4, YPOS_KEY_APOSTROPHE = 4, 136 YPOS_KEY_COMMA = 4, YPOS_KEY_DOT = 4, YPOS_KEY_SLASH = 4, 137 YPOS_KEY_UP = 4, YPOS_KEY_VOLUMEUP = 4, 138 139 YPOS_KEY_LEFTSHIFT = 5, YPOS_KEY_LEFTALT = 5, YPOS_KEY_FN = 5, YPOS_KEY_SYM = 5, 140 YPOS_KEY_SPACE = 5, YPOS_KEY_QI = 5, YPOS_KEY_RIGHTCTRL = 5, YPOS_KEY_LEFT = 5, 141 YPOS_KEY_DOWN = 5, YPOS_KEY_RIGHT = 5, YPOS_KEY_VOLUMEDOWN = 5, 142 }; 143 144 /* Keypad matrix mapping. */ 145 146 #define PHYS_KEY(X) {XPOS_KEY_##X, YPOS_KEY_##X, SIZE_KEY_##X} 147 #define PHYS_NULL {0, 0, 0} 148 149 static const uint32_t keypos[Jz4740_keypad_gpio_inputs_count][Jz4740_keypad_gpio_outputs_count][3] = { 150 151 {PHYS_KEY(F1), PHYS_KEY(F2), PHYS_KEY(F3), PHYS_KEY(F4), PHYS_KEY(F5), PHYS_KEY(F6), 152 PHYS_KEY(F7), PHYS_NULL}, 153 154 {PHYS_KEY(Q), PHYS_KEY(W), PHYS_KEY(E), PHYS_KEY(R), PHYS_KEY(T), PHYS_KEY(Y), 155 PHYS_KEY(U), PHYS_KEY(I)}, 156 157 {PHYS_KEY(A), PHYS_KEY(S), PHYS_KEY(D), PHYS_KEY(F), PHYS_KEY(G), PHYS_KEY(H), 158 PHYS_KEY(J), PHYS_KEY(K)}, 159 160 {PHYS_KEY(ESCAPE), PHYS_KEY(Z), PHYS_KEY(X), PHYS_KEY(C), PHYS_KEY(V), PHYS_KEY(B), 161 PHYS_KEY(N), PHYS_KEY(M)}, 162 163 {PHYS_KEY(TAB), PHYS_KEY(DOWNSHIFT) /* CAPSLOCK */, PHYS_KEY(BACKSLASH), 164 PHYS_KEY(APOSTROPHE), PHYS_KEY(COMMA), PHYS_KEY(DOT), PHYS_KEY(SLASH), PHYS_KEY(UP)}, 165 166 {PHYS_KEY(O), PHYS_KEY(L), PHYS_KEY(EQUAL), PHYS_KEY(SYM) /* RIGHTALT */, 167 PHYS_KEY(SPACE), PHYS_KEY(QI) /* F13 */, PHYS_KEY(RIGHTCTRL), PHYS_KEY(LEFT)}, 168 169 {PHYS_KEY(F8), PHYS_KEY(P), PHYS_KEY(BACKSPACE), PHYS_KEY(ENTER), PHYS_KEY(VOLUMEUP), 170 PHYS_KEY(VOLUMEDOWN), PHYS_KEY(DOWN), PHYS_KEY(RIGHT)}, 171 172 {PHYS_KEY(LEFTSHIFT), PHYS_KEY(LEFTALT), PHYS_KEY(FN), PHYS_NULL, PHYS_NULL, 173 PHYS_NULL, PHYS_NULL, PHYS_NULL} 174 }; 175 176 177 178 static uint32_t bitmask(uint32_t size) 179 { 180 return (1 << size) - 1; 181 } 182 183 static uint32_t truncate_channel(uint32_t value, uint32_t size) 184 { 185 return (value >> (8 - size)) & bitmask(size); 186 } 187 188 static void set_pixel(uint32_t pos, uint8_t bpp, uint32_t value) 189 { 190 if (bpp <= 8) *(uint8_t *) pos = value; 191 else if (bpp <= 16) *(uint16_t *) pos = value; 192 else *(uint32_t *) pos = value; 193 } 194 195 /* Show the state of a key on the display. */ 196 197 static void fill_rectangle(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t rgb) 198 { 199 uint8_t bpp = l4re_video_bits_per_pixel(&fbi.pixel_info); 200 uint8_t bytes_per_pixel = fbi.pixel_info.bytes_per_pixel; 201 uint32_t bytes_per_line = fbi.bytes_per_line; 202 uint32_t pos; 203 uint32_t col, row; 204 205 rgb = (truncate_channel(rgb >> 16, fbi.pixel_info.r.size) << fbi.pixel_info.r.shift) | 206 (truncate_channel(rgb >> 8, fbi.pixel_info.g.size) << fbi.pixel_info.g.shift) | 207 (truncate_channel(rgb, fbi.pixel_info.b.size) << fbi.pixel_info.b.shift); 208 209 for (row = 0; row < h; row++) 210 { 211 pos = (uint32_t) fb + (y + row) * bytes_per_line + x * bytes_per_pixel; 212 213 for (col = 0; col < w; col++) 214 { 215 set_pixel(pos, bpp, rgb); 216 pos += bytes_per_pixel; 217 } 218 } 219 } 220 221 /* Show the keypad status on the display. */ 222 223 static void show_keypad(void) 224 { 225 uint32_t rowsize = fbi.height / ROWS, colsize; 226 uint8_t column, row; 227 uint32_t mask; 228 const uint32_t *pos; 229 230 for (column = 0; column < columns; column++) 231 232 for (row = 0, mask = 1 << (rows - 1); 233 row < rows; 234 row++, mask >>= 1) 235 { 236 /* Obtain the physical position. */ 237 238 pos = keypos[row][column]; 239 240 /* Obtain the width of the key. */ 241 242 colsize = (pos[2] * fbi.width) / WIDTH; 243 244 /* Plot the rectangle for the key. */ 245 246 fill_rectangle((pos[0] * fbi.width) / WIDTH, pos[1] * rowsize, colsize, rowsize, 247 keymem[column] & mask ? 0xffffff : 0); 248 } 249 250 /* Refresh the display. */ 251 252 l4re_util_video_goos_fb_refresh(&gfb, 0, 0, fbi.width, fbi.height); 253 } 254 255 256 257 int main(void) 258 { 259 l4_cap_idx_t keypad_cap; 260 l4re_ds_t mem; 261 262 if (l4re_util_video_goos_fb_setup_name(&gfb, "fb")) 263 return 1; 264 265 if (l4re_util_video_goos_fb_view_info(&gfb, &fbi)) 266 return 1; 267 268 if (!(fb = l4re_util_video_goos_fb_attach_buffer(&gfb))) 269 return 1; 270 271 /* Obtain a reference to the keypad. */ 272 273 keypad_cap = l4re_env_get_cap("keypad"); 274 275 if (l4_is_invalid_cap(keypad_cap)) 276 return 1; 277 278 /* Obtain a reference to the keypad data. */ 279 280 Keypad keypad = {.ref={.cap=keypad_cap}, .iface=&client_iface_Keypad}; 281 282 if (keypad.iface->get_keypad_data(keypad.ref, &mem)) 283 return 1; 284 285 /* Attach the keypad data to a region in this task. */ 286 287 if (l4re_rm_attach((void **) &keymem, l4re_ds_size(mem), 288 L4RE_RM_F_SEARCH_ADDR | L4RE_RM_F_R, 289 mem, 0, 290 L4_PAGESHIFT)) 291 return 1; 292 293 /* Show the keypad state. */ 294 295 while (1) 296 show_keypad(); 297 298 return 0; 299 }