Go Down

Topic: How to initialize SoftwareSerial in a class in a new Library (Read 4141 times) previous topic - next topic

jrullan

Jul 22, 2014, 11:34 pm Last Edit: Jul 23, 2014, 12:09 am by jrullan Reason: 1
This might be a silly question but I have forgotten my C/C++ classes a long time ago. I am trying to create a library for my GPRS shield and I want to include the SoftwareSerial object inside the new object I will be creating. The header file would include I presume the declaration of the class member:

Code: [Select]

class SMS
{
 public:
   SMS();
   void begin();
   SoftwareSerial smsSerial;
   
 private:  
 
}


The constructor for SoftwareSerial have to be passed the RX and TX pins. And I want these to be parameters of a function, maybe begin:

Code: [Select]

SMS::SMS()
{
 smsFlags = B00000000;
}

void SMS::begin(int rxPin, int txPin)
{
 smsSerial = new SoftwareSerial(rxPin,txPin); //<---- Is this what I'm supposed to do?
}


Not sure if I should use "new"  or not.


xartefact

Two ways :

Dynamically

Code: [Select]

class SMS {
 public:
   SMS();
   ~SMS();
   void begin(uint_8 rxPin, uint_8 txPin, long speed);
  
 private:  
   SoftwareSerial *smsSerial;
   int smsFlags; ???
}

SMS::SMS() {
  smsFlags = B00000000;
  smsSerial = NULL;
}
SMS::~SMS() {
  if(smsSerial != NULL) free(smsSerial);
}
void SMS::begin(uint_8 rxPin, uint_8 txPin, long speed)
{
 smsSerial = new SoftwareSerial(rxPin,txPin);   // it return a pointer to a SoftwareSeril object
 smsSerial->begin(speed);
}


Statically

Code: [Select]

class SMS {
 public:
   SMS();
   void begin(uint_8 rxPin, uint_8 txPin, long speed);
  
 private:  
   SoftwareSerial smsSerial;
   int smsFlags; ???
}

SMS::SMS(){
  smsFlags = B00000000;
}
void SMS::begin(uint_8 rxPin, uint_8 txPin, long speed) : smsSerial (rxPin, txPin)
{
 smsSerial.begin(speed);
}

Go Up