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

Commit f624e4e

Browse files
committed
add support for tinkerforge
1 parent 919c677 commit f624e4e

File tree

7 files changed

+384
-7
lines changed

7 files changed

+384
-7
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ message(STATUS "ENABLE_SPIDEV = " ${ENABLE_SPIDEV})
1616
option(ENABLE_V4L2 "Enable the V4L2 grabber" ON)
1717
message(STATUS "ENABLE_V4L2 = " ${ENABLE_V4L2})
1818

19+
option(ENABLE_TINKERFORGE "Enable the TINKERFORGE device" OFF)
20+
message(STATUS "ENABLE_TINKERFORGE = " ${ENABLE_TINKERFORGE})
21+
1922
# Createt the configuration file
2023
# configure a header file to pass some of the CMake settings
2124
# to the source code
@@ -62,6 +65,10 @@ set(CMAKE_FIND_LIBRARY_SUFFIXES_OLD)
6265
find_package(libusb-1.0 REQUIRED)
6366
find_package(Threads REQUIRED)
6467

68+
if (ENABLE_TINKERFORGE)
69+
find_package(libtinkerforge-1.0 REQUIRED)
70+
endif (ENABLE_TINKERFORGE)
71+
6572
include(${QT_USE_FILE})
6673
add_definitions(${QT_DEFINITIONS})
6774
# TODO[TvdZ]: This linking directory should only be added if we are cross compiling

HyperionConfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@
88

99
// Define to enable the spi-device
1010
#cmakedefine ENABLE_SPIDEV
11+
12+
// Define to enable the spi-device
13+
#cmakedefine ENABLE_TINKERFORGE

cmake/Findlibtinkerforge-1.0.cmake

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# - Try to find libtinkerforge-1.0
2+
# Once done this will define
3+
#
4+
# LIBTINKERFORGE_1_FOUND - system has libtinkerforge
5+
# LIBTINKERFORGE_1_INCLUDE_DIRS - the libtinkerforge include directory
6+
# LIBTINKERFORGE_1_LIBRARIES - Link these to use libtinkerforge
7+
# LIBTINKERFORGE_1_DEFINITIONS - Compiler switches required for using libtinkerforge
8+
#
9+
# Adapted from cmake-modules Google Code project
10+
#
11+
# Copyright (c) 2006 Andreas Schneider <[email protected]>
12+
#
13+
# (Changes for libtinkerforge) Copyright (c) 2014 Björn Bilger <[email protected]>
14+
#
15+
# Redistribution and use is allowed according to the terms of the New BSD license.
16+
#
17+
# CMake-Modules Project New BSD License
18+
#
19+
# Redistribution and use in source and binary forms, with or without
20+
# modification, are permitted provided that the following conditions are met:
21+
#
22+
# * Redistributions of source code must retain the above copyright notice, this
23+
# list of conditions and the following disclaimer.
24+
#
25+
# * Redistributions in binary form must reproduce the above copyright notice,
26+
# this list of conditions and the following disclaimer in the
27+
# documentation and/or other materials provided with the distribution.
28+
#
29+
# * Neither the name of the CMake-Modules Project nor the names of its
30+
# contributors may be used to endorse or promote products derived from this
31+
# software without specific prior written permission.
32+
#
33+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
34+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35+
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
37+
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38+
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39+
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
40+
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42+
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43+
#
44+
45+
46+
if (LIBTINKERFORGE_1_LIBRARIES AND LIBTINKERFORGE_1_INCLUDE_DIRS)
47+
# in cache already
48+
set(LIBTINKERFORGE_FOUND TRUE)
49+
else (LIBTINKERFORGE_1_LIBRARIES AND LIBTINKERFORGE_1_INCLUDE_DIRS)
50+
find_path(LIBTINKERFORGE_1_INCLUDE_DIR
51+
NAMES
52+
tinkerforge/tinkerforge.h
53+
PATHS
54+
/usr/include
55+
/usr/local/include
56+
/opt/local/include
57+
/sw/include
58+
PATH_SUFFIXES
59+
tinkerforge-1.0
60+
)
61+
62+
find_library(LIBTINKERFORGE_1_LIBRARY
63+
NAMES
64+
tinkerforge-1.0
65+
PATHS
66+
/usr/lib
67+
/usr/local/lib
68+
/opt/local/lib
69+
/sw/lib
70+
)
71+
72+
set(LIBTINKERFORGE_1_INCLUDE_DIRS
73+
${LIBTINKERFORGE_1_INCLUDE_DIR}
74+
)
75+
set(LIBTINKERFORGE_1_LIBRARIES
76+
${LIBTINKERFORGE_1_LIBRARY}
77+
)
78+
79+
if (LIBTINKERFORGE_1_INCLUDE_DIRS AND LIBTINKERFORGE_1_LIBRARIES)
80+
set(LIBTINKERFORGE_1_FOUND TRUE)
81+
endif (LIBTINKERFORGE_1_INCLUDE_DIRS AND LIBTINKERFORGE_1_LIBRARIES)
82+
83+
if (LIBTINKERFORGE_1_FOUND)
84+
if (NOT libtinkerforge_1_FIND_QUIETLY)
85+
message(STATUS "Found libtinkerforge-1.0:")
86+
message(STATUS " - Includes: ${LIBTINKERFORGE_1_INCLUDE_DIRS}")
87+
message(STATUS " - Libraries: ${LIBTINKERFORGE_1_LIBRARIES}")
88+
endif (NOT libtinkerforge_1_FIND_QUIETLY)
89+
else (LIBTINKERFORGE_1_FOUND)
90+
message(FATAL_ERROR "Could not find libtinkerforge")
91+
endif (LIBTINKERFORGE_1_FOUND)
92+
93+
# show the LIBTINKERFORGE_1_INCLUDE_DIRS and LIBTINKERFORGE_1_LIBRARIES variables only in the advanced view
94+
mark_as_advanced(LIBTINKERFORGE_1_INCLUDE_DIRS LIBTINKERFORGE_1_LIBRARIES)
95+
96+
endif (LIBTINKERFORGE_1_LIBRARIES AND LIBTINKERFORGE_1_INCLUDE_DIRS)

