C++ and Arduino Functions

So, I'm trying to create a class to subclass and allow for a simplistic run loop to allow me to simulate "threads." Each item will have the ability to basically say "don't run again for xx microseconds." For this, I need the "micros()" function.

So I have a C++ object:

class RunLoopItem
{

public:

RunLoopItem ( ) :
runTime ( 0 )
{

}

~RunLoopItem ()
{

}

void run ()
{
if ( 0>runTime )
reallyRun() ;
}

private:
virtual void reallyRun ()
{

}
UInt32 runTime ;

} ;

In the "run" method, if I replace "0" with "micros()", I get an error:

In member function 'void RunLoopItem::run()':

...And that's it... Anyone know why this is?

Some notes:
--> It compiles just fine with the "0" in place
--> "UInt32" is typedef'd to "unsigned long." I picked it up from programming with the CoreFoundation and like it because it's very clear...
--> NO "C" function will work when placed inside a C++ method... "malloc," which works in "loop" and "setup," does not work in the method, even though the method ".h" file is included within the same file as the "setup" and "loop" functions

First, note that "if (0>runTime)" is always true since runTime is an unsigned type thus could never be less than 0.

It compiles fine for me if I replace "UInt32" with "unsigned long".

It might be that you're including a header file which uses a namespace. Try replacing 'micros()' with '::micros()'.

I know 0>runTime is always true - I did that so that I'd know it would compile correctly...

I just tried the ::micros() and it didn't work... and I'm not using a namespace in the headers

This is weird. And annoying.

Just so everyone knows, here is the full extent of the code I have:

sketch_090526a:

#ifndef iRunLoopItem
#include "RunLoopItem.h"
#endif

#define numberOfItems 5
RunLoopItem * items[numberOfItems] ;

void setup ()
{
for ( int i=0 ; i<numberOfItems ; i++ )
{
items = 0 ;

  • }*

// items[0] = new RunLoopItemSUBCLASS ( ...params... ) ; <-- Example of what I'll do
}
void loop ()
{

}[/quote]
RunLoopItem.h:
> #ifndef iDefinitions
> #include "definitions.h"
> #endif
>
> #define iRunLoopItem
>
>
>
> class RunLoopItem
> {
*> *
> public:
*> *
> RunLoopItem ( ) :
> runTime ( 0 )
> {
*> *
> }
*> *
> ~RunLoopItem ()
> {
*> *
> }
*> *
*> *
> void run ()
> {
> run ( ::micros() ) ;
> }
> void run ( UInt32 inTime )
> {
> if ( inTime>runTime )
> reallyRun() ;
> }
*> *
> private:
> virtual void reallyRun ()
> {
*> *
> }
> UInt32 runTime ;
*> *
*> *
*> *
*> *
> } ;
definitions.h:
> #define iDefinitions
>
>
> typedef unsigned long UInt32 ;
In "RunLoopItem.h,"
This works:
> void run ()
> {
> run ( 0 ) ;
> }
This doesn't:
> void run ()
> {
> run ( ::micros() ) ;
> }
Nor does this:
> void run ()
> {
> run ( micros() ) ;
> }

You'll need to include the <WProgram.h> in your header that will use Arduino specific functions/datatypes. :slight_smile:

Thanks!

Is that written somewhere painfully obvious on the website, or do you just have to find out here on the forums?

Is that written somewhere painfully obvious on the website, or do you just have to find out here on the forums?

In short; no.

But here is:
http://arduino.cc/en/Hacking/LibraryTutorial
http://www.arduino.cc/playground/Code/Library

Has that <WProgram.h> requirement gone away in version 0015 of the tools? I swear his code compiled fine on my system without it.

Also, if you get error messages like that which seem to have the details missing, try turning on build.verbose in the preferences file--although you might also need to try to compile outside the IDE as I recall...

It seems the IDE has problems parsing some kinds of error messages.

--Phil.

Has that <WProgram.h> requirement gone away in version 0015 of the tools?

No.

I swear his code compiled fine on my system without it.

How many times in my life have I said that!

  • Brian

Ok... So now I need "malloc," which is in stdlib.h

So I included it... and it's still not working...

Is there anything stupid I should know about when it comes to avr-gcc?

Do you really need malloc? When you've only got 2048 (or 1024) bytes of ram, the overhead of using memory allocation is usually not worth it. I betcha you could do it without malloc() or new(). Can you show what you need it for?

-Paul

I don't get this little snippet:

runTime ( 0 )
 {
   
 }

That looks like a syntax error to me...?

Mikal

runTime ( 0 )

{
 
}




That looks like a syntax error to me...?

It is the syntax for initializing a private member variable when constructing an object. :slight_smile:

Almost equivalent of:

{
   runTime = 0;
}

:slight_smile:

Ding dong! Didn't see the colon above. Thanks. Incidentally, that syntax is not restricted to private members, but you probably knew that.