remove useless buffering code, allow to select the port to which we want to send output

This commit is contained in:
Laurent Vivier 2007-09-11 23:01:30 +00:00
parent 78e15acf9d
commit f5740cf4e3
2 changed files with 76 additions and 92 deletions

View File

@ -16,18 +16,11 @@
#include "head.h" #include "head.h"
#include "driver.h" #include "driver.h"
#include "config.h" #include "config.h"
#include "serial.h"
static short out_refnum0 = -1; static short out_refnum[SERIAL_PORT_NB];
static short out_refnum1 = -1;
#ifdef USE_CLI #ifdef USE_CLI
static short in_refnum0 = -1; static short in_refnum[SERIAL_PORT_NB];
static short in_refnum1 = -1;
#endif
#if USE_BUFFER
#define BUFFER_LEN 80
static char buffer[256];
static int buff_len;
#endif #endif
/* /*
@ -50,7 +43,7 @@ static int buff_len;
* *
*/ */
int setserial(short refNum, unsigned int bitrate, unsigned int datasize, static int setserial(short refNum, unsigned int bitrate, unsigned int datasize,
int parity, int stopbits) int parity, int stopbits)
{ {
CntrlParam param; CntrlParam param;
@ -187,50 +180,25 @@ int setserial(short refNum, unsigned int bitrate, unsigned int datasize,
return res; return res;
} }
void serial_put(char c) int serial_is_available(unsigned int port)
{ {
#if USE_BUFFER if (port > SERIAL_PORT_NB)
buffer[buff_len++] = c; return 0;
if (out_refnum[port] == -1)
return 0;
if ( c == '\n' ) return 1;
{
/* add '\r' and flush buffer */
buffer[buff_len++] = '\r';
goto flush;
} }
/* if buffer is full (BUFFER_LEN - 1), flush it
* we take BUFFER_LEN - 1 to have enough room
* if we need to add '\r' on '\n'
*/
if (buff_len == BUFFER_LEN - 1)
goto flush;
void serial_put(unsigned int port, char c)
{
if (!serial_is_available(port))
return; return;
flush:
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 ( c == '\n' )
{ write(out_refnum[port], "\n\r", 2);
if (out_refnum0 != -1)
write(out_refnum0, "\n\r", 2);
if (out_refnum1 != -1)
write(out_refnum1, "\n\r", 2);
}
else else
{ write(out_refnum[port], &c, 1);
if (out_refnum0 != -1)
write(out_refnum0, &c, 1);
if (out_refnum1 != -1)
write(out_refnum1, &c, 1);
}
#endif
} }
void serial_init(emile_l2_header_t* info) void serial_init(emile_l2_header_t* info)
@ -240,15 +208,24 @@ void serial_init(emile_l2_header_t* info)
res = read_config_modem(info, res = read_config_modem(info,
&bitrate, &parity, &datasize, &stopbits); &bitrate, &parity, &datasize, &stopbits);
if (res != -1) if (res == -1)
{ {
res = OpenDriver(c2pstring(".AOut"), &out_refnum0); out_refnum[SERIAL_MODEM_PORT] = -1;
#ifdef USE_CLI
in_refnum[SERIAL_MODEM_PORT] = -1;
#endif
}
else
{
res = OpenDriver(c2pstring(".AOut"),
&out_refnum[SERIAL_MODEM_PORT]);
if (res != noErr) { if (res != noErr) {
printf("Cannot open modem output port (%d)\n", res); printf("Cannot open modem output port (%d)\n", res);
} }
else else
{ {
res = setserial(out_refnum0, bitrate, res = setserial(out_refnum[SERIAL_MODEM_PORT],
bitrate,
datasize, datasize,
parity, parity,
stopbits); stopbits);
@ -258,13 +235,14 @@ void serial_init(emile_l2_header_t* info)
} }
} }
#ifdef USE_CLI #ifdef USE_CLI
res = OpenDriver(c2pstring(".AIn"), &in_refnum0); res = OpenDriver(c2pstring(".AIn"),
&in_refnum[SERIAL_MODEM_PORT]);
if (res != noErr) { if (res != noErr) {
printf("Cannot open modem input port (%d)\n", res); printf("Cannot open modem input port (%d)\n", res);
} }
else else
{ {
res = setserial(in_refnum0, bitrate, res = setserial(in_refnum[SERIAL_MODEM_PORT], bitrate,
datasize, datasize,
parity, parity,
stopbits); stopbits);
@ -278,14 +256,24 @@ void serial_init(emile_l2_header_t* info)
res = read_config_printer(info, res = read_config_printer(info,
&bitrate, &parity, &datasize, &stopbits); &bitrate, &parity, &datasize, &stopbits);
if (res != -1) { if (res == -1)
res = OpenDriver(c2pstring(".BOut"), &out_refnum1); {
out_refnum[SERIAL_PRINTER_PORT] = -1;
#ifdef USE_CLI
in_refnum[SERIAL_PRINTER_PORT] = -1;
#endif
}
else
{
res = OpenDriver(c2pstring(".BOut"),
&in_refnum[SERIAL_PRINTER_PORT]);
if (res != noErr) { if (res != noErr) {
printf("Cannot open printer output port (%d)\n", res); printf("Cannot open printer output port (%d)\n", res);
} }
else else
{ {
res = setserial(out_refnum1, bitrate, res = setserial(in_refnum[SERIAL_PRINTER_PORT],
bitrate,
datasize, datasize,
parity, parity,
stopbits); stopbits);
@ -295,13 +283,15 @@ void serial_init(emile_l2_header_t* info)
} }
} }
#ifdef USE_CLI #ifdef USE_CLI
res = OpenDriver(c2pstring(".BIn"), &in_refnum1); res = OpenDriver(c2pstring(".BIn"),
&in_refnum[SERIAL_PRINTER_PORT]);
if (res != noErr) { if (res != noErr) {
printf("Cannot open printer input port (%d)\n", res); printf("Cannot open printer input port (%d)\n", res);
} }
else else
{ {
res = setserial(in_refnum1, bitrate, res = setserial(in_refnum[SERIAL_PRINTER_PORT],
bitrate,
datasize, datasize,
parity, parity,
stopbits); stopbits);
@ -312,38 +302,27 @@ void serial_init(emile_l2_header_t* info)
} }
#endif /* USE_CLI */ #endif /* USE_CLI */
} }
#if USE_BUFFER
buff_len = 0;
#endif
} }
#ifdef USE_CLI #ifdef USE_CLI
int serial_getchar(void) int serial_getchar(unsigned int port)
{ {
int count; int count;
char c; char c;
if (in_refnum0 != -1) if (!serial_is_available(port))
{ return 0;
count = read(in_refnum0, &c, 1);
if (count == 1)
return c;
}
if (in_refnum1 != -1) count = read(in_refnum[port], &c, 1);
{
count = read(in_refnum1, &c, 1);
if (count == 1) if (count == 1)
return c; return c;
}
return 0; return 0;
} }
int serial_keypressed() int serial_keypressed(unsigned int port)
{ {
if (serial_getchar() != 0) if (serial_getchar(port) != 0)
return 1; return 1;
return 0; return 0;

View File

@ -6,12 +6,17 @@
#include "head.h" #include "head.h"
extern int setserial(short refNum, unsigned int bitrate, unsigned int datasize, enum {
int parity, int stopbits); SERIAL_MODEM_PORT = 0,
SERIAL_PRINTER_PORT = 1,
extern void serial_put(char c); SERIAL_PORT_NB,
};
extern int serial_is_available(unsigned int port);
extern void serial_put(unsigned int port, char c);
#ifdef USE_CLI #ifdef USE_CLI
extern int serial_keypressed(void); extern int serial_keypressed(unsigned int port);
extern int serial_getchar(void); extern int serial_getchar(unsigned int port);
#endif #endif
extern void serial_init(emile_l2_header_t* info); extern void serial_init(emile_l2_header_t* info);