I wrote a (badly) abstracted class that allows me to support both the ENC28J60 and the WIZ5100 Ethernet chips, through EtherCard and the official Ethernet library.
Basically I have a base class and then two different classes extending it that implement support for the two chips. Only one of them gets compiled at a time, depending on the value of a #define.
Everything is working great, but I have some problems with #includes. I would like to #include the relevant libraries only in the file defining the extended class, but if I do so, it seems the Arduino headers don't get included, as I get errors like:
In file included from HTTPRequestParser.h:25,
from WebServerBase.h:23,
from WebServer_ENC28J60.h:27,
from smartstrip.cpp:34:
Panic.h:60: error: 'byte' does not name a type
Panic.h:73: error: expected `)' before '_led'
Panic.h: In member function 'void Panic::infiniteBlink()':
Panic.h:65: error: 'led' was not declared in this scope
Panic.h:65: error: 'HIGH' was not declared in this scope
Panic.h:65: error: 'digitalWrite' was not declared in this scope
Panic.h:66: error: 'delay' was not declared in this scope
Panic.h:67: error: 'LOW' was not declared in this scope
Panic.h: In member function 'void Panic::setup()':
Panic.h:78: error: 'led' was not declared in this scope
Panic.h:78: error: 'OUTPUT' was not declared in this scope
Panic.h:78: error: 'pinMode' was not declared in this scope
Panic.h: In member function 'void Panic::panic(__FlashStringHelper*)':
Panic.h:94: error: 'Serial' was not declared in this scope
Panic.h: In member function 'void Panic::__assert_fail(const char*, const char*, int, const char*)':
Panic.h:106: error: 'Serial' was not declared in this scope
So, what I have to do is to put at the beginning of the sketch's main file:
#ifdef USE_ENC28J60
#include <EtherCard.h>
#else
#include <SPI.h>
#include <Ethernet.h>
#endif
#include "WebServer_ENC28J60.h"
#include "WebServer_WIZ5100.h"
But this breaks encapsulation, so I'd like to remove it. So there must be something I can't understand about the Arduino proprocessing. Can you help me?
The code is browseable at GitHub - SukkoPera/SmartStrip: A sketch for Arduino for advanced control of relays through a web interface, supporting always ON/always OFF and temperature-controlled relays.
Thanks!