Fixed the open of device until device is available

Former-commit-id: b8700368fadb53784c5838ab046431a7f58f108c
This commit is contained in:
T. van der Zwan 2014-01-07 13:20:53 +00:00
parent 9396583551
commit ab0ea90b55
2 changed files with 36 additions and 4 deletions

View File

@ -3,6 +3,9 @@
#include <cerrno> #include <cerrno>
#include <cstring> #include <cstring>
// QT includes
#include <QFile>
// Local LedDevice includes // Local LedDevice includes
#include "LedDevicePiBlaster.h" #include "LedDevicePiBlaster.h"
@ -24,22 +27,39 @@ LedDevicePiBlaster::~LedDevicePiBlaster()
} }
} }
int LedDevicePiBlaster::open() int LedDevicePiBlaster::open(bool report)
{ {
if (_fid != nullptr) if (_fid != nullptr)
{ {
// The file pointer is already open // The file pointer is already open
if (report)
{
std::cerr << "Attempt to open allready opened device (" << _deviceName << ")" << std::endl; std::cerr << "Attempt to open allready opened device (" << _deviceName << ")" << std::endl;
}
return -1;
}
if (!QFile::exists(_deviceName.c_str()))
{
if (report)
{
std::cerr << "The device(" << _deviceName << ") does not yet exist, can not connect (yet)." << std::endl;
}
return -1; return -1;
} }
_fid = fopen(_deviceName.c_str(), "w"); _fid = fopen(_deviceName.c_str(), "w");
if (_fid == nullptr) if (_fid == nullptr)
{
if (report)
{ {
std::cerr << "Failed to open device (" << _deviceName << "). Error message: " << strerror(errno) << std::endl; std::cerr << "Failed to open device (" << _deviceName << "). Error message: " << strerror(errno) << std::endl;
}
return -1; return -1;
} }
std::cout << "Connect to device(" << _deviceName << ")" << std::endl;
return 0; return 0;
} }
@ -54,6 +74,12 @@ int LedDevicePiBlaster::open()
// 7 25 P1-22 // 7 25 P1-22
int LedDevicePiBlaster::write(const std::vector<ColorRgb> & ledValues) int LedDevicePiBlaster::write(const std::vector<ColorRgb> & ledValues)
{ {
// Attempt to open if not yet opened
if (_fid == nullptr && open(false) < 0)
{
return -1;
}
unsigned colorIdx = 0; unsigned colorIdx = 0;
for (unsigned iChannel=0; iChannel<8; ++iChannel) for (unsigned iChannel=0; iChannel<8; ++iChannel)
{ {
@ -85,6 +111,12 @@ int LedDevicePiBlaster::write(const std::vector<ColorRgb> & ledValues)
int LedDevicePiBlaster::switchOff() int LedDevicePiBlaster::switchOff()
{ {
// Attempt to open if not yet opened
if (_fid == nullptr && open(false) < 0)
{
return -1;
}
for (unsigned iChannel=0; iChannel<8; ++iChannel) for (unsigned iChannel=0; iChannel<8; ++iChannel)
{ {
if (_channelAssignment[iChannel] != ' ') if (_channelAssignment[iChannel] != ' ')

View File

@ -20,7 +20,7 @@ public:
virtual ~LedDevicePiBlaster(); virtual ~LedDevicePiBlaster();
int open(); int open(bool report = true);
/// ///
/// Writes the colors to the PiBlaster device /// Writes the colors to the PiBlaster device