3
3
#include < cerrno>
4
4
#include < cstring>
5
5
6
+ // QT includes
7
+ #include < QFile>
8
+
6
9
// Local LedDevice includes
7
10
#include " LedDevicePiBlaster.h"
8
11
@@ -24,22 +27,39 @@ LedDevicePiBlaster::~LedDevicePiBlaster()
24
27
}
25
28
}
26
29
27
- int LedDevicePiBlaster::open ()
30
+ int LedDevicePiBlaster::open (bool report )
28
31
{
29
32
if (_fid != nullptr )
30
33
{
31
34
// 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
+ }
33
48
return -1 ;
34
49
}
35
50
36
51
_fid = fopen (_deviceName.c_str (), " w" );
37
52
if (_fid == nullptr )
38
53
{
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
+ }
40
58
return -1 ;
41
59
}
42
60
61
+ std::cout << " Connect to device(" << _deviceName << " )" << std::endl;
62
+
43
63
return 0 ;
44
64
}
45
65
@@ -54,6 +74,12 @@ int LedDevicePiBlaster::open()
54
74
// 7 25 P1-22
55
75
int LedDevicePiBlaster::write (const std::vector<ColorRgb> & ledValues)
56
76
{
77
+ // Attempt to open if not yet opened
78
+ if (_fid == nullptr && open (false ) < 0 )
79
+ {
80
+ return -1 ;
81
+ }
82
+
57
83
unsigned colorIdx = 0 ;
58
84
for (unsigned iChannel=0 ; iChannel<8 ; ++iChannel)
59
85
{
@@ -85,6 +111,12 @@ int LedDevicePiBlaster::write(const std::vector<ColorRgb> & ledValues)
85
111
86
112
int LedDevicePiBlaster::switchOff ()
87
113
{
114
+ // Attempt to open if not yet opened
115
+ if (_fid == nullptr && open (false ) < 0 )
116
+ {
117
+ return -1 ;
118
+ }
119
+
88
120
for (unsigned iChannel=0 ; iChannel<8 ; ++iChannel)
89
121
{
90
122
if (_channelAssignment[iChannel] != ' ' )
0 commit comments