From 33f1ef457b8c85f06f2291978ab04842e85e8912 Mon Sep 17 00:00:00 2001 From: penfold42 Date: Sat, 25 Jun 2016 22:44:52 +1000 Subject: [PATCH] More logging cleanup on led devices (#47) --- libsrc/leddevice/LedDeviceAPA102.cpp | 12 ---------- libsrc/leddevice/LedDevicePiBlaster.cpp | 16 ++++++------- libsrc/leddevice/LedDevicePiBlaster.h | 4 ++++ libsrc/leddevice/LedSpiDevice.cpp | 4 +++- libsrc/leddevice/LedUdpDevice.cpp | 31 ++++++++----------------- 5 files changed, 25 insertions(+), 42 deletions(-) diff --git a/libsrc/leddevice/LedDeviceAPA102.cpp b/libsrc/leddevice/LedDeviceAPA102.cpp index e7dc7eb6..85b3bac8 100644 --- a/libsrc/leddevice/LedDeviceAPA102.cpp +++ b/libsrc/leddevice/LedDeviceAPA102.cpp @@ -27,9 +27,6 @@ int LedDeviceAPA102::write(const std::vector &ledValues) const unsigned int endFrameSize = std::max(((max_leds + 15) / 16), 4); const unsigned int APAbufferSize = (max_leds * 4) + startFrameSize + endFrameSize; -//printf ("_mLedCount %d _HW_ledcount %d max_leds %d APAbufferSize %d\n", -// _mLedCount, _HW_ledcount, max_leds, APAbufferSize); - if(_ledBuffer.size() != APAbufferSize){ _ledBuffer.resize(APAbufferSize, 0xFF); _ledBuffer[0] = 0x00; @@ -54,15 +51,6 @@ int LedDeviceAPA102::write(const std::vector &ledValues) _ledBuffer[4+iLed*4+3] = 0x00; } -/* -for (unsigned i=0; i< _ledBuffer.size(); i+=4) { - printf ("i %2d led %2d RGB 0x0%02x%02x%02x%02x\n",i, i/4-1, - _ledBuffer[i+0], - _ledBuffer[i+1], - _ledBuffer[i+2], - _ledBuffer[i+3]); -} -*/ return writeBytes(_ledBuffer.size(), _ledBuffer.data()); } diff --git a/libsrc/leddevice/LedDevicePiBlaster.cpp b/libsrc/leddevice/LedDevicePiBlaster.cpp index ced94ed4..4e75b424 100644 --- a/libsrc/leddevice/LedDevicePiBlaster.cpp +++ b/libsrc/leddevice/LedDevicePiBlaster.cpp @@ -16,7 +16,8 @@ LedDevicePiBlaster::LedDevicePiBlaster(const std::string & deviceName, const Json::Value & gpioMapping) : _deviceName(deviceName), - _fid(nullptr) + _fid(nullptr), + _log(Logger::getInstance("LedDevice")) { signal(SIGPIPE, SIG_IGN); @@ -39,13 +40,13 @@ LedDevicePiBlaster::LedDevicePiBlaster(const std::string & deviceName, const Jso const int gpio = gpioMap.get("gpio",-1).asInt(); const int ledindex = gpioMap.get("ledindex",-1).asInt(); const std::string ledcolor = gpioMap.get("ledcolor","z").asString(); -// printf ("got gpio %d ledindex %d color %c\n", gpio,ledindex, ledcolor[0]); + // ignore missing/invalid settings if ( (gpio >= 0) && (gpio < signed(TABLE_SZ)) && (ledindex >= 0) ){ _gpio_to_led[gpio] = ledindex; _gpio_to_color[gpio] = ledcolor[0]; // 1st char of string } else { - printf ("IGNORING gpio %d ledindex %d color %c\n", gpio,ledindex, ledcolor[0]); + Warning( _log, "IGNORING gpio %d ledindex %d color %c", gpio,ledindex, ledcolor[0]); } } } @@ -67,7 +68,7 @@ int LedDevicePiBlaster::open(bool report) // The file pointer is already open if (report) { - std::cerr << "Attempt to open allready opened device (" << _deviceName << ")" << std::endl; + Error( _log, "Device (%s) is already open.", _deviceName.c_str() ); } return -1; } @@ -76,7 +77,7 @@ int LedDevicePiBlaster::open(bool report) { if (report) { - std::cerr << "The device(" << _deviceName << ") does not yet exist, can not connect (yet)." << std::endl; + Error( _log, "The device (%s) does not yet exist.", _deviceName.c_str() ); } return -1; } @@ -86,12 +87,12 @@ int LedDevicePiBlaster::open(bool report) { if (report) { - std::cerr << "Failed to open device (" << _deviceName << "). Error message: " << strerror(errno) << std::endl; + Error( _log, "Failed to open device (%s). Error message: %s", _deviceName.c_str(), strerror(errno) ); } return -1; } - std::cout << "Connect to device(" << _deviceName << ")" << std::endl; + Info( _log, "Connected to device(%s)", _deviceName.c_str()); return 0; } @@ -111,7 +112,6 @@ int LedDevicePiBlaster::write(const std::vector & ledValues) if ( (valueIdx >= 0) && (valueIdx < (signed) ledValues.size()) ) { double pwmDutyCycle = 0.0; -// printf ("iPin %d valueIdx %d color %c\n", iPin, valueIdx, _gpio_to_color[ iPins[iPin] ] ) ; switch (_gpio_to_color[ i ]) { case 'r': diff --git a/libsrc/leddevice/LedDevicePiBlaster.h b/libsrc/leddevice/LedDevicePiBlaster.h index 531372a6..4fe9b958 100644 --- a/libsrc/leddevice/LedDevicePiBlaster.h +++ b/libsrc/leddevice/LedDevicePiBlaster.h @@ -9,6 +9,7 @@ // Hyperion-Leddevice includes #include +#include class LedDevicePiBlaster : public LedDevice { @@ -58,4 +59,7 @@ private: /// File-Pointer to the PiBlaster device FILE * _fid; + + /// logger instance + Logger* _log; }; diff --git a/libsrc/leddevice/LedSpiDevice.cpp b/libsrc/leddevice/LedSpiDevice.cpp index a4c7125c..2bd2f3c9 100644 --- a/libsrc/leddevice/LedSpiDevice.cpp +++ b/libsrc/leddevice/LedSpiDevice.cpp @@ -3,6 +3,7 @@ #include #include #include +#include // Linux includes #include @@ -10,6 +11,7 @@ // Local Hyperion includes #include "LedSpiDevice.h" +#include LedSpiDevice::LedSpiDevice(const std::string& outputDevice, const unsigned baudrate, const int latchTime_ns) : @@ -34,7 +36,7 @@ int LedSpiDevice::open() if (mFid < 0) { - std::cerr << "Failed to open device('" << mDeviceName << "') " << std::endl; + Error( Logger::getInstance("LedDevice"), "Failed to open device (%s). Error message: %s", mDeviceName.c_str(), strerror(errno) ); return -1; } diff --git a/libsrc/leddevice/LedUdpDevice.cpp b/libsrc/leddevice/LedUdpDevice.cpp index d8b4fd12..a7a6461c 100644 --- a/libsrc/leddevice/LedUdpDevice.cpp +++ b/libsrc/leddevice/LedUdpDevice.cpp @@ -18,8 +18,7 @@ LedUdpDevice::LedUdpDevice(const std::string& outputDevice, const unsigned baudrate, const int latchTime_ns) : mDeviceName(outputDevice), mBaudRate_Hz(baudrate), - mLatchTime_ns(latchTime_ns), - mFid(-1) + mLatchTime_ns(latchTime_ns) { udpSocket = new QUdpSocket(); QString str = QString::fromStdString(mDeviceName); @@ -38,38 +37,26 @@ LedUdpDevice::LedUdpDevice(const std::string& outputDevice, const unsigned baudr LedUdpDevice::~LedUdpDevice() { -// close(mFid); + udpSocket->close(); } int LedUdpDevice::open() { - udpSocket->bind(QHostAddress::Any, 7755); + QHostAddress _localAddress = QHostAddress::Any; + quint16 _localPort = 0; - -/* - if (mFid < 0) - { - std::cerr << "Failed to open device('" << mDeviceName << "') " << std::endl; - return -1; - } -*/ + WarningIf( !udpSocket->bind(_localAddress, _localPort), + Logger::getInstance("LedDevice"), "LedUdpDevice: Couldnt bind local address: %s", strerror(errno)); return 0; } int LedUdpDevice::writeBytes(const unsigned size, const uint8_t * data) { -/* - if (mFid < 0) - { - return -1; - } -*/ -// int retVal = udpSocket->writeDatagram((const char *)data,size,QHostAddress::LocalHost,9998); - int retVal = udpSocket->writeDatagram((const char *)data,size,_address,_port); + qint64 retVal = udpSocket->writeDatagram((const char *)data,size,_address,_port); - if (retVal == 0 && mLatchTime_ns > 0) + if (retVal >= 0 && mLatchTime_ns > 0) { // The 'latch' time for latching the shifted-value into the leds timespec latchTime; @@ -78,6 +65,8 @@ int LedUdpDevice::writeBytes(const unsigned size, const uint8_t * data) // Sleep to latch the leds (only if write succesfull) nanosleep(&latchTime, NULL); + } else { + Warning( Logger::getInstance("LedDevice"), "LedUdpDevice: Error sending: %s", strerror(errno)); } return retVal;