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

Commit d8559d1

Browse files
committed
Added PiBlaster device
1 parent 260bcab commit d8559d1

File tree

6 files changed

+225
-58
lines changed

6 files changed

+225
-58
lines changed

deploy/hyperion.tar.gz

1.86 KB
Binary file not shown.

libsrc/leddevice/CMakeLists.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,31 @@ SET(Leddevice_HEADERS
2121
${CURRENT_HEADER_DIR}/LedDeviceFactory.h
2222

2323
${CURRENT_SOURCE_DIR}/LedRs232Device.h
24-
25-
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
26-
${CURRENT_SOURCE_DIR}/LedDeviceSedu.h
27-
${CURRENT_SOURCE_DIR}/LedDeviceWs2812b.h
28-
${CURRENT_SOURCE_DIR}/LedDeviceWs2811.h
24+
2925
${CURRENT_SOURCE_DIR}/LedDeviceLightpack.h
30-
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.h
3126
${CURRENT_SOURCE_DIR}/LedDeviceMultiLightpack.h
27+
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.h
28+
${CURRENT_SOURCE_DIR}/LedDevicePiBlaster.h
29+
${CURRENT_SOURCE_DIR}/LedDeviceSedu.h
30+
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
31+
${CURRENT_SOURCE_DIR}/LedDeviceWs2812b.h
3232
${CURRENT_SOURCE_DIR}/LedDeviceWs2811.h
3333
)
3434

3535
SET(Leddevice_SOURCES
3636
${CURRENT_SOURCE_DIR}/LedDeviceFactory.cpp
3737

38-
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.cpp
3938
${CURRENT_SOURCE_DIR}/LedRs232Device.cpp
40-
${CURRENT_SOURCE_DIR}/LedDeviceSedu.cpp
41-
${CURRENT_SOURCE_DIR}/LedDeviceTest.cpp
42-
${CURRENT_SOURCE_DIR}/LedDeviceWs2811.cpp
43-
${CURRENT_SOURCE_DIR}/LedDeviceWs2812b.cpp
39+
4440
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.cpp
4541
${CURRENT_SOURCE_DIR}/LedDeviceLightpack.cpp
46-
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.cpp
4742
${CURRENT_SOURCE_DIR}/LedDeviceMultiLightpack.cpp
43+
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.cpp
44+
${CURRENT_SOURCE_DIR}/LedDevicePiBlaster.cpp
45+
${CURRENT_SOURCE_DIR}/LedDeviceSedu.cpp
46+
${CURRENT_SOURCE_DIR}/LedDeviceTest.cpp
4847
${CURRENT_SOURCE_DIR}/LedDeviceWs2811.cpp
48+
${CURRENT_SOURCE_DIR}/LedDeviceWs2812b.cpp
4949
)
5050

5151
if(ENABLE_SPIDEV)

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
// Build configuration
23
#include <HyperionConfig.h>
34

45
// Leddevice includes
@@ -11,14 +12,15 @@
1112
#include "LedDeviceWs2801.h"
1213
#endif
1314

15+
#include "LedDeviceAdalight.h"
16+
#include "LedDeviceLightpack.h"
17+
#include "LedDeviceMultiLightpack.h"
18+
#include "LedDevicePaintpack.h"
19+
#include "LedDevicePiBlaster.h"
1420
#include "LedDeviceSedu.h"
1521
#include "LedDeviceTest.h"
1622
#include "LedDeviceWs2811.h"
1723
#include "LedDeviceWs2812b.h"
18-
#include "LedDeviceAdalight.h"
19-
#include "LedDevicePaintpack.h"
20-
#include "LedDeviceLightpack.h"
21-
#include "LedDeviceMultiLightpack.h"
2224

