miércoles, 26 de junio de 2013

(II) C++ crash examples tutorial using Robotis CM-900 board and IDE (II)

In this post I will try to show one important C++ feature, virtual classes as interfaces, defining a rol that several classes can implement. Also

Here you can download the file with the source code.

_2_CM900_CPP_Interfaces

[sourcecode language="cpp"]

// C++ crash tutorial with Robotis CM-900.

// First example: http://softwaresouls.com/softwaresouls/

/*
Classes inheritance and interface creation in C++
*/

// Abstract class as Interface for communication device classes http://www.cplusplus.com/doc/tutorial/polymorphism/
class CommunicationDevice
{
public:
virtual void setup(int baudrate=0)=0; // baudrate=0, default parameter in case is not passed any parameter.
//...)=0; is a pure virtual method that convert the class in abstract http://www.cplusplus.com/doc/tutorial/polymorphism/
//      because is not straight usable , a derived classimpllementing the pure virtual methods must be created
virtual void send(char *message)=0;   // default parameter in case is not passed any parameter.

virtual void send(int i)=0; //Method overloading http://www.cplusplus.com/doc/tutorial/functions2/ Same method name with different parameter types

};

class Zigbee : public CommunicationDevice //inherits the interface a should define the methos to be able to create objects.
{
public:
void setup(int baudrate=0);
void send (char *message);
void send(int i);
};

class USB : public CommunicationDevice //inherits the interface a should define the methos to be able to create objects.
{
public:
void setup(int baudrate=0);
void send (char *message);
void send(int i);
};

class Debug {
CommunicationDevice &commDevice;
public:
Debug(CommunicationDevice &aCommDevice) : commDevice(aCommDevice) {}; // member object commDevice is instantiated by reference to avoid object copy
void show(char *message)
{
commDevice.send(message); // the "commDevice"  method called depends on the object passed to the constructor
}

void show(int i)
{
commDevice.send(i);
}
};

void Zigbee::setup(int baudrate)
{
if (baudrate==0)
baudrate=57600; //only changes local copy, not the variable passed as value parameter http://www.cplusplus.com/doc/tutorial/functions2/
Serial2.begin(baudrate);
}

void Zigbee::send(char *message)
{
Serial2.println(message);
}

void Zigbee::send(int i)
{
Serial2.println(i);
}

void USB::setup(int baudrate)
{
SerialUSB.begin();
}

void USB::send(char *message)
{
SerialUSB.println(message);
}

void USB::send(int i)
{
SerialUSB.println(i);
}

Zigbee zigbee;
USB usb;

const byte NumberOfCommsDevices=2;
CommunicationDevice *commsDevices[]={&usb, &zigbee};
//={usb, zigbee};

void setup()
{
//  Individually:

//  zigbee.setup();
//  usb.setup();

//  collectively. using the common Interface "CommunicationDevice"
for (int i=0;i<NumberOfCommsDevices;i++) commsDevices[i]->setup();
delay(2000); // waiting for 2 seconds, let the user activate the monitor/console window (Control+Shift+M).
}

int counter=0;
void loop()
{
++counter;
Debug debug1(usb); // Debug constructor accepts any class inhereting the Interface "CommunicationDevice", Debug(CommunicationDevice &aCommDevice)
debug1.show("Hola"); //debug object will call the method "send" implemented in the object passed to the constructor. In this case "usb.send"

//Showing the counter for each iteration until the 2000th iteration
if (counter <=2000)
debug1.show(counter);
else
{
// Showing the counter every 100 iterations and waiting for 2 seconds
if (counter%100==0) // Operator modulo http://www.cplusplus.com/doc/tutorial/operators/ (returns the remainder)
{
debug1.show(counter);
delay(2000);
}
}
}

[/sourcecode]

No hay comentarios:

Publicar un comentario