Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit b870036

Browse files
committed
Fixed the open of device until device is available
1 parent d8559d1 commit b870036

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

libsrc/leddevice/LedDevicePiBlaster.cpp

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#include <cerrno>
44
#include <cstring>
55

6+
// QT includes
7+
#include <QFile>
8+
69
// Local LedDevice includes
710
#include "LedDevicePiBlaster.h"
811

@@ -24,22 +27,39 @@ LedDevicePiBlaster::~LedDevicePiBlaster()
2427
}
2528
}
2629

27-
int LedDevicePiBlaster::open()
30+
int LedDevicePiBlaster::open(bool report)
2831
{
2932
if (_fid != nullptr)
3033
{
3134
// The file pointer is already open
32-
std::cerr << "Attempt to open allready opened device (" << _deviceName << ")" << std::endl;
35+
if (report)
36+
{
37+
std::cerr << "Attempt to open allready opened device (" << _deviceName << ")" << std::endl;
38+
}
39+
return -1;
40+
}
41+
42+
if (!QFile::exists(_deviceName.c_str()))
43+
{
44+
if (report)
45+
{
46+
std::cerr << "The device(" << _deviceName << ") does not yet exist, can not connect (yet)." << std::endl;
47+
}
3348
return -1;
3449
}
3550

3651
_fid = fopen(_deviceName.c_str(), "w");
3752
if (_fid == nullptr)
3853
{
39-
std::cerr << "Failed to open device (" << _deviceName << "). Error message: " << strerror(errno) << std::endl;
54+
if (report)
55+
{
56+
std::cerr << "Failed to open device (" << _deviceName << "). Error message: " << strerror(errno) << std::endl;
57+
}
4058
return -1;
4159
}
4260

61+
std::cout << "Connect to device(" << _deviceName << ")" << std::endl;
62+
4363
return 0;
4464
}
4565

@@ -54,6 +74,12 @@ int LedDevicePiBlaster::open()
5474
// 7 25 P1-22
5575
int LedDevicePiBlaster::write(const std::vector<ColorRgb> & ledValues)
5676
{
77+
// Attempt to open if not yet opened
78+
if (_fid == nullptr && open(false) < 0)
79+
{
80+
return -1;
81+
}
82+
5783
unsigned colorIdx = 0;
5884
for (unsigned iChannel=0; iChannel<8; ++iChannel)
5985
{
@@ -85,6 +111,12 @@ int LedDevicePiBlaster::write(const std::vector<ColorRgb> & ledValues)
85111

86112
int LedDevicePiBlaster::switchOff()
87113
{
114+
// Attempt to open if not yet opened
115+
if (_fid == nullptr && open(false) < 0)
116+
{
117+
return -1;
118+
}
119+
88120
for (unsigned iChannel=0; iChannel<8; ++iChannel)
89121
{
90122
if (_channelAssignment[iChannel] != ' ')

libsrc/leddevice/LedDevicePiBlaster.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class LedDevicePiBlaster : public LedDevice
2020

2121
virtual ~LedDevicePiBlaster();
2222

23-
int open();
23+
int open(bool report = true);
2424

2525
///
2626
/// Writes the colors to the PiBlaster device

0 commit comments

Comments
 (0)