home *** CD-ROM | disk | FTP | other *** search
File List | 1989-10-04 | 4.8 KB | 232 lines |
- _CONCURRENT C FOR REAL-TIME PROGRAMMING_
- by N.H. Gehani and W.D. Roome
-
- [LISTING ONE]
-
-
- process spec ttyOutput()
- {
- trans void put(int nc, char *pc);
- trans async ready(), start(), stop();
- };
-
- process spec ttyInput()
- {
- trans void put(char c);
- trans int get(int wait);
- };
-
- process spec ttyReader()
- {
- trans async ready();
- trans char get();
- };
-
- process spec ttyLine();
-
- process ttyInput ttyIn;
- process ttyOutput ttyOut;
- process ttyReader ttyRdr;
-
-
- [LISTIN╟ TWO]
-
- #include "concurrentc.h"
- #include "tty.h"
-
- void ttyReply(msg, reply)
- char *msg, *reply;
- {
- int c;
- char *nasty = "\r\nWAKE UP!\r\n";
-
- while (1) {
- ttyOut.put(strlen(msg), msg);
- c = within 30 ? ttyIn.get(1) : -1;
- if (c != -1)
- break;
- ttyOut.put(strlen(nasty), nasty);
- }
- while (c != '\n') {
- *reply++ = c;
- c = ttyIn.get(1);
- }
- *reply = '\0';
- }
-
-
- [LISTIN╟ THREE]
-
- #include "concurrentc.h"
- #include "tty.h"
-
- void ttyInit()
- {
- ttyOut = create ttyOutput() priority(1);
- c_associate(ttyOut.ready, 1/*DUM*//*PSoutput-ready-interruptPS*/);
- ttyRdr = create ttyReader() priority(2);
- c_associate(ttyRdr.ready, 2/*DUM*//*PSinput-ready-interruptPS*/);
- ttyIn = create ttyInput();
- create ttyLine();
- }
-
-
- [LISTIN╟ FOUR]
-
- #define M_RdrBuff 2048
- #define M_OutBuff 2048
- #define M_InBuff 1024
- #define M_LineLen 1024
-
- #define StopChar 0x13 /* ctl-S */
- #define StartChar 0x11 /* ctl-Q */
- #define EraseChar '\b' /* backspace */
- #define KillChar 0x03 /* ctl-C */
-
-
-
- [LISTIN╟ FIVE]
-
- #include "concurrentc.h"
- #include "tty.h"
- #include "defs.h"
-
- process body ttyReader()
- {
- int n = 0, in = 0, out = 0;
- char c, buff[M_RdrBuff];
-
- while (1) {
- select {
- accept ready();
- c = uartGetChar();
- if (c == StartChar) {
- ttyOut.start();
- } else if (c == StopChar) {
- ttyOut.stop();
- } else if (n < M_RdrBuff) {
- buff[in] = c;
- in = (in + 1) % M_RdrBuff;
- n++;
- }
- or (n > 0):
- accept get()
- treturn buff[out];
- out = (out + 1) % M_RdrBuff;
- n--;
- }
- }
- }
-
- [LISTIN╟ SIX]
-
- #include "concurrentc.h"
- #include "tty.h"
- #include "defs.h"
-
- process body ttyLine()
- {
- int n = 0, putline = 0, i;
- char c, buff[M_LineLen];
-
- while (1) {
- switch (c = ttyRdr.get()) {
- case EraseChar:
- ttyOut.put(3, "\b \b");
- if (n > 0)
- --n;
- break;
- case KillChar:
- if (n > 0)
- ttyOut.put(2, "\r\n");
- n = 0;
- break;
- è case '\r':
- case '\n':
- ttyOut.put(2, "\r\n");
- buff[n++] = '\n';
- putline = 1;
- break;
- default:
- ttyOut.put(1, &c);
- buff[n++] = c;
- if (n >= M_LineLen)
- putline = 1;
- break;
- }
- if (putline) {
- for (i = 0; i < n; i++)
- ttyIn.put(buff[i]);
- putline = n = 0;
- }
- }
- }
-
-
- [LISTIN╟ SEVEN]
- è
- #include "concurrentc.h"
- #include "tty.h"
- #include "defs.h"
-
- process body ttyInput()
- {
- int n = 0, in = 0, out = 0;
- char buff[M_InBuff];
-
- while (1) {
- select {
- (n < M_InBuff):
- accept put(c)
- buff[in] = c;
- in = (in + 1) % M_RdrBuff;
- n++;
- or (n > 0):
- accept get(wait)
- treturn buff[out];
- out = (out + 1) % M_RdrBuff;
- n--;
- or (n == 0):
- accept get(wait) suchthat (!wait)
- treturn -1;
- }
- }
- }
-
-
- [LISTIN╟ EIGHT]
-
- #include "concurrentc.h"
- #include "tty.h"
- #include "defs.h"
-
- process body ttyOutput()
- {
- int n = 0, in = 0, out = 0;
- int running = 1, ttyrdy = 1, i;
- char buff[M_OutBuff];
-
- while (1) {
- select {
- accept start()
- running = 1;
- or accept stop()
- running = 0;
- or accept ready()
- ttyrdy = 1;
- or accept put(nc, pc) suchthat (n+nc <= M_OutBuff) {
- for (i = 0; i < nc; i++) {
- buff[in] = pc[i];
- in = (in + 1) % M_OutBuff;
- }
- n += nc;
- }
- }
- if (ttyrdy && running && n > 0) {
- uartPutChar(buff[out]);
- out = (out + 1) % M_OutBuff;
- n--;
- ttyrdy = 0;
- }
- }
- }
-