cogstart error in C++
Enrique
Posts: 90
I am getting the following error
"invalid conversion from 'void(*)()' to 'void(*)(void*)'"
on the line
int cogID=cogstart((&Blinker), NULL, &cogStack, sizeof(cogStack));
of the following program
/*
* This is a simple example of a method running in its own cog. This class will toggle the indicated pin
* at the frequency received as the second argument, the toggling method runs in its own cog.
*/
#include <propeller.h>
#include "pin.h"
#define LMM_STACK_AREA 40 // Number of int locations for the cog method to work
#define NUMBER_OF_INT_LOCAL_VARIABLES 0 //
class Toggler
{
static int waitTime; // Time interval between blinks
static int ledPin; // The pin for the LED we will blink
static int cogStack[LMM_STACK_AREA + NUMBER_OF_INT_LOCAL_VARIABLES]; // Stack area used by the cog
public:
Toggler(int ledPinArg, int timesPerSecondArg) // Starts toggling ledPinArg at a frequency of timePerSecondArg
{
// Prepare things
waitTime=CLKFREQ/timesPerSecondArg;
ledPin=ledPinArg;
// Start the cog
int cogID=cogstart((&Blinker), NULL, &cogStack, sizeof(cogStack));
}
private:
static void Blinker() // Does the actual blinking, it will run in a different cog
{
// Set the pin for output
pinOutput(ledPin);
while(1)
{
// Turn the LED on and wait
pinHigh(ledPin);
waitcnt(CNT+waitTime);
// Turn the LED off and wait
pinLow(ledPin);
waitcnt(CNT+waitTime);
}
}
};
What am I doing wrong?
Thanks,
Enrique
"invalid conversion from 'void(*)()' to 'void(*)(void*)'"
on the line
int cogID=cogstart((&Blinker), NULL, &cogStack, sizeof(cogStack));
of the following program
/*
* This is a simple example of a method running in its own cog. This class will toggle the indicated pin
* at the frequency received as the second argument, the toggling method runs in its own cog.
*/
#include <propeller.h>
#include "pin.h"
#define LMM_STACK_AREA 40 // Number of int locations for the cog method to work
#define NUMBER_OF_INT_LOCAL_VARIABLES 0 //
class Toggler
{
static int waitTime; // Time interval between blinks
static int ledPin; // The pin for the LED we will blink
static int cogStack[LMM_STACK_AREA + NUMBER_OF_INT_LOCAL_VARIABLES]; // Stack area used by the cog
public:
Toggler(int ledPinArg, int timesPerSecondArg) // Starts toggling ledPinArg at a frequency of timePerSecondArg
{
// Prepare things
waitTime=CLKFREQ/timesPerSecondArg;
ledPin=ledPinArg;
// Start the cog
int cogID=cogstart((&Blinker), NULL, &cogStack, sizeof(cogStack));
}
private:
static void Blinker() // Does the actual blinking, it will run in a different cog
{
// Set the pin for output
pinOutput(ledPin);
while(1)
{
// Turn the LED on and wait
pinHigh(ledPin);
waitcnt(CNT+waitTime);
// Turn the LED off and wait
pinLow(ledPin);
waitcnt(CNT+waitTime);
}
}
};
What am I doing wrong?
Thanks,
Enrique
Comments
to this:
cogstart will pass its second parameter to Blinker in the 'arg' parameter. You can use this parameter to communicate information between the main program and the program you're starting on another COG.
Thanks for the reply
Your suggestion got rid of the previous error and I am now getting a new one
'lvalue required as unary & operand'
on the line
int cogID=cogstart((&Toggler::Blinker(NULL)), NULL, &cogStack, sizeof(cogStack));
This is what the code looks like now
/*
* This is a simple example of a method running in its own cog. This class will toggle the indicated pin
* at the frequency received as the second argument, the toggling method runs in its own cog.
*/
#include <propeller.h>
#include "pin.h"
#define LMM_STACK_AREA 40 // Number of int locations for the cog method to work
#define NUMBER_OF_INT_LOCAL_VARIABLES 0 //
class Toggler
{
int waitTime; // Time interval between blinks
int ledPin; // The pin for the LED we will blink
int cogStack[LMM_STACK_AREA + NUMBER_OF_INT_LOCAL_VARIABLES]; // Stack area used by the cog
public:
Toggler(int ledPinArg, int timesPerSecondArg);
private:
void Blinker(void *arg);
};
Toggler::Toggler(int ledPinArg, int timesPerSecondArg) // Starts toggling ledPinArg at a frequency of timePerSecondArg
{
// Prepare things
waitTime=CLKFREQ/timesPerSecondArg;
ledPin=ledPinArg;
// Start the cog
int cogID=cogstart((&Toggler::Blinker(NULL)), NULL, &cogStack, sizeof(cogStack));
}
void Toggler::Blinker(void *arg) // Does the actual blinking, it will run in a different cog
{
// Set the pin for output
pinOutput(ledPin);
while(1)
{
// Turn the LED on and wait
pinHigh(ledPin);
waitcnt(CNT+waitTime);
// Turn the LED off and wait
pinLow(ledPin);
waitcnt(CNT+waitTime);
}
}
I would suggest that you do not do this is C++.
Even if the bulk of your program is C++ I would define the function to be loaded to COG as C function. If it needs parameters passed into par I would use a regular struct.
Also, you should not try to pass an argument to the function. "&func(NULL)" means "the address of the result of func(NULL)", whereas "&func" just means "the address of func". The second one, namely the address of the function itself, is what you want to pass to cogstart,
Thanks,
Enrique
You should be able to use a function with a global namespace. For example ::do_toggle below works fine for me. The start function is slightly different, but it shouldn't matter. The example is a small modification for the toggle/lmm_c_toggle demo.