Added PiBlaster device

Former-commit-id: d8559d17a5c1496aa5274da2b56a3e7b45a6d1f9
This commit is contained in:
T. van der Zwan 2014-01-04 10:35:11 +00:00
parent 4fe1a8de85
commit 9396583551
6 changed files with 230 additions and 63 deletions

Binary file not shown.

View File

@ -21,31 +21,31 @@ SET(Leddevice_HEADERS
${CURRENT_HEADER_DIR}/LedDeviceFactory.h
${CURRENT_SOURCE_DIR}/LedRs232Device.h
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
${CURRENT_SOURCE_DIR}/LedDeviceSedu.h
${CURRENT_SOURCE_DIR}/LedDeviceWs2812b.h
${CURRENT_SOURCE_DIR}/LedDeviceWs2811.h
${CURRENT_SOURCE_DIR}/LedDeviceLightpack.h
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.h
${CURRENT_SOURCE_DIR}/LedDeviceMultiLightpack.h
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.h
${CURRENT_SOURCE_DIR}/LedDevicePiBlaster.h
${CURRENT_SOURCE_DIR}/LedDeviceSedu.h
${CURRENT_SOURCE_DIR}/LedDeviceTest.h
${CURRENT_SOURCE_DIR}/LedDeviceWs2812b.h
${CURRENT_SOURCE_DIR}/LedDeviceWs2811.h
)
SET(Leddevice_SOURCES
${CURRENT_SOURCE_DIR}/LedDeviceFactory.cpp
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.cpp
${CURRENT_SOURCE_DIR}/LedRs232Device.cpp
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.cpp
${CURRENT_SOURCE_DIR}/LedDeviceLightpack.cpp
${CURRENT_SOURCE_DIR}/LedDeviceMultiLightpack.cpp
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.cpp
${CURRENT_SOURCE_DIR}/LedDevicePiBlaster.cpp
${CURRENT_SOURCE_DIR}/LedDeviceSedu.cpp
${CURRENT_SOURCE_DIR}/LedDeviceTest.cpp
${CURRENT_SOURCE_DIR}/LedDeviceWs2811.cpp
${CURRENT_SOURCE_DIR}/LedDeviceWs2812b.cpp
${CURRENT_SOURCE_DIR}/LedDeviceAdalight.cpp
${CURRENT_SOURCE_DIR}/LedDeviceLightpack.cpp
${CURRENT_SOURCE_DIR}/LedDevicePaintpack.cpp
${CURRENT_SOURCE_DIR}/LedDeviceMultiLightpack.cpp
${CURRENT_SOURCE_DIR}/LedDeviceWs2811.cpp
)
if(ENABLE_SPIDEV)

View File

