|
DATAIO4PORT
Table of Contents |
dataIO4port
Global CONstantsSOURCE CODE... FF = 12 ' form feed CR = 13 ' carriage return NL = 13 MAXSTR_LENGTH = 32 ' this limits the size of a binary string that can be entered Used Objects
dataIO4port.spin
│
└──FullDuplexSerial4port.spin
SOURCE CODE... uarts : "FullDuplexSerial4port" PUBlic Spin methodsExtended sendStr(port,stringptr)Send stringSOURCE CODE... PUB Str(port,stringptr)
repeat strsize(stringptr)
uarts.tx(port,byte[stringptr++])
Strln(port,strAddr)Print a zero-terminated stringSOURCE CODE... PUB Strln(port,strAddr) str(port,strAddr) uarts.tx(port,CR) Dec(port,value)Print a decimal numberSOURCE CODE... PUB Dec(port,value) | i decl(port,value,10,0) Decf(port,value, width)Prints signed decimal value in space-padded, fixed-width fieldSOURCE CODE... PUB Decf(port,value, width) | i decl(port,value,width,1) Decx(port,value, digits)Prints zero-padded, signed-decimal string-- if value is negative, field width is digits+1 SOURCE CODE... PUB Decx(port,value, digits) | i decl(port,value,digits,2) Decl(port,value,digits,flag)SOURCE CODE... PUB Decl(port,value,digits,flag) | i, x
digits := 1 #> digits <# 10
x := value == NEGX 'Check for max negative
if value < 0
value := ||(value+x) 'If negative, make positive; adjust for max negative
uarts.tx(port,"-")
i := 1_000_000_000
if flag & 3
if digits < 10 ' less than 10 digits?
repeat (10 - digits) ' yes, adjust divisor
i /= 10
repeat digits
if value => i
uarts.tx(port,value / i + "0")
value //= i
result~~
elseif (i == 1) OR result OR (flag & 2)
uarts.tx(port,"0")
elseif flag & 1
uarts.tx(port," ")
i /= 10
DecDp(port,value,places)SOURCE CODE... PUB DecDp(port,value,places) | divisor ' prints out a fixed point number with a decimal point, places
divisor := 1
repeat places
divisor := divisor * 10
dec(port,value/divisor)
uarts.tx(port,".")
decx(port,||value//divisor,places)
Hex(port,value, digits)Print a hexadecimal numberSOURCE CODE... PUB Hex(port,value, digits)
value <<= (8 - digits) << 2
repeat digits
uarts.tx(port,lookupz((value <-= 4) & $F : "0".."9", "A".."F"))
iHex(port,value, digits)Print an indicated hexadecimal numberSOURCE CODE... PUB iHex(port,value, digits) uarts.tx(port,"$") hex(port,value,digits) Bin(port,value, digits)Print a binary numberSOURCE CODE... PUB Bin(port,value, digits)
value <<= 32 - digits
repeat digits
uarts.tx(port,(value <-= 1) & 1 + "0")
PadChar(port,count, txbyte)SOURCE CODE... PUB PadChar(port,count, txbyte)
repeat count
uarts.tx(port,txbyte)
iBin(port,value, digits)Print an indicated binary numberSOURCE CODE... PUB iBin(port,value, digits) uarts.tx(port,"%") bin(port,value,digits) Putc(port,txbyte)Send a byte to the terminalSOURCE CODE... PUB Putc(port,txbyte) uarts.tx(port,txbyte) NewLine(port)SOURCE CODE... PUB NewLine(port) putc(port,CR) Cls(port)Extended receiveSOURCE CODE... PUB Cls(port)
putc(port,FF)
{the following added from PST primarily for data input}
Getc(port)Get a character-- will not block if nothing in uart buffer SOURCE CODE... PUB Getc(port) return uarts.rxcheck(port) ' return rx StrIn(port,stringptr)TTA from PST.
Receive a string (carriage return terminated) and stores it (zero terminated) starting at stringptr.
Waits until full string received.
Parameter:
stringptr - pointer to memory in which to store received string characters.
Memory reserved must be large enough for all string characters plus a zero terminator.
SOURCE CODE... PUB StrIn(port,stringptr) StrInMax(port, stringptr, -1) StrInMax(port, stringptr, maxcount)from PST, modified
Receive a string of characters (either carriage return terminated or maxcount in length) and stores it (zero terminated)
starting at stringptr. Waits until either full string received or maxcount characters received.
Parameters:
stringptr - pointer to memory in which to store received string characters.
Memory reserved must be large enough for all string characters plus a zero terminator (maxcount + 1).
maxcount - maximum length of string to receive, or -1 for unlimited.
SOURCE CODE... PUB StrInMax(port, stringptr, maxcount) | char, ticks
maxcount <#= MAXSTR_LENGTH
repeat maxcount 'While maxcount not reached
if (byte[stringptr++] := uarts.rx(0)) == NL 'Get chars until NL
quit
byte[stringptr+(byte[stringptr-1] == NL)]~ 'Zero terminate string; overwrite NL or append 0 char
StrInB(port, stringptr, seconds)SOURCE CODE... PUB StrInB(port, stringptr, seconds) | char, ticks
ticks := clkfreq*seconds+cnt
repeat MAXSTR_LENGTH
repeat
char := uarts.rxcheck(port)
until char > -1
case char
32..126 : byte[stringptr++] := char ' encompasses all numeric chars in dec and hex
8, 127 :stringptr--
13 : quit
if ticks -= cnt < 0
quit
byte[stringptr]~
DecIn(port)PST. Receive carriage return terminated string of characters representing a decimal value. Returns: the corresponding decimal value. SOURCE CODE... PUB DecIn(port) : value StrInMax(port, @str_buffer, MAXSTR_LENGTH) value := StrToBase(@str_buffer, 10) HexIn(port)from PST. Receive carriage return terminated string of characters representing a hexadecimal value. Returns: the corresponding hexadecimal value. SOURCE CODE... PUB HexIn(port) : value StrInMax(port, @str_buffer, MAXSTR_LENGTH) value := StrToBase(@str_buffer, 16) BinIn(port)from PST. Receive carriage return terminated string of characters representing a binary value. Returns: the corresponding binary value. Note that the constant MAXSTR_LENGTH limits the # of digits SOURCE CODE... PUB BinIn(port) : value StrInMax(port, @str_buffer, MAXSTR_LENGTH) value := StrToBase(@str_buffer, 2) PRIvate Spin methodsStrToBase(stringptr, base)from PST. Converts a zero terminated string representation of a number to a value in the designated base. Ignores all non-digit characters (except negative (-) when base is decimal (10)). SOURCE CODE... PRI StrToBase(stringptr, base) : value | chr, index
value := index := 0
repeat until ((chr := byte[stringptr][index++]) == 0)
chr := -15 + --chr & %11011111 + 39*(chr > 56) 'Make "0"-"9","A"-"F","a"-"f" be 0 - 15, others out of range
if (chr > -1) and (chr < base) 'Accumulate valid values into result; ignore others
value := value * base + chr
if (base == 10) and (byte[stringptr] == "-") 'If decimal, address negative sign; ignore otherwise
value := - value
Global VARiablesSOURCE CODE...
byte str_buffer[MAXSTR_LENGTH+1] 'String buffer for numerical string input only
' This is in VAR space. If multiple cogs will be _inputing_ numerical data
' simultaneously, it is necessary to delare another instance so that
' the buffers do not clash! That applies to numerical parsing only.
License┌──────────────────────────────────────────────────────────────────────────────────────┐ │ TERMS OF USE: MIT License │ ├──────────────────────────────────────────────────────────────────────────────────────┤ │Permission is hereby granted, free of charge, to any person obtaining a copy of this │ │software and associated documentation files (the "Software"), to deal in the Software │ │without restriction, including without limitation the rights to use, copy, modify, │ │merge, publish, distribute, sublicense, and/or sell copies of the Software, and to │ │permit persons to whom the Software is furnished to do so, subject to the following │ │conditions: │ │ │ │The above copyright notice and this permission notice shall be included in all copies │ │or substantial portions of the Software. │ │ │ │THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, │ │INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A │ │PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT │ │HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF │ │CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE │ │OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. │ └──────────────────────────────────────────────────────────────────────────────────────┘ |