diff --git a/newlib/libc/sys/sh3eb/console.c b/newlib/libc/sys/sh3eb/console.c index 7ea3e6e3e..39319048c 100644 --- a/newlib/libc/sys/sh3eb/console.c +++ b/newlib/libc/sys/sh3eb/console.c @@ -1,6 +1,8 @@ #include +#include #include + /* * Console instance for printf etc. */ @@ -11,6 +13,115 @@ _console _console_ctx = { .buf = {0}, }; +/* + * GetKeyWait provides a row from 2 (bottom) to 10 (top). + * The only exception is the AC key with a row of 1. + * This macro maps the row to 0 (top) to 8 (bottom) + * and 0 (AC) + */ +#define _CONSOLE_CONV_ROW(row) (!!(row - _CASIO_ROW_AC) * (_CASIO_ROW_MAX - row)) + +/* + * GetKeyWait provides a column from 2 (right) to 10 (left). + * The only exception is the AC key with a column of 1. + * This macro maps the column to 0 (left) to 8 (right) + * and 0 (AC) + */ +#define _CONSOLE_CONV_COL(col) (!!(col - _CASIO_COL_AC) * (_CASIO_COL_MAX - col)) + +#define _CONSOLE_MAX_ROW (_CASIO_ROW_MAX - _CASIO_ROW_MIN + 1) +#define _CONSOLE_MAX_COL (_CASIO_COL_MAX - _CASIO_COL_MIN + 1) + +#define _CONSOLE_MODES 3 +#define _CONSOLE_MODE_NORMAL 0 +#define _CONSOLE_MODE_SHIFT 1 +#define _CONSOLE_MODE_ALPHA 2 + +static const char row_col_to_ascii[_CONSOLE_MODES][_CONSOLE_MAX_ROW][_CONSOLE_MAX_COL] = +{ + //row_col_to_ascii[_CONSOLE_MAX_MODE_NORMAL] + { + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, '(', ')', ',', 0}, + {'7', '8', '9', '\b', 0, 0}, + {'4', '5', '6', '\x9E', '\xF6', 0}, + {'1', '2', '3', '+', '-', 0}, + {'0', '.', 0, ' ', '\n', 0} + }, + + //row_col_to_ascii[_CONSOLE_MAX_MODE_SHIFT] + { + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 'e', 0, 0, 0}, + {0, 0, 0, 'x', 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, '{', '}', 0}, + {0, 0, 0, '[', ']', 0}, + {'i', '=', 0, 0, '\n', 0}, + }, + //row_col_to_ascii[_CONSOLE_MAX_MODE_ALPHA] + { + {0, 0, 0, 0, 0, 0}, + {0, 0, 0, 0, 0, 0}, + {0, 0, 'r', 0, 0, 0}, + {'A', 'B', 'C', 'D', 'E', 'F'}, + {'G', 'H', 'I', 'J', 'K', 'L'}, + {'M', 'N', 'O', '\b', 0, 0}, + {'P', 'Q', 'R', 'S', 'T', 0}, + {'U', 'V', 'W', 'X', 'Y', 0}, + {'Z', ' ', '"', 0, 0, 0}, + }, +}; + + + +/* + * + */ +int _console_read(_console *ctx, int file, char *ptr, int len) { + char mode = _CONSOLE_MODE_NORMAL; + int col, row; + int i = 0; + + while (i < len) { + GetKeyWait(&col, &row, 0, 0, 0, 0); + + if (col == _CASIO_COL_SHIFT && row == _CASIO_ROW_SHIFT) { + mode = _CONSOLE_MODE_SHIFT; + continue; + } + if (col == _CASIO_COL_ALPHA && row == _CASIO_ROW_ALPHA) { + mode = _CONSOLE_MODE_ALPHA; + continue; + } + + // append char to string + ptr[i] = row_col_to_ascii[mode][_CONSOLE_CONV_ROW(row)][_CONSOLE_CONV_COL(col)]; + + // if the read character was a LF, stop + if (ptr[i] == '\n') { + return i + 1; + } + + // print character to stdout + _console_write(&_console_ctx, 1, ptr+i, 1); + _console_flush(&_console_ctx); + Bdisp_PutDisp_DD(); + + // reset mode + mode = _CONSOLE_MODE_NORMAL; + + // increment i if the current char was not '\0' only + i += !!ptr[i]; + } + + return i; +} /* * Sets the internal cursor position on the console. @@ -164,4 +275,4 @@ void _console_flush(_console *ctx) { ctx->buf[0] = '\0'; ctx->buf_strlen = 0; -} \ No newline at end of file +} diff --git a/newlib/libc/sys/sh3eb/sys/console.h b/newlib/libc/sys/sh3eb/sys/console.h index 334794111..31499360f 100644 --- a/newlib/libc/sys/sh3eb/sys/console.h +++ b/newlib/libc/sys/sh3eb/sys/console.h @@ -60,6 +60,7 @@ typedef struct { */ extern _console _console_ctx; +int _console_read(_console *ctx, int file, char *ptr, int len); int _console_write(_console *ctx, int file, char *ptr, int len); void _console_set_cursor(_console *ctx, unsigned char x, unsigned char y); void _console_flush(_console *ctx); diff --git a/newlib/libc/sys/sh3eb/sys/keybios.h b/newlib/libc/sys/sh3eb/sys/keybios.h new file mode 100644 index 000000000..848fa5f3f --- /dev/null +++ b/newlib/libc/sys/sh3eb/sys/keybios.h @@ -0,0 +1,158 @@ +/*****************************************************************/ +/* */ +/* CASIO fx-9860G SDK Library */ +/* */ +/* File name : keybios.h */ +/* */ +/* Copyright (c) 2006 CASIO COMPUTER CO., LTD. */ +/* */ +/*****************************************************************/ + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef __KEYBIOS_H__ +#define __KEYBIOS_H__ + + +// Defines + +// Character codes +#define KEY_CHAR_0 0x30 +#define KEY_CHAR_1 0x31 +#define KEY_CHAR_2 0x32 +#define KEY_CHAR_3 0x33 +#define KEY_CHAR_4 0x34 +#define KEY_CHAR_5 0x35 +#define KEY_CHAR_6 0x36 +#define KEY_CHAR_7 0x37 +#define KEY_CHAR_8 0x38 +#define KEY_CHAR_9 0x39 +#define KEY_CHAR_DP 0x2e +#define KEY_CHAR_EXP 0x0f +#define KEY_CHAR_PMINUS 0x87 +#define KEY_CHAR_PLUS 0x89 +#define KEY_CHAR_MINUS 0x99 +#define KEY_CHAR_MULT 0xa9 +#define KEY_CHAR_DIV 0xb9 +#define KEY_CHAR_FRAC 0xbb +#define KEY_CHAR_LPAR 0x28 +#define KEY_CHAR_RPAR 0x29 +#define KEY_CHAR_COMMA 0x2c +#define KEY_CHAR_STORE 0x0e +#define KEY_CHAR_LOG 0x95 +#define KEY_CHAR_LN 0x85 +#define KEY_CHAR_SIN 0x81 +#define KEY_CHAR_COS 0x82 +#define KEY_CHAR_TAN 0x83 +#define KEY_CHAR_SQUARE 0x8b +#define KEY_CHAR_POW 0xa8 +#define KEY_CHAR_IMGNRY 0x7f50 +#define KEY_CHAR_LIST 0x7f51 +#define KEY_CHAR_MAT 0x7f40 +#define KEY_CHAR_EQUAL 0x3d +#define KEY_CHAR_PI 0xd0 +#define KEY_CHAR_ANS 0xc0 +#define KEY_CHAR_LBRCKT 0x5b +#define KEY_CHAR_RBRCKT 0x5d +#define KEY_CHAR_LBRACE 0x7b +#define KEY_CHAR_RBRACE 0x7d +#define KEY_CHAR_CR 0x0d +#define KEY_CHAR_CUBEROOT 0x96 +#define KEY_CHAR_RECIP 0x9b +#define KEY_CHAR_ANGLE 0x7f54 +#define KEY_CHAR_EXPN10 0xb5 +#define KEY_CHAR_EXPN 0xa5 +#define KEY_CHAR_ASIN 0x91 +#define KEY_CHAR_ACOS 0x92 +#define KEY_CHAR_ATAN 0x93 +#define KEY_CHAR_ROOT 0x86 +#define KEY_CHAR_POWROOT 0xb8 +#define KEY_CHAR_SPACE 0x20 +#define KEY_CHAR_DQUATE 0x22 +#define KEY_CHAR_VALR 0xcd +#define KEY_CHAR_THETA 0xce +#define KEY_CHAR_A 0x41 +#define KEY_CHAR_B 0x42 +#define KEY_CHAR_C 0x43 +#define KEY_CHAR_D 0x44 +#define KEY_CHAR_E 0x45 +#define KEY_CHAR_F 0x46 +#define KEY_CHAR_G 0x47 +#define KEY_CHAR_H 0x48 +#define KEY_CHAR_I 0x49 +#define KEY_CHAR_J 0x4a +#define KEY_CHAR_K 0x4b +#define KEY_CHAR_L 0x4c +#define KEY_CHAR_M 0x4d +#define KEY_CHAR_N 0x4e +#define KEY_CHAR_O 0x4f +#define KEY_CHAR_P 0x50 +#define KEY_CHAR_Q 0x51 +#define KEY_CHAR_R 0x52 +#define KEY_CHAR_S 0x53 +#define KEY_CHAR_T 0x54 +#define KEY_CHAR_U 0x55 +#define KEY_CHAR_V 0x56 +#define KEY_CHAR_W 0x57 +#define KEY_CHAR_X 0x58 +#define KEY_CHAR_Y 0x59 +#define KEY_CHAR_Z 0x5a + + +// Control codes +#define KEY_CTRL_NOP 0 +#define KEY_CTRL_EXE 30004 +#define KEY_CTRL_DEL 30025 +#define KEY_CTRL_AC 30015 +#define KEY_CTRL_FD 30046 +#define KEY_CTRL_XTT 30001 +#define KEY_CTRL_EXIT 30002 +#define KEY_CTRL_SHIFT 30006 +#define KEY_CTRL_ALPHA 30007 +#define KEY_CTRL_OPTN 30008 +#define KEY_CTRL_VARS 30016 +#define KEY_CTRL_UP 30018 +#define KEY_CTRL_DOWN 30023 +#define KEY_CTRL_LEFT 30020 +#define KEY_CTRL_RIGHT 30021 +#define KEY_CTRL_F1 30009 +#define KEY_CTRL_F2 30010 +#define KEY_CTRL_F3 30011 +#define KEY_CTRL_F4 30012 +#define KEY_CTRL_F5 30013 +#define KEY_CTRL_F6 30014 +#define KEY_CTRL_CATALOG 30100 +#define KEY_CTRL_CAPTURE 30055 +#define KEY_CTRL_CLIP 30050 +#define KEY_CTRL_PASTE 30036 +#define KEY_CTRL_INS 30033 +#define KEY_CTRL_MIXEDFRAC 30054 +#define KEY_CTRL_FRACCNVRT 30026 +#define KEY_CTRL_QUIT 30029 +#define KEY_CTRL_PRGM 30028 +#define KEY_CTRL_SETUP 30037 +#define KEY_CTRL_PAGEUP 30052 +#define KEY_CTRL_PAGEDOWN 30053 +#define KEY_CTRL_MENU 30003 +#define KEY_CTRL_RESERVE1 30060 +#define KEY_CTRL_RESERVE2 30061 +#define KEY_CTRL_RESERVE3 30062 + + +// in Bkey_GetKeyWait function +#define KEYWAIT_HALTON_TIMEROFF 0 +#define KEYWAIT_HALTOFF_TIMEROFF 1 +#define KEYWAIT_HALTON_TIMERON 2 + +#define KEYREP_NOEVENT 0 +#define KEYREP_KEYEVENT 1 +#define KEYREP_TIMEREVENT 2 + + +#endif + +#ifdef __cplusplus +} +#endif diff --git a/newlib/libc/sys/sh3eb/syscalls.c b/newlib/libc/sys/sh3eb/syscalls.c index 77d5496d4..6a1d27c99 100644 --- a/newlib/libc/sys/sh3eb/syscalls.c +++ b/newlib/libc/sys/sh3eb/syscalls.c @@ -15,6 +15,7 @@ _read (int file, char *ptr, int len) { + _console_read(&_console_ctx, file, ptr, len); return len; }