libsrc/leddevice/CMakeLists.txt

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@ SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/leddevice)
66
#add libusb and pthreads (required for the Lighpack usb device)
77
find_package(libusb-1.0 REQUIRED)
88
find_package(Threads REQUIRED)
9+
if(ENABLE_TINKERFORGE)
10+
find_package(libtinkerforge-1.0 REQUIRED)
11+
endif(ENABLE_TINKERFORGE)
12+
913

1014
include_directories(
11-
../../include/hidapi
12-
${LIBUSB_1_INCLUDE_DIRS}) # for Lightpack device
15+
../../include/hidapi
16+
${LIBUSB_1_INCLUDE_DIRS}) # for Lightpack device
17+
18+
19+
if(ENABLE_TINKERFORGE)
20+
include_directories(
21+
${LIBTINKERFORGE_1_INCLUDE_DIRS}) # for Tinkerforge device
22+
endif(ENABLE_TINKERFORGE)
1323

1424
# Group the headers that go through the MOC compiler
1525
SET(Leddevice_QT_HEADERS
@@ -67,6 +77,17 @@ if(ENABLE_SPIDEV)
6777
)
6878
endif(ENABLE_SPIDEV)
6979

80+
if(ENABLE_TINKERFORGE)
81+
SET(Leddevice_HEADERS
82+
${Leddevice_HEADERS}
83+
${CURRENT_SOURCE_DIR}/LedDeviceTinkerforge.h
84+
)
85+
SET(Leddevice_SOURCES
86+
${Leddevice_SOURCES}
87+
${CURRENT_SOURCE_DIR}/LedDeviceTinkerforge.cpp
88+
)
89+
endif(ENABLE_TINKERFORGE)
90+
7091

7192
QT4_WRAP_CPP(Leddevice_HEADERS_MOC ${Leddevice_QT_HEADERS})
7293

@@ -78,12 +99,19 @@ add_library(leddevice
7899
)
79100

