I have been reading about passing a port to a function, and seems simple enough. Now i'm trying to expand this concept to a class. To test I created a simple LED class:
class LED
{
//variables
public:
protected:
private:
volatile uint8_t *ledPort;
uint8_t ledPin;
//functions
public:
LED();
~LED();
void initLED(volatile uint8_t *port, volatile uint8_t *dirport, uint8_t pin); //Setup port and pin for LED use
void on(void);
void off(void);
protected:
private:
LED( const LED &c );
LED& operator=( const LED &c );
And here is the code for the class:
#include "LED.h"
// default constructor
LED::LED()
{
} //LED
void LED::initLED(volatile uint8_t *port, volatile uint8_t *dirport, uint8_t pin)
{
*ledPort=*port;
ledPin=pin;
*dirport |= (1<<ledPin);
}
void LED::on(void)
{
*ledPort |= (1<<ledPin);
}
void LED::off(void)
{
*ledPort &= (1<<ledPin);
}
// default destructor
LED::~LED()
{
} //~LED
Here i'm trying to keep in the function the address of PORTB so I can use it on the led::on and led::off members. The led::iniLED is called from the main function.
Is this correct?
EDIT:
The blinking was not working, but by changing: *ledPort=*port; to ledPort=port; on the initLED function the blinker works.