From 3d6e6953b1078b5d064f331498187f1e63565c41 Mon Sep 17 00:00:00 2001 From: Laurent Vivier Date: Tue, 30 Aug 2005 08:03:50 +0000 Subject: [PATCH] Add serial_cursor_save(and serial_cursor_restore(), and open serial ports for input, read character from serial ports, detect keypressed --- second/serial.c | 147 +++++++++++++++++++++++++++++++++++++++--------- second/serial.h | 2 + 2 files changed, 122 insertions(+), 27 deletions(-) diff --git a/second/serial.c b/second/serial.c index 775597c..a701f66 100644 --- a/second/serial.c +++ b/second/serial.c @@ -12,8 +12,12 @@ #include "glue.h" #include "head.h" -static short refnum0 = -1; -static short refnum1 = -1; +static short out_refnum0 = -1; +static short out_refnum1 = -1; +#ifdef USE_CLI +static short in_refnum0 = -1; +static short in_refnum1 = -1; +#endif #if USE_BUFFER #define BUFFER_LEN 80 @@ -236,6 +240,27 @@ ssize_t write(int fd, const void *buf, size_t count) return param.ioActCount; } +#ifdef USE_CLI +ssize_t read(int fd, void *buf, size_t count) +{ + int res; + ParamBlockRec param; + + param.ioCompletion = 0; + param.ioVRefNum = 0; + param.ioRefNum = fd; + param.ioBuffer = (u_int32_t)buf; + param.ioReqCount= count; + param.ioPosMode = fsAtMark; + param.ioPosOffset = 0; + res = PBReadSync(¶m); + if (res != noErr) + return 0; + + return param.ioActCount; +} +#endif + void serial_put(char c) { #if USE_BUFFER @@ -259,25 +284,25 @@ void serial_put(char c) return; flush: - if (refnum0 != -1) - write(refnum0, buffer, buff_len); - if (refnum1 != -1) - write(refnum1, buffer, buff_len); + if (out_refnum0 != -1) + write(out_refnum0, buffer, buff_len); + if (out_refnum1 != -1) + write(out_refnum1, buffer, buff_len); buff_len = 0; #else if ( c == '\n' ) { - if (refnum0 != -1) - write(refnum0, "\n\r", 2); - if (refnum1 != -1) - write(refnum1, "\n\r", 2); + if (out_refnum0 != -1) + write(out_refnum0, "\n\r", 2); + if (out_refnum1 != -1) + write(out_refnum1, "\n\r", 2); } else { - if (refnum0 != -1) - write(refnum0, &c, 1); - if (refnum1 != -1) - write(refnum1, &c, 1); + if (out_refnum0 != -1) + write(out_refnum0, &c, 1); + if (out_refnum1 != -1) + write(out_refnum1, &c, 1); } #endif } @@ -287,37 +312,73 @@ void serial_init(emile_l2_header_t* info) int res; if (info->console_mask & STDOUT_SERIAL0) { - res = OpenDriver(c2pstring(".AOut"), &refnum0); + res = OpenDriver(c2pstring(".AOut"), &out_refnum0); if (res != noErr) { - printf("Cannot open modem port (%d)\n", res); + printf("Cannot open modem output port (%d)\n", res); } else { - res = setserial(refnum0, info->serial0_bitrate, + res = setserial(out_refnum0, info->serial0_bitrate, info->serial0_datasize, info->serial0_parity, info->serial0_stopbits); if (res != noErr) { - printf("Cannot setup modem port (%d)\n", res); + printf("Cannot setup modem output port (%d)\n", + res); } } - } - - if (info->console_mask & STDOUT_SERIAL1) { - res = OpenDriver(c2pstring(".BOut"), &refnum1); +#ifdef USE_CLI + res = OpenDriver(c2pstring(".AIn"), &in_refnum0); if (res != noErr) { - printf("Cannot open printer port (%d)\n", res); + printf("Cannot open modem input port (%d)\n", res); } else { - res = setserial(refnum1, info->serial1_bitrate, + res = setserial(in_refnum0, info->serial0_bitrate, + info->serial0_datasize, + info->serial0_parity, + info->serial0_stopbits); + if (res != noErr) { + printf("Cannot setup modem input port (%d)\n", + res); + } + } +#endif /* USE_CLI */ + } + + if (info->console_mask & STDOUT_SERIAL1) { + res = OpenDriver(c2pstring(".BOut"), &out_refnum1); + if (res != noErr) { + printf("Cannot open printer output port (%d)\n", res); + } + else + { + res = setserial(out_refnum1, info->serial1_bitrate, info->serial1_datasize, info->serial1_parity, info->serial1_stopbits); if (res != noErr) { - printf("Cannot setup printer port (%d)\n", res); + printf("Cannot setup printer output port (%d)\n" + , res); } } +#ifdef USE_CLI + res = OpenDriver(c2pstring(".BIn"), &in_refnum1); + if (res != noErr) { + printf("Cannot open printer input port (%d)\n", res); + } + else + { + res = setserial(in_refnum1, info->serial1_bitrate, + info->serial1_datasize, + info->serial1_parity, + info->serial1_stopbits); + if (res != noErr) { + printf("Cannot setup printer input port (%d)\n" + , res); + } + } +#endif /* USE_CLI */ } #if USE_BUFFER @@ -326,13 +387,45 @@ void serial_init(emile_l2_header_t* info) } #ifdef USE_CLI -int serial_keypressed() +void serial_cursor_save(void) { - return 0; + serial_put(''); + serial_put('7'); +} + +void serial_cursor_restore(void) +{ + serial_put(''); + serial_put('8'); } int serial_getchar(void) { + int count; + char c; + + if (in_refnum0 != -1) + { + count = read(in_refnum0, &c, 1); + if (count == 1) + return c; + } + + if (in_refnum1 != -1) + { + count = read(in_refnum1, &c, 1); + if (count == 1) + return c; + } + return -1; } + +int serial_keypressed() +{ + if (serial_getchar() != -1) + return 1; + + return 0; +} #endif diff --git a/second/serial.h b/second/serial.h index 1436a3e..36af91b 100644 --- a/second/serial.h +++ b/second/serial.h @@ -15,6 +15,8 @@ extern int setserial(short refNum, unsigned int bitrate, unsigned int datasize, extern void serial_put(char c); #ifdef USE_CLI +extern void serial_cursor_save(void); +extern void serial_cursor_restore(void); extern int serial_keypressed(void); extern int serial_getchar(void); #endif