Commit d7167ba3 authored by Mart Lubbers's avatar Mart Lubbers

Initial commit

parents
Clean System Files/*.o
Clean System Files/*.abc
Clean System Files/*.tcl
TTY
sapl
TTY.prj
|| ?module? TTY
TTY.TTYopen2_13 !_x_0 !_x_1::I !_x_2::I !_x_3::I !_x_4::I !_x_5::B !_x_6 = error "no AnyCodeExpr"
TTY.toInt_14::I !_x_0 = case _x_0 (TTY.B0 -> 0) (TTY.B50 -> 1) (TTY.B75 -> 2) (TTY.B110 -> 3) (TTY.B134 -> 4) (TTY.B150 -> 5) (TTY.B200 -> 6) (TTY.B300 -> 7) (TTY.B600 -> 8) (TTY.B1200 -> 9) (TTY.B1800 -> 10) (TTY.B2400 -> 11) (TTY.B4800 -> 12) (TTY.B9600 -> 13) (TTY.B19200 -> 14) (TTY.B38400 -> 15) (TTY.B57600 -> 16) (TTY.B115200 -> 17) (TTY.B230400 -> 18)
TTY.toInt_15::I !_x_0 = case _x_0 (TTY.BytesizeFive -> 0) (TTY.BytesizeSix -> 1) (TTY.BytesizeSeven -> 2) (TTY.BytesizeEight -> 3)
TTY.toInt_16::I !_x_0 = case _x_0 (TTY.ParityNone -> 0) (TTY.ParityEven -> 1) (TTY.ParityOdd -> 2) (TTY.ParityMark -> 3) (TTY.ParitySpace -> 4)
TTY.toInt_17::I !_x_0 = case _x_0 (TTY.StopbitsOne -> 0) (TTY.StopbitsOnePointFive -> 1) (TTY.StopbitsTwo -> 2)
TTY.TTYopen !fn_0 !ts_1 !w_2 = TTY.TTYopen2_13 fn_0 (TTY.toInt_14 (select ts_1::TTY._TTYSettings 0)) (TTY.toInt_15 (select ts_1::TTY._TTYSettings 1)) (TTY.toInt_16 (select ts_1::TTY._TTYSettings 2)) (TTY.toInt_17 (select ts_1::TTY._TTYSettings 3)) (select ts_1::TTY._TTYSettings 4) w_2
TTY.TTYclose !f_0::I !w_1 = error "no AnyCodeExpr"
TTY.TTYreadc !w_0::I = error "no AnyCodeExpr"
TTY.TTYreadline !t_0::I = error "no AnyCodeExpr"
TTY.TTYerror !w_0 = error "no AnyCodeExpr"
TTY.zero_5 = TTY._TTYSettings TTY.B9600 TTY.BytesizeEight TTY.ParityNone TTY.StopbitsOne False
TTY.Start !w_0 = let !_x_1_0 = TTY.TTYopen "/dev/ttyUSB0" TTY.zero_5 w_0 in case (StdBool.not (select _x_1_0::_Tuple3 0)) (True -> TTY.TTYerror (select _x_1_0::_Tuple3 2)) (False -> let !_x_3_0 = TTY.TTYreadline (select _x_1_0::_Tuple3 1), !_x_3_1 = TTY.TTYclose (select _x_3_0::_Tuple2 1) (select _x_1_0::_Tuple3 2) in case (StdBool.not (select _x_3_1::_Tuple2 0)) (True -> TTY.TTYerror (select _x_3_1::_Tuple2 1)) (False -> let !_x_5_0 = TTY.TTYerror (select _x_3_1::_Tuple2 1) in <{_Tuple2!1}> (select _x_3_0::_Tuple2 0) (select _x_5_0::_Tuple2 1)) )
<{TTY.TD;TTY}> _x_0::B = _predefined._Nil
<{TTY.TD;ByteSize}> _x_0::B = _predefined._Nil
<{TTY.TD;Parity}> _x_0::B = _predefined._Nil
<{TTY.TD;StopBits}> _x_0::B = _predefined._Nil
<{TTY.TD;BaudRate}> _x_0::B = _predefined._Nil
<{TTY.TD;TTYSettings}> _x_0::B = _predefined._Nil
:: TTY.ByteSize = TTY.BytesizeFive | TTY.BytesizeSix | TTY.BytesizeSeven | TTY.BytesizeEight
:: TTY.Parity = TTY.ParityNone | TTY.ParityEven | TTY.ParityOdd | TTY.ParityMark | TTY.ParitySpace
:: TTY.StopBits = TTY.StopbitsOne | TTY.StopbitsOnePointFive | TTY.StopbitsTwo
:: TTY.BaudRate = TTY.B0 | TTY.B50 | TTY.B75 | TTY.B110 | TTY.B134 | TTY.B150 | TTY.B200 | TTY.B300 | TTY.B600 | TTY.B1200 | TTY.B1800 | TTY.B2400 | TTY.B4800 | TTY.B9600 | TTY.B19200 | TTY.B38400 | TTY.B57600 | TTY.B115200 | TTY.B230400
|| Converted Records
:: TTY._TTYSettings = {TTY.baudrate, TTY.bytesize, TTY.parity, TTY.stopbits, TTY.xonxoff::B}
:: <{TTY.gEq_s;}> = {!TTY.gEq_s}
:: <{TTY.gEq_ss;}> = {!TTY.gEq_ss}
:: <{TTY.gEq_sss;}> = {!TTY.gEq_sss}
:: <{TTY.gEq_ssss;}> = {!TTY.gEq_ssss}
:: <{TTY.gEq_sssss;}> = {!TTY.gEq_sssss}
:: <{TTY.gEq_ssssss;}> = {!TTY.gEq_ssssss}
:: <{TTY.gEq_sssssss;}> = {!TTY.gEq_sssssss}
:: <{TTY.gEq_ssssssss;}> = {!TTY.gEq_ssssssss}
:: <{TTY.gEq_sssssssss;}> = {!TTY.gEq_sssssssss}
:: <{TTY.bimap_s;}> = {TTY.bimap_s}
:: <{TTY.bimap_ss;}> = {TTY.bimap_ss}
:: <{TTY.bimap_sss;}> = {TTY.bimap_sss}
#define Clean(a)
typedef struct clean_string *CleanString;
/* a string in Clean is:
struct clean_string {
size_t clean_string_length;
char clean_string_characters[clean_string_length];
};
The string does not end with a '\0' !
*/
#ifndef _WIN64
/* CleanStringLength(clean_string) returns the length of the clean_string in characters */
#define CleanStringLength(clean_string) (*(unsigned long *)(clean_string))
/* CleanStringCharacters(clean_string) returns a pointer to the characters of the clean_string */
#define CleanStringCharacters(clean_string) ((char*)(1+(unsigned long *)(clean_string)))
/* CleanStringSizeInts(string_length) return size of *CleanString in integers */
#define CleanStringSizeInts(string_length) (1+(((unsigned long)(string_length)+(sizeof(unsigned long)-1))>>(1+(sizeof(unsigned long)>>2))))
/* CleanStringVariable(clean_string,string_length) defines variable clean_string with length string_length,
before using the clean_string variable, cast to CleanString, except for the macros above */
#define CleanStringVariable(clean_string,string_length) unsigned long clean_string[CleanStringSizeInts(string_length)]
/* CleanStringSizeBytes(string_length) return size of *CleanString in bytes */
#define CleanStringSizeBytes(string_length) ((sizeof(unsigned long)<<1)+(((unsigned long)(string_length)+(sizeof(unsigned long)-1)) & -(sizeof(unsigned long))))
typedef long *CleanIntArray;
/* CleanIntArraySize(clean_array) returns the size (number of elements) of the clean_int_array */
#define CleanIntArraySize(clean_int_array) (((unsigned long *)(clean_int_array))[-2])
/* CleanRealArraySize(clean_real_array) returns the size (number of elements) of the clean_real_array */
#define CleanRealArraySize(clean_real_array) (((unsigned long *)(clean_real_array))[-2])
/* CleanCharArraySize(clean_char_array) returns the size (number of elements) of the clean_char_array */
#define CleanCharArraySize(clean_char_array) (((unsigned long *)(clean_char_array))[-1])
#else
/* CleanStringLength(clean_string) returns length of the clean_string in characters */
#define CleanStringLength(clean_string) (*(unsigned __int64 *)(clean_string))
/* CleanStringCharacters(clean_string) returns a pointer to the characters of the clean_string */
#define CleanStringCharacters(clean_string) ((char*)(1+(unsigned __int64 *)(clean_string)))
/* CleanStringSizeInts(string_length) return size of *CleanString in integers */
#define CleanStringSizeInts(string_length) (1+(((unsigned __int64)(string_length)+7)>>3))
/* CleanStringVariable(clean_string,string_length) defines variable clean_string with length string_length,
before using the clean_string variable, cast to CleanString, except for the macros above */
#define CleanStringVariable(clean_string,string_length) unsigned __int64 clean_string[CleanStringSizeInts(string_length)]
/* CleanStringSizeBytes(string_length) return size of *CleanString in bytes */
#define CleanStringSizeBytes(string_length) (8+(((unsigned __int64)(string_length)+7) & -8))
typedef __int64 *CleanIntArray;
/* CleanIntArraySize(clean_array) returns the size (number of elements) of the clean_int_array */
#define CleanIntArraySize(clean_int_array) (((unsigned __int64 *)(clean_int_array))[-2])
/* CleanRealArraySize(clean_real_array) returns the size (number of elements) of the clean_real_array */
#define CleanRealArraySize(clean_real_array) (((unsigned __int64 *)(clean_real_array))[-2])
/* CleanCharArraySize(clean_char_array) returns the size (number of elements) of the clean_char_array */
#define CleanCharArraySize(clean_char_array) (((unsigned __int64 *)(clean_char_array))[-1])
#endif
typedef double *CleanRealArray;
typedef unsigned char *CleanCharArray;
TTY: TTY.prj TTY.icl TTY.dcl Clean\ System\ Files/tty.o
cpm make
Clean\ System\ Files/tty.o: tty.c
gcc -g -c $< -o "$@"
clean:
$(RM) -r Clean\ System\ Files/* TTY sapl
definition module TTY
from Data.Maybe import :: Maybe
from StdFile import class FileSystem
from StdClass import class zero
:: *TTY
:: ByteSize = BytesizeFive | BytesizeSix | BytesizeSeven | BytesizeEight
:: Parity = ParityNone | ParityEven | ParityOdd | ParityMark | ParitySpace
:: StopBits = StopbitsOne | StopbitsOnePointFive | StopbitsTwo
:: BaudRate = B0 | B50 | B75 | B110 | B134 | B150 | B200 | B300 | B600 |
B1200 | B1800 | B2400 | B4800 | B9600 | B19200 | B38400 | B57600 |
B115200 | B230400
:: TTYSettings = {
baudrate :: BaudRate,
bytesize :: ByteSize,
parity :: Parity,
stopbits :: StopBits,
xonxoff :: Bool
}
instance zero TTYSettings
TTYopen :: !String !TTYSettings !*env -> (!Bool,!*TTY,!*env)
TTYclose :: !*TTY !*env -> (!Bool, !*env)
TTYreadc :: !*TTY -> (!Char, !*TTY)
TTYreadline :: !*TTY -> (!String, !*TTY)
TTYerror :: !*env -> (!String, !*env)
implementation module TTY
import Data.Maybe
import StdFunc
import StdFile
import StdMisc
import StdBool
import StdString
import code from "tty."
:: *TTY :== Int
instance zero TTYSettings where
zero = {TTYSettings |
baudrate = B9600,
bytesize = BytesizeEight,
parity = ParityNone,
stopbits = StopbitsOne,
xonxoff = False}
instance toInt BaudRate where
toInt B0 = 0
toInt B50 = 1
toInt B75 = 2
toInt B110 = 3
toInt B134 = 4
toInt B150 = 5
toInt B200 = 6
toInt B300 = 7
toInt B600 = 8
toInt B1200 = 9
toInt B1800 = 10
toInt B2400 = 11
toInt B4800 = 12
toInt B9600 = 13
toInt B19200 = 14
toInt B38400 = 15
toInt B57600 = 16
toInt B115200 = 17
toInt B230400 = 18
instance toInt ByteSize where
toInt BytesizeFive = 0
toInt BytesizeSix = 1
toInt BytesizeSeven = 2
toInt BytesizeEight = 3
instance toInt Parity where
toInt ParityNone = 0
toInt ParityEven = 1
toInt ParityOdd = 2
toInt ParityMark = 3
toInt ParitySpace = 4
instance toInt StopBits where
toInt StopbitsOne = 0
toInt StopbitsOnePointFive = 1
toInt StopbitsTwo = 2
TTYopen :: !String !TTYSettings !*env -> (!Bool, !*TTY, !*env)
TTYopen fn ts w = TTYopen2
fn
(toInt ts.baudrate)
(toInt ts.bytesize)
(toInt ts.parity)
(toInt ts.stopbits)
ts.xonxoff
w
where
TTYopen2 :: !String !Int !Int !Int !Int !Bool !*env -> (!Bool, !*TTY, !*env)
TTYopen2 _ _ _ _ _ _ _ = code {
ccall ttyopen "SIIIII:VII:A"
}
TTYclose :: !*TTY !*env -> (!Bool, !*env)
TTYclose f w = code {
ccall ttyclose "I:I:A"
}
TTYerror :: !*env -> (!String, !*env)
TTYerror w = code {
ccall ttyerror ":S:A"
}
TTYreadc :: !*TTY -> (!Char, !*TTY)
TTYreadc w = code {
ccall ttyreadc "I:VII"
}
TTYreadline :: !*TTY -> (!String, !*TTY)
TTYreadline t = code {
ccall ttyreadline "I:VSI"
}
Start :: *World -> (!String, *World)
Start w
#! (ok, tty, w) = TTYopen "/dev/ttyUSB0" zero w
| not ok = TTYerror w
#! (c, tty) = TTYreadline tty
#! (ok, w) = TTYclose tty w
| not ok = TTYerror w
#! (s, w) = TTYerror w
= (c, w)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include "Clean.h"
#define INITIAL_BUFFERSIZE 1024
#define die(s) {perror(s);exit(EXIT_FAILURE);}
static speed_t baudrates[] = {B0, B50, B75, B110, B134, B150, B200, B300, B600,
B1200, B1800, B2400, B4800, B9600, B19200, B38400, B57600, B115200,
B230400};
static int bytesizes[4] = {CS5, CS6, CS7, CS8};
//static int stopbits[3] = {StopbitsOne, StopbitsOnePointFive, StopbitsTwo};
static char *error = "";
static char *cleanStringToCString(CleanString s)
{
unsigned long len = CleanStringLength(s);
char *cs = (char *)malloc(len+1);
if(cs == NULL)
die("malloc");
memcpy(cs, CleanStringCharacters(s), len);
cs[len] = '\0';
return cs;
}
void ttyopen(CleanString fn, int baudrate, int bytesize, int parity, int stopbits, int xonoff, int *status, FILE **f)
{
struct termios tio;
char *cs_fn = cleanStringToCString(fn);
int fd = open(cs_fn, O_RDWR | O_NOCTTY);
*status = 0;
if(fd < 0){
error = strerror(errno);
} else {
//Get
tcgetattr(fd, &tio);
//Baudrate
cfsetispeed(&tio, baudrates[baudrate]);
//Bytesize
tio.c_cflag |= CS5 | CS6 | CS7 | CS8;
tio.c_cflag -= CS5 | CS6 | CS7 | CS8;
tio.c_cflag |= bytesizes[bytesize];
//Parity
tio.c_cflag |= PARENB | PARODD | CMSPAR;
tio.c_cflag -= PARENB | PARODD | CMSPAR;
if(parity == 1){
tio.c_cflag |= PARENB | PARODD;
} else if(parity == 2){
tio.c_cflag |= PARENB;
} else if(parity == 3){
tio.c_cflag |= PARODD | PARENB | CMSPAR;
} else if(parity == 4){
tio.c_cflag |= PARENB | CMSPAR;
}
//Stopbits
tio.c_cflag |= CSTOPB;
tio.c_cflag -= stopbits != 0 ? 0 : CSTOPB;
//Xonoff
tio.c_cflag |= IXON;
tio.c_cflag -= xonoff == 1 ? 0 : IXON;
//Set
tcsetattr(fd, TCSANOW, &tio);
*f = fdopen(fd, "r+");
if(*f == NULL){
printf("Couldn't open\n");
fflush(stdout);
} else {
printf("Succesfully opened\n");
fflush(stdout);
setbuf(*f, NULL);
*status = 1;
}
error = strerror(errno);
}
free(cs_fn);
}
void ttyerror(CleanString *result)
{
CleanStringVariable(clean_string, strlen(error));
*result = (CleanString) clean_string;
memcpy(CleanStringCharacters(clean_string), error, strlen(error));
CleanStringLength(clean_string) = strlen(error);
}
void ttyreadc(FILE *fd, int *c, FILE **fdo)
{
*c = fgetc(fd);
*fdo = fd;
}
void ttyreadline(FILE *fd, CleanString *result, FILE **fdo)
{
size_t bufsize = INITIAL_BUFFERSIZE;
char *buf = (char *)malloc(bufsize+1);
int c, i = 0;
if(buf == NULL)
die("malloc");
while((c = fgetc(fd)) != EOF && c != '\n'){
if(i >= bufsize)
if((buf = realloc(buf, bufsize *= 2)) == NULL)
die("realloc");
buf[i++] = c;
}
buf[i] = '\0';
CleanStringVariable(cleanOutput, strlen(buf));
*result = (CleanString) cleanOutput;
memcpy(CleanStringCharacters(cleanOutput), buf, strlen(buf));
CleanStringLength(cleanOutput) = strlen(buf);
*fdo = fd;
free(buf);
}
int ttyclose(FILE *fd)
{
return fclose(fd) == 0 ? 1 : 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment