May 04, 2024, 06:32:39 PM

News:

You can now use Vixen to program your Prop-1 and Prop-2 controllers!  Get started quickly and easily, without having to learn PBASIC.  Details in the Library forum.


Opening a new COG for a secondary routine

Started by Laytonk, December 06, 2015, 05:30:46 PM

Previous topic - Next topic

Laytonk

Hi Guys, I have a quick question about opening a new COG for a secondary routine it runs in the background to control haze and or smoke. I am using your current template, and this works, but my question is am I starting the cog the best way? In Variables, pub main I am using"cognew(haze, @stack0)" to start the cog.  Under Support Routines, variables I am using "long  stack0[32]" for memory,  then I have a Con, Secondary Control 
Below is a example of the code I am using.
Thanks for your Help
Keith   
Sample code:                                     
var 
pub main 
setup                                                          ' setup io and objects
     cognew(haze, @stack0)                                         ' start haze cog  (turned off for now)
   
repeat                                                     
      if (ttl_pin == 0)          ' wait for trigger on IN0
        quit 
My Code Here

pub setup                                                                                                                   
'' Configure HC-8+ IO and support objects
  time.start                                                     ' setup for timing/delays
  prng.seed(cnt, cnt ~> 2, $EF8_7E4, cnt << 2, cnt)              ' setup randomizer                             
  io.start(0, 0)                                                 ' clear pins (this cog)                                                         
  outa[OUT7..OUT0] := %0000_0000                                 ' clear outputs
  dira[OUT7..OUT0] := %1111_1111                                 ' output mode
  if (PCB_REV =< "E")                                 
  io.low(OUT_EN)                                               ' enable output driver
  outs.start(8, OUT0)                                            ' start outputs driver *
  fader.start(8, outs.address)                                   ' start fader manager *
  set_rs485(NOCON)                                               ' RS-485 not connected
  ap16_setup                                                     ' start serial for AP-16+ *
  bcog := cognew(background, @bcstack) + 1                       ' start background cog *





con

  { =================================== }
  {                                     }
  {  S E C O N D A R Y   C O N T R O L  }
  {                                     }
  { =================================== }


pri haze                                                                     ' launch with cognew
'' Secondary prop control
'' -- runs when IN7 is active triggered

  repeat
if (ttl_pin == 7)                                                ' wait for trigger on IN7
        outs.high(7)                                             ' on
        time.pause(2000)                                   ' wait 2 sec
        outs.low(7)                                              ' off
   
     
   
con

  { --------------------------------- }
  {  S U P P O R T   R O U T I N E S  }
  { --------------------------------- }

var

  long  ttlpins                                                  ' updated by background cog
  long  dmxaddr                                                                 
  long  stack0[32]

JonnyMac

December 06, 2015, 11:50:10 PM #1 Last Edit: December 06, 2015, 11:52:12 PM by JonnyMac
Tip: Select from the Font Face drop-down the Courier font and paste your code between those tags. To make things even more readable, click on the Change Color dialog and select Blue -- this will set off your code like this:

  repeat                                                     
    if (ttl_pin == 0) 
      quit 


Okay... on to coding tips.

1) Name your IO pins in the constants section at the top. Numbers like 0 and 7 are meaningless to others -- giving those pins names can help you and others follow a listing.

2) If you cog is going to run in the background all the time, put the cognew() call at the end of setup. In fact, you can see in the current template I start the background cog that way; this is the cog that controls the R/G LED and scans the TTL and DMX inputs into global variables so the can be accessed by any other cog, including your new cog. I tend to define my stack space for a background cog in a VAR section right above the main PRI routine for the cog. The compiler doesn't care, but I find this helps me maintain things should I chose to remove the the cog.

3) Style can differ -- here's how I would code some of the elements you have in your program.

  repeat until (ttl_pin == MAIN_TRIGGER)

  repeat
    repeat until (ttl_pin == FOG_TRIGGER)
    outs.high(FOG_CTRL)
    time.pause(FOG_TIMING)
    outs.low(FOG_CTRL)


Using named constants like this really doesn't take any more time, and will make the program easier to follow, easier to debug, and easier to modify. I know, I know, you just want something quick and dirty. After 35 years of programming I believe that it's never quick, and it's always more dirty that you want when you embed magic numbers into your program listings. ;)
Jon McPhalen
EFX-TEK Hollywood Office

Laytonk

Thanks for your help that makes much more sense, I will work on my code so it will be easier to read and follow. You defined your bcog := cognew(background, @bcstack) + 1 should I follow suit by naming mine something like hcog := cognew(haze, @hcstack)+ 1

Thanks for the input
Keith

JonnyMac

December 07, 2015, 09:40:37 AM #3 Last Edit: December 07, 2015, 10:35:11 AM by JonnyMac
You don't have to if it's a simple process that:
-- nothing else is dependent on
-- is not going to be stopped

If either of these cases are true, then you want to create a flag variable that the cog is running. If you need to stop your cog, you do it like this:

  cogstop(hcog - 1)
  hcog := 0


Cognew returns the actual cog number used (0..7), or -1 if no cogs are available. We add one so that any non-zero value (remember, false = 0) means the cog is loaded and running.

Jon McPhalen
EFX-TEK Hollywood Office

Laytonk