Permalink
Browse files

Merge branch 'master' of github.com:kipr/botui

  • Loading branch information...
2 parents 7dd22f4 + deede96 commit 92a871593d01c2a7bfe86f1102f1c1a6c33de84c @bmcdorman bmcdorman committed Mar 20, 2013
@@ -0,0 +1,63 @@
+#ifndef _CAMERAINPUTMANAGER_H_
+#define _CAMERAINPUTMANAGER_H_
+
+#include <QObject>
+#include <QTimer>
+#include "Singleton.h"
+#include <kovan/camera.hpp>
+
+class CameraInputManager : public QObject, public Singleton<CameraInputManager>
+{
+Q_OBJECT
+
+public:
+ CameraInputManager();
+
+ void setInputProvider(Camera::InputProvider *inputProvider);
+ Camera::InputProvider *inputProvider() const;
+
+ void setFrameRate(int frameRate);
+ int frameRate() const;
+
+ void setWidth(const unsigned width);
+ void setHeight(const unsigned height);
+
+ bool retain();
+ bool release();
+
+ bool isOpen() const;
+
+ cv::Mat image() const;
+
+signals:
+ void frameAvailable(const cv::Mat &image);
+
+public slots:
+ void updateCamera();
+
+private:
+ Camera::InputProvider *m_inputProvider;
+ QTimer *m_timer;
+ cv::Mat m_image;
+ int m_frameRate;
+ int m_refs;
+};
+
+class CameraInputAdapter : public Camera::InputProvider
+{
+public:
+ CameraInputAdapter(CameraInputManager *manager);
+ ~CameraInputAdapter();
+
+ virtual bool open(const int number);
+ virtual bool isOpen() const;
+ virtual void setWidth(const unsigned width);
+ virtual void setHeight(const unsigned height);
+ virtual bool next(cv::Mat &image);
+ virtual bool close();
+
+private:
+ CameraInputManager *m_manager;
+};
+
+#endif
@@ -23,16 +23,12 @@ Q_OBJECT
~CameraWidget();
public slots:
- void updateCamera();
+ void updateImage();
void currentIndexChanged(const int &index);
private:
- void setUpdateFast();
- void setUpdateSlow();
-
Ui::CameraWidget *ui;
Camera::Device *m_device;
- QTimer *m_timer;
ChannelConfigurationsModel *m_model;
};
@@ -0,0 +1,39 @@
+#ifndef _CHANNELSETTINGSWIDGET_H_
+#define _CHANNELSETTINGSWIDGET_H_
+
+#include "StandardWidget.h"
+#include <kovan/config.hpp>
+
+#include <QModelIndex>
+
+namespace Ui
+{
+ class ChannelSettingsWidget;
+}
+
+class ChannelSettingsWidget : public StandardWidget
+{
+Q_OBJECT
+public:
+ ChannelSettingsWidget(Device *device, QWidget *parent = 0);
+ ~ChannelSettingsWidget();
+
+ Q_PROPERTY(Config config READ config WRITE setConfig);
+ void setConfig(const Config &config);
+ const Config &config() const;
+
+ Q_PROPERTY(QModelIndex index READ index WRITE setIndex);
+ void setIndex(const QModelIndex &index);
+ const QModelIndex &index() const;
+
+signals:
+ void configChanged(const QModelIndex &index, const Config &config);
+
+private:
+ Ui::ChannelSettingsWidget *ui;
+ Config m_config;
+ QModelIndex m_index;
+};
+
+
+#endif
@@ -23,7 +23,8 @@ Q_OBJECT
const QString &file() const;
private slots:
- void edit();
+ void configure();
+ void options();
void up();
void down();
void add();
@@ -0,0 +1,142 @@
+#include "CameraInputManager.h"
+#include <QDebug>
+
+CameraInputManager::CameraInputManager()
+ : m_timer(new QTimer(this)),
+ m_frameRate(1),
+ m_refs(0)
+{
+ m_image = cv::Mat();
+ connect(m_timer, SIGNAL(timeout()), SLOT(updateCamera()));
+}
+
+void CameraInputManager::setInputProvider(Camera::InputProvider *inputProvider)
+{
+ delete m_inputProvider;
+ m_inputProvider = inputProvider;
+}
+
+Camera::InputProvider *CameraInputManager::inputProvider() const
+{
+ return m_inputProvider;
+}
+
+void CameraInputManager::setFrameRate(int frameRate)
+{
+ m_frameRate = frameRate;
+ m_timer->start(1000/frameRate);
+}
+
+int CameraInputManager::frameRate() const
+{
+ return m_frameRate;
+}
+
+void CameraInputManager::setWidth(const unsigned width)
+{
+ m_inputProvider->setWidth(width);
+}
+
+void CameraInputManager::setHeight(const unsigned height)
+{
+ m_inputProvider->setHeight(height);
+}
+
+bool CameraInputManager::retain()
+{
+ ++m_refs;
+ updateCamera();
+
+ return true;
+}
+
+bool CameraInputManager::release()
+{
+ --m_refs;
+ if(m_refs < 0) {
+ qWarning() << "Release called when reference count was 0!";
+ m_refs = 0;
+ return false;
+ }
+ if(m_refs == 0)
+ return m_inputProvider->close();
+
+ return true;
+}
+
+bool CameraInputManager::isOpen() const
+{
+ return m_inputProvider->isOpen();
+}
+
+cv::Mat CameraInputManager::image() const
+{
+ return m_image;
+}
+
+void CameraInputManager::updateCamera()
+{
+ if(m_refs > 0 && !m_inputProvider->isOpen()) {
+ if(!m_inputProvider->open(0)) {
+ setFrameRate(1);
+ return;
+ }
+ //m_inputProvider->setWidth(320);
+ //m_inputProvider->setHeight(240);
+ setFrameRate(20);
+ }
+
+ if(!m_inputProvider->next(m_image)) {
+ qWarning() << "Camera update failed";
+ //ui->camera->setInvalid(true);
+ m_inputProvider->close();
+ setFrameRate(1);
+ m_image = cv::Mat();
+ }
+
+ emit frameAvailable(m_image);
+}
+
+CameraInputAdapter::CameraInputAdapter(CameraInputManager *manager)
+ : m_manager(manager)
+{
+}
+
+CameraInputAdapter::~CameraInputAdapter()
+{
+}
+
+
+bool CameraInputAdapter::open(const int number)
+{
+ m_manager->retain();
+
+ return true;
+}
+
+bool CameraInputAdapter::isOpen() const
+{
+ return m_manager->isOpen();
+}
+
+void CameraInputAdapter::setWidth(const unsigned width)
+{
+ m_manager->setWidth(width);
+}
+
+void CameraInputAdapter::setHeight(const unsigned height)
+{
+ m_manager->setHeight(height);
+}
+
+bool CameraInputAdapter::next(cv::Mat &image)
+{
+ if(!m_manager->isOpen()) return false;
+ image = m_manager->image();
+ return true;
+}
+
+bool CameraInputAdapter::close()
+{
+ return m_manager->release();
+}
View
@@ -1,21 +1,16 @@
#include "CameraWidget.h"
#include "ui_CameraWidget.h"
-#include "MenuBar.h"
-#include "RootController.h"
-#include "StatusBar.h"
#include "Device.h"
#include "ChannelConfigurationsModel.h"
+#include "CameraInputManager.h"
#include <QDebug>
-#include <QTimer>
-#include <QCoreApplication>
#include <kovan/camera.hpp>
CameraWidget::CameraWidget(Device *device, QWidget *parent)
: StandardWidget(device, parent),
ui(new Ui::CameraWidget),
- m_device(new Camera::Device),
- m_timer(new QTimer(this)),
+ m_device(new Camera::Device(new CameraInputAdapter(&CameraInputManager::ref()))),
m_model(new ChannelConfigurationsModel(this))
{
ui->setupUi(this);
@@ -29,8 +24,10 @@ CameraWidget::CameraWidget(Device *device, QWidget *parent)
connect(ui->config, SIGNAL(currentIndexChanged(int)), SLOT(currentIndexChanged(int)));
- connect(m_timer, SIGNAL(timeout()), SLOT(updateCamera()));
- updateCamera();
+ CameraInputManager::ref().setInputProvider(new Camera::UsbInputProvider);
+ connect(&CameraInputManager::ref(), SIGNAL(frameAvailable(cv::Mat)), SLOT(updateImage()));
+ m_device->open();
+ updateImage();
}
CameraWidget::~CameraWidget()
@@ -40,25 +37,13 @@ CameraWidget::~CameraWidget()
delete m_device;
}
-void CameraWidget::updateCamera()
+void CameraWidget::updateImage()
{
- if(!m_device->isOpen()) {
- if(!m_device->open()) {
- setUpdateSlow();
- return;
- }
- m_device->setWidth(320);
- m_device->setHeight(240);
- ui->camera->setInvalid(false);
- setUpdateFast();
- }
if(!m_device->update()) {
qWarning() << "camera update failed";
ui->camera->setInvalid(true);
- m_device->close();
- setUpdateSlow();
return;
- }
+ } else ui->camera->setInvalid(false);
int h = 0;
const static int hStep = 137; // Golden angle
@@ -76,6 +61,7 @@ void CameraWidget::updateCamera()
}
}
+ if(image.empty()) qWarning() << "EMPTY!";
ui->camera->updateImage(image);
}
@@ -87,13 +73,3 @@ void CameraWidget::currentIndexChanged(const int &index)
m_device->setConfig(conf ? *conf : Config());
delete conf;
}
-
-void CameraWidget::setUpdateFast()
-{
- m_timer->start(75); // 10 FPS
-}
-
-void CameraWidget::setUpdateSlow()
-{
- m_timer->start(1000);
-}
Oops, something went wrong.

0 comments on commit 92a8715

Please sign in to comment.