From 0203f78a651fb37e456d1f59e9527263c98b164e Mon Sep 17 00:00:00 2001 From: yikestone Date: Fri, 6 Dec 2019 18:08:16 +0530 Subject: [PATCH] added blinking squares --- qt_pi/CMakeLists.txt | 48 ++++--- qt_pi/include/mapview/mapview.h | 60 ++++++++ qt_pi/include/mapview/vertex.h | 67 +++++++++ qt_pi/include/speller/speller.h | 18 +++ qt_pi/package.xml | 32 +++-- qt_pi/resources/resources.qrc | 6 + qt_pi/resources/shaders/simple.frag | 8 ++ qt_pi/resources/shaders/simple.vert | 10 ++ qt_pi/src/ble_connect.c | 2 +- qt_pi/src/main.cpp | 23 +++ qt_pi/src/mapview.cpp | 210 ++++++++++++++++++++++++++++ qt_pi/src/speller.cpp | 5 + qt_pi/tests/ble_test.cpp | 8 +- 13 files changed, 462 insertions(+), 35 deletions(-) create mode 100644 qt_pi/include/mapview/mapview.h create mode 100644 qt_pi/include/mapview/vertex.h create mode 100644 qt_pi/include/speller/speller.h create mode 100644 qt_pi/resources/resources.qrc create mode 100644 qt_pi/resources/shaders/simple.frag create mode 100644 qt_pi/resources/shaders/simple.vert create mode 100644 qt_pi/src/main.cpp create mode 100644 qt_pi/src/mapview.cpp create mode 100644 qt_pi/src/speller.cpp diff --git a/qt_pi/CMakeLists.txt b/qt_pi/CMakeLists.txt index 520a8dd..b5d24c9 100644 --- a/qt_pi/CMakeLists.txt +++ b/qt_pi/CMakeLists.txt @@ -3,7 +3,25 @@ project(qt_pi) set(CMAKE_CXX_FLAGS "-fpermissive -std=c++0x") -find_package(catkin REQUIRED) +find_package(catkin REQUIRED COMPONENTS +qt_build +roscpp +roslib +) + +find_package(Qt5 REQUIRED COMPONENTS Core Gui OpenGL) + + +#set(FORMS ui/speller.ui) +set(MOC include/mapview/mapview.h) +set(HPP include/speller/speller.h include/mapview/vertex.h) +set(SRCS src/speller.cpp src/mapview.cpp ) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +#qt5_wrap_ui(FORMS_HPP ${FORMS}) +qt5_wrap_cpp(MOC_HPP ${MOC}) +qt5_add_resources(RCC resources/resources.qrc) catkin_python_setup() catkin_package(DEPENDS) @@ -17,18 +35,8 @@ find_path(Mcrypt_INCLUDE_DIR mcrypt.h PATHS set(Mcrypt_LIB_PATHS /usr/lib) find_library(Mcrypt_LIBS NAMES mcrypt rtfilter PATHS ${Mcrypt_LIB_PATHS}) -include_directories(include) - -install(DIRECTORY config - DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} -) - -install(DIRECTORY launch - DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} -) - -install(DIRECTORY nodes - DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION} +include_directories(include +${catkin_INCLUDE_DIRS} ) pkg_search_module(GATTLIB REQUIRED gattlib) @@ -38,9 +46,17 @@ include_directories(${GLIB_INCLUDE_DIRS}) include_directories(${Mcrypt_INCLUDE_DIRS}) link_directories(${Mcrypt_LIBS}) -add_library(ble_connect_lib include/ble_connect/ble_connect.h src/ble_connect.c) +add_library(ble_connect include/ble_connect/ble_connect.h src/ble_connect.c) -target_link_libraries(ble_connect_lib ${GATTLIB_LIBRARIES} ${GATTLIB_LDFLAGS} ${GLIB_LDFLAGS} pthread ${Mcrypt_LIBS}) +target_link_libraries(ble_connect ${GATTLIB_LIBRARIES} ${GATTLIB_LDFLAGS} ${GLIB_LDFLAGS} pthread ${Mcrypt_LIBS}) add_executable(ble_connect_test tests/ble_test.cpp) -target_link_libraries(ble_connect_test ble_connect_lib) +target_link_libraries(ble_connect_test ble_connect) + + + +add_library(speller ${SRCS} ${FORMS_HPP} ${MOC_HPP} ${RCC} ${HPP}) +target_link_libraries(speller Qt5::Widgets Qt5::Core Qt5::OpenGL Qt5::Gui ${catkin_LIBRARIES}) + +add_executable(speller_ui src/main.cpp) +target_link_libraries(speller_ui speller) diff --git a/qt_pi/include/mapview/mapview.h b/qt_pi/include/mapview/mapview.h new file mode 100644 index 0000000..e338ee5 --- /dev/null +++ b/qt_pi/include/mapview/mapview.h @@ -0,0 +1,60 @@ +#ifndef MAPVIEW +#define MAPVIEW + +#include "vertex.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class QOpenGLShaderProgram; + +class MapView : public QOpenGLWindow, protected QOpenGLFunctions_3_1 { + Q_OBJECT +public: + explicit MapView(QWidget *parent = 0); + ~MapView(); + void start_blinking(); + void stop_blinking(); + volatile int sleep_control = 1; + +protected: + GLsizei count; + GLint startingElements[4] = {0, 4, 8, 12}; + GLsizei counts[4] = {4, 4, 4, 4}; + void initializeGL() override; + void paintGL() override; + void resizeGL(int, int) override; + void teardownGL(); + void printContextInformation(); +signals: + void update_GUI(); + +private: + QOpenGLBuffer m_vertex; + QOpenGLVertexArrayObject m_object; + QOpenGLShaderProgram *m_program; + void run(); + std::thread *t; + int blinking; + // QTimer *t1; + // QTimer *t2; + // QTimer *t3; + // QTimer *t4; + // int blinking = 0; + // int state = 0; + // private slots: + // void t1Slot(); + // void t2Slot(); + // void t3Slot(); + // void t4Slot(); +}; + +#endif diff --git a/qt_pi/include/mapview/vertex.h b/qt_pi/include/mapview/vertex.h new file mode 100644 index 0000000..9530a26 --- /dev/null +++ b/qt_pi/include/mapview/vertex.h @@ -0,0 +1,67 @@ +#ifndef VERTEX_H +#define VERTEX_H + +#include + +class Vertex { +public: + // Constructors + Q_DECL_CONSTEXPR Vertex(); + Q_DECL_CONSTEXPR explicit Vertex(const QVector3D &position); + Q_DECL_CONSTEXPR Vertex(const QVector3D &position, const QVector3D &color); + + // Accessors / Mutators + Q_DECL_CONSTEXPR const QVector3D &position() const; + Q_DECL_CONSTEXPR const QVector3D &color() const; + void setPosition(const QVector3D &position); + void setColor(const QVector3D &color); + + // OpenGL Helpers + static const int PositionTupleSize = 3; + static const int ColorTupleSize = 3; + static Q_DECL_CONSTEXPR int positionOffset(); + static Q_DECL_CONSTEXPR int colorOffset(); + static Q_DECL_CONSTEXPR int stride(); + +private: + QVector3D m_position; + QVector3D m_color; +}; + +/******************************************************************************* + * Inline Implementation + ******************************************************************************/ + +// Note: Q_MOVABLE_TYPE means it can be memcpy'd. +Q_DECLARE_TYPEINFO(Vertex, Q_MOVABLE_TYPE); + +// Constructors +Q_DECL_CONSTEXPR inline Vertex::Vertex() {} +Q_DECL_CONSTEXPR inline Vertex::Vertex(const QVector3D &position) + : m_position(position) {} +Q_DECL_CONSTEXPR inline Vertex::Vertex(const QVector3D &position, + const QVector3D &color) + : m_position(position), m_color(color) {} + +// Accessors / Mutators +Q_DECL_CONSTEXPR inline const QVector3D &Vertex::position() const { + return m_position; +} +Q_DECL_CONSTEXPR inline const QVector3D &Vertex::color() const { + return m_color; +} +void inline Vertex::setPosition(const QVector3D &position) { + m_position = position; +} +void inline Vertex::setColor(const QVector3D &color) { m_color = color; } + +// OpenGL Helpers +Q_DECL_CONSTEXPR inline int Vertex::positionOffset() { + return offsetof(Vertex, m_position); +} +Q_DECL_CONSTEXPR inline int Vertex::colorOffset() { + return offsetof(Vertex, m_color); +} +Q_DECL_CONSTEXPR inline int Vertex::stride() { return sizeof(Vertex); } + +#endif // VERTEX_H diff --git a/qt_pi/include/speller/speller.h b/qt_pi/include/speller/speller.h new file mode 100644 index 0000000..cdc5928 --- /dev/null +++ b/qt_pi/include/speller/speller.h @@ -0,0 +1,18 @@ +#ifndef SPELLER +#define SPELLER + +#include +#include + +class Speller { + +public: + explicit Speller(ros::NodeHandle _nh); + ~Speller(); + + MapView *window; + +private: + ros::NodeHandle nh; +}; +#endif diff --git a/qt_pi/package.xml b/qt_pi/package.xml index 407d1a7..17b5179 100644 --- a/qt_pi/package.xml +++ b/qt_pi/package.xml @@ -1,17 +1,19 @@ - qt_pi - 0.0.0 - The qt_pi package - Rishabh Kundu - MIT - catkin + qt_pi + 0.0.0 + The qt_pi package + Rishabh Kundu + MIT + catkin + qt_build + roscpp + roslib - rospy - std_msgs - geometry_msgs - nav_msgs - tf - python-serial - - - + rospy + std_msgs + geometry_msgs + nav_msgs + tf + python-serial + + \ No newline at end of file diff --git a/qt_pi/resources/resources.qrc b/qt_pi/resources/resources.qrc new file mode 100644 index 0000000..0248cfc --- /dev/null +++ b/qt_pi/resources/resources.qrc @@ -0,0 +1,6 @@ + + + shaders/simple.frag + shaders/simple.vert + + diff --git a/qt_pi/resources/shaders/simple.frag b/qt_pi/resources/shaders/simple.frag new file mode 100644 index 0000000..ed9c30e --- /dev/null +++ b/qt_pi/resources/shaders/simple.frag @@ -0,0 +1,8 @@ +#version 330 +in vec4 vColor; +out vec4 fColor; + +void main() +{ + fColor = vColor; +} diff --git a/qt_pi/resources/shaders/simple.vert b/qt_pi/resources/shaders/simple.vert new file mode 100644 index 0000000..10ded2e --- /dev/null +++ b/qt_pi/resources/shaders/simple.vert @@ -0,0 +1,10 @@ +#version 330 +layout(location = 0) in vec3 position; +layout(location = 1) in vec3 color; +out vec4 vColor; + +void main() +{ + gl_Position = vec4(position, 1.0); + vColor = vec4(color, 1.0); +} diff --git a/qt_pi/src/ble_connect.c b/qt_pi/src/ble_connect.c index 3249ca2..7e9c683 100644 --- a/qt_pi/src/ble_connect.c +++ b/qt_pi/src/ble_connect.c @@ -21,7 +21,7 @@ int data_buffer_length; int data_index; int status = 0; -int write_to_buffer = 0; +volatile int write_to_buffer = 0; pthread_t thread; diff --git a/qt_pi/src/main.cpp b/qt_pi/src/main.cpp new file mode 100644 index 0000000..56466f9 --- /dev/null +++ b/qt_pi/src/main.cpp @@ -0,0 +1,23 @@ +#include "speller/speller.h" +#include +#include + +int main(int argc, char *argv[]) { + QGuiApplication app(argc, argv); + ros::init(argc, argv, "speller"); + ros::NodeHandle nh; + QSurfaceFormat format; + format.setSwapInterval(1); + format.setRenderableType(QSurfaceFormat::OpenGL); + format.setProfile(QSurfaceFormat::CoreProfile); + format.setVersion(3, 3); + + // Set the window up + Speller spell(nh); + + spell.window->setFormat(format); + // spell.window->resize(QSize(800, 600)); + spell.window->show(); + + return app.exec(); +} diff --git a/qt_pi/src/mapview.cpp b/qt_pi/src/mapview.cpp new file mode 100644 index 0000000..e35a6f8 --- /dev/null +++ b/qt_pi/src/mapview.cpp @@ -0,0 +1,210 @@ +#include "mapview/mapview.h" + +MapView::MapView(QWidget *parent) { + connect(this, SIGNAL(update_GUI()), this, SLOT(update())); + blinking = 0; + count = 4; +} + +MapView::~MapView() { + stop_blinking(); + makeCurrent(); + teardownGL(); +} + +void MapView::stop_blinking() { + if (!blinking) + return; + + blinking = 0; + + t->join(); + free(t); +} + +void MapView::start_blinking() { + if (blinking) + return; + blinking = 1; + t = new std::thread(&MapView::run, this); + sleep(0); +} + +void MapView::run() { + usleep(500000); + uint fps = 0; + uint sq_state = 0; + while (blinking) { + + auto time = std::chrono::high_resolution_clock::now(); + if (!(fps % 4)) + sq_state = sq_state ^ (0b00000001); + if (!(fps % 5)) + sq_state = sq_state ^ (0b00000010); + if (!(fps % 6)) + sq_state = sq_state ^ (0b00000100); + if (!(fps % 7)) + sq_state = sq_state ^ (0b00001000); + + count = 0; + for (GLint i = 0; i < 4; i++) { + if ((sq_state >> i) & 0b1) { + startingElements[count] = i * 4; + count++; + } + } + fps++; + + emit(update_GUI()); + while (!sleep_control) + sleep(0); + sleep_control = 0; + + usleep(16665 - std::chrono::duration_cast( + std::chrono::high_resolution_clock::now() - time) + .count()); + } + + // while (blinking) { + // emit(update_GUI()); + // printf("WOah\n"); + // + // count = 4; + // sleep(1); + // emit(update_GUI()); + // count = 0; + // sleep(1); + // } +} + +static const Vertex sg_vertexes[] = { + Vertex(QVector3D(-0.55f, 0.55f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + Vertex(QVector3D(-0.45f, 0.55f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + Vertex(QVector3D(-0.45f, 0.45f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + Vertex(QVector3D(-0.55f, 0.45f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + + Vertex(QVector3D(-0.55f, -0.55f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + Vertex(QVector3D(-0.45f, -0.55f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + Vertex(QVector3D(-0.45f, -0.45f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + Vertex(QVector3D(-0.55f, -0.45f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + + Vertex(QVector3D(0.55f, 0.55f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + Vertex(QVector3D(0.45f, 0.55f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + Vertex(QVector3D(0.45f, 0.45f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + Vertex(QVector3D(0.55f, 0.45f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + + Vertex(QVector3D(0.55f, -0.55f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + Vertex(QVector3D(0.45f, -0.55f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + Vertex(QVector3D(0.45f, -0.45f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + Vertex(QVector3D(0.55f, -0.45f, 1.0f), QVector3D(0.75f, 0.75f, 0.75f)), + + // Vertex(QVector3D(-0.75f, 0.75f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // Vertex(QVector3D(-0.25f, 0.75f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // Vertex(QVector3D(-0.25f, 0.25f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // Vertex(QVector3D(-0.75f, 0.25f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // + // Vertex(QVector3D(-0.75f, -0.75f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // Vertex(QVector3D(-0.25f, -0.75f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // Vertex(QVector3D(-0.25f, -0.25f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // Vertex(QVector3D(-0.75f, -0.25f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // + // Vertex(QVector3D(0.75f, 0.75f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // Vertex(QVector3D(0.25f, 0.75f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // Vertex(QVector3D(0.25f, 0.25f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // Vertex(QVector3D(0.75f, 0.25f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // + // Vertex(QVector3D(0.75f, -0.75f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // Vertex(QVector3D(0.25f, -0.75f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // Vertex(QVector3D(0.25f, -0.25f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)), + // Vertex(QVector3D(0.75f, -0.25f, 1.0f), QVector3D(0.0f, 0.0f, 0.0f)) +}; + +void MapView::initializeGL() { + initializeOpenGLFunctions(); + printContextInformation(); + + glClearColor(0, 0, 0, 1); + { + // Create Shader (Do not release until VAO is created) + m_program = new QOpenGLShaderProgram(); + m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, + ":/shaders/simple.vert"); + m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, + ":/shaders/simple.frag"); + m_program->link(); + m_program->bind(); + + // Create Buffer (Do not release until VAO is created) + m_vertex.create(); + m_vertex.bind(); + m_vertex.setUsagePattern(QOpenGLBuffer::StaticDraw); + m_vertex.allocate(sg_vertexes, sizeof(sg_vertexes)); + + // Create Vertex Array Object + m_object.create(); + m_object.bind(); + m_program->enableAttributeArray(0); + m_program->enableAttributeArray(1); + m_program->setAttributeBuffer(0, GL_FLOAT, Vertex::positionOffset(), + Vertex::PositionTupleSize, Vertex::stride()); + m_program->setAttributeBuffer(1, GL_FLOAT, Vertex::colorOffset(), + Vertex::ColorTupleSize, Vertex::stride()); + + // Release (unbind) all + m_object.release(); + m_vertex.release(); + m_program->release(); + } +} + +void MapView::paintGL() { + glClear(GL_COLOR_BUFFER_BIT); + m_program->bind(); + { + m_object.bind(); + glMultiDrawArrays(GL_TRIANGLE_FAN, startingElements, counts, count); + m_object.release(); + } + m_program->release(); + sleep_control = 1; +} + +void MapView::resizeGL(int w, int h) { + glViewport(0, 0, w, h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); +} + +void MapView::printContextInformation() { + QString glType; + QString glVersion; + QString glProfile; + + // Get Version Information + glType = (context()->isOpenGLES()) ? "OpenGL ES" : "OpenGL"; + glVersion = reinterpret_cast(glGetString(GL_VERSION)); + + // Get Profile Information +#define CASE(c) \ + case QSurfaceFormat::c: \ + glProfile = #c; \ + break + switch (format().profile()) { + CASE(NoProfile); + CASE(CoreProfile); + CASE(CompatibilityProfile); + } +#undef CASE + + // qPrintable() will print our QString w/o quotes around it. + qDebug() << qPrintable(glType) << qPrintable(glVersion) << "(" + << qPrintable(glProfile) << ")"; +} + +void MapView::teardownGL() { + m_object.destroy(); + m_vertex.destroy(); + delete m_program; +} diff --git a/qt_pi/src/speller.cpp b/qt_pi/src/speller.cpp new file mode 100644 index 0000000..f1e5be9 --- /dev/null +++ b/qt_pi/src/speller.cpp @@ -0,0 +1,5 @@ +#include "speller/speller.h" + +Speller::Speller(ros::NodeHandle _nh) : nh(_nh) { window = new MapView(); } + +Speller::~Speller() {} diff --git a/qt_pi/tests/ble_test.cpp b/qt_pi/tests/ble_test.cpp index 865af77..c5295c4 100644 --- a/qt_pi/tests/ble_test.cpp +++ b/qt_pi/tests/ble_test.cpp @@ -8,9 +8,9 @@ int main() { insight_start_notif(); struct insight_data *buf = - (struct insight_data *)malloc(sizeof(struct insight_data) * 10); + (struct insight_data *)malloc(sizeof(struct insight_data) * 1000); - int len = 10; + int len = 1000; insight_write_to_buffer(len, buf); @@ -22,7 +22,9 @@ int main() { printf("Stopping notif %d \n", insight_stop_notif()); for (int i = 0; i < len; i++) { - printf("%f\n", buf[i].uV[0]); + for (int j = 0; j < 5; j++) + printf("%f ", buf[i].uV[j]); + printf("\n"); } printf("Destroy called %d\n", insight_destroy());