80101
target_link_libraries(leddevice
81-
hyperion-utils
82-
serialport
83-
${LIBUSB_1_LIBRARIES} #apt-get install libusb-1.0-0-dev
84-
${CMAKE_THREAD_LIBS_INIT}
85-
${QT_LIBRARIES}
102+
hyperion-utils
103+
serialport
104+
${LIBUSB_1_LIBRARIES} #apt-get install libusb-1.0-0-dev
105+
${CMAKE_THREAD_LIBS_INIT}
106+
${QT_LIBRARIES}
86107
)
108+
109+
if(ENABLE_TINKERFORGE)
110+
target_link_libraries(leddevice
111+
${LIBTINKERFORGE_1_LIBRARIES}
112+
)
113+
endif()
114+
87115
if(APPLE)
88116
target_link_libraries(leddevice hidapi-mac)
89117
else()

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#include "LedDeviceWs2801.h"
1717
#endif
1818

19+
#ifdef ENABLE_TINKERFORGE
20+
#include "LedDeviceTinkerforge.h"
21+
#endif
22+
1923
#include "LedDeviceAdalight.h"
2024
#include "LedDeviceLightpack.h"
2125
#include "LedDeviceMultiLightpack.h"
@@ -87,6 +91,22 @@ LedDevice * LedDeviceFactory::construct(const Json::Value & deviceConfig)
8791
device = deviceWs2801;
8892
}
8993
#endif
94+
95+
#ifdef ENABLE_TINKERFORGE
96+
else if (type=="tinkerforge")
97+
{
98+
const std::string host = deviceConfig.get("output", "127.0.0.1").asString();
99+
const uint16_t port = deviceConfig.get("port", 4223).asInt();
100+
const std::string uid = deviceConfig["uid"].asString();
101+
const unsigned rate = deviceConfig["rate"].asInt();
102+
103+
LedDeviceTinkerforge* deviceTinkerforge = new LedDeviceTinkerforge(host, port, uid, rate);
104+
deviceTinkerforge->open();
105+
106+
device = deviceTinkerforge;
107+
}
108+
#endif
109+
90110
// else if (type == "ws2811")
91111
// {
92112
// const std::string output = deviceConfig["output"].asString();
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
// STL includes
3+
#include <cerrno>
4+
#include <cstring>
5+
6+
// Local LedDevice includes
7+
#include "LedDeviceTinkerforge.h"
8+
9+
static const unsigned MAX_NUM_LEDS = 320;
10+
static const unsigned MAX_NUM_LEDS_SETTABLE = 16;
11+
12+
LedDeviceTinkerforge::LedDeviceTinkerforge(const std::string &host, uint16_t port, const std::string &uid, const unsigned interval) :
13+
LedDevice(),
14+
_host(host),
15+
_port(port),
16+
_uid(uid),
17+
_interval(interval),
18+
_ipConnection(nullptr),
19+
_ledStrip(nullptr),
20+
_colorChannelSize(0)
21+
{
22+
// empty
23+
}
24+
25+
LedDeviceTinkerforge::~LedDeviceTinkerforge()
26+
{
27+
// Close the device (if it is opened)
28+
if (_ipConnection != nullptr && _ledStrip != nullptr)
29+
{
30+
switchOff();
31+
}
32+
if (_ipConnection != nullptr)
33+
delete _ipConnection;
34+
if (_ledStrip != nullptr)
35+
delete _ledStrip;
36+
}
37+
38+
int LedDeviceTinkerforge::open()
39+
{
40+
_ipConnection = new IPConnection;
41+
ipcon_create(_ipConnection);
42+
43+
int connectionStatus = ipcon_connect(_ipConnection, _host.c_str(), _port);
44+
if (connectionStatus < 0)
45+
{
46+
std::cerr << "Attempt to connect to master brick (" << _host << ":" << _port << ") failed with status " << connectionStatus << std::endl;
47+
return -1;
48+
}
49+
50+
_ledStrip = new LEDStrip;
51+
led_strip_create(_ledStrip, _uid.c_str(), _ipConnection);
52+
53+
int frameStatus = led_strip_set_frame_duration(_ledStrip, _interval);
54+
if (frameStatus < 0)
55+
{
56+
std::cerr << "Attempt to connect to led strip bricklet (led_strip_set_frame_duration()) failed with status " << frameStatus << std::endl;
57+
return -1;
58+
}
59+
60+
return 0;
61+
}
62+
63+
int LedDeviceTinkerforge::write(const std::vector<ColorRgb> &ledValues)
64+
{
65+
std::cerr << "Write" << std::endl;
66+
67+
unsigned nrLedValues = ledValues.size();
68+
69+
if (nrLedValues > MAX_NUM_LEDS)
70+
{
71+
std::cerr << "Invalid attempt to write led values. Not more than " << MAX_NUM_LEDS << " leds are allowed." << std::endl;
72+
return -1;
73+
}
74+
75+
if (_colorChannelSize < nrLedValues)
76+
{
77+
_redChannel.resize(nrLedValues, uint8_t(0));
78+
_greenChannel.resize(nrLedValues, uint8_t(0));
79+
_blueChannel.resize(nrLedValues, uint8_t(0));
80+
}
81+
_colorChannelSize = nrLedValues;
82+
83+
auto redIt = _redChannel.begin();
84+
auto greenIt = _greenChannel.begin();
85+
auto blueIt = _blueChannel.begin();
86+
87+
for (const ColorRgb &ledValue : ledValues)
88+
{
89+
*redIt = ledValue.red;
90+
redIt++;
91+
*greenIt = ledValue.green;
92+
greenIt++;
93+
*blueIt = ledValue.blue;
94+
blueIt++;
95+
}
96+
97+
return transferLedData(_ledStrip, 0, _colorChannelSize, &_redChannel[0], &_greenChannel[0], &_blueChannel[0]);
98+
}
99+
100+
int LedDeviceTinkerforge::switchOff()
101+
{
102+
std::cerr << "Switchoff" << std::endl;
103+
std::fill(_redChannel.begin(), _redChannel.end(), 0);
104+
std::fill(_greenChannel.begin(), _greenChannel.end(), 0);
105+
std::fill(_blueChannel.begin(), _blueChannel.end(), 0);
106+
107+
return transferLedData(_ledStrip, 0, _colorChannelSize, &_redChannel[0], &_greenChannel[0], &_blueChannel[0]);
108+
}
109+
110+
int LedDeviceTinkerforge::transferLedData(LEDStrip *ledStrip, unsigned index, unsigned length, uint8_t *redChannel, uint8_t *greenChannel, uint8_t *blueChannel)
111+
{
112+
// we need that array size no matter how many leds will really be set
113+
uint8_t _reds[MAX_NUM_LEDS_SETTABLE];
114+
uint8_t _greens[MAX_NUM_LEDS_SETTABLE];
115+
uint8_t _blues[MAX_NUM_LEDS_SETTABLE];
116+
117+
int status = E_INVALID_PARAMETER;
118+
unsigned i;
119+
unsigned int copyLength;
120+
121+
if (index >= 0 && length > 0 && index < length && length <= MAX_NUM_LEDS)
122+
{
123+
for (i = index; i < length; i += MAX_NUM_LEDS_SETTABLE)
124+
{
125+
copyLength = (i + MAX_NUM_LEDS_SETTABLE > length) ? length - i : MAX_NUM_LEDS_SETTABLE;
126+
127+
memcpy(_reds, redChannel + i, copyLength * sizeof(uint8_t));
128+
memcpy(_greens, greenChannel + i, copyLength * sizeof(uint8_t));
129+
memcpy(_blues, blueChannel + i, copyLength * sizeof(uint8_t));
130+
131+
status = led_strip_set_rgb_values(ledStrip, i, copyLength, _reds, _greens, _blues);
132+
133+
if (status != E_OK)
134+
{
135+
std::cerr << "Setting led values failed with status " << status << std::endl;
136+
break;
137+
}
138+
}
139+
}
140+
return status;
141+
}

0 commit comments

Comments
 (0)