Shop OBEX P1 Docs P2 Docs Learn Events
How many Cogs for multiple objects — Parallax Forums

How many Cogs for multiple objects

oodesoodes Posts: 131
edited 2013-06-27 08:29 in General Discussion
Hello

If I declare an object 4 times and that object uses a cog do I use up 4 cogs? for example
OBJ
sdfat[4]: "fsrw"  

Fsrw starts a new cog so does use 4 cogs? I am under the impression it uses 4 cogs but when I run the free_cogs object below and increase or decrease sdfat to sdfat[6] or sdfat[2] it always returns the same number of free cogs.
PUB free_cogs : free | i, cog[8], jmp_0


  jmp_0 := %010111_0001_1111_000000000_000000000
  repeat while (cog[free] := cognew(@jmp_0, 0)) => 0 
    free++
  if free
    repeat i from 0 to free - 1
      cogstop(cog[i])
  return free

Can anyone clear this up for me?

Thanks

Des

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2013-06-27 08:12
    Cogs are allocated dynamically with COGNEW (and COGINIT). Objects that use a cog for their functioning always have some kind of initialization routine that does the COGNEW. By convention, this is usually .start and most such objects also provide a .stop method that stops and releases the cog. Until your program calls the .start method for the object, it won't use any additional cogs.

    fsrw is an intermediate level object that uses a low level SPI driver object to actually talk to the SD card and the low level object is the one that uses an additional cog. The .mount method calls the SPI driver's .start method and there's an .unmount method that calls the SPI driver's .stop method. Until you mount an SD card, there are no additional cogs used.
  • oodesoodes Posts: 131
    edited 2013-06-27 08:29
    Mike ,

    Thanks for getting back to me. I'm calling the .mount method before I run the 'Free_cogs' method. My program wont run unless I have an SD card mounted. I'm aware that the safespi low level object is what starts the cog. But does this object release the cogs when not using them? I'm currently using sdfat[3] to talk to 3 files on the SD card but I need to increase this to 4 however i have no spare cogs and the system gets hung up sometimes.

    Des
Sign In or Register to comment.