Shop OBEX P1 Docs P2 Docs Learn Events
PASM to C++ Class questions. — Parallax Forums

PASM to C++ Class questions.

I am using SimpleIde and have compiled the GCC Demo C++ toggle demo. It's an interesting place to start but...
I would like to converting cog PASM codes to C or C++, I prefer C++, if possible. The code currently runs in a COG. I want to convert it so that it will be more easily ported to other controllers. I am familiar with Spin2CPP. The code I want to convert can be put in a basic class structure of:

class foo_cog
{
public:
foo_cog();
foo_cog(int * data);
~foo_cog()
void foo_cog_run(); // will call foo_cog_sub1&2

int data_array[10]; // accessible by hub?
int data_item_1;

private:
int data_2;
int data_3;
int foo_cog_sub1(int a);
int foo_cog_sub2(int * ptr);
}


I would like to instantiate the class in the HUB main and launch the cog with foo_cog_run as in the C++ toggle demo.

First, I am assuming the compiled class will be small enough to run in a cog -- a basic.

Questions:
"Is this even possible or should I use C code"? (I'll have questions about that also).

Will the public data be retained by the HUB where the class is instantiaged?

Will the private data and methods be accessable by the cog?

If only the foo_run be placed in the cog, can the cog access foo_ cog_sub1 &2:

Can I instantiate multiple foo_class and run each indifferent cogs. (Am I just reaching). Can multiple cogs have access to foo_cog_sub1 & foo_cog_sub2? Is there a re-entry problem?

Should foo_cog_run incorporate all data declarations and use go_to's to avoid using f00_cog_subs?

Any hints to "HOW" would be appreciated.

Comments

  • greybeard wrote: »
    I am using SimpleIde and have compiled the GCC Demo C++ toggle demo. It's an interesting place to start but...
    I would like to converting cog PASM codes to C or C++, I prefer C++, if possible. The code currently runs in a COG. I want to convert it so that it will be more easily ported to other controllers. I am familiar with Spin2CPP. The code I want to convert can be put in a basic class structure of:

    class foo_cog
    {
    public:
    foo_cog();
    foo_cog(int * data);
    ~foo_cog()
    void foo_cog_run(); // will call foo_cog_sub1&2

    int data_array[10]; // accessible by hub?
    int data_item_1;

    private:
    int data_2;
    int data_3;
    int foo_cog_sub1(int a);
    int foo_cog_sub2(int * ptr);
    }


    I would like to instantiate the class in the HUB main and launch the cog with foo_cog_run as in the C++ toggle demo.

    First, I am assuming the compiled class will be small enough to run in a cog -- a basic.

    Questions:
    "Is this even possible or should I use C code"? (I'll have questions about that also).

    Will the public data be retained by the HUB where the class is instantiaged?

    Will the private data and methods be accessable by the cog?

    If only the foo_run be placed in the cog, can the cog access foo_ cog_sub1 &2:

    Can I instantiate multiple foo_class and run each indifferent cogs. (Am I just reaching). Can multiple cogs have access to foo_cog_sub1 & foo_cog_sub2? Is there a re-entry problem?

    Should foo_cog_run incorporate all data declarations and use go_to's to avoid using f00_cog_subs?

    Any hints to "HOW" would be appreciated.

    You can do this, and I've done it before. It's pretty cool :) Make sure that your method definitions on foo_cog reside in the header, not a separately compiled source file.

    MotorDriver.cogcpp (I don't think SimpleIDE supports this extension - I used this in a PropWare project)
    #include "MotorDriver.h"
    
    _NAKED int main () {
        MotorDriver *self = (MotorDriver *) PAR;
    
        self->run();
    
        return 0;
    }
    

    MotorDriver.h
    class MotorDriver {
    public:
      typedef enum {
        NO_ERROR,
        // some other errors...
        INSTANCE_ALREADY_INVOKED     = -5
      } ErrorCode;
    
      MotorDriver (int x) : m_x(x) { }
    
      static int invoke (MotorDriver &instance) {
        if (-1 == instance.m_cogId)
          return instance.m_cogId = cognew(_load_start_MotorDriver_cog, (uint32_t) &instance);
        else
          return INSTANCE_ALREADY_INVOKED;
      }
    
    private:
      const int m_x;
    }
    

    main.cpp
    #include "MotorDriver.h"
    
    int main () {
      MotorDriver driver(1);
      MotorDriver::invoke(driver);
    
      // Do other stuff...
    }
    
Sign In or Register to comment.