2325
LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
2426
{
@@ -29,17 +31,17 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
2931

3032
LedDevice* device = nullptr;
3133
if (false) {}
32-
#ifdef ENABLE_SPIDEV
33-
else if (type == "ws2801" || type == "lightberry")
34+
else if (type == "adalight")
3435
{
3536
const std::string output = deviceConfig["output"].asString();
3637
const unsigned rate = deviceConfig["rate"].asInt();
3738

38-
LedDeviceWs2801* deviceWs2801 = new LedDeviceWs2801(output, rate);
39-
deviceWs2801->open();
39+
LedDeviceAdalight* deviceAdalight = new LedDeviceAdalight(output, rate);
40+
deviceAdalight->open();
4041

41-
device = deviceWs2801;
42+
device = deviceAdalight;
4243
}
44+
#ifdef ENABLE_SPIDEV
4345
else if (type == "lpd6803" || type == "ldp6803")
4446
{
4547
const std::string output = deviceConfig["output"].asString();
@@ -60,6 +62,16 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
6062

6163
device = deviceLpd8806;
6264
}
65+
else if (type == "ws2801" || type == "lightberry")
66+
{
67+
const std::string output = deviceConfig["output"].asString();
68+
const unsigned rate = deviceConfig["rate"].asInt();
69+
70+
LedDeviceWs2801* deviceWs2801 = new LedDeviceWs2801(output, rate);
71+
deviceWs2801->open();
72+
73+
device = deviceWs2801;
74+
}
6375
#endif
6476
// else if (type == "ws2811")
6577
// {
@@ -78,33 +90,6 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
7890

7991
// device = deviceWs2811;
8092
// }
81-
else if (type == "ws2812b")
82-
{
83-
LedDeviceWs2812b * deviceWs2812b = new LedDeviceWs2812b();
84-
deviceWs2812b->open();
85-
86-
device = deviceWs2812b;
87-
}
88-
else if (type == "adalight")
89-
{
90-
const std::string output = deviceConfig["output"].asString();
91-
const unsigned rate = deviceConfig["rate"].asInt();
92-
93-
LedDeviceAdalight* deviceAdalight = new LedDeviceAdalight(output, rate);
94-
deviceAdalight->open();
95-
96-
device = deviceAdalight;
97-
}
98-
else if (type == "sedu")
99-
{
100-
const std::string output = deviceConfig["output"].asString();
101-
const unsigned rate = deviceConfig["rate"].asInt();
102-
103-
LedDeviceSedu* deviceSedu = new LedDeviceSedu(output, rate);
104-
deviceSedu->open();
105-
106-
device = deviceSedu;
107-
}
10893
else if (type == "lightpack")
10994
{
11095
const std::string output = deviceConfig.get("output", "").asString();
@@ -114,25 +99,52 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
11499

115100
device = deviceLightpack;
116101
}
102+
else if (type == "multi-lightpack")
103+
{
104+
LedDeviceMultiLightpack* deviceLightpack = new LedDeviceMultiLightpack();
105+
deviceLightpack->open();
106+
107+
device = deviceLightpack;
108+
}
117109
else if (type == "paintpack")
118110
{
119111
LedDevicePaintpack * devicePainLightpack = new LedDevicePaintpack();
120112
devicePainLightpack->open();
121113

122114
device = devicePainLightpack;
123115
}
124-
else if (type == "multi-lightpack")
116+
else if (type == "piblaster")
125117
{
126-
LedDeviceMultiLightpack* deviceLightpack = new LedDeviceMultiLightpack();
127-
deviceLightpack->open();
118+
const std::string output = deviceConfig.get("output", "").asString();
119+
const std::string assignment = deviceConfig.get("assignment", "").asString();
128120

129-
device = deviceLightpack;
121+
LedDevicePiBlaster * devicePiBlaster = new LedDevicePiBlaster(output, assignment);
122+
devicePiBlaster->open();
123+
124+
device = devicePiBlaster;
125+
}
126+
else if (type == "sedu")
127+
{
128+
const std::string output = deviceConfig["output"].asString();
129+
const unsigned rate = deviceConfig["rate"].asInt();
130+
131+
LedDeviceSedu* deviceSedu = new LedDeviceSedu(output, rate);
132+
deviceSedu->open();
133+
134+
device = deviceSedu;
130135
}
131136
else if (type == "test")
132137
{
133138
const std::string output = deviceConfig["output"].asString();
134139
device = new LedDeviceTest(output);
135140
}
141+
else if (type == "ws2812b")
142+
{
143+
LedDeviceWs2812b * deviceWs2812b = new LedDeviceWs2812b();
144+
deviceWs2812b->open();
145+
146+
device = deviceWs2812b;
147+
}
136148
else
137149
{
138150
std::cout << "Unable to create device " << type << std::endl;
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
// STL includes
3+
#include <cerrno>
4+
#include <cstring>
5+
6+
// Local LedDevice includes
7+
#include "LedDevicePiBlaster.h"
8+
9+
LedDevicePiBlaster::LedDevicePiBlaster(const std::string & deviceName, const std::string & channelAssignment) :
10+
_deviceName(deviceName),
11+
_channelAssignment(channelAssignment),
12+
_fid(nullptr)
13+
{
14+
// empty
15+
}
16+
17+
LedDevicePiBlaster::~LedDevicePiBlaster()
18+
{
19+
// Close the device (if it is opened)
20+
if (_fid != nullptr)
21+
{
22+
fclose(_fid);
23+
_fid = nullptr;
24+
}
25+
}
26+
27+
int LedDevicePiBlaster::open()
28+
{
29+
if (_fid != nullptr)
30+
{
31+
// The file pointer is already open
32+
std::cerr << "Attempt to open allready opened device (" << _deviceName << ")" << std::endl;
33+
return -1;
34+
}
35+
36+
_fid = fopen(_deviceName.c_str(), "w");
37+
if (_fid == nullptr)
38+
{
39+
std::cerr << "Failed to open device (" << _deviceName << "). Error message: " << strerror(errno) << std::endl;
40+
return -1;
41+
}
42+
43+
return 0;
44+
}
45+
46+
//Channel number GPIO number Pin in P1 header
47+
// 0 4 P1-7
48+
// 1 17 P1-11
49+
// 2 18 P1-12
50+
// 3 21 P1-13
51+
// 4 22 P1-15
52+
// 5 23 P1-16
53+
// 6 24 P1-18
54+
// 7 25 P1-22
55+
int LedDevicePiBlaster::write(const std::vector<ColorRgb> & ledValues)
56+
{
57+
unsigned colorIdx = 0;
58+
for (unsigned iChannel=0; iChannel<8; ++iChannel)
59+
{
60+
double pwmDutyCycle = 0.0;
61+
switch (_channelAssignment[iChannel])
62+
{
63+
case 'r':
64+
pwmDutyCycle = ledValues[colorIdx].red / 255.0;
65+
++colorIdx;
66+
break;
67+
case 'g':
68+
pwmDutyCycle = ledValues[colorIdx].green / 255.0;
69+
++colorIdx;
70+
break;
71+
case 'b':
72+
pwmDutyCycle = ledValues[colorIdx].blue / 255.0;
73+
++colorIdx;
74+
break;
75+
default:
76+
continue;
77+
}
78+
79+
fprintf(_fid, "%i=%f\n", iChannel, pwmDutyCycle);
80+
fflush(_fid);
81+
}
82+
83+
return 0;
84+
}
85+
86+
int LedDevicePiBlaster::switchOff()
87+
{
88+
for (unsigned iChannel=0; iChannel<8; ++iChannel)
89+
{
90+
if (_channelAssignment[iChannel] != ' ')
91+
{
92+
fprintf(_fid, "%i=%f\n", iChannel, 0.0);
93+
fflush(_fid);
94+
}
95+
}
96+
97+
return 0;
98+
}

libsrc/leddevice/LedDevicePiBlaster.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
#pragma once
3+
4+
// STL includes
5+
#include <cstdio>
6+
7+
// Hyperion-Leddevice includes
8+
#include <leddevice/LedDevice.h>
9+
10+
class LedDevicePiBlaster : public LedDevice
11+
{
12+
public:
13+
///
14+
/// Constructs the PiBlaster device which writes to the indicated device and for the assigned
15+
/// channels
16+
/// @param deviceName The name of the output device
17+
/// @param channelAssignment The RGB-Channel assignment (8 characters long)
18+
///
19+
LedDevicePiBlaster(const std::string & deviceName, const std::string & channelAssignment);
20+
21+
virtual ~LedDevicePiBlaster();
22+
23+
int open();
24+
25+
///
26+
/// Writes the colors to the PiBlaster device
27+
///
28+
/// @param ledValues The color value for each led
29+
///
30+
/// @return Zero on success else negative
31+
///
32+
int write(const std::vector<ColorRgb> &ledValues);
33+
34+
///
35+
/// Switches off the leds
36+
///
37+
/// @return Zero on success else negative
38+
///
39+
int switchOff();
40+
41+
private:
42+
43+
/// The name of the output device (very likely '/dev/pi-blaster')
44+
const std::string _deviceName;
45+
46+
/// String with eight characters with the rgb-channel assignment per pwm-channel
47+
/// ('r' = red, 'g' = green, 'b' = blue, ' ' = empty)
48+
const std::string _channelAssignment;
49+
50+
/// File-Pointer to the PiBlaster device
51+
FILE * _fid;
52+
};

test/TestUartHighSpeed.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,17 +315,22 @@ void test3bitsEncoding()
315315

316316
for (unsigned i=0; i<100; ++i)
317317
{
318-
write(uart0_filestream, colorRedSignal.data(), colorRedSignal.size());
318+
size_t res;
319+
res = write(uart0_filestream, colorRedSignal.data(), colorRedSignal.size());
320+
(void)res;
319321
usleep(100000);
320-
write(uart0_filestream, colorGreenSignal.data(), colorGreenSignal.size());
322+
res = write(uart0_filestream, colorGreenSignal.data(), colorGreenSignal.size());
323+
(void)res;
321324
usleep(100000);
322-
write(uart0_filestream, colorBlueSignal.data(), colorBlueSignal.size());
325+
res = write(uart0_filestream, colorBlueSignal.data(), colorBlueSignal.size());
326+
(void)res;
323327
usleep(100000);
324328
}
325-
write(uart0_filestream, colorBlackSignal.data(), colorBlackSignal.size());
326-
329+
size_t res = write(uart0_filestream, colorBlackSignal.data(), colorBlackSignal.size());
330+
(void)res;
327331
//----- CLOSE THE UART -----
328-
close(uart0_filestream);
332+
res = close(uart0_filestream);
333+
(void)res;
329334

330335
std::cout << "Program finished" << std::endl;
331336
}

0 commit comments

Comments
 (0)