Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • danc/MicroCART
  • snawerdt/MicroCART_17-18
  • bbartels/MicroCART_17-18
  • jonahu/MicroCART
4 results
Show changes
/**
@file
@brief Implementation
@date 2011
@author
Ryan Pavlik
<rpavlik@iastate.edu> and <abiryan@ryand.net>
http://academic.cleardefinition.com/
Iowa State University Virtual Reality Applications Center
Human-Computer Interaction Graduate Program
*/
// Copyright Iowa State University 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Internal Includes
#include "HIDDevice.h"
#include "vrpn_HumanInterface.h"
// Library/third-party includes
#include <QDateTime>
// Standard includes
// - none
class HIDDevice::VRPNDevice : public vrpn_HidInterface {
HIDDevice * _container;
public:
VRPNDevice(vrpn_HidAcceptor * acceptor, HIDDevice * container)
: vrpn_HidInterface(acceptor)
, _container(container)
{}
protected:
void on_data_received(size_t bytes, vrpn_uint8 *buffer) {
_container->send_data_signal(bytes, reinterpret_cast<char *>(buffer));
}
};
HIDDevice::HIDDevice(vrpn_HidAcceptor * acceptor, QObject * parent)
: QObject(parent)
, _connected(false)
, _device(new VRPNDevice(acceptor, this))
, _startingTimestamp(-1) {}
HIDDevice::~HIDDevice() {
delete _device;
_device = NULL;
}
void HIDDevice::do_update() {
bool wasConnected = _connected;
_connected = _device->connected();
if (_connected && !wasConnected) {
emit message("Connected to device!");
} else if (!_connected && wasConnected) {
emit message("Lost connection to device!");
}
_device->update();
}
void HIDDevice::send_data_signal(size_t bytes, const char * buffer) {
qint64 current = QDateTime::currentMSecsSinceEpoch();
if (_startingTimestamp < 0) {
_startingTimestamp = current;
}
emit inputReport(QByteArray(buffer, bytes), current - _startingTimestamp);
}
void HIDDevice::send_message_signal(QString const& msg) {
emit message(msg);
}
/** @file
@brief Header
@date 2011
@author
Ryan Pavlik
<rpavlik@iastate.edu> and <abiryan@ryand.net>
http://academic.cleardefinition.com/
Iowa State University Virtual Reality Applications Center
Human-Computer Interaction Graduate Program
*/
// Copyright Iowa State University 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
// Internal Includes
#include "vrpn_Shared.h"
// Library/third-party includes
#include <QObject>
#include <QByteArray>
#include <QString>
// Standard includes
// - none
class vrpn_HidAcceptor;
/// Qt wrapper for a debugging HidInterface.
class HIDDevice: public QObject {
Q_OBJECT
public:
explicit HIDDevice(vrpn_HidAcceptor * acceptor, QObject * parent = NULL);
~HIDDevice();
signals:
void inputReport(QByteArray buffer, qint64 timestamp);
void message(QString const& msg);
public slots:
void do_update();
protected:
bool _connected;
class VRPNDevice;
friend class HIDDevice::VRPNDevice;
void send_data_signal(size_t bytes, const char * buffer);
void send_message_signal(QString const& msg);
VRPNDevice * _device;
qint64 _startingTimestamp;
};
/**
@file
@brief Implementation
@date 2011
@author
Ryan Pavlik
<rpavlik@iastate.edu> and <abiryan@ryand.net>
http://academic.cleardefinition.com/
Iowa State University Virtual Reality Applications Center
Human-Computer Interaction Graduate Program
*/
// Copyright Iowa State University 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// Internal Includes
#include "Inspector.h"
#include "vrpn_Shared.h"
// Library/third-party includes
// - none
// Standard includes
#include <stdexcept>
#include <iostream>
//#include <stdint.h>
template<typename T>
T getFromByteArray(QByteArray const& input) {
union {
char bytes[sizeof(T)];
T value;
};
for (int i = 0; i < sizeof(T); ++i) {
bytes[i] = input.at(i);
}
return value;
}
Inspector::Inspector(std::size_t first_index, std::size_t length, bool signedVal, bool bigEndian, QObject * parent)
: QObject(parent)
, _first(first_index)
, _length(length)
, _signed(signedVal)
, _bigEndian(bigEndian) {
switch (_length) {
case 1:
case 2:
case 4:
break;
default:
throw std::logic_error("Can't get an integer with that many bytes!");
}
}
void Inspector::updatedData(QByteArray buf, qint64 timestamp) {
QByteArray myPortion;
myPortion.reserve(_length);
if (!_bigEndian) {
myPortion = buf.mid(_first, _length);
} else {
unsigned i;
for (i = 0; i < _length; ++i) {
myPortion.prepend(buf.at(_first + i));
}
}
switch (_length) {
case 1:
_sendNewValue(timestamp, _signed ?
myPortion.at(0)
: getFromByteArray<vrpn_uint8>(myPortion));
break;
case 2:
_sendNewValue(timestamp, _signed ?
getFromByteArray<vrpn_int16>(myPortion) :
getFromByteArray<vrpn_uint16>(myPortion));
break;
case 4:
_sendNewValue(timestamp, _signed ?
getFromByteArray<vrpn_int32>(myPortion) :
getFromByteArray<vrpn_uint32>(myPortion));
break;
}
}
void Inspector::_sendNewValue(qint64 timestamp, float val) {
emit newValue(val);
emit newValue(float(timestamp) / 1000.0f, val);
}
/** @file
@brief Header
@date 2011
@author
Ryan Pavlik
<rpavlik@iastate.edu> and <abiryan@ryand.net>
http://academic.cleardefinition.com/
Iowa State University Virtual Reality Applications Center
Human-Computer Interaction Graduate Program
*/
// Copyright Iowa State University 2011.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
// Internal Includes
// - none
// Library/third-party includes
#include <QObject>
#include <QByteArray>
// Standard includes
// - none
/// Class to extract bytes from an array and turn into some kind of value
class Inspector : public QObject {
Q_OBJECT
public:
explicit Inspector(std::size_t first_index, std::size_t length, bool signedVal, bool bigEndian = false, QObject * parent = NULL);
~Inspector() {}
signals:
void newValue(float val);
void newValue(float elapsed, float val);
public slots:
void updatedData(QByteArray buf, qint64 timestamp);
private:
void _sendNewValue(qint64 timestamp, float val);
std::size_t _first;
std::size_t _length;
bool _signed;
bool _bigEndian;
};