@ -1,4 +1,5 @@
// Build configuration
#include <HyperionConfig.h>
// Leddevice includes
@ -11,14 +12,15 @@
#include "LedDeviceWs2801.h"
#endif
#include "LedDeviceAdalight.h"
#include "LedDeviceLightpack.h"
#include "LedDeviceMultiLightpack.h"
#include "LedDevicePaintpack.h"
#include "LedDevicePiBlaster.h"
#include "LedDeviceSedu.h"
#include "LedDeviceTest.h"
#include "LedDeviceWs2811.h"
#include "LedDeviceWs2812b.h"
#include "LedDeviceAdalight.h"
#include "LedDevicePaintpack.h"
#include "LedDeviceLightpack.h"
#include "LedDeviceMultiLightpack.h"
LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
{
@ -29,17 +31,17 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
LedDevice* device = nullptr;
if (false) {}
#ifdef ENABLE_SPIDEV
else if (type == "ws2801" || type == "lightberry")
else if (type == "adalight")
{
const std::string output = deviceConfig["output"].asString();
const unsigned rate = deviceConfig["rate"].asInt();
LedDeviceWs2801* deviceWs2801 = new LedDeviceWs2801(output, rate);
deviceWs2801->open();
LedDeviceAdalight* deviceAdalight = new LedDeviceAdalight(output, rate);
deviceAdalight->open();
device = deviceWs2801;
device = deviceAdalight;
}
#ifdef ENABLE_SPIDEV
else if (type == "lpd6803" || type == "ldp6803")
{
const std::string output = deviceConfig["output"].asString();
@ -60,6 +62,16 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
device = deviceLpd8806;
}
else if (type == "ws2801" || type == "lightberry")
{
const std::string output = deviceConfig["output"].asString();
const unsigned rate = deviceConfig["rate"].asInt();
LedDeviceWs2801* deviceWs2801 = new LedDeviceWs2801(output, rate);
deviceWs2801->open();
device = deviceWs2801;
}
#endif
// else if (type == "ws2811")
// {
@ -78,22 +90,38 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
// device = deviceWs2811;
// }
else if (type == "ws2812b")
{
LedDeviceWs2812b * deviceWs2812b = new LedDeviceWs2812b();
deviceWs2812b->open();
device = deviceWs2812b;
}
else if (type == "adalight")
else if (type == "lightpack")
{
const std::string output = deviceConfig["output"].asString();
const unsigned rate = deviceConfig["rate"].asInt();
const std::string output = deviceConfig.get("output", "").asString();
LedDeviceAdalight* deviceAdalight = new LedDeviceAdalight(output, rate);
deviceAdalight->open();
LedDeviceLightpack* deviceLightpack = new LedDeviceLightpack();
deviceLightpack->open(output);
device = deviceAdalight;
device = deviceLightpack;
}
else if (type == "multi-lightpack")
{
LedDeviceMultiLightpack* deviceLightpack = new LedDeviceMultiLightpack();
deviceLightpack->open();
device = deviceLightpack;
}
else if (type == "paintpack")
{
LedDevicePaintpack * devicePainLightpack = new LedDevicePaintpack();
devicePainLightpack->open();
device = devicePainLightpack;
}
else if (type == "piblaster")
{
const std::string output = deviceConfig.get("output", "").asString();
const std::string assignment = deviceConfig.get("assignment", "").asString();
LedDevicePiBlaster * devicePiBlaster = new LedDevicePiBlaster(output, assignment);
devicePiBlaster->open();
device = devicePiBlaster;
}
else if (type == "sedu")
{
@ -105,34 +133,18 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
device = deviceSedu;
}
else if (type == "lightpack")
{
const std::string output = deviceConfig.get("output", "").asString();
LedDeviceLightpack* deviceLightpack = new LedDeviceLightpack();
deviceLightpack->open(output);
device = deviceLightpack;
}
else if (type == "paintpack")
{
LedDevicePaintpack * devicePainLightpack = new LedDevicePaintpack();
devicePainLightpack->open();
device = devicePainLightpack;
}
else if (type == "multi-lightpack")
{
LedDeviceMultiLightpack* deviceLightpack = new LedDeviceMultiLightpack();
deviceLightpack->open();
device = deviceLightpack;
}
else if (type == "test")
{
const std::string output = deviceConfig["output"].asString();
device = new LedDeviceTest(output);
}
else if (type == "ws2812b")
{
LedDeviceWs2812b * deviceWs2812b = new LedDeviceWs2812b();
deviceWs2812b->open();
device = deviceWs2812b;
}
else
{
std::cout << "Unable to create device " << type << std::endl;

View File

@ -0,0 +1,98 @@
// STL includes
#include <cerrno>
#include <cstring>
// Local LedDevice includes
#include "LedDevicePiBlaster.h"
LedDevicePiBlaster::LedDevicePiBlaster(const std::string & deviceName, const std::string & channelAssignment) :
_deviceName(deviceName),
_channelAssignment(channelAssignment),
_fid(nullptr)
{
// empty
}
LedDevicePiBlaster::~LedDevicePiBlaster()
{
// Close the device (if it is opened)
if (_fid != nullptr)
{
fclose(_fid);
_fid = nullptr;
}
}
int LedDevicePiBlaster::open()
{
if (_fid != nullptr)
{
// The file pointer is already open
std::cerr << "Attempt to open allready opened device (" << _deviceName << ")" << std::endl;
return -1;
}
_fid = fopen(_deviceName.c_str(), "w");
if (_fid == nullptr)
{
std::cerr << "Failed to open device (" << _deviceName << "). Error message: " << strerror(errno) << std::endl;
return -1;
}
return 0;
}
//Channel number GPIO number Pin in P1 header
// 0 4 P1-7
// 1 17 P1-11
// 2 18 P1-12
// 3 21 P1-13
// 4 22 P1-15
// 5 23 P1-16
// 6 24 P1-18
// 7 25 P1-22
int LedDevicePiBlaster::write(const std::vector<ColorRgb> & ledValues)
{
unsigned colorIdx = 0;
for (unsigned iChannel=0; iChannel<8; ++iChannel)
{
double pwmDutyCycle = 0.0;
switch (_channelAssignment[iChannel])
{
case 'r':
pwmDutyCycle = ledValues[colorIdx].red / 255.0;
++colorIdx;
break;
case 'g':
pwmDutyCycle = ledValues[colorIdx].green / 255.0;
++colorIdx;
break;
case 'b':
pwmDutyCycle = ledValues[colorIdx].blue / 255.0;
++colorIdx;
break;
default:
continue;
}
fprintf(_fid, "%i=%f\n", iChannel, pwmDutyCycle);
fflush(_fid);
}
return 0;
}
int LedDevicePiBlaster::switchOff()
{
for (unsigned iChannel=0; iChannel<8; ++iChannel)
{
if (_channelAssignment[iChannel] != ' ')
{
fprintf(_fid, "%i=%f\n", iChannel, 0.0);
fflush(_fid);
}
}
return 0;
}

View File

@ -0,0 +1,52 @@
#pragma once
// STL includes
#include <cstdio>
// Hyperion-Leddevice includes
#include <leddevice/LedDevice.h>
class LedDevicePiBlaster : public LedDevice
{
public:
///
/// Constructs the PiBlaster device which writes to the indicated device and for the assigned
/// channels
/// @param deviceName The name of the output device
/// @param channelAssignment The RGB-Channel assignment (8 characters long)
///
LedDevicePiBlaster(const std::string & deviceName, const std::string & channelAssignment);
virtual ~LedDevicePiBlaster();
int open();
///
/// Writes the colors to the PiBlaster device
///
/// @param ledValues The color value for each led
///
/// @return Zero on success else negative
///
int write(const std::vector<ColorRgb> &ledValues);
///
/// Switches off the leds
///
/// @return Zero on success else negative
///
int switchOff();
private:
/// The name of the output device (very likely '/dev/pi-blaster')
const std::string _deviceName;
/// String with eight characters with the rgb-channel assignment per pwm-channel
/// ('r' = red, 'g' = green, 'b' = blue, ' ' = empty)
const std::string _channelAssignment;
/// File-Pointer to the PiBlaster device
FILE * _fid;
};

View File

@ -315,17 +315,22 @@ void test3bitsEncoding()
for (unsigned i=0; i<100; ++i)
{
write(uart0_filestream, colorRedSignal.data(), colorRedSignal.size());
size_t res;
res = write(uart0_filestream, colorRedSignal.data(), colorRedSignal.size());
(void)res;
usleep(100000);
write(uart0_filestream, colorGreenSignal.data(), colorGreenSignal.size());
res = write(uart0_filestream, colorGreenSignal.data(), colorGreenSignal.size());
(void)res;
usleep(100000);
write(uart0_filestream, colorBlueSignal.data(), colorBlueSignal.size());
res = write(uart0_filestream, colorBlueSignal.data(), colorBlueSignal.size());
(void)res;
usleep(100000);
}
write(uart0_filestream, colorBlackSignal.data(), colorBlackSignal.size());
size_t res = write(uart0_filestream, colorBlackSignal.data(), colorBlackSignal.size());
(void)res;
//----- CLOSE THE UART -----
close(uart0_filestream);
res = close(uart0_filestream);
(void)res;
std::cout << "Program finished" << std::endl;
}