using an overidding class to add a variable...what is the best way?

Thread Starter

Gibson486

Joined Jul 20, 2012
360
I am no software guru, so do not judge if this is a dumb question :(

I am trying to make a class for a motor driver chip.

This chip has two direction pins to dictate whether we want to control the motor in Forward or Reverse.

On some designs, people will hook both pins to a micro, in other designs, people will just fan out from an inverter gate to ensure two different states from one pin.

I started making my class like this

Code:
class motorDriver
{
    uint8_t pwmPin;
    uint8_t directionPin;
    uint8_t enablePin;
    uint8_t enableFWDpin;
     uint8_t enableREVpin;
      
    public:
    motorDriver(uint8_t pwmpin, uint8_t directionpin, uint8_t enablepin, uint8_t enablefwd, uint8_t enablerev)
    {
        pwmPin = pwmpin;
        directionPin = directionpin;
        enablePin = enablepin;
        enableFWDpin = enablefwd;
        enableREVpin = enablerev;
    }
  
};
I was going to make an overriding class to add the extra pin if it was a design that used two pins on the micro

Code:
class motorDriverWith2Pins : public motorDriver
{
  //add another variable?
}
However,

1. this does not seem elegent
2. It does not seem possible to simply add a variable since I need to add that parameter in the base class anyways. If I wanted to redefine a variable, this would work, but not to add?

Any suggestions on a better way? or maybe some pointers on what I can read on? Maybe I am overlooking the obvious?
 

xox

Joined Sep 8, 2017
936
I am no software guru, so do not judge if this is a dumb question :(

I am trying to make a class for a motor driver chip.

This chip has two direction pins to dictate whether we want to control the motor in Forward or Reverse.

On some designs, people will hook both pins to a micro, in other designs, people will just fan out from an inverter gate to ensure two different states from one pin.

I started making my class like this

Code:
class motorDriver
{
    uint8_t pwmPin;
    uint8_t directionPin;
    uint8_t enablePin;
    uint8_t enableFWDpin;
     uint8_t enableREVpin;
     
    public:
    motorDriver(uint8_t pwmpin, uint8_t directionpin, uint8_t enablepin, uint8_t enablefwd, uint8_t enablerev)
    {
        pwmPin = pwmpin;
        directionPin = directionpin;
        enablePin = enablepin;
        enableFWDpin = enablefwd;
        enableREVpin = enablerev;
    }
 
};
I was going to make an overriding class to add the extra pin if it was a design that used two pins on the micro

Code:
class motorDriverWith2Pins : public motorDriver
{
  //add another variable?
}
However,

1. this does not seem elegent
2. It does not seem possible to simply add a variable since I need to add that parameter in the base class anyways. If I wanted to redefine a variable, this would work, but not to add?

Any suggestions on a better way? or maybe some pointers on what I can read on? Maybe I am overlooking the obvious?
Some rules of thumb for OOP:
  • Encapsulate your data properly. Classes should only have access to what is absolutely necessary to perform the task at hand. Less is more. Also avoid defining functions with too many arguments (more than say 2 or 3). Better to pass the data as a struct or POD-like class.
  • Approach the problem from both directions. First the high-level interface (start with simple "stub" functions that just print to the terminal) and then gradually incorporate that with the low-level stuff, gluing it all together using whatever technique is most appropriate. Sometimes functional programming will fit the bill while at other times declarative constructs will make more sense. In other words...everything isn't necessarily an object!
  • Use virtual methods to extend functionality. In your case that means the toggling of IO pins or whatnot. The input struct can actually be a template parameter if you like but that requires a bit more advanced technique called CRTP.
 

spinnaker

Joined Oct 29, 2009
7,830
I am no software guru, so do not judge if this is a dumb question :(

I am trying to make a class for a motor driver chip.

This chip has two direction pins to dictate whether we want to control the motor in Forward or Reverse.

On some designs, people will hook both pins to a micro, in other designs, people will just fan out from an inverter gate to ensure two different states from one pin.

I started making my class like this

Code:
class motorDriver
{
    uint8_t pwmPin;
    uint8_t directionPin;
    uint8_t enablePin;
    uint8_t enableFWDpin;
     uint8_t enableREVpin;
     
    public:
    motorDriver(uint8_t pwmpin, uint8_t directionpin, uint8_t enablepin, uint8_t enablefwd, uint8_t enablerev)
    {
        pwmPin = pwmpin;
        directionPin = directionpin;
        enablePin = enablepin;
        enableFWDpin = enablefwd;
        enableREVpin = enablerev;
    }
 
};
I was going to make an overriding class to add the extra pin if it was a design that used two pins on the micro

Code:
class motorDriverWith2Pins : public motorDriver
{
  //add another variable?
}
However,

1. this does not seem elegent
2. It does not seem possible to simply add a variable since I need to add that parameter in the base class anyways. If I wanted to redefine a variable, this would work, but not to add?

Any suggestions on a better way? or maybe some pointers on what I can read on? Maybe I am overlooking the obvious?
1. Why on earth would you say that? Of course it is. It is one of the many beauties of OOP.

2. Of course you can. But what if you don't have the code for that class to change it? Or what if you want to use the base class for other purposes?
 

Thread Starter

Gibson486

Joined Jul 20, 2012
360
Thanks guys!

I had a really crummy OOP class. 10 years later, I am teaching myself. It is easier now that I am actually interested in it!
 
Top