compile error when calling delay from within a library?

I must be doing something wrong, or misunderstanding something. I have a library which has a function that calls delay, and this gives a compiler error:

Arduino: 1.5.6-r2 (Windows 7), Board: "Digistump DigiX (standard)"

C:\Users\tastewar\Documents\Arduino\libraries\TESTLIB\TESTLIB.cpp: In function 'void HoldOnAMInute(long unsigned int)':
C:\Users\tastewar\Documents\Arduino\libraries\TESTLIB\TESTLIB.cpp:27: error: 'delay' was not declared in this scope

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

This is in a stripped down project/library just for reproducing this. Here's the sketch

#include <Stream.h>
#include <TESTLIB.h>

Foo myFoo(Serial2);

void setup() {
  // put your setup code here, to run once:
  Serial2.begin ( 9600 );
}

void loop() {
  // put your main code here, to run repeatedly:
  myFoo.bar ( "hello world" );
  myFoo.HoldOnAMInute ( 1234 );
}

And the TESTLIB.h file:

class Foo : Stream
{
  private:
    Stream *_dev; // Somewhere to store the object's pointer

  public:
    Foo ( Stream& dev );
    ~Foo ( );
    size_t write ( uint8_t val );  // Call a function on the stored object pointer
    int available ( ) { return _dev->available ( ); }
    int read ( ) { return _dev->read ( ); }
    int peek ( ) { return _dev->peek ( ); }
    void flush ( ) { return _dev->flush ( ); }
    size_t bar ( char *string );
    void HoldOnAMInute ( unsigned long ms );
};

and the TESTLIB.cpp file:

#include <Stream.h>
#include "TESTLIB.h"

Foo::Foo ( Stream& dev )
{
	_dev = &dev;
}

Foo::~Foo ( )
{
}

size_t Foo::write ( uint8_t val )
{
	return _dev->write(val);
}

size_t Foo::bar ( char *string )
{
	size_t a;
	a=print ( "prefix" );
	return a+print ( string );
}

void HoldOnAMInute ( unsigned long ms )
{
	delay ( ms );
}

Is there some very generic header file I have to include in my library, or before including the library's .h file?

#include <Arduino.h>

in TESTLIB.cpp

1 Like

Or even in the header file would make more sense - that way if you try and use Arduino defined types like 'byte' in the header, or other such Arduino-y things you won't have any errors.

Thank you so much, folks; all compiles now! (and realized I forgot a Foo:: in front of the HoldOnAMinute function...)

Now I just have to make the real one do useful things :slight_smile:

Just curious, what Stream type of device do you want to interface with the foo class?

FYI, the Stream class is intended for device that can stream bytes. If you have a block type of device, you may want to define write(byte*,int) as well, so you don't keep calling the write(byte) and slow down.

It's a serial device, and I've wanted to be able to communicate with it via both hardware and software serial ports. I confess that those methods are in there to avoid compile errors, and not because I fully understand what's going on under the covers. Are you saying that when I call something like the "print" method, it will end up calling the single byte "write" method if there isn't an implementation of the write(byte*, int) method? And that all I need to do is add another of those "hey just call the method of the passed in class" entries for it to work more efficiently? (Not that efficiency is paramount here, but I'm happy to gain some by including one more line!), e.g.

    size_t write ( uint8_t* val, int num ) { return _dev->write ( val, num ); }

println calls print, which calls write(byte*, int), which calls write(byte) repreatedly. If you think print is better done with sending out a packet at a time instead of byte after byte, you can write your own write(byte*,int) so it decides what to do in case of the length of the message. Otherwise the default behavior is done.

With the cases of interest being serial, I don't think I can write a better string handler than the one built-in!