| 1 | /** |
|---|
| 2 | ConsolTest.c |
|---|
| 3 | |
|---|
| 4 | ÀÌ ¸ðµâÀº DHL_OS_Printf ¿Í DHL_OS_GetChar ¿¡ ´ëÇÑ Å×½ºÆ® ·çƾÀÌ´Ù. |
|---|
| 5 | |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | #include "DHL_OSAL.h" |
|---|
| 10 | |
|---|
| 11 | //#include "DHL_APITest.h" |
|---|
| 12 | |
|---|
| 13 | //#include <string.h> |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | #if COMMENT |
|---|
| 18 | ____Test_1____(){} |
|---|
| 19 | #endif |
|---|
| 20 | |
|---|
| 21 | /* |
|---|
| 22 | ±âº» ÀÔ/Ãâ·Â µ¿ÀÛ Å×½ºÆ®. |
|---|
| 23 | |
|---|
| 24 | ±ÛÀÚ ÇÑÀÚ¸¦ ÀÔ·Â ¹Þ°í ÀÔ·Â ¹ÞÀº ±ÛÀÚÀÇ ASCII Äڵ带 ´Ù½Ã Ãâ·ÂÇÑ´Ù. |
|---|
| 25 | |
|---|
| 26 | üũ Æ÷ÀÎÆ®: |
|---|
| 27 | |
|---|
| 28 | 1. ÀÔ·ÂÀÌ ¾ø´Â °æ¿ì NULL ÀÌ ¸®ÅϵǴÂÁö ¾Æ´Ï¸é ´ë±âÇÏ´ÂÁö È®ÀÎÇÑ´Ù. |
|---|
| 29 | |
|---|
| 30 | */ |
|---|
| 31 | void Console_EchoTest(void) |
|---|
| 32 | { |
|---|
| 33 | UINT32 key, prev_key = 0; |
|---|
| 34 | int count = 0; |
|---|
| 35 | |
|---|
| 36 | DHL_OS_Printf("\n\n%s\n\n", __func__); |
|---|
| 37 | |
|---|
| 38 | DHL_OS_Printf("press same key 5 times to exit..\n"); |
|---|
| 39 | |
|---|
| 40 | while (1) |
|---|
| 41 | { |
|---|
| 42 | key = DHL_OS_GetChar(); |
|---|
| 43 | |
|---|
| 44 | if (key >= 0x20 && key < 0x7f) // ASCII |
|---|
| 45 | DHL_OS_Printf("input char: %02x, '%c'\n", key, key); |
|---|
| 46 | else |
|---|
| 47 | DHL_OS_Printf("input char: %08x\n", key); |
|---|
| 48 | |
|---|
| 49 | // check exit condition. |
|---|
| 50 | count = (key == prev_key) ? count+1 : 0; |
|---|
| 51 | prev_key = key; |
|---|
| 52 | |
|---|
| 53 | if (count >= 5) { |
|---|
| 54 | DHL_OS_Printf("exit test\n"); |
|---|
| 55 | break; |
|---|
| 56 | } |
|---|
| 57 | } |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | #if COMMENT |
|---|
| 64 | ____Test_2____(){} |
|---|
| 65 | #endif |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | /* |
|---|
| 70 | DHL_EXTKEY_t enum ŸÀÔÀº API header ÆÄÀÏ·Î À̵¿ÇÔ. |
|---|
| 71 | */ |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | struct DHL_EXTKEY_NAME_t { |
|---|
| 75 | UINT32 extkey; |
|---|
| 76 | char *name; |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | static struct DHL_EXTKEY_NAME_t extkeytbl[] = |
|---|
| 80 | { |
|---|
| 81 | DHL_EXTKEY_TAB, "TAB", |
|---|
| 82 | DHL_EXTKEY_CR, "CR", |
|---|
| 83 | DHL_EXTKEY_ESC, "ESC", |
|---|
| 84 | |
|---|
| 85 | DHL_EXTKEY_F1, "F1", |
|---|
| 86 | DHL_EXTKEY_F2, "F2", |
|---|
| 87 | DHL_EXTKEY_F3, "F3", |
|---|
| 88 | DHL_EXTKEY_F4, "F4", |
|---|
| 89 | DHL_EXTKEY_F5, "F5", |
|---|
| 90 | DHL_EXTKEY_F6, "F6", |
|---|
| 91 | DHL_EXTKEY_F7, "F7", |
|---|
| 92 | DHL_EXTKEY_F8, "F8", |
|---|
| 93 | DHL_EXTKEY_F9, "F9", |
|---|
| 94 | DHL_EXTKEY_F10, "F10", |
|---|
| 95 | DHL_EXTKEY_F11, "F11", |
|---|
| 96 | DHL_EXTKEY_F12, "F12", |
|---|
| 97 | |
|---|
| 98 | DHL_EXTKEY_DEL, "DEL", |
|---|
| 99 | |
|---|
| 100 | DHL_EXTKEY_UP, "Up", |
|---|
| 101 | DHL_EXTKEY_DN, "Down", |
|---|
| 102 | DHL_EXTKEY_RG, "Right", |
|---|
| 103 | DHL_EXTKEY_LF, "Left", |
|---|
| 104 | |
|---|
| 105 | DHL_EXTKEY_PGUP, "PgUp", |
|---|
| 106 | DHL_EXTKEY_PGDN, "PgDn", |
|---|
| 107 | |
|---|
| 108 | DHL_EXTKEY_HOME, "Home", |
|---|
| 109 | DHL_EXTKEY_END, "End", |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | static char *get_key_string(int key) |
|---|
| 113 | { |
|---|
| 114 | int i; |
|---|
| 115 | for (i=0; i<sizeof(extkeytbl)/sizeof(extkeytbl[0]); i++) { |
|---|
| 116 | if (extkeytbl[i].extkey == key) |
|---|
| 117 | return extkeytbl[i].name; |
|---|
| 118 | } |
|---|
| 119 | return "?"; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | /* |
|---|
| 125 | È®Àå Ű ÀÔ/Ãâ·Â µ¿ÀÛ Å×½ºÆ®. |
|---|
| 126 | |
|---|
| 127 | 1. È®Àå ۸¦ Áö¿øÇÏ´Â °æ¿ì Ű Äڵ尡 Á¤»óÀûÀÎÁö üũÇÑ´Ù. |
|---|
| 128 | |
|---|
| 129 | */ |
|---|
| 130 | void Console_ExtKeyEchoTest(void) |
|---|
| 131 | { |
|---|
| 132 | UINT32 key, prev_key = 0; |
|---|
| 133 | int count = 0; |
|---|
| 134 | UINT32 ms; |
|---|
| 135 | |
|---|
| 136 | DHL_OS_Printf("\n\n%s\n\n", __func__); |
|---|
| 137 | |
|---|
| 138 | DHL_OS_Printf("press same key 5 times to exit..\n"); |
|---|
| 139 | |
|---|
| 140 | while (1) |
|---|
| 141 | { |
|---|
| 142 | key = DHL_OS_GetChar(); |
|---|
| 143 | |
|---|
| 144 | ms = DHL_OS_GetMsCount(); |
|---|
| 145 | if (key >= 0x20 && key < 0x7f) // ASCII |
|---|
| 146 | DHL_OS_Printf("(%04u) input char: %02x, '%c'\n", |
|---|
| 147 | ms%10000, key, key); |
|---|
| 148 | else if (key <= 0xff) // 8-bits |
|---|
| 149 | DHL_OS_Printf("(%04u) input char: %02x, '%s'\n", |
|---|
| 150 | ms%10000, key, get_key_string(key)); |
|---|
| 151 | else |
|---|
| 152 | DHL_OS_Printf("(%04u) input char: %08x, '%s'\n", |
|---|
| 153 | ms%10000, key, get_key_string(key)); |
|---|
| 154 | |
|---|
| 155 | // check exit condition. |
|---|
| 156 | count = (key == prev_key) ? count+1 : 0; |
|---|
| 157 | prev_key = key; |
|---|
| 158 | |
|---|
| 159 | if (count >= 5) { |
|---|
| 160 | DHL_OS_Printf("exit test\n"); |
|---|
| 161 | break; |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | /* |
|---|
| 168 | press same key 5 times to exit.. |
|---|
| 169 | (4071) input char: 61, 'a' |
|---|
| 170 | (4292) input char: 62, 'b' |
|---|
| 171 | (4544) input char: 63, 'c' |
|---|
| 172 | (4776) input char: 64, 'd' |
|---|
| 173 | (7508) input char: 7f31317e, 'F1' |
|---|
| 174 | (7860) input char: 7f31327e, 'F2' |
|---|
| 175 | (8130) input char: 7f31337e, 'F3' |
|---|
| 176 | (9681) input char: 7f000044, 'Left' |
|---|
| 177 | (0023) input char: 7f000043, 'Right' |
|---|
| 178 | (0515) input char: 7f000041, 'Up' |
|---|
| 179 | (0767) input char: 7f000042, 'Down' |
|---|
| 180 | (4563) input char: 20, ' ' |
|---|
| 181 | (4743) input char: 20, ' ' |
|---|
| 182 | (4944) input char: 20, ' ' |
|---|
| 183 | (5115) input char: 20, ' ' |
|---|
| 184 | (5305) input char: 20, ' ' |
|---|
| 185 | (5658) input char: 20, ' ' |
|---|
| 186 | exit test |
|---|
| 187 | |
|---|
| 188 | */ |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | #if COMMENT |
|---|
| 192 | ____Test_3____(){} |
|---|
| 193 | #endif |
|---|
| 194 | |
|---|
| 195 | |
|---|
| 196 | |
|---|
| 197 | /* |
|---|
| 198 | string.h ÀÇ memmove°¡ Á¦°øµÇÁö ¾Ê´Â systemÀ» À§ÇÑ ¹è·Á. |
|---|
| 199 | */ |
|---|
| 200 | #if NEED_MEMMOVE_DEFINE |
|---|
| 201 | static void memmove(void *dst, void *src, int len) |
|---|
| 202 | { |
|---|
| 203 | UINT8 *pd = dst, *ps = src; |
|---|
| 204 | |
|---|
| 205 | if (len <= 0) return; // nothing to move. |
|---|
| 206 | |
|---|
| 207 | if (dst < src) { // copy from first |
|---|
| 208 | while (len-- > 0) *pd++ = *ps++; |
|---|
| 209 | } |
|---|
| 210 | else if (src < dst) { // copy from last |
|---|
| 211 | pd += len-1; ps += len-1; |
|---|
| 212 | while (len-- > 0) *pd-- = *ps--; |
|---|
| 213 | } |
|---|
| 214 | else { |
|---|
| 215 | // we don't need to move. |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | #endif |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | /* |
|---|
| 222 | wait console input, and make up line command and return. |
|---|
| 223 | |
|---|
| 224 | @buf is address of buffer where input command is stored. |
|---|
| 225 | @size is maximum size of @buf. |
|---|
| 226 | |
|---|
| 227 | return the number of characters input. |
|---|
| 228 | return -1 if input is cancelled by ESC (or Ctrl+C). |
|---|
| 229 | |
|---|
| 230 | @buf does not contain trailing CR/LF. |
|---|
| 231 | |
|---|
| 232 | This does not privode command history function. |
|---|
| 233 | Use mini shell for more powerful shell feature. |
|---|
| 234 | |
|---|
| 235 | */ |
|---|
| 236 | #define DHL_CONSOLE_FLAG_NOECHO 0x1 |
|---|
| 237 | |
|---|
| 238 | static int get_line(char *buf, int size, UINT32 flags) |
|---|
| 239 | { |
|---|
| 240 | char *p = buf; |
|---|
| 241 | UINT32 key; |
|---|
| 242 | char tmp[10] = {0, }; |
|---|
| 243 | BOOL cancelled = FALSE; |
|---|
| 244 | |
|---|
| 245 | while (1) |
|---|
| 246 | { |
|---|
| 247 | key = DHL_OS_GetChar(); |
|---|
| 248 | |
|---|
| 249 | if (key == 0) |
|---|
| 250 | continue; |
|---|
| 251 | |
|---|
| 252 | if (key == 0xd || key == 0xa) { |
|---|
| 253 | *p = 0; // null terminate and exit |
|---|
| 254 | if (!(flags & DHL_CONSOLE_FLAG_NOECHO)) |
|---|
| 255 | DHL_OS_Printf("\n"); |
|---|
| 256 | break; |
|---|
| 257 | } |
|---|
| 258 | else if (key == 0x1b) { // ESC key |
|---|
| 259 | cancelled = TRUE; |
|---|
| 260 | break; |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | // process input key |
|---|
| 264 | // |
|---|
| 265 | if (key == '\b' || key == 0x7F) { // backspace is treated as special |
|---|
| 266 | if (p > buf) { // go back ÇÒ ±ÛÀÚ°¡ ÀÖ´Â °æ¿ì¿¡¸¸.. |
|---|
| 267 | if (!(flags & DHL_CONSOLE_FLAG_NOECHO)) |
|---|
| 268 | DHL_OS_Printf("\b \b"); |
|---|
| 269 | *--p = ' '; |
|---|
| 270 | } |
|---|
| 271 | continue; |
|---|
| 272 | } |
|---|
| 273 | else if ((int)(p - buf) < size-1) { |
|---|
| 274 | if (!(flags & DHL_CONSOLE_FLAG_NOECHO)) { |
|---|
| 275 | tmp[0] = key; tmp[1] = 0; // ÇÑ ±ÛÀÚ¸¸ Ãâ·Â. |
|---|
| 276 | DHL_OS_Printf(tmp); |
|---|
| 277 | } |
|---|
| 278 | |
|---|
| 279 | *p++ = key; |
|---|
| 280 | } |
|---|
| 281 | else { |
|---|
| 282 | // do nothing, just ignore key.. buffer full |
|---|
| 283 | } |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | if (cancelled) |
|---|
| 287 | return -1; |
|---|
| 288 | |
|---|
| 289 | // trim trailing spaces |
|---|
| 290 | |
|---|
| 291 | p = buf + strlen(buf) - 1; |
|---|
| 292 | |
|---|
| 293 | while (p >= buf && (*p == ' ' || *p == '\t')) { |
|---|
| 294 | *p-- = 0; |
|---|
| 295 | //n_trim++; |
|---|
| 296 | } |
|---|
| 297 | |
|---|
| 298 | // skip head spaces and tab |
|---|
| 299 | |
|---|
| 300 | p = buf; |
|---|
| 301 | while (*p == ' ' || *p == '\t') p++; |
|---|
| 302 | |
|---|
| 303 | // shift strings |
|---|
| 304 | if (p != buf) |
|---|
| 305 | memmove(buf, p, strlen(p)+1); |
|---|
| 306 | |
|---|
| 307 | return strlen(buf); |
|---|
| 308 | |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | void Console_LineTest(void) |
|---|
| 313 | { |
|---|
| 314 | int n; |
|---|
| 315 | char buf[200]; |
|---|
| 316 | |
|---|
| 317 | DHL_OS_Printf("\n\n%s\n\n", __func__); |
|---|
| 318 | |
|---|
| 319 | DHL_OS_Printf("type 'exit' to exit..\n"); |
|---|
| 320 | |
|---|
| 321 | while (1) |
|---|
| 322 | { |
|---|
| 323 | DHL_OS_Printf("input: "); |
|---|
| 324 | |
|---|
| 325 | // ¿ø·¡ driver¿¡¼´Â echo¸¦ ÇÏÁö ¾ÊÀ¸¹Ç·Î |
|---|
| 326 | // DHL¿¡¼ echo¸¦ ÇØÁÖ´Â °ÍÀÌ ¸Â´Ù. |
|---|
| 327 | n = get_line(buf, sizeof(buf), 0); |
|---|
| 328 | |
|---|
| 329 | if (n < 0) { // cancelled. |
|---|
| 330 | DHL_OS_Printf(" ESC\n"); |
|---|
| 331 | continue; |
|---|
| 332 | } |
|---|
| 333 | |
|---|
| 334 | if (n == 0) continue; |
|---|
| 335 | |
|---|
| 336 | DHL_OS_Printf("[%s]\n", buf); |
|---|
| 337 | |
|---|
| 338 | // check exit condition |
|---|
| 339 | if (strcmp(buf, "exit") == 0) { |
|---|
| 340 | DHL_OS_Printf("exit test\n"); |
|---|
| 341 | break; |
|---|
| 342 | } |
|---|
| 343 | } |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | |
|---|
| 347 | /* |
|---|
| 348 | Å×½ºÆ® °á°ú: |
|---|
| 349 | |
|---|
| 350 | Console_LineTest |
|---|
| 351 | |
|---|
| 352 | type 'exit' to exit.. |
|---|
| 353 | input: This is test |
|---|
| 354 | [This is test] |
|---|
| 355 | input: exit |
|---|
| 356 | [exit] |
|---|
| 357 | exit test |
|---|
| 358 | |
|---|
| 359 | */ |
|---|
| 360 | |
|---|
| 361 | /* end of file */ |
|---|
| 362 | |
|---|