cogs and arrays question from newbee
10gigbill
Posts: 79
I want to turn LEDs on and off and with serial commands to the prop.
The command structure is cxxxxp cr lf . that’s c an asci 0,1,or 3 . for off, on, or flash.
4 characters that are ignored xxxx. The p is the pin number followed by a space
Cr and lf.
I read in the string of 8 bytes, check for the space and cr, if it look like a legal command
I set a flag and store it in an array with the command in the array position corresponding to the pin number.
I want to have a program running in a separate cog to update the pins 2 times a second and not interfere with the serial communications.
One program is updating the array and another in another cog is accessing the array, all this works for one pass but then it gets goofy
part running in new cog seems to be the bugger?
See code. Thanks I’m really new at this..
Con
_Clkmode = Xtal1 + pll16x
_Xinfreq = 5_000_000
high = 1
low = 0
out = $FF
Var
byte V[noparse][[/noparse]10]
byte indx
long stack[noparse][[/noparse]9]
byte first
byte X[noparse][[/noparse]10]
Obj
pst:"Parallax Serial Terminal"
Pub main | str_ok,value,pin,delay
first:= 0
repeat
str_ok:=chek 'get the input string flag
if str_ok == true
pst.str(string(pst#NL,"good string"))
lights 'if the input string is ok do lights
else
pst.str(string(pst#NL, "bad string"))
waitcnt(clkfreq + cnt) 'wait one second
Pub chek :flap | value,flag,spaceflag 'get the input string and check if it is valid
if first== 0 ' only initialize pst. first time
pst.Start(115_200) 'one second delay here first time- Set Terminal to 115200 baud
pst.str(string(pst#NL, "first trip")) 'tell me you are starting
repeat indx from 0 to 8 'init array with 3's
X[noparse][[/noparse]indx]:= "3" ' init array with 0's
V[noparse][[/noparse]indx]:= "0"
cognew(toggle, @stack)
first:= 1 'flag first round done
pst.Str(String(pst#NL, "enter the code now")) 'six character codes (six asci numbers) followed by $20 + $41
pst.Chars(pst#NL,1)
flag := false
spaceflag := false
repeat indx from 0 to 6 'input 7 characters
value := pst.charIn 'look for a space
if value == $20 'I got a space character
spaceflag :=true 'set spaceflag and
QUIT 'quit repeat loop ...all comand strings end with space and "A"
V[noparse][[/noparse]indx]:=value
value := pst.charIn 'get next character after the space s/b "A"
if ((value =="A") and (spaceflag == true)and (indx ==6))
flag := true 'I got 6 characters followed by space and "A"
flap:=flag 'return the flag for good or bad string
Pub Lights | cmnd,point,inx
point:= V- 48 'demo prop port A out starts at 16 not zero asci 0 = 48
cmnd := V[noparse][[/noparse]0] 'the command is in the first byte of array V
X[noparse][[/noparse]point]:= cmnd 'put the command for the point in array x
repeat inx from 0 to 5 'print out the X array
pst.chars(pst#NL,1)
pst.char(X[noparse][[/noparse]inx])
Pub toggle | pin,cmd,idx,stat
DirA[noparse][[/noparse]16..23]:= out
repeat
repeat idx from 0 to 5 'scan array
pin:= 16 + idx 'output pin is 16 + the index
stat:= X[noparse][[/noparse]idx] 'the status of the pin is stored in the array
if stat== "0"
outA[noparse][[/noparse]pin]:= low
if stat == "1"
outA[noparse][[/noparse]pin]:= high
if stat == "3" ' if the status is a "3" toggle the output pin
!outA[noparse][[/noparse]pin]
waitcnt(10_000_000 + cnt)
The command structure is cxxxxp cr lf . that’s c an asci 0,1,or 3 . for off, on, or flash.
4 characters that are ignored xxxx. The p is the pin number followed by a space
Cr and lf.
I read in the string of 8 bytes, check for the space and cr, if it look like a legal command
I set a flag and store it in an array with the command in the array position corresponding to the pin number.
I want to have a program running in a separate cog to update the pins 2 times a second and not interfere with the serial communications.
One program is updating the array and another in another cog is accessing the array, all this works for one pass but then it gets goofy
part running in new cog seems to be the bugger?
See code. Thanks I’m really new at this..
Con
_Clkmode = Xtal1 + pll16x
_Xinfreq = 5_000_000
high = 1
low = 0
out = $FF
Var
byte V[noparse][[/noparse]10]
byte indx
long stack[noparse][[/noparse]9]
byte first
byte X[noparse][[/noparse]10]
Obj
pst:"Parallax Serial Terminal"
Pub main | str_ok,value,pin,delay
first:= 0
repeat
str_ok:=chek 'get the input string flag
if str_ok == true
pst.str(string(pst#NL,"good string"))
lights 'if the input string is ok do lights
else
pst.str(string(pst#NL, "bad string"))
waitcnt(clkfreq + cnt) 'wait one second
Pub chek :flap | value,flag,spaceflag 'get the input string and check if it is valid
if first== 0 ' only initialize pst. first time
pst.Start(115_200) 'one second delay here first time- Set Terminal to 115200 baud
pst.str(string(pst#NL, "first trip")) 'tell me you are starting
repeat indx from 0 to 8 'init array with 3's
X[noparse][[/noparse]indx]:= "3" ' init array with 0's
V[noparse][[/noparse]indx]:= "0"
cognew(toggle, @stack)
first:= 1 'flag first round done
pst.Str(String(pst#NL, "enter the code now")) 'six character codes (six asci numbers) followed by $20 + $41
pst.Chars(pst#NL,1)
flag := false
spaceflag := false
repeat indx from 0 to 6 'input 7 characters
value := pst.charIn 'look for a space
if value == $20 'I got a space character
spaceflag :=true 'set spaceflag and
QUIT 'quit repeat loop ...all comand strings end with space and "A"
V[noparse][[/noparse]indx]:=value
value := pst.charIn 'get next character after the space s/b "A"
if ((value =="A") and (spaceflag == true)and (indx ==6))
flag := true 'I got 6 characters followed by space and "A"
flap:=flag 'return the flag for good or bad string
Pub Lights | cmnd,point,inx
point:= V- 48 'demo prop port A out starts at 16 not zero asci 0 = 48
cmnd := V[noparse][[/noparse]0] 'the command is in the first byte of array V
X[noparse][[/noparse]point]:= cmnd 'put the command for the point in array x
repeat inx from 0 to 5 'print out the X array
pst.chars(pst#NL,1)
pst.char(X[noparse][[/noparse]inx])
Pub toggle | pin,cmd,idx,stat
DirA[noparse][[/noparse]16..23]:= out
repeat
repeat idx from 0 to 5 'scan array
pin:= 16 + idx 'output pin is 16 + the index
stat:= X[noparse][[/noparse]idx] 'the status of the pin is stored in the array
if stat== "0"
outA[noparse][[/noparse]pin]:= low
if stat == "1"
outA[noparse][[/noparse]pin]:= high
if stat == "3" ' if the status is a "3" toggle the output pin
!outA[noparse][[/noparse]pin]
waitcnt(10_000_000 + cnt)
Comments
see attached code.
I use FullDuplexSerial.spin which requires 4 parameters so you would start it by...
pst.start(31,30,0,115200)
and I would do this at the beginning of main, not within chek.
Next, the waitcnt is not indented so it is never executed because the repeat loop only goes to the statement
pst.str(string(pst#NL, "bad string"))
Dinner ready.. will continue later
You need to tidy your code up. The indentation is incorrect. So that is the place to start first.
Use 2 character spaces for each level of indentation and ensure the instructions that are meant to be in the same level are aligned correctly.
There is an archive command within PropTool to zip up all the code used and this is the best method to post your code. If you need to just post a snippet you can use the Insert Formatted Code button "#" which will preserve indentation.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Links to other interesting threads:
· Home of the MultiBladeProps: TriBlade,·RamBlade,·SixBlade, website
· Single Board Computer:·3 Propeller ICs·and a·TriBladeProp board (ZiCog Z80 Emulator)
· Prop Tools under Development or Completed (Index)
· Emulators: CPUs Z80 etc; Micros Altair etc;· Terminals·VT100 etc; (Index) ZiCog (Z80) , MoCog (6809)·
· Prop OS: SphinxOS·, PropDos , PropCmd··· Search the Propeller forums·(uses advanced Google search)
My cruising website is: ·www.bluemagic.biz·· MultiBlade Props: www.cluso.bluemagic.biz
Post Edited (Cluso99) : 2/18/2010 8:39:10 AM GMT
I will work on this some more. All the code seems to work to check for valid commands and
put the commands into an array. The toggle code seems to be a problem.
thanks again more later
Bill
Dave
[noparse][[/noparse] code] your code here [noparse][[/noparse] /code]
but leave out the spaces within the [noparse]/noparse.
For example:
For full programs attach a zip as you did.
Oh and try to use a consistent indentation, it makes it much easier to read, I use three spaces.
OK aligning the comments was anal [noparse]:)[/noparse]
Graham
Post Edited (Graham Stabler) : 2/18/2010 6:18:38 PM GMT
Just a note, PST is indeed a driver (haven't actually looked at the code, but I assume it's just a wrapper around FullDuplexSerial) and can be started by pst.Start(baud) [noparse][[/noparse]uses pins 30,31 by default].
sorry I broke the rules here and started another thread.. The later thread has a cleaner version. and if you change the stack space it works fine.
Bill