2014-01-04 11:35:11 +01:00
|
|
|
|
|
|
|
// STL includes
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstring>
|
|
|
|
|
2014-01-07 14:20:53 +01:00
|
|
|
// QT includes
|
|
|
|
#include <QFile>
|
|
|
|
|
2014-01-04 11:35:11 +01:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-07 14:20:53 +01:00
|
|
|
int LedDevicePiBlaster::open(bool report)
|
2014-01-04 11:35:11 +01:00
|
|
|
{
|
|
|
|
if (_fid != nullptr)
|
|
|
|
{
|
|
|
|
// The file pointer is already open
|
2014-01-07 14:20:53 +01:00
|
|
|
if (report)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2014-01-04 11:35:11 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_fid = fopen(_deviceName.c_str(), "w");
|
|
|
|
if (_fid == nullptr)
|
|
|
|
{
|
2014-01-07 14:20:53 +01:00
|
|
|
if (report)
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to open device (" << _deviceName << "). Error message: " << strerror(errno) << std::endl;
|
|
|
|
}
|
2014-01-04 11:35:11 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-01-07 14:20:53 +01:00
|
|
|
std::cout << "Connect to device(" << _deviceName << ")" << std::endl;
|
|
|
|
|
2014-01-04 11:35:11 +01:00
|
|
|
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)
|
|
|
|
{
|
2014-01-07 14:20:53 +01:00
|
|
|
// Attempt to open if not yet opened
|
|
|
|
if (_fid == nullptr && open(false) < 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
2015-02-23 15:53:05 +01:00
|
|
|
|
2015-02-09 23:11:59 +01:00
|
|
|
std::vector<int> iPins = {4, 17, 18, 27, 21, 22, 23, 24, 25};
|
2014-01-07 14:20:53 +01:00
|
|
|
|
2014-01-04 11:35:11 +01:00
|
|
|
unsigned colorIdx = 0;
|
2015-03-20 21:11:39 +01:00
|
|
|
for (unsigned iPin=0; iPin<iPins.size(); ++iPin)
|
2014-01-04 11:35:11 +01:00
|
|
|
{
|
|
|
|
double pwmDutyCycle = 0.0;
|
2015-03-20 21:11:39 +01:00
|
|
|
switch (_channelAssignment[iPin])
|
2014-01-04 11:35:11 +01:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-20 21:11:39 +01:00
|
|
|
fprintf(_fid, "%i=%f\n", iPins[iPin], pwmDutyCycle);
|
2014-01-04 11:35:11 +01:00
|
|
|
fflush(_fid);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LedDevicePiBlaster::switchOff()
|
|
|
|
{
|
2014-01-07 14:20:53 +01:00
|
|
|
// Attempt to open if not yet opened
|
|
|
|
if (_fid == nullptr && open(false) < 0)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-02-09 23:11:59 +01:00
|
|
|
std::vector<int> iPins = {4, 17, 18, 21, 22, 23, 24, 25};
|
|
|
|
|
2015-03-20 21:11:39 +01:00
|
|
|
for (unsigned iPin=0; iPin<iPins.size(); ++iPin)
|
2014-01-04 11:35:11 +01:00
|
|
|
{
|
2015-03-20 21:11:39 +01:00
|
|
|
if (_channelAssignment[iPin] != ' ')
|
2014-01-04 11:35:11 +01:00
|
|
|
{
|
2015-03-20 21:11:39 +01:00
|
|
|
fprintf(_fid, "%i=%f\n", iPins[iPin], 0.0);
|
2014-01-04 11:35:11 +01:00
|
|
|
fflush(_fid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|