Charith Dassanayake / CPPToPigpio
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DigitalOut.cc Source File

DigitalOut.cc

00001 namespace CPPToPigpio{
00002     DigitalOut::DigitalOut(PinName pin) : _pinNumber(pin)
00003     {
00004         CPPToPigpio::_handlePigpioInitialisationIfNeeded();
00005         gpioSetMode(static_cast<int>(pin), PI_OUTPUT);
00006     }
00007     DigitalOut::DigitalOut(PinName pin, int value) : _pinNumber(pin)
00008     {
00009         CPPToPigpio::_handlePigpioInitialisationIfNeeded();
00010         gpioSetMode(static_cast<int>(pin), PI_OUTPUT);
00011         DigitalOut::write(value);
00012     }
00013     
00014     DigitalOut::DigitalOut(int pin) : DigitalOut(static_cast<PinName> (pin))
00015     {
00016         //Doesn't need _handlePigpioInitialisationIfNeeded() since calls other constructor  
00017     }
00018 
00019     DigitalOut::DigitalOut(int pin, int value) : DigitalOut(static_cast<PinName> (pin), value)
00020     {
00021         //Doesn't need _handlePigpioInitialisationIfNeeded() since calls other constructor  
00022     }
00023     
00024     void DigitalOut::write(int value)
00025     {   
00026         //Should we perror here if value isn't 0 or 1? MBED should allow value == 2, but pigpio might not
00027         if(value == 0) gpioWrite(static_cast<int>(_pinNumber), 0);
00028         else gpioWrite(static_cast<int>(_pinNumber), 1);    
00029         _lastWriteValue = value;
00030     }
00031 
00032     int DigitalOut::read()
00033     {
00034         return _lastWriteValue;
00035     }
00036     
00037     DigitalOut::operator int()
00038     {
00039         return DigitalOut::read();
00040     }
00041     
00042     DigitalOut & DigitalOut::operator=(int value)
00043     {
00044         DigitalOut::write(value);
00045         return *this;
00046     }
00047     
00048     DigitalOut & DigitalOut::operator=(DigitalOut &rhs)
00049     {
00050         DigitalOut::write(rhs.read());
00051         return *this;
00052     }
00053 
00054     DigitalOut::~DigitalOut()
00055     {
00056         CPPToPigpio::_handlePigpioTermination(); 
00057     }
00058 
00059 }