Program won't continue past launching a cog
W9GFO
Posts: 4,010
Ok, maybe this is really simple but I don't know why this isn't working. I'm trying out different ways of monitoring buttons. The following code gets stuck launching the cog, it does not proceed to displaying the text on the LCD. The method I want to start in the new cog repeats forever but I expected the program to continue on after launching the cog. If I move the command to display the LCD text above launching the cog, then I see the text - so I know the LCD is working fine. Does the program launched into a new cog affect the main program flow?
Rich H
ButtonTest
ButtonCog
p.s. There are brackets with a "1" and a "2" in them following the last two Button PUBs but they won't show up!?
Rich H
ButtonTest
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 LCD_Pin = 27 ' I/O Pin on ProtoBot For LCD LCD_Baud = 19_200 ' LCD Baud Rate LCD_Lines = 4 ' Parallax 4X20 Serial LCD (#27979) VAR long Bt1 long Bt2 long Bt3 long stack[noparse][[/noparse]20] OBJ LCD : "debug_lcd" ButtonCog : "ButtonCog" PUB Init LCD.init(LCD_Pin, LCD_Baud, LCD_Lines) ' Initialize LCD Object LCD.cursor(0) ' Turn Off Cursor LCD.backlight(true) ' Turn On Backlight LCD.cls coginit(7, ButtonCog.Start, @stack) '****** Doesn't get past this point LCD.str(string("Button Test")) Main PUB Main Repeat Bt1 := ButtonCog.Button1 Bt2 := ButtonCog.Button2 Bt3 := ButtonCog.Button3 debug PUB debug LCD.gotoxy(0, 1) ' Position Cursor LCD.decx(Bt1, 3) LCD.gotoxy(6, 1) LCD.decx(Bt2, 3) LCD.gotoxy(12, 1) LCD.decx(Bt3, 3) ' Print Fractional Part
ButtonCog
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 Bt1 = 23 Bt2 = 22 Bt3 = 21 VAR long Button := 0 PUB Start ina[noparse][[/noparse]Bt1..Bt3]~ Repeat repeat while ina[noparse][[/noparse]Bt1..Bt3] == %000 ' stay here until a button is pressed Button[noparse][[/noparse]0] := 0 Button := 0 Button := 0 if ina[noparse][[/noparse]Bt1] == 1 Button[noparse][[/noparse]0]++ if ina[noparse][[/noparse]Bt2] == 1 Button++ if ina[noparse][[/noparse]Bt3] == 1 Button++ waitcnt(clkfreq/100 +cnt) PUB Button1 : result result := Button[noparse][[/noparse]0] PUB Button2 result := Button PUB Button3 result := Button
p.s. There are brackets with a "1" and a "2" in them following the last two Button PUBs but they won't show up!?
Comments
You can't do this. Any method you launch in a cog *must* be in the object you are launching from.
Do it this way.
... and make stack a bit bigger, the extra call uses some more of it.
I'm sure people will also chime in and tell you that coginit is not such a good idea and you'd probably be better off using cognew unless you have a *really* good reason for it.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"VOOM"?!? Mate, this bird wouldn't "voom" if you put four million volts through it! 'E's bleedin' demised!
Ok, I missed that part. Thanks.
No good reason, other than it is shorter and seemingly, easy to understand.
Rich H
Yes, I think it should be.
The same error is in the previous program yet it works fine.
Rich H
Yes, the default is to input so it should work, just pointing it out to help avoid problems elsewhere trying to set input states.
-Phil
Ok, for normal usecases it's fine to use cognew, but when you run out of COGs you should know which COG can be replaced if needed and then use COGINIT to launch the needed code there. Of course you should be aware of which objects do start their own COG(s). So, for the main program there is no reason not to use COGINIT as long as you know what you are doing.
I'd second your concerns for objects because an object does not know in which environment it's used. So it should NEVER use coginit ... hm ... did I say never? ;o)
-Phil