Shop OBEX P1 Docs P2 Docs Learn Events
Program won't continue past launching a cog — Parallax Forums

Program won't continue past launching a cog

W9GFOW9GFO Posts: 4,010
edited 2009-05-12 06:11 in Propeller 1
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
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!?
attachment.php?attachmentid=60828
226 x 179 - 18K

Comments

  • BradCBradC Posts: 2,601
    edited 2009-05-11 22:58
    W9GFO said...

      coginit(7, ButtonCog.Start, @stack)          '****** Doesn't get past this point
    
    



    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.
    PUB RunTheCOG
       ButtonCog.Start
    
    PUB Start
      coginit(7,RunTheCOG, @stack)
    
    
    


    ... 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!
  • T ChapT Chap Posts: 4,223
    edited 2009-05-11 22:58
    What is ina[noparse][[/noparse]Bt1..Bt3]~ doing? Should that be dira?
  • W9GFOW9GFO Posts: 4,010
    edited 2009-05-11 23:06
    BradC said...
    You can't do this. Any method you launch in a cog *must* be in the object you are launching from.

    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.

    Ok, I missed that part. Thanks.

    No good reason, other than it is shorter and seemingly, easy to understand.

    Rich H
  • W9GFOW9GFO Posts: 4,010
    edited 2009-05-11 23:21
    TChapman said...
    What is ina[noparse][[/noparse]Bt1..Bt3]~ doing? Should that be dira?

    Yes, I think it should be.

    The same error is in the previous program yet it works fine.

    Rich H
  • T ChapT Chap Posts: 4,223
    edited 2009-05-12 00:14
    W9GFO said...
    TChapman said...
    What is ina[noparse][[/noparse]Bt1..Bt3]~ doing? Should that be dira?

    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 Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-05-12 01:48
    Also, never use coginit; use cognew instead. There is simply no good reason to have control over which cog gets initialized with your code. Let the Propeller make that decision for you.

    -Phil
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-05-12 05:55
    Never say never ;o)

    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 Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-05-12 06:11
    MagIO2 said...
    ...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.
    It's still better to do a COGSTOP on the cog you want to vacate and then a COGNEW. The fact is that all of one's programming can (and should) be done without ever touching COGINIT. The only possible exception to this rule, that I can think of, is a program that must start another program in its own cog. An example would be a bootloader which starts the Spin interpreter in its own Cog 0.

    -Phil
Sign In or Register to comment.