parent
89d234c99d
commit
9ade31e72f
@ -1,91 +0,0 @@
|
||||
#include "./serial.hpp"
|
||||
|
||||
bool SerialPortManager::shouldRun;
|
||||
int SerialPortManager::serialPort;
|
||||
|
||||
void SerialPortManager::init(){
|
||||
struct termios tty;
|
||||
|
||||
cout << "opening serial port /dev/ttyUSB0" << endl;
|
||||
serialPort = open("/dev/ttyUSB0", O_RDWR); //open serial port
|
||||
cout << "done" << endl;
|
||||
|
||||
if (serialPort < 0){
|
||||
printf("Error %i from open: %s\n", errno, strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
if(tcgetattr(serialPort, &tty) != 0){ //read serial port configuration
|
||||
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
//local flags
|
||||
tty.c_lflag &= ~ICANON; //enable canonical mode
|
||||
tty.c_lflag &= ~ECHO; // Disable echo
|
||||
tty.c_lflag &= ~ECHOE; // Disable erasure
|
||||
tty.c_lflag &= ~ECHONL; // Disable new-line echo
|
||||
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
|
||||
//input modes
|
||||
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
|
||||
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytes
|
||||
//output modes
|
||||
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
|
||||
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
|
||||
//c_cc
|
||||
tty.c_cc[VTIME] = 1; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
|
||||
tty.c_cc[VMIN] = 0;
|
||||
//baudrate
|
||||
cfsetispeed(&tty, B9600); //set baudrate (input)
|
||||
cfsetospeed(&tty, B9600); // (output)
|
||||
|
||||
// Save tty settings, also checking for error
|
||||
if (tcsetattr(serialPort, TCSANOW, &tty) != 0) {
|
||||
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
|
||||
return;
|
||||
}
|
||||
shouldRun = false;
|
||||
cout << "starting hardware watcher thread" << endl;
|
||||
std::thread* t = new std::thread(SerialPortManager::runPort);
|
||||
cout << "done" << endl;
|
||||
Threadweaver::stick_this_thread_to_core(t,CAMCORE);
|
||||
Threadweaver::hardwareWatcherThread = t;
|
||||
shouldRun = true;
|
||||
}
|
||||
|
||||
void SerialPortManager::runPort(){
|
||||
while(!shouldRun){}
|
||||
char read_buf [256];
|
||||
int n = 0;
|
||||
while(shouldRun){
|
||||
n = read(serialPort, &read_buf, sizeof(read_buf));
|
||||
if(n > 0){
|
||||
char c = read_buf[0];
|
||||
switch(c){
|
||||
case 'U':
|
||||
cout << c << "++" << endl;
|
||||
ui::UiController::selectedUp();
|
||||
break;
|
||||
case 'D':
|
||||
cout << c << "--" << endl;
|
||||
ui::UiController::selectedDown();
|
||||
break;
|
||||
case '1':
|
||||
ui::UiController::click();
|
||||
break;
|
||||
case '2':
|
||||
ui::UiController::click();
|
||||
break;
|
||||
case '3':
|
||||
ui::UiController::click();
|
||||
break;
|
||||
case '4':
|
||||
ui::UiController::click();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::fill_n(read_buf, n, 0);
|
||||
}
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
// C library headers
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// Linux headers
|
||||
#include <fcntl.h> // Contains file controls like O_RDWR
|
||||
#include <errno.h> // Error integer and strerror() function
|
||||
#include <termios.h> // Contains POSIX terminal control definitions
|
||||
#include <unistd.h> // write(), read(), close()
|
||||
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "../../ui/_ui.hpp"
|
||||
|
||||
class SerialPortManager{
|
||||
public:
|
||||
static void init();
|
||||
static void runPort();
|
||||
static bool shouldRun;
|
||||
static int serialPort;
|
||||
};
|
@ -0,0 +1,49 @@
|
||||
#include "uiController.hpp"
|
||||
|
||||
UiController::UiController()
|
||||
{
|
||||
std::cout << "init ui controller" << endl;
|
||||
showMenu = true;
|
||||
menuSize = cv::Size2i(400, 400);
|
||||
menuPos = cv::Point2i(DEFAULT_UI_SIZE_X - menuSize.width, (DEFAULT_UI_SIZE_Y / 2) - (menuSize.height / 2)); // centered right
|
||||
selectedIndex = 0;
|
||||
menuItemNames = {"set vr mode", "set cinema mode", "settings"};
|
||||
vector<std::function<void()>> functions = {psvr::Psvr::vrmode, psvr::Psvr::cinemaMode, UiController::openSettings};
|
||||
unsigned int i = 0;
|
||||
for (std::string name : menuItemNames)
|
||||
{
|
||||
menuItems[name] = functions.at(i);
|
||||
i++;
|
||||
}
|
||||
update();
|
||||
cout << "done init ui controller" << endl;
|
||||
}
|
||||
|
||||
void UiController::update()
|
||||
{
|
||||
time_t now = time(0);
|
||||
menutime = ctime(&now);
|
||||
menuTitle = std::string("P-OS ") + VERSION + " " + menutime;
|
||||
}
|
||||
|
||||
void UiController::selectedUp()
|
||||
{
|
||||
if (selectedIndex < menuItems.size() - 1)
|
||||
selectedIndex++;
|
||||
}
|
||||
|
||||
void UiController::selectedDown()
|
||||
{
|
||||
if (selectedIndex > 0)
|
||||
selectedIndex--;
|
||||
}
|
||||
|
||||
void UiController::openSettings()
|
||||
{
|
||||
}
|
||||
|
||||
void UiController::click()
|
||||
{
|
||||
exitCalled = true;
|
||||
// std::invoke(menuItems[menuItemNames.at(selectedIndex)]);
|
||||
}
|
@ -0,0 +1,49 @@
|
||||
/*#include <iostream>
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <functional>
|
||||
|
||||
#include <GL/glut.h>
|
||||
*/
|
||||
#include "../../hardware/cameras/_cam.hpp"
|
||||
#include "../../hardware/psvr/_psvr.hpp"
|
||||
#include <opencv2/imgcodecs/imgcodecs.hpp>
|
||||
#include <opencv2/core.hpp>
|
||||
#include <opencv2/core/opengl.hpp>
|
||||
|
||||
#include "../../patterns/observer/observable.hpp"
|
||||
|
||||
#define DEFAULT_UI_WINDOW_AMOUNT 1 // 2 windows, one for each eye
|
||||
#define DEFAULT_UI_OFFSET_X 1080 // 1080
|
||||
#define DEFAULT_UI_OFFSET_Y 0
|
||||
#define DEFAULT_UI_SIZE_X 1920 // psvr is 960x1080 per eye
|
||||
#define DEFAULT_UI_SIZE_Y 1080
|
||||
|
||||
using namespace std;
|
||||
using namespace cv;
|
||||
using namespace cv::ogl;
|
||||
using namespace psvr;
|
||||
|
||||
/// @brief this class is used to process view logic and interaction [model]
|
||||
class UiController
|
||||
{
|
||||
public:
|
||||
UiController();
|
||||
void selectedUp();
|
||||
void selectedDown();
|
||||
void click();
|
||||
void update();
|
||||
void openSettings();
|
||||
vector<std::string> menuItemNames;
|
||||
bool showMenu;
|
||||
bool exitCalled;
|
||||
bool runIntro;
|
||||
string menuTitle;
|
||||
cv::Size2i menuSize;
|
||||
cv::Point2i menuPos;
|
||||
unsigned int selectedIndex;
|
||||
map<std::string, std::function<void()>> menuItems;
|
||||
char *menutime;
|
||||
};
|
@ -0,0 +1,14 @@
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
/// @brief this class manages the UI(s) [windows] used by the application, right now only one should ever be used, especially in opengl mode
|
||||
class UiManager
|
||||
{
|
||||
public:
|
||||
static std::vector<std::mutex*> accessLocks;
|
||||
static std::vector<Ui *> managedUIs;
|
||||
static bool uiShouldRun;
|
||||
static void init();
|
||||
static void cleanup();
|
||||
static void beginDrawRoutineForUi(Ui *u);
|
||||
};
|
@ -0,0 +1,102 @@
|
||||
#include "./serial.hpp"
|
||||
#include <string>
|
||||
|
||||
SerialPortManager::SerialPortManager()
|
||||
{
|
||||
struct termios tty;
|
||||
|
||||
std::cout << "opening serial port /dev/ttyUSB0" << std::endl;
|
||||
serialPort = open("/dev/ttyUSB0", O_RDWR); // open serial port
|
||||
std::cout << "done" << std::endl;
|
||||
|
||||
if (serialPort < 0)
|
||||
{
|
||||
printf("Error %i from open: %s\n", errno, strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
if (tcgetattr(serialPort, &tty) != 0)
|
||||
{ // read serial port configuration
|
||||
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
// local flags
|
||||
tty.c_lflag &= ~ICANON; // enable canonical mode
|
||||
tty.c_lflag &= ~ECHO; // Disable echo
|
||||
tty.c_lflag &= ~ECHOE; // Disable erasure
|
||||
tty.c_lflag &= ~ECHONL; // Disable new-line echo
|
||||
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
|
||||
// input modes
|
||||
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
|
||||
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // Disable any special handling of received bytes
|
||||
// output modes
|
||||
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)
|
||||
tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed
|
||||
// c_cc
|
||||
tty.c_cc[VTIME] = 1; // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.
|
||||
tty.c_cc[VMIN] = 0;
|
||||
// baudrate
|
||||
cfsetispeed(&tty, B9600); // set baudrate (input)
|
||||
cfsetospeed(&tty, B9600); // (output)
|
||||
|
||||
// Save tty settings, also checking for error
|
||||
if (tcsetattr(serialPort, TCSANOW, &tty) != 0)
|
||||
{
|
||||
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
|
||||
return;
|
||||
}
|
||||
shouldRun = false;
|
||||
std::cout << "starting hardware watcher thread" << std::endl;
|
||||
std::thread *t = new std::thread(runPort);
|
||||
std::cout << "done" << std::endl;
|
||||
Threadweaver::stick_this_thread_to_core(t, CAMCORE);
|
||||
//Threadweaver::hardwareWatcherThread = t;
|
||||
shouldRun = true;
|
||||
}
|
||||
|
||||
void SerialPortManager::runPort()
|
||||
{
|
||||
while (!shouldRun)
|
||||
{
|
||||
}
|
||||
char read_buf[256];
|
||||
int n = 0;
|
||||
while (shouldRun)
|
||||
{
|
||||
n = read(serialPort, &read_buf, sizeof(read_buf));
|
||||
if (n > 0)
|
||||
{
|
||||
char c = read_buf[0];
|
||||
sendMessage(std::string()+c);
|
||||
/*
|
||||
switch (c)
|
||||
{
|
||||
case 'U':
|
||||
cout << c << "++" << endl;
|
||||
ui::UiController::selectedUp();
|
||||
break;
|
||||
case 'D':
|
||||
cout << c << "--" << endl;
|
||||
ui::UiController::selectedDown();
|
||||
break;
|
||||
case '1':
|
||||
ui::UiController::click();
|
||||
break;
|
||||
case '2':
|
||||
ui::UiController::click();
|
||||
break;
|
||||
case '3':
|
||||
ui::UiController::click();
|
||||
break;
|
||||
case '4':
|
||||
ui::UiController::click();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
*/
|
||||
}
|
||||
std::fill_n(read_buf, n, 0);
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
// C library headers
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// Linux headers
|
||||
#include <fcntl.h> // Contains file controls like O_RDWR
|
||||
#include <errno.h> // Error integer and strerror() function
|
||||
#include <termios.h> // Contains POSIX terminal control definitions
|
||||
#include <unistd.h> // write(), read(), close()
|
||||
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "../../patterns/observer/observable.hpp"
|
||||
#include "../../patterns/observer/observer.hpp"
|
||||
#include "../../threadweaver/threadweaver.hpp"
|
||||
|
||||
class SerialPortManager : Observable
|
||||
{
|
||||
public:
|
||||
SerialPortManager();
|
||||
void runPort();
|
||||
|
||||
private:
|
||||
bool shouldRun;
|
||||
int serialPort;
|
||||
};
|
@ -0,0 +1,21 @@
|
||||
#include "observable.hpp"
|
||||
|
||||
void Observable::Attach(Observer *observer)
|
||||
{
|
||||
list_observer_.push_back(observer);
|
||||
}
|
||||
|
||||
void Observable::Detach(Observer *observer)
|
||||
{
|
||||
list_observer_.remove(observer);
|
||||
}
|
||||
|
||||
void Observable::sendMessage(std::string str)
|
||||
{
|
||||
std::list<Observer*>::iterator iterator = list_observer_.begin();
|
||||
while (iterator != list_observer_.end())
|
||||
{
|
||||
(*iterator)->Update(str);
|
||||
++iterator;
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <string>
|
||||
#include "observer.hpp"
|
||||
|
||||
class Observable {
|
||||
public:
|
||||
~Observable(){};
|
||||
void Attach(Observer *observer) = 0;
|
||||
void Detach(Observer *observer) = 0;
|
||||
void sendMessage(std::string str);
|
||||
private:
|
||||
std::list<Observer*> list_observer_;
|
||||
};
|
@ -0,0 +1,6 @@
|
||||
#include <string>
|
||||
|
||||
class Observer {
|
||||
public:
|
||||
virtual void Update(const std::string &message_from_subject) = 0;
|
||||
};
|
@ -0,0 +1,24 @@
|
||||
#include "window.hpp"
|
||||
|
||||
void Window::draw(){
|
||||
try{
|
||||
drawAccess.lock();
|
||||
if(drawSurface.empty()){
|
||||
drawAccess.unlock();
|
||||
return;
|
||||
}
|
||||
cv::UMat drawBuffer = drawSurface;
|
||||
drawAccess.unlock();
|
||||
|
||||
#ifdef OGLWIN
|
||||
drawTexture.copyFrom(drawBuffer);
|
||||
cv::imshow(this->myWindow, this->drawTexture);
|
||||
#else
|
||||
cv::imshow(this->myWindow, drawBuffer);
|
||||
#endif
|
||||
cv::waitKey(10);
|
||||
}
|
||||
catch(...){
|
||||
drawAccess.unlock();
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
#include <opencv2/core/core.hpp>
|
||||
#include <opencv2/core/opengl.hpp>
|
||||
#include <opencv2/highgui.hpp>
|
||||
#include <GL/glut.h>
|
||||
#include <mutex>
|
||||
|
||||
/// @brief this class represents a single window, there should only ever be one if the app is in opengl mode.
|
||||
class Window
|
||||
{
|
||||
public:
|
||||
cv::UMat drawSurface; // Current frame
|
||||
cv::ogl::Texture2D drawTexture = cv::ogl::Texture2D();
|
||||
int id;
|
||||
std::mutex drawAccess;
|
||||
std::string myWindow;
|
||||
void draw();
|
||||
};
|
@ -0,0 +1,32 @@
|
||||
#include "windowManager.hpp"
|
||||
#include <thread>
|
||||
namespace ui
|
||||
{
|
||||
|
||||
// initializes the ui manager, following default values
|
||||
WindowManager::WindowManager()
|
||||
{
|
||||
uiShouldRun = false;
|
||||
for (int i = 0; i < DEFAULT_UI_WINDOW_AMOUNT; i++)
|
||||
{
|
||||
Window *newWindow = new Window();
|
||||
newWindow->myWindow = "project- UI" + std::to_string(i);
|
||||
#ifndef OGLWIN
|
||||
cv::namedWindow(newWindow->myWindow); //if we're in opengl mode, the window needs to be created in the same thread as the execution, not here
|
||||
#endif
|
||||
cv::moveWindow(newWindow->myWindow, DEFAULT_UI_OFFSET_X + i * 960, DEFAULT_UI_OFFSET_Y);
|
||||
cv::resizeWindow(newWindow->myWindow, DEFAULT_UI_SIZE_X, DEFAULT_UI_SIZE_Y);
|
||||
|
||||
//cout << "window: " << newWindow->myWindow << " created at " << DEFAULT_UI_OFFSET_X + i * 960 << " , " << DEFAULT_UI_OFFSET_Y << endl;
|
||||
|
||||
newWindow->id = i;
|
||||
|
||||
managedUIs.push_back(newWindow); // add new ui in the ui map, mapped to the window's name
|
||||
}
|
||||
uiShouldRun = true;
|
||||
}
|
||||
|
||||
void WindowManager::cleanup()
|
||||
{
|
||||
}
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue