March 05, 2005

code for clock

still dirty but here is the running pic code


' Modified Furniture Client 0001
' Based on code By Tom Igoe, 2004
dataByte var byte
dataByte2 var byte

init Var Byte
init = 0

loginTime Var Byte(80)
sendTime Var Byte(17)

h2 VAR BYTE
m2 Var BYTE
s2 Var BYTE

h VAR BYTE(2)
m Var BYTE(2)
s Var BYTE(2)

ndeg Var Word
nmin Var word

dirt var Byte
dirt = 0

tx var portc.6
rx var portc.7
xportRx var portc.0
xportTx var portc.1
xportDTR var portc.2
inv9600 con 16468 ' baudmode for serin2 and serout2: 9600 8-N-1 inverted
non9600 con 84 ' baudmode for serin2 and serout2: 9600 8-N-1 non-inverted

led1 VAR portC.4
led2 VAR portC.3


inputMaxLength con 96 ' max. input string
nameMaxLength con 20 ' max name string
cmdMaxLength con 10 ' max command string
asciiSpace con 32 ' constant for ascii SPACE character
lf con 10 ' constant for ascii linefeed character
cr con 13 ' constant for ascii carriage return character
i var byte ' counter
j Var Byte
inputArray var byte(inputMaxLength) ' input strung from cobox
nameArray var byte(nameMaxLength) ' name of person who sent to us
cmdArray var byte(cmdMaxLength) ' command string they sent us
nameLength var byte ' length of their name
cmdLength var byte ' length of the command
chunkCursorPos var byte ' array pointer
currentChar var byte ' array pointer
'dataByte var byte ' used when we just want one byte
clear ' clear all variables

Pause 1000 ' Wait a second at startup

login:
' wait for an ascii C, signifying a connection.
' this assumes you're in autoconnect mode (ConnectMode D5)
'while dataByte <> 67
' serin2 rx, non9600, [dataByte]
'wend
' wait until the intro text is past. It ends with a > and a linefeed:
serin2 xportrx, non9600, [WAIT(">"), dataByte]
pause 100
' send your login
serout2 xporttx, non9600, ["login 1x", lf]

main:

serin2 xportrx, non9600, [STR inputArray\96\lf]
nodata:
' parse the name from the input string:
gosub getName
' if we got no name, it's garbage. Go back to main:
'if nameLength = 0 then main
'IF nameArray[0] = 97 And nameArray[1] =108 And nameArray[2] =108 Then
'SEROUT2 xporttx, non9600, ["send troublemaker", "all rejected"]
'Goto main
'Endif
' parse the command from the input string:
'gosub getCommand
' do something based on the first letter of the command:
'select case CmdArray[0]
'case 65: ' ascii A
' serout2 xporttx, non9600, ["send " ,STR nameArray, " ", lf]
'case 69: ' ascii E
' serout2 xporttx, non9600, ["send ", STR nameArray, " I'll shut up already!", lf]
'end select
'serout2 xportTx, non9600, [dataByte, 13]
serout2 tx, inv9600, [STR nameArray, STR cmdArray, 10, 13]
'serout2 xporttx, non9600, ["get_time", lf]
'serout2 xporttx, non9600, ["send troublemaker f ya", lf]
' clear the arrays for the next round of input:
GOSUB getTime
gosub clearName
gosub clearCommand
gosub clearInput
goto main


getTime:
SEROUT2 xporttx, non9600, ["get_time", LF]
serin2 xportrx, non9600, [wait ("5"), str loginTime\80\">"]
for i = 0 to 17
sendTime[i] = loginTime[i + 5]
next i
for j = 0 to 1
h[j] = loginTime[j + 14]
next j
for j = 0 to 1
m[j] = loginTime[j + 17]
next j
for j = 0 to 1
s[j] = loginTime[j + 20]
next j
h2 = (h[0]-48)*10 + (h[1]-48)
m2 = (m[0]-48)*10 + (m[1]-48)
s2 = (s[0]-48)*10 + (s[1]-48)
ndeg = (h2 * 60 / 4) + (m2 / 4)
nmin = ((m2 // 4) * 60 / 4) + (s2 / 4)
serout2 xporttx, non9600, ["send all ", DEC ndeg ," degrees ", DEC nmin, " minutes ", 13, 10]
'serout2 xporttx, non9600, ["send ", STR nameArray, " " , str sendTime\17, "---", DEC ndeg ,"deg ", DEC nmin, "min", 13, 10]
serout2 tx, inv9600, [str sendTime\17, 13, 10]

Return

getName:
' iterate over the input array, copying bytes into the name array
' until we get a space (ascii 32)
currentChar = 0
while inputArray[currentChar] <> asciiSpace
nameArray[currentChar] = inputArray[currentChar]
currentChar = currentChar + 1
' if we overflow the array, we have a bogus name. go back to main
if currentChar >= nameMaxLength then main
wend
nameLength = currentChar
return
clearName:
' fill the name array with 0
for currentChar = 0 to (nameMaxLength - 1)
nameArray[currentChar] = 0
next
nameLength = 0
return
getCommand:
' iterate over the input array, copying bytes into the command array
' start from three bytes after the name
' (because the name is followed by " : " )
currentChar = nameLength + 3
chunkCursorPos = 0
while inputArray[currentChar] <> asciispace
cmdArray[chunkCursorPos] = inputArray[currentChar]
chunkCursorPos = chunkCursorPos + 1
currentChar = currentChar + 1
cmdLength = chunkCursorPos
' if we overflow the array, we have a bogus command. go back to main
if chunkCursorPos >= cmdMaxLength then main
wend
return
clearCommand:
' fill the command array with 0
for currentChar = 0 to (cmdMaxLength - 1)
cmdArray[currentChar] = 0
next
cmdLength = 0
return
clearInput:
' fill the input array with 0
for currentChar = 0 to (inputMaxLength -1)
inputArray[currentChar] = 0
next
return


Posted by William Blaze at March 5, 2005 03:00 PM | TrackBack