Abstracting Arduino/Particle Hardware - C++ help needed

I’m trying to abstract hardware related functionality such the pinMode, digitalWrite etc. Towards that end, I have an abstract class like so:

class Hal
{
  protected:
    Hal() {}
    virtual ~Hal() {}
  public:
    virtual void doPinMode(uint8_t pin, uint8_t mode) {}
    virtual void doDigitalWrite(uint8_t pin, uint8_t value) {}
    virtual void doAnalogWrite(uint8_t pin, int16_t value) {}
    virtual void attach(uint8_t pin) { }
    virtual void detach() { }
    virtual void write(int position) { }    
    virtual int doConstrain(int numberToConstrain, int lowerBound, int upperBound) {
      return 0;
    }
    virtual long doMap(long value, long sourceMin, long sourceMax, long targetMin, long targetMax) {
      return 0;
    }
};

I’m guessing you could imagine the implementation of say doPinMode to look like this (for a class that is intended to be used with an Arduino or Particle

void HalArduino::doPinMode(uint8_t pin, uint8_t mode)
{
  pinMode(pin, mode);
}

void HalArduino::doDigitalWrite(uint8_t pin, uint8_t value)
{
  digitalWrite(pin, value);
}

What I’d really like is for the abstract methods of my base class to be called pinMode and digitalWrite instead of doPinMode and doDigitalWrite. Of course if I did that, it would be a recursive call unless I can qualify the original pinMode and digitalWrite calls with something. I was thinking Arduino::pinMode for example, but that doesn’t work.

I’m not very familiar with C++ so I was hoping one of you C++ gurus can help.

Not sure about the use of that extra abstraction, since Wiring and Particle HAL already do that.

pinMode() is called the same for Arduino and Particle. ARM based Arduino clones already support INPUT_PULLUP (and some INPUT_PULLDOWN) just the same.
Similarly digitalWrite() does exactly that in both worlds.

Can you elaborate some more to convince us? :wink:

Yes, I should have elaborate more here. I develop my code in Visual Studio (obviously keeping an eye on what works in Particle). I can develop, debug and test all of the primary logic in Visual Studio. I have an implementation of the Hal class that I use while developing in Visual Studio. It looks like this::

class HalConsole : public Hal
{
public:
	HalConsole();
	~HalConsole();
	void doPinMode(uint8_t pin, uint8_t mode);
	void doDigitalWrite(uint8_t pin, uint8_t value);	
	void doAnalogWrite(uint8_t pin, int16_t value);
	virtual void attach(uint8_t pin);
	virtual void detach();
	virtual void write(int position);
	int doConstrain(int numberToConstrain, int lowerBound, int upperBound);
	long doMap(long value, long sourceMin, long sourceMax, long targetMin, long targetMax);
};

and the implementation of pinMode and digitalWrite look like so

void HalConsole::doPinMode(uint8_t pin, uint8_t mode)
{
	char pinMode[15];
	switch (mode)
	{
	case 0:
		strcpy_s(pinMode, "INPUT");
		break;
	case 1:
		strcpy_s(pinMode, "OUTPUT");
		break;
	case 2:
		strcpy_s(pinMode, "INPUT_PULLUP");
		break;
	default:
		strcpy_s(pinMode, "UNSUPPORTED");
		break;
	}

	cout << "pinMode(" << unsigned(pin) << "," << pinMode << ")" << endl;
}

void HalConsole::doDigitalWrite(uint8_t pin, uint8_t value)
{
	cout << "digitalWrite(" << unsigned(pin) << "," << unsigned(value) << ")" << endl;
}

I have other implementations that I use for testing but I hope the above gives you a better idea of what I’m doing/trying do.
If there is another way to do this, I’m all ears!

Of course all this works, I just wish I could call those methods pinMode and digitalWrite instead of doPinMode etc.

I’m not sure if this would help you for your purpose, but the Particle firmware repo also provides something like a virtual device, maybe something in there might provide some ideas.
I personally haven’t worked with it, but maybe @rickkas7 could give you some pointers or hook you up with the engineers behind the development.

Interesting. Thanks @ScruffR. I’ll take a look and see what I can figure out.

Is there any way to qualify the pinMode function so as to disambiguate it?