From 4968a118164298d686ed9d6d779a1d75a4e26fa2 Mon Sep 17 00:00:00 2001 From: penfold42 Date: Sun, 4 Sep 2016 17:07:10 +1000 Subject: [PATCH] Tpm2net rewrite (#217) * initial tpm2net rewrite - doesnt compile yet * new tpm2.net drive compiles. rename old tmp2net typos to tpm2net fixed compiler warning in ws2812b * added packetsize calculations and the end of frame character. Output looks good on tcpdump * removed old tpm2net driver renamed tpm2netnew to tpm2net --- libsrc/leddevice/LedDeviceFactory.cpp | 2 +- libsrc/leddevice/LedDeviceSchemas.qrc | 2 +- libsrc/leddevice/LedDeviceTpm2net.cpp | 88 ++++++++++++------- libsrc/leddevice/LedDeviceTpm2net.h | 34 ++++--- libsrc/leddevice/LedDeviceWS2812b.h | 2 +- ...chema-tmp2net.json => schema-tpm2net.json} | 0 6 files changed, 75 insertions(+), 53 deletions(-) rename libsrc/leddevice/schemas/{schema-tmp2net.json => schema-tpm2net.json} (100%) diff --git a/libsrc/leddevice/LedDeviceFactory.cpp b/libsrc/leddevice/LedDeviceFactory.cpp index b4baa439..5a6984e6 100755 --- a/libsrc/leddevice/LedDeviceFactory.cpp +++ b/libsrc/leddevice/LedDeviceFactory.cpp @@ -93,7 +93,7 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig) #endif // network lights - LedDevice::addToDeviceMap("tmp2net", LedDeviceTpm2net::construct); + LedDevice::addToDeviceMap("tpm2net", LedDeviceTpm2net::construct); LedDevice::addToDeviceMap("udpraw", LedDeviceUdpRaw::construct); LedDevice::addToDeviceMap("e131", LedDeviceUdpE131::construct); #ifdef ENABLE_TINKERFORGE diff --git a/libsrc/leddevice/LedDeviceSchemas.qrc b/libsrc/leddevice/LedDeviceSchemas.qrc index 4e7f956d..f2b16b5b 100644 --- a/libsrc/leddevice/LedDeviceSchemas.qrc +++ b/libsrc/leddevice/LedDeviceSchemas.qrc @@ -21,7 +21,7 @@ schemas/schema-sedu.json schemas/schema-sk6812rgbw-spi.json schemas/schema-tinkerforge.json - schemas/schema-tmp2net.json + schemas/schema-tpm2net.json schemas/schema-tpm2.json schemas/schema-udpraw.json schemas/schema-ws2801.json diff --git a/libsrc/leddevice/LedDeviceTpm2net.cpp b/libsrc/leddevice/LedDeviceTpm2net.cpp index 5075f188..29c7a47b 100644 --- a/libsrc/leddevice/LedDeviceTpm2net.cpp +++ b/libsrc/leddevice/LedDeviceTpm2net.cpp @@ -1,29 +1,32 @@ + // STL includes #include #include #include -#include -// Local-Hyperion includes +// Linux includes +#include +#include +#include + +#include +#include + +// hyperion local includes #include "LedDeviceTpm2net.h" - LedDeviceTpm2net::LedDeviceTpm2net(const Json::Value &deviceConfig) - : LedDevice() - , _socket(this) + : ProviderUdp(deviceConfig) + { setConfig(deviceConfig); } -LedDeviceTpm2net::~LedDeviceTpm2net() -{ -} - bool LedDeviceTpm2net::setConfig(const Json::Value &deviceConfig) { - _host = QHostAddress(QString::fromStdString(deviceConfig.get("output", "127.0.0.1").asString())); - _port = deviceConfig.get("port", 65506).asInt(); - + ProviderUdp::setConfig(deviceConfig); + _LatchTime_ns = deviceConfig.get("latchtime",104000).asInt(); + _tpm2_max = deviceConfig.get("max-packet",170).asInt(); return true; } @@ -32,32 +35,53 @@ LedDevice* LedDeviceTpm2net::construct(const Json::Value &deviceConfig) return new LedDeviceTpm2net(deviceConfig); } -int LedDeviceTpm2net::write(const std::vector & ledValues) + +// populates the headers + +int LedDeviceTpm2net::write(const std::vector &ledValues) { - if (_ledBuffer.size() == 0) + uint8_t * _tpm2_buffer = (uint8_t*) malloc(_tpm2_max+7); + + int retVal = 0; + + _ledCount = ledValues.size(); + _tpm2ByteCount = 3 * _ledCount; + _tpm2TotalPackets = 1 + _tpm2ByteCount / _tpm2_max; + + int _thisPacketBytes = 0; + _tpm2ThisPacket = 1; + + const uint8_t * rawdata = reinterpret_cast(ledValues.data()); + + for (int rawIdx = 0; rawIdx < _tpm2ByteCount; rawIdx++) { - _ledBuffer.resize(7 + 3*ledValues.size()); - _ledBuffer[0] = 0x9c; // block-start byte TPM.NET - _ledBuffer[1] = 0xDA; - _ledBuffer[2] = ((3 * ledValues.size()) >> 8) & 0xFF; // frame size high byte - _ledBuffer[3] = (3 * ledValues.size()) & 0xFF; // frame size low byte - _ledBuffer[4] = 1; // packets number - _ledBuffer[5] = 1; // Number of packets - _ledBuffer[(int)(7 + 3*ledValues.size()-1)] = 0x36; // block-end byte + if (rawIdx % _tpm2_max == 0) // start of new packet + { + _thisPacketBytes = (_tpm2ByteCount - rawIdx < _tpm2_max) ? _tpm2ByteCount % _tpm2_max : _tpm2_max; +// is this the last packet? ? ^^ last packet : ^^ earlier packets + + _tpm2_buffer[0] = 0x9c; // Packet start byte + _tpm2_buffer[1] = 0xda; // Packet type Data frame + _tpm2_buffer[2] = (_thisPacketBytes >> 8) & 0xff; // Frame size high + _tpm2_buffer[3] = _thisPacketBytes & 0xff; // Frame size low + _tpm2_buffer[4] = _tpm2ThisPacket++; // Packet Number + _tpm2_buffer[5] = _tpm2TotalPackets; // Number of packets + } + + _tpm2_buffer [6 + rawIdx%_tpm2_max] = rawdata[rawIdx]; + +// is this the last byte of last packet || last byte of other packets + if ( (rawIdx == _tpm2ByteCount-1) || (rawIdx %_tpm2_max == _tpm2_max-1) ) + { + _tpm2_buffer [6 + rawIdx%_tpm2_max +1] = 0x36; // Packet end byte + retVal &= writeBytes(_thisPacketBytes+7, _tpm2_buffer); + } } - // write data - memcpy(6 + _ledBuffer.data(), ledValues.data() /*Max 1,490 bytes*/, ledValues.size() * 3); - - _socket.connectToHost(_host, _port); - _socket.write((const char *)_ledBuffer.data()); - _socket.close(); - - return 0; + return retVal; } int LedDeviceTpm2net::switchOff() { - memset(6 + _ledBuffer.data(), 0, _ledBuffer.size() - 5); - return 0; + return write(std::vector(_ledCount, ColorRgb{0,0,0})); } diff --git a/libsrc/leddevice/LedDeviceTpm2net.h b/libsrc/leddevice/LedDeviceTpm2net.h index 1f3ec05f..2a6e7a76 100644 --- a/libsrc/leddevice/LedDeviceTpm2net.h +++ b/libsrc/leddevice/LedDeviceTpm2net.h @@ -1,15 +1,17 @@ #pragma once // STL includes -#include #include -#include -#include -// Leddevice includes -#include + +// hyperion includes +#include "ProviderUdp.h" + +#define TPM2_DEFAULT_PORT 65506 /// -class LedDeviceTpm2net : public LedDevice +/// Implementation of the LedDevice interface for sending led colors via udp/E1.31 packets +/// +class LedDeviceTpm2net : public ProviderUdp { public: /// @@ -19,8 +21,6 @@ public: /// LedDeviceTpm2net(const Json::Value &deviceConfig); - virtual ~LedDeviceTpm2net(); - /// /// Sets configuration /// @@ -31,23 +31,21 @@ public: /// constructs leddevice static LedDevice* construct(const Json::Value &deviceConfig); + /// - /// Writes the given led-color values to the output stream + /// Writes the led color values to the led-device /// /// @param ledValues The color-value per led + /// @return Zero on succes else negative /// - /// @return Zero on success else negative - /// - virtual int write(const std::vector & ledValues); + virtual int write(const std::vector &ledValues); /// Switch the leds off virtual int switchOff(); private: - /// The host of the master brick - QHostAddress _host; - - /// The port of the master brick - uint16_t _port; - QUdpSocket _socket; + int _tpm2_max; + int _tpm2ByteCount; + int _tpm2TotalPackets; + int _tpm2ThisPacket; }; diff --git a/libsrc/leddevice/LedDeviceWS2812b.h b/libsrc/leddevice/LedDeviceWS2812b.h index 041a58e3..1d8b60ff 100644 --- a/libsrc/leddevice/LedDeviceWS2812b.h +++ b/libsrc/leddevice/LedDeviceWS2812b.h @@ -146,7 +146,7 @@ public: /// /// @param deviceConfig the json device config /// @return true if success - bool setConfig(const Json::Value&) {}; + bool setConfig(const Json::Value&) {return true;}; /// constructs leddevice static LedDevice* construct(const Json::Value &); diff --git a/libsrc/leddevice/schemas/schema-tmp2net.json b/libsrc/leddevice/schemas/schema-tpm2net.json similarity index 100% rename from libsrc/leddevice/schemas/schema-tmp2net.json rename to libsrc/leddevice/schemas/schema-tpm2net.json