Go Down

Topic: How create a "single" library that can choose to use Serial or Network hardware? (Read 521 times) previous topic - next topic

nunzio raciti

Hi all,

I'm working on a library which connect Arduino with an Android device,
I'm getting  crazy trying to make a single library able to use the Serial or the Network starting from the user choice...
I try to explain better,

inside .../arduino1.0.3/libraries/mylibrary
I have:
- AndroidDEV.h and AndroidDEV.cpp which defines a common class "AndroidDev"
useful in order to manage the Android phone/tablet
- AndoidBT.h and AndroidBT.cpp which extends the "AndroidDev" class including some specialization regarding the transport by BlueTooth (trough the Hardware Serial)
- AndoidNET.h and AndroidNET.cpp which extends the "AndroidDev" class including some specialization regarding the transport by Network.

My Goal is enable the user to choice only one of transports, eg.

android_bt_example.ino:

#include <AndroidBT.h>
AndroidBT android;

void setup()
{

    Serial.begin(115200);
    android.begin(&Serial, LANDSCAPE);
....
....

or

android_net_example.ino

#include <AndroidNET.h>
AndroidNET android;
EthernetClient client;

void setup()
{

   Ethernet.begin(mac) ;
   delay(1000);  Serial.println("connecting...");
   client.connect("192.168.2.1", 2345);
   android.begin(&client, LANDSCAPE);
....
....


Building the BT example as the IDE uses all "cpp" files that are inside the directory, shows errors building the "AndroidNET.cpp" file...

In file included from /opt/arduino-1.0.3/libraries/mylibrary/AndroidNET.cpp:21:
/opt/arduino-1.0.3/libraries/mylibrary/AndroidNET.h:38: error: 'EthernetClient' has not been declared
/opt/arduino-1.0.3/libraries/mylibrary/AndroidNET.h:44: error: ISO C++ forbids declaration of 'EthernetClient'

I'd like to avoid using two different libraries  =(

Can anyone help me?

Thanks.
Nunzio



























PaulS

Quote
inside .../arduino1.0.3/libraries/mylibrary

This is NOT where your library goes.

Your library is not a core library.

Quote
- AndroidDEV.h and AndroidDEV.cpp which defines a common class "AndroidDev"
useful in order to manage the Android phone/tablet

The file names should exactly match the class name, including case.

Quote
In file included from /opt/arduino-1.0.3/libraries/mylibrary/AndroidNET.cpp:21:
/opt/arduino-1.0.3/libraries/mylibrary/AndroidNET.h:38: error: 'EthernetClient' has not been declared

You haven't shown any code, so all we can do is assume that the compiler is right. It usually is.

nunzio raciti

Quote

inside .../arduino1.0.3/libraries/mylibrary
      This is NOT where your library goes.
Your library is not a core library.

I hope it will become in the future  :smiley-roll-sweat:
Quote

The file names should exactly match the class name, including case.

Of course, I made several attempts without getting improvements
Quote

You haven't shown any code, so all we can do is assume that the compiler is right. It usually is.

Yes, the compiler is right, I just need to know how declare classes or some tricks or at end  create two libraries.

my approach until now was to extend the base class with the class able to manage the HardwareSerial

Code: [Select]

//! The AndroidBT class.
/*! The AndroidBT class, specialized for the Bluetooth connection. */
class AndroidBT: public AndroidDev
{
public:
AndroidBT();
//! The function with which we initialize the library.
/*! \param serial HardwareSerial class pointer argument.
\param mode the modality in which we prefer to start the library.
\return 0 = success, -1 = error. */
int8_t begin(HardwareSerial *serial, modes_t mode=LANDSCAPE);

private:
int read();
int available();
size_t write(uint8_t c);
HardwareSerial *_serial;
};

or the Network:
Code: [Select]

//! The AndroidNET class.
/*! The AndroidNET class, specialized for the Network. */
class AndroidNET: public AndroidDev
{
public:
AndroidNET();
//! The function with which we initialize the library.
/*! \param serial HardwareSerial class pointer argument.
\param mode the modality in which we prefer to start the library.
\return 0 = success, -1 = error. */
int8_t begin(EthernetClient *client, modes_t mode=AUTO_REDRAW_LANDSCAPE);

private:
int read();
int available();
size_t write(uint8_t c);
EthernetClient *_client;
};










PaulS

How is the AndroidBT class supposed to know what an EthernetClient is?

nunzio raciti

this is the point, I would like to choice the AndroidBT + AndroidDEV classes or the AndroidNET (who need EthernetClient) + AndroidDEV,
but, as they are together under the same directory, the IDE try to build they both.
Obviously the bulk of the code is inside the AndroidDEV class, while AndroidBT and AndroidNET are very small...

PaulS

Quote
I would like to choice the AndroidBT + AndroidDEV classes or the AndroidNET (who need EthernetClient) + AndroidDEV,

When do you want to make this choice? At compile time? Or at run time?

Regardless, because all the files are in the same directory, they will all be compiled.

Quote
Obviously the bulk of the code is inside the AndroidDEV class, while AndroidBT and AndroidNET are very small

No, it isn't obvious, because you haven't posted enough code. On the other hand, it is irrelevant.

What is the problem you are having? Is it the fact that the AndroidBT class knows nothing about what an EthernetClient is? If that is the case, why are you not including the EhternetClient.h file, so that AndroidBT KNOWS what an EthernetClient is?

nunzio raciti

Quote

When do you want to make this choice? At compile time? Or at run time?

At compile time.
Quote

What is the problem you are having? Is it the fact that the AndroidBT class knows nothing about what an EthernetClient is? If that is the case, why are you not including the EhternetClient.h file, so that AndroidBT KNOWS what an EthernetClient is?

I like to be able to connect trough BlueTooth without configure, compile and link the part relating to the network, but probably I need to split my work on two libraries.

PaulS

Quote
I like to be able to connect trough BlueTooth without configure, compile and link the part relating to the network

What gets compiled and what gets linked in are two different things. If you don't create an instance of a class, the class code won't get linked in, even though it was compiled.

PeterH

It  seems to me that you need to either depend on both libraries and have some scheme to inform your library which variant to use at runtime, or come up with an abstraction of the function that each variant provides and enable the appropriate implementation of that abstraction to be provided by the code that uses your library. With out having any idea of how the library would interact with the bluetooth/ethernet libraries I can't give any concrete suggestions for the design of that abstraction but if it's designed to send and receive messages you might design it along the lines of a character I/O stream but working on messages rather than characters.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up