Pages: [1]   Go Down
Author Topic: How create a "single" library that can choose to use Serial or Network hardware?  (Read 389 times)
0 Members and 1 Guest are viewing this topic.
Italia
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley-cry

Can anyone help me?

Thanks.
Nunzio


























Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Italia
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//! 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:
//! 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;
};








Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Italia
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Italia
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46300
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11196
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: