Report
4 months ago.

How to share pins in a class?

I'm trying build a library that sets two objects on the same pins, at least I think that's how to describe what I'm trying to do. Its all working in a one big main.cpp, but now I need to do the same in a library. The problem is setting esp and esp2 on the same pins.

Normally I can work this out by looking at examples but can't find anything like this.

It's a simple two file library, ESP8266.CPP and EAP8266.h.

This is what I have but clearly its not working.

..

// from main.ccp existing working program not using a library
//  Serial esp(D8,D2,230400);   // tx, rx, baudrate 
//  Serial esp2(D8,D2,230400);  // tx, rx, baudrate 

// I want to set just one object here:
// Serial esp(D8,D2,230400);   // tx, rx, baudrate 




// this is my ESP8266.cpp
ESP8266::ESP8266(PinName tx, PinName rx, uint32_t _baud) : esp(tx, rx), esp2(tx, rx)
{    
    esp.baud(_baud);    
    esp2.baud(_baud);
    
    timeout=5000;
    debugOn=debug;        
}

    //........ rest of code




// this is my ESP8266.h
class ESP8266
{
public:

    ESP8266(PinName tx, PinName rx, uint32_t baud);
    
    //......... rest of code
    
    
private:

    RawSerial esp;
    RawSerial esp2;

..

Can anyone help please :)

Comment on this question

1 Answer

4 months ago.

It looks like you have a serial port and you want two ESP8266 objects to have access to it? Most straightforward way is to create the RawSerial object at top level along with both ESP8266 objects and pass a Reference to RawSerial object into ctor of each ESP object. Something like this:

// main.cpp
RawSerial serial(D8, D2, 230400);
ESP8266 esp1(serial);
ESP8266 esp2(serial);


// ESP8266.h
class ESP8266 {
public:
 
    ESP8266(RawSerial& serial) :
        serial_(serial) {

        serial_.printf("Print stuff\n");
   }
      
private:
    RawSerial& serial_;
}; 

Hi Graham, thanks for the tips.

I've done it slightly differently as below and this seems to be working okay, however not too sure if it is correct and if I need to do anything else. It is for just one ESP8266 wi-fi module. Sending data goes to esp1 object and there is a ring buffer on object esp2 to read data from the module.

//  main.cpp

#include "mbed.h"
#include "ESP8266.h"

ESP8266 esp(D8,D2,460800);     // tx, rx, baud STM32


//  ESP8266.h file

class ESP8266
{
public:

    ESP8266(PinName tx, PinName rx, uint32_t baud);
    
    bool init(void);

    // rest of functions...

private:
    
    RawSerial esp;        // AT cammand sending to wi-fi module
    RawSerial esp2;       // interrupt ring buffer, serial data from wi-fi module
};


//  ESP8266.cpp file

ESP8266::ESP8266(PinName tx, PinName rx, uint32_t _baud) : esp(tx, rx), esp2(tx, rx)
{   
    esp.baud(_baud);
    esp2.baud(_baud);
    esp2.attach(callback(this,&ESP8266::espRxbuff),Serial::RxIrq);    
}
posted by Paul Staron 26 Mar 2019

I prefer my answer because it makes the dependencies explicit. The reality is you only have one single serial port connected to the tx and rx pin. I assume the intent is that both EPS8266 objects use the same serial port. Your solution to create two serial port objects on the same pins may work if the underlying Serial class uses a Singleton to ensure you have only one serial port object for the particular UART peripheral. I believe the library does in fact do this. It might work okay but it smells a little funny.

Also I don't quite understand class design here. You have two serial ports (which are actually the same serial port due to Singleton) inside the same class. It looks like what you want is One serial port that is shared between two different EPS8266 objects? But as long as it is working for you, that's the most important thing.

posted by Graham S. 26 Mar 2019

You need to log in to post a question