diff options
author | zheng_wenlong <wenlong_zheng@nexty-ele.com> | 2019-06-28 12:46:58 +0900 |
---|---|---|
committer | zheng_wenlong <wenlong_zheng@nexty-ele.com> | 2019-06-28 12:46:58 +0900 |
commit | cb26312c0d4b0f896cc883d43236427d9ae8e0cc (patch) | |
tree | 4b5b6296d4d34d8b45aaccf7fba3a8a05e490cf4 | |
parent | 5a815e80ae229d127d27cd4bc7b5cd9698042d64 (diff) |
fix bug can not subscribe at first time
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | app/app.pro | 12 | ||||
-rw-r--r-- | app/camera.cpp | 130 | ||||
-rw-r--r-- | app/camera.h | 67 | ||||
-rw-r--r-- | app/main.cpp | 3 | ||||
-rw-r--r-- | app/main.qml | 75 |
6 files changed, 32 insertions, 258 deletions
@@ -1,4 +1,3 @@ build *.pro.user -app/config.tests -.vscode
\ No newline at end of file +.vscode diff --git a/app/app.pro b/app/app.pro index d0b2961..130c74b 100644 --- a/app/app.pro +++ b/app/app.pro @@ -1,18 +1,8 @@ TARGET = tachometer QT = quickcontrols2 -equals(QT_ARCH, "arm") { - QMAKE_CXXFLAGS += -mfp16-format=ieee -} - -HEADERS += \ - camera.h - SOURCES += \ - main.cpp \ - camera.cpp - -LIBS += -lopencv_core -lopencv_highgui -lopencv_videoio + main.cpp RESOURCES += \ main.qrc \ diff --git a/app/camera.cpp b/app/camera.cpp deleted file mode 100644 index f935f22..0000000 --- a/app/camera.cpp +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Copyright (c) 2017 TOYOTA MOTOR CORPORATION - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "camera.h" -#include <QPainter> -#include <opencv2/imgproc.hpp> - -#include <linux/videodev2.h> -#include <fcntl.h> -#include <unistd.h> -#include <sys/ioctl.h> - -using namespace cv; - -Camera::Camera() { - running = false; - timer = new QTimer(this); - capture = new VideoCapture(); - - connect(timer, SIGNAL(timeout()), this, SLOT(grab())); -} - -Camera::~Camera() { - if (timer->isActive()) - timer->stop(); - if (capture && capture->isOpened()) - capture->release(); - delete timer; - delete capture; -} - -void Camera::paint(QPainter *painter) { - painter->drawImage(0, 0, image); -} - -void Camera::enumerateCameras() { - int maxID = 20; - for (int idx = 2; idx <maxID; idx++){ - std::stringstream no; - no << "/dev/video" << idx; - qDebug() << idx; - int fd = open(no.str().c_str(), O_RDWR); - if (fd != -1){ - struct v4l2_capability cap; - - if (ioctl(fd,VIDIOC_QUERYCAP,&cap) != -1){ - if ((cap.capabilities & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING)) == (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING)){ - qDebug()<< (char*)(cap.driver) << ">" << (char*)(cap.card) << ">" << (char*)(cap.bus_info);// << (cap.version>>16)&0xFF << (cap.version>>8)&0XFF << cap.version&0xFF; - cam_arr.push_back(idx); // vector of all available cameras - } - } - close(fd); - } - } - - camcnt = cam_arr.length(); -} - -void Camera::start(int no, int fps, QString res) { - int retryCount = 0; - while(!capture->open(no)){ - if(retryCount++==5){ - qDebug()<< "Try to open camera for 5 times failed, give up."; - return; - }else{ - qDebug()<< "open camera failed, retry " << retryCount; - usleep(200000); - } - } - - capture->set(CAP_PROP_FRAME_WIDTH, res.section("*", 0, 0).toInt()); - capture->set(CAP_PROP_FRAME_HEIGHT, res.section("*", 1, 1).toInt()); - - if (fps > 0){ - timer->start(1000/fps); - running = true; - emit isrunningChanged(running); - } -} - -void Camera::stop() { - if (timer->isActive()) - timer->stop(); - if (capture->isOpened()){ - qDebug()<< "release camera."; - capture->release(); - } - image = QImage(); - update(); - running = false; - emit isrunningChanged(running); -} - -QVariantList Camera::camranum() const{ - return cam_arr; -} - -int Camera::cameraCnt() { - return camcnt; -} - -bool Camera::isrunning() const{ - return running; -} - -void Camera::grab() { - if (capture && capture->isOpened()){ - Mat frame; - capture->read(frame); - - if (!frame.empty()){ - image = QImage((const uchar*)(frame.data), frame.cols, frame.rows, frame.step, QImage::Format_RGB888).rgbSwapped(); - image = image.scaled(this->width(), this->height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); - update(); - } - } -} diff --git a/app/camera.h b/app/camera.h deleted file mode 100644 index 11b5e82..0000000 --- a/app/camera.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2017 TOYOTA MOTOR CORPORATION - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef CAMERA_H -#define CAMERA_H - -#include <QSize> -#include <QTimer> -#include <QImage> -#include <QQuickPaintedItem> -#include <opencv2/videoio.hpp> -#include <iostream> -#include <opencv2/core.hpp> -#include <opencv2/imgcodecs.hpp> -#include <opencv2/highgui.hpp> - -using namespace std; - -static QVariantList cam_arr_bak; - -class Camera : public QQuickPaintedItem -{ - Q_OBJECT - Q_PROPERTY(bool isrunning READ isrunning NOTIFY isrunningChanged) -public: - Camera(); - ~Camera(); - void paint(QPainter *painter); - bool isrunning() const; - Q_INVOKABLE QVariantList camranum() const; - - Q_INVOKABLE void start(int no, int fps, QString res); - Q_INVOKABLE void stop(); - - Q_INVOKABLE void enumerateCameras(); - Q_INVOKABLE int cameraCnt(); - -signals: - void isrunningChanged(const bool& isrunning); - -public slots: - void grab(); - -private: - bool running; - QImage image; - QTimer* timer; - cv::VideoCapture* capture; - - QVariantList cam_arr; - int camcnt; -}; - -#endif // CAMERA_H diff --git a/app/main.cpp b/app/main.cpp index ab74471..1f422b5 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -24,7 +24,6 @@ #include <QtQml/QQmlApplicationEngine> #include <QtQuickControls2/QQuickStyle> #include <unistd.h> -#include "camera.h" #include <QQuickWindow> @@ -55,8 +54,6 @@ int main(int argc, char *argv[]) parser.process(app); QStringList positionalArguments = parser.positionalArguments(); - qmlRegisterType<Camera>("Camera", 1, 0, "Camera"); - QQmlApplicationEngine engine; QQmlContext *context = engine.rootContext(); QUrl bindingAddress; diff --git a/app/main.qml b/app/main.qml index be756b8..8fdc3b7 100644 --- a/app/main.qml +++ b/app/main.qml @@ -18,7 +18,6 @@ import QtQuick 2.6 import QtQuick.Layouts 1.3
import QtQuick.Controls 2.0
import QtQuick.Window 2.2
-import Camera 1.0
import QtWebSockets 1.0
ApplicationWindow {
@@ -50,6 +49,8 @@ ApplicationWindow { }else if (websocket.status === WebSocket.Open){
console.log ("Socket Open")
do_subscribe()
+ do_unsubscribe()
+ do_subscribe()
}else if (websocket.status === WebSocket.Closed){
console.log ("Socket closed")
}
@@ -57,7 +58,7 @@ ApplicationWindow { onTextMessageReceived: {
var message_json = JSON.parse(message);
-
+ console.log ("Get message: ", message)
if (message_json[0] === msgid_enu.event){
var propertyName = message_json[2].event.split("/")[1]
if (propertyName === "messages.vehicle.average.speed"){
@@ -191,23 +192,12 @@ ApplicationWindow { width: 320
height: 180
- Camera {
- id: camdev
- width: camarea.width
- height: camarea.height
- onIsrunningChanged: {
- camerabg_up.visible = !isrunning
- }
- Image {
- id: camerabg_up
- anchors.centerIn: parent
- width: 200
- height: 200
- source: "images/camera/camera_bg.svg"
- }
- }
- Component.onCompleted: {
- camdev.enumerateCameras();
+ Image {
+ id: camerabg_up
+ anchors.centerIn: parent
+ width: 200
+ height: 200
+ source: "images/camera/camera_bg.svg"
}
}
}
@@ -230,13 +220,8 @@ ApplicationWindow { }else{
do_subscribe()
}
- var number = camdev.camranum();
- console.log("tachometer visible is " + visible)
- camdev.start(number[0], "30", "640*480")
}else{
- do_subscribe()
- console.log("tachometer visible is " + visible)
- camdev.stop()
+ do_unsubscribe()
}
}
@@ -248,30 +233,30 @@ ApplicationWindow { function do_subscribe() {
do_call(api_str, "subscribe", "vehicle.average.speed")
do_call(api_str, "subscribe", "engine.speed")
- do_call(api_str, "subscribe", "fuel.level")
- do_call(api_str, "subscribe", "transmission.gearinfo")
- do_call(api_str, "subscribe", "transmission.mode")
- do_call(api_str, "subscribe", "DriveMode")
- do_call(api_str, "subscribe", "transmission.shift.gear")
- do_call(api_str, "subscribe", "Transmission.SiftPosition.neutral")
- do_call(api_str, "subscribe", "Transmission.SiftPosition.driving")
- do_call(api_str, "subscribe", "Transmission.SiftPosition.parking")
- do_call(api_str, "subscribe", "Transmission.SiftPosition.reverse")
- do_call(api_str, "subscribe", "Transmission.SiftPosition.B")
+ // do_call(api_str, "subscribe", "fuel.level")
+ // do_call(api_str, "subscribe", "transmission.gearinfo")
+ // do_call(api_str, "subscribe", "transmission.mode")
+ // do_call(api_str, "subscribe", "DriveMode")
+ // do_call(api_str, "subscribe", "transmission.shift.gear")
+ // do_call(api_str, "subscribe", "Transmission.SiftPosition.neutral")
+ // do_call(api_str, "subscribe", "Transmission.SiftPosition.driving")
+ // do_call(api_str, "subscribe", "Transmission.SiftPosition.parking")
+ // do_call(api_str, "subscribe", "Transmission.SiftPosition.reverse")
+ // do_call(api_str, "subscribe", "Transmission.SiftPosition.B")
}
function do_unsubscribe() {
do_call(api_str, "unsubscribe", "vehicle.average.speed")
do_call(api_str, "unsubscribe", "engine.speed")
- do_call(api_str, "unsubscribe", "fuel.level")
- do_call(api_str, "unsubscribe", "transmission.gearinfo")
- do_call(api_str, "unsubscribe", "transmission.mode")
- do_call(api_str, "unsubscribe", "DriveMode")
- do_call(api_str, "unsubscribe", "transmission.shift.gear")
- do_call(api_str, "unsubscribe", "Transmission.SiftPosition.neutral")
- do_call(api_str, "unsubscribe", "Transmission.SiftPosition.driving")
- do_call(api_str, "unsubscribe", "Transmission.SiftPosition.parking")
- do_call(api_str, "unsubscribe", "Transmission.SiftPosition.reverse")
- do_call(api_str, "unsubscribe", "Transmission.SiftPosition.B")
+ // do_call(api_str, "unsubscribe", "fuel.level")
+ // do_call(api_str, "unsubscribe", "transmission.gearinfo")
+ // do_call(api_str, "unsubscribe", "transmission.mode")
+ // do_call(api_str, "unsubscribe", "DriveMode")
+ // do_call(api_str, "unsubscribe", "transmission.shift.gear")
+ // do_call(api_str, "unsubscribe", "Transmission.SiftPosition.neutral")
+ // do_call(api_str, "unsubscribe", "Transmission.SiftPosition.driving")
+ // do_call(api_str, "unsubscribe", "Transmission.SiftPosition.parking")
+ // do_call(api_str, "unsubscribe", "Transmission.SiftPosition.reverse")
+ // do_call(api_str, "unsubscribe", "Transmission.SiftPosition.B")
}
}
|