From 99fd50805cc7b326aae421f5c2bebdd6d48deae8 Mon Sep 17 00:00:00 2001 From: johan Date: Sun, 23 Feb 2014 22:39:23 +0100 Subject: [PATCH] Added image handler to v4l2 grabber to send colors to Hyperion Former-commit-id: be6fb4dd8080b3325ba6161f48c093f8a145786d --- include/grabber/DispmanxWrapper.h | 2 +- include/grabber/V4L2Wrapper.h | 59 +++++++++++++++++++++++++ libsrc/grabber/v4l2/CMakeLists.txt | 2 + libsrc/grabber/v4l2/V4L2Wrapper.cpp | 67 +++++++++++++++++++++++++++++ src/hyperiond/hyperiond.cpp | 12 +++--- 5 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 include/grabber/V4L2Wrapper.h create mode 100644 libsrc/grabber/v4l2/V4L2Wrapper.cpp diff --git a/include/grabber/DispmanxWrapper.h b/include/grabber/DispmanxWrapper.h index f0156740..4868c1a0 100644 --- a/include/grabber/DispmanxWrapper.h +++ b/include/grabber/DispmanxWrapper.h @@ -73,7 +73,7 @@ private: const int _updateInterval_ms; /// The timeout of the led colors [ms] const int _timeout_ms; - /// The priority of the led colors [ms] + /// The priority of the led colors const int _priority; /// The timer for generating events with the specified update rate diff --git a/include/grabber/V4L2Wrapper.h b/include/grabber/V4L2Wrapper.h new file mode 100644 index 00000000..39a38d7c --- /dev/null +++ b/include/grabber/V4L2Wrapper.h @@ -0,0 +1,59 @@ +#pragma once + +// Hyperion includes +#include +#include + +// Grabber includes +#include + +class V4L2Wrapper : public QObject +{ + Q_OBJECT + +public: + V4L2Wrapper(const std::string & device, + int input, + VideoStandard videoStandard, + int width, + int height, + int frameDecimation, + int pixelDecimation, + Hyperion * hyperion, + int hyperionPriority); + virtual ~V4L2Wrapper(); + +public slots: + void start(); + + void stop(); + + void setCropping(int cropLeft, + int cropRight, + int cropTop, + int cropBottom); + + void set3D(VideoMode mode); + +private slots: + void newFrame(const Image & image); + +private: + /// The timeout of the led colors [ms] + const int _timeout_ms; + + /// The priority of the led colors + const int _priority; + + /// The V4L2 grabber + V4L2Grabber _grabber; + + /// The processor for transforming images to led colors + ImageProcessor * _processor; + + /// The Hyperion instance + Hyperion * _hyperion; + + /// The list with computed led colors + std::vector _ledColors; +}; diff --git a/libsrc/grabber/v4l2/CMakeLists.txt b/libsrc/grabber/v4l2/CMakeLists.txt index aaafd384..5c7844e0 100644 --- a/libsrc/grabber/v4l2/CMakeLists.txt +++ b/libsrc/grabber/v4l2/CMakeLists.txt @@ -4,6 +4,7 @@ SET(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/grabber/v4l2) SET(V4L2_QT_HEADERS ${CURRENT_HEADER_DIR}/V4L2Grabber.h + ${CURRENT_HEADER_DIR}/V4L2Wrapper.h ) SET(V4L2_HEADERS @@ -12,6 +13,7 @@ SET(V4L2_HEADERS SET(V4L2_SOURCES ${CURRENT_SOURCE_DIR}/V4L2Grabber.cpp + ${CURRENT_SOURCE_DIR}/V4L2Wrapper.cpp ) QT4_WRAP_CPP(V4L2_HEADERS_MOC ${V4L2_QT_HEADERS}) diff --git a/libsrc/grabber/v4l2/V4L2Wrapper.cpp b/libsrc/grabber/v4l2/V4L2Wrapper.cpp new file mode 100644 index 00000000..e9745ad3 --- /dev/null +++ b/libsrc/grabber/v4l2/V4L2Wrapper.cpp @@ -0,0 +1,67 @@ +#include + +#include + +V4L2Wrapper::V4L2Wrapper(const std::string &device, + int input, + VideoStandard videoStandard, + int width, + int height, + int frameDecimation, + int pixelDecimation, + Hyperion *hyperion, + int hyperionPriority) : + _timeout_ms(1000), + _priority(hyperionPriority), + _grabber(device, + input, + videoStandard, + width, + height, + frameDecimation, + pixelDecimation, + pixelDecimation), + _processor(ImageProcessorFactory::getInstance().newImageProcessor()), + _hyperion(hyperion), + _ledColors(hyperion->getLedCount(), ColorRgb{0,0,0}) +{ + // connect the new frame signal using a queued connection, because it will be called from a different thread + QObject::connect(&_grabber, SIGNAL(newFrame(Image)), this, SLOT(newFrame(Image)), Qt::QueuedConnection); +} + +V4L2Wrapper::~V4L2Wrapper() +{ + delete _processor; +} + +void V4L2Wrapper::start() +{ + _grabber.start(); +} + +void V4L2Wrapper::stop() +{ + _grabber.stop(); +} + +void V4L2Wrapper::setCropping(int cropLeft, int cropRight, int cropTop, int cropBottom) +{ + _grabber.setCropping(cropLeft, cropRight, cropTop, cropBottom); +} + +void V4L2Wrapper::set3D(VideoMode mode) +{ + _grabber.set3D(mode); +} + +void V4L2Wrapper::newFrame(const Image &image) +{ + // TODO: add a signal detector + + // process the new image + _processor->process(image, _ledColors); + + // send colors to Hyperion + _hyperion->setColors(_priority, _ledColors, _timeout_ms); +} + diff --git a/src/hyperiond/hyperiond.cpp b/src/hyperiond/hyperiond.cpp index 4541ca20..71b01c0b 100644 --- a/src/hyperiond/hyperiond.cpp +++ b/src/hyperiond/hyperiond.cpp @@ -24,7 +24,7 @@ #ifdef ENABLE_V4L2 // v4l2 grabber -#include +#include #endif // XBMC Video checker includes @@ -172,19 +172,20 @@ int main(int argc, char** argv) #ifdef ENABLE_V4L2 // construct and start the v4l2 grabber if the configuration is present - V4L2Grabber * v4l2Grabber = nullptr; + V4L2Wrapper * v4l2Grabber = nullptr; if (config.isMember("grabber-v4l2")) { const Json::Value & grabberConfig = config["grabber-v4l2"]; - v4l2Grabber = new V4L2Grabber( + v4l2Grabber = new V4L2Wrapper( grabberConfig.get("device", "/dev/video0").asString(), grabberConfig.get("input", 0).asInt(), - parseVideoStandard(grabberConfig.get("standard", "NONE").asString()), + parseVideoStandard(grabberConfig.get("standard", "no-change").asString()), grabberConfig.get("width", -1).asInt(), grabberConfig.get("height", -1).asInt(), grabberConfig.get("frameDecimation", 2).asInt(), grabberConfig.get("sizeDecimation", 8).asInt(), - grabberConfig.get("sizeDecimation", 8).asInt()); + &hyperion, + grabberConfig.get("priority", 800).asInt()); v4l2Grabber->set3D(parse3DMode(grabberConfig.get("mode", "2D").asString())); v4l2Grabber->setCropping( grabberConfig.get("cropLeft", 0).asInt(), @@ -192,7 +193,6 @@ int main(int argc, char** argv) grabberConfig.get("cropTop", 0).asInt(), grabberConfig.get("cropBottom", 0).asInt()); - // TODO: create handler v4l2Grabber->start(); std::cout << "V4l2 grabber created and started" << std::endl; }