Commit 3031fc22 authored by Gertulla, Janis Luca's avatar Gertulla, Janis Luca
Browse files

added everything

parent 846111ea
......@@ -20,16 +20,22 @@ SOURCES += \
lib.cpp \
main.cpp \
maintab.cpp \
settingstab.cpp \
valuestab.cpp \
window.cpp
HEADERS += \
client.h \
lib.h \
maintab.h \
settingstab.h \
valuestab.h \
window.h
FORMS += \
maintab.ui \
settingstab.ui \
valuestab.ui \
window.ui
# Default rules for deployment.
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.13.0, 2020-09-16T08:42:59. -->
<!-- Written by QtCreator 4.13.0, 2020-10-02T11:10:10. -->
<qtcreator>
<data>
<variable>EnvironmentId</variable>
......@@ -323,8 +323,9 @@
<valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:/home/fl0wless/Arbeit/UE49Interlock/UE49Interlock.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">/home/fl0wless/Arbeit/UE49Interlock/UE49Interlock.pro</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">UE49Interlock2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:/home/fl0wless/Arbeit/ue49interlock/UE49Interlock.pro</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey">/home/fl0wless/Arbeit/ue49interlock/UE49Interlock.pro</value>
<value type="QString" key="RunConfiguration.Arguments"></value>
<value type="bool" key="RunConfiguration.Arguments.multi">false</value>
<value type="QString" key="RunConfiguration.OverrideDebuggerStartup"></value>
......
#include "client.h"
#include "window.h"
Client::Client(std::shared_ptr<Config> config) : QTcpSocket(), config(config)
Client::Client() : QTcpSocket()
{
config = std::make_unique<Config>();
connect(this, &QAbstractSocket::readyRead, this, &Client::handleMessage);
pingInterval = std::make_unique<QTimer>(this);
......@@ -15,18 +17,21 @@ Client::Client(std::shared_ptr<Config> config) : QTcpSocket(), config(config)
connect(pingInterval.get(), &QTimer::timeout, this, &Client::ping);
connect(this, &QAbstractSocket::connected, [=](){
connect(this, &QAbstractSocket::connected, [=]()
{
pingInterval->start();
});
connect(this, &QAbstractSocket::disconnected, [=](){
connect(this, &QAbstractSocket::disconnected, [=]()
{
// make sure these do not trigger after any disconnect
pingInterval->stop();
pingTimeout->stop();
emit log("Client disconnect.");
});
connect(pingTimeout.get(), &QTimer::timeout, [=](){ //
connect(pingTimeout.get(), &QTimer::timeout, [=]()
{ //
pingInterval->stop();
disconnectFromHost();
emit log("No pong received within 3 seconds, resetting connection.");
......@@ -34,124 +39,149 @@ Client::Client(std::shared_ptr<Config> config) : QTcpSocket(), config(config)
}
Client::~Client(){
Client::~Client()
{
}
void Client::ping() {
emit log("Sending ping");
void Client::ping()
{
// emit log("Sending ping");
uint8_t pingData[2] = {0b00000010, 'p'};
write((const char *) pingData, 2);
flush();
pingTimeout->start();
}
void Client::handleMessage() {
void Client::handleMessage()
{
QByteArray msg = readAll();
while(msg.length() > 0){
while(msg.length() > 0)
{
auto msgHeader = msg[0] >> 4;
auto msgLen = msg[0] & 0xF;
auto repr = std::accumulate(msg.begin(), msg.end(), QString(""), [](QString a, int b){
auto repr = std::accumulate(msg.begin(), msg.end(), QString(""), [](QString a, unsigned int b)
{
return std::move(a) + QString("\\") + QString::number(b, 16) + QString(" ");
});
if(msgHeader == MsgPing) // pong
{
if(msgLen == 2 && msg[1] == 'p') {
emit log("Received valid pong!: " + repr + "(" + msg + ")");
if(msgLen == 2 && msg[1] == 'p')
{
// emit log("Received valid pong!: " + repr + "(" + msg + ")");
pingInterval->start();
}
else {
else
{
emit log("Received invalid pong-like message!: " + repr + "(" + msg + ")");
}
}
else if(msgHeader == MsgState) // state change
{
if(msgLen == 4) {
auto state = std::make_shared<StateMessage>();
state->operable = msg[1] & (1 << 7);
state->override = msg[1] & (1 << 6);
state->error = msg[1] & (1 << 5);
state->mode = msg[1] & (1 << 4);
state->settingVTdet = msg[2] & (1 << 7);
state->settingVdet = msg[2] & (1 << 6);
state->settingVfoil = msg[2] & (1 << 5);
state->settingVdps = msg[2] & (1 << 4);
state->settingHVmcp = msg[2] & (1 << 3);
state->settingRbeamline = msg[2] & (1 << 2);
state->pumpEnabled = msg[2] & (1 << 1);
emit log("Received new state!: " + repr);
emit newState(state);
} else {
if(msgLen == 4)
{
StateMessage newStat;
newStat.operable = msg[1] & (1 << 7);
newStat.override = msg[1] & (1 << 6);
newStat.error = msg[1] & (1 << 5);
newStat.linked = msg[1] & (1 << 4);
newStat.settingVTdet = msg[2] & (1 << 7);
newStat.settingVdet = msg[2] & (1 << 6);
newStat.settingVfoil = msg[2] & (1 << 5);
newStat.settingVdps = msg[2] & (1 << 4);
newStat.settingHVmcp = msg[2] & (1 << 3);
newStat.settingRbeamline = msg[2] & (1 << 2);
newStat.pumpEnabled = msg[2] & (1 << 1);
// emit log("Received new state!: " + repr);
emit newState(newStat);
}
else
{
emit log("Received state-like garbage!");
}
}
else if(msgHeader == MsgMeasurement){ // new measurements
if(msgLen == 12) {
else if(msgHeader == MsgMeasurement)
{ // new measurements
if(msgLen == 12)
{
// emit log("Received measurements");
char *data = msg.data();
auto meas = std::make_shared<MeasurementMessage>();
MeasurementMessage newMeas;
newMeas.pGrat = (((int16_t) msg[1]) << 8) | (((int16_t) msg[2]) & 0xFF);
newMeas.pMCP = (((int16_t) msg[3]) << 8) | (((int16_t) msg[4]) & 0xFF);
newMeas.pVV = (((int16_t) msg[5]) << 8) | (((int16_t) msg[6]) & 0xFF);
meas->pGrat = (((int16_t) msg[1]) << 8) | (((int16_t) msg[2]) & 0xFF);
meas->pMCP = (((int16_t) msg[3]) << 8) | (((int16_t) msg[4]) & 0xFF);
meas->pVV = (((int16_t) msg[5]) << 8) | (((int16_t) msg[6]) & 0xFF);
newMeas.tDet = (((int16_t) msg[7]) << 8) | (((int16_t) msg[8]) & 0xFF);
newMeas.tDPS = (((int16_t) msg[9]) << 8) | (((int16_t) msg[10]) & 0xFF);
meas->tDet = (((int16_t) msg[7]) << 8) | (((int16_t) msg[8]) & 0xFF);
meas->tDPS = (((int16_t) msg[9]) << 8) | (((int16_t) msg[10]) & 0xFF);
newMeas.vTdetHigh = data[11] & (1 << 7);
newMeas.vdetHigh = data[11] & (1 << 6);
newMeas.vfoilHigh = data[11] & (1 << 5);
newMeas.vdpsHigh = data[11] & (1 << 4);
meas->vTdetHigh = data[11] & (1 << 7);
meas->vdetHigh = data[11] & (1 << 6);
meas->vfoilHigh = data[11] & (1 << 5);
meas->vdpsHigh = data[11] & (1 << 4);
newMeas.vTdetLow = data[11] & (1 << 3);
newMeas.vdetLow = data[11] & (1 << 2);
newMeas.vfoilLow = data[11] & (1 << 1);
newMeas.vdpsLow = data[11] & (1 << 0);
meas->vTdetLow = data[11] & (1 << 3);
meas->vdetLow = data[11] & (1 << 2);
meas->vfoilLow = data[11] & (1 << 1);
meas->vdpsLow = data[11] & (1 << 0);
emit newMeasurements(newMeas);
}
emit newMeasurements(meas);
} else {
else
{
emit log("Error: received measurement-like garbage!");
}
}
else {
else
{
emit log("Got garbage: " + repr);
}
msg.remove(0, msgLen);
}
}
void Client::sendState(std::shared_ptr<StateMessage> state){
void Client::onSendState(StateMessage &state)
{
uint8_t msg[4];
emit log("Writing state...");
// emit log("Writing state...");
msg[0] = 0b00010100;
msg[1] = 0;
msg[1] |= state->operable << 7;
msg[1] |= state->override << 6;
msg[1] |= state->error << 5;
msg[1] |= state->mode << 4;
msg[1] |= state.operable << 7;
msg[1] |= state.override << 6;
msg[1] |= state.error << 5;
msg[1] |= state.linked << 4;
msg[2] = 0;
msg[2] |= state->settingVTdet << 7;
msg[2] |= state->settingVdet << 6;
msg[2] |= state->settingVfoil << 5;
msg[2] |= state->settingVdps << 4;
msg[2] |= state.settingVTdet << 7;
msg[2] |= state.settingVdet << 6;
msg[2] |= state.settingVfoil << 5;
msg[2] |= state.settingVdps << 4;
msg[2] |= state->settingHVmcp << 3;
msg[2] |= state->settingRbeamline << 2;
msg[2] |= state.settingHVmcp << 3;
msg[2] |= state.settingRbeamline << 2;
msg[2] |= state->pumpEnabled << 1;
msg[2] |= state.pumpEnabled << 1;
write((const char *) msg, 4);
flush();
}
void Client::onNewConfig(Config &newConfig)
{
*config = newConfig;
}
......@@ -6,61 +6,7 @@
#include "lib.h"
// what ino/client intend
struct StateMessage { // repr: 1 + 1(header) bytes
bool operable;
bool override;
bool error;
bool mode;
bool settingVTdet;
bool settingVdet;
bool settingVfoil;
bool settingVdps;
bool settingHVmcp;
bool settingRbeamline;
bool pumpEnabled;
};
// what the ino actually measures
struct MeasurementMessage { // repr: 11 + 1(header) bytes
int16_t pGrat;
int16_t pMCP;
int16_t pVV;
int16_t tDet;
int16_t tDPS;
bool vTdetHigh;
bool vdetHigh;
bool vfoilHigh;
bool vdpsHigh;
bool vTdetLow;
bool vdetLow;
bool vfoilLow;
bool vdpsLow;
};
struct PingMessage {
uint8_t p; // redundancy/"checksum"
};
enum MsgType {
MsgPing = 0,
MsgState = 1 << 0,
MsgMeasurement = 1 << 1,
MsgError = 1 << 2, // needed?
};
enum Mode {
ModeWet = 0,
ModeDry = 1,
};
......@@ -68,25 +14,28 @@ class Client : public QTcpSocket
{
Q_OBJECT
public:
Client(std::shared_ptr<Config> config);
Client();
~Client();
signals:
// void new_pong(PingMessage *p);
void newState(std::shared_ptr<StateMessage> state);
void newMeasurements(std::shared_ptr<MeasurementMessage> measurements);
void newState(StateMessage &state);
void newMeasurements(MeasurementMessage &measurements);
void log(QString msg);
public slots:
void sendState(std::shared_ptr<StateMessage> state);
void onSendState(StateMessage &newState);
void onNewConfig(Config &newConfig);
private:
std::unique_ptr<QTimer> pingInterval;
std::unique_ptr<QTimer> pingTimeout;
std::unique_ptr<QTimer> pingInterval;
std::unique_ptr<QTimer> pingTimeout;
std::shared_ptr<Config> config;
std::unique_ptr<Config> config;
void handleMessage();
void ping();
......
#include "lib.h"
StateMessage::StateMessage()
{
this->error = false;
this->linked = true;
this->operable = true;
this->override = false;
this->pumpEnabled = false;
this->settingHVmcp = true;
this->settingRbeamline = true;
this->settingVTdet = false;
this->settingVdet = false;
this->settingVdps = false;
this->settingVfoil = true;
}
StateMessage::StateMessage(StateMessage &other)
{
this->error = other.error;
this->linked = other.linked;
this->operable = other.operable;
this->override = other.override;
this->pumpEnabled = other.pumpEnabled;
this->settingHVmcp = other.settingHVmcp;
this->settingRbeamline = other.settingRbeamline;
this->settingVTdet = other.settingVTdet;
this->settingVdet = other.settingVdet;
this->settingVdps = other.settingVdps;
this->settingVfoil = other.settingVfoil;
}
StateMessage::StateMessage(StateMessage *other) : StateMessage()
{
if(other != nullptr)
{
this->error = other->error;
this->linked = other->linked;
this->operable = other->operable;
this->override = other->override;
this->pumpEnabled = other->pumpEnabled;
this->settingHVmcp = other->settingHVmcp;
this->settingRbeamline = other->settingRbeamline;
this->settingVTdet = other->settingVTdet;
this->settingVdet = other->settingVdet;
this->settingVdps = other->settingVdps;
this->settingVfoil = other->settingVfoil;
}
}
void StateMessage::operator=(StateMessage &other)
{
this->error = other.error;
this->linked = other.linked;
this->operable = other.operable;
this->override = other.override;
this->pumpEnabled = other.pumpEnabled;
this->settingHVmcp = other.settingHVmcp;
this->settingRbeamline = other.settingRbeamline;
this->settingVTdet = other.settingVTdet;
this->settingVdet = other.settingVdet;
this->settingVdps = other.settingVdps;
this->settingVfoil = other.settingVfoil;
}
Config::Config() // default
{
this->networkIP = "192.168.171.12";
this->networkPort = 5000;
this->displayModeDynamic = 0;
this->pressureUnitType = 1; // mBar
this->gaugeTypeGrat = 0; // PBR
this->gaugeTypeMcp = 0; // PBR
this->gaugeTypeVv = 2; // IKR
}
Config::Config(Config &other)
{
this->networkIP = other.networkIP;
this->networkPort = other.networkPort;
this->displayModeDynamic = other.displayModeDynamic;
this->pressureUnitType = other.pressureUnitType;
this->gaugeTypeGrat = other.gaugeTypeGrat;
this->gaugeTypeMcp = other.gaugeTypeMcp;
this->gaugeTypeVv = other.gaugeTypeVv;
}
Config::Config(Config *other) : Config()
{
if(other != nullptr)
{
this->networkIP = other->networkIP;
this->networkPort = other->networkPort;
this->displayModeDynamic = other->displayModeDynamic;
this->pressureUnitType = other->pressureUnitType;
this->gaugeTypeGrat = other->gaugeTypeGrat;
this->gaugeTypeMcp = other->gaugeTypeMcp;
this->gaugeTypeVv = other->gaugeTypeVv;
}
}
void Config::operator=(Config &other)
{
this->networkIP = other.networkIP;
this->networkPort = other.networkPort;
this->displayModeDynamic = other.displayModeDynamic;
this->pressureUnitType = other.pressureUnitType;
this->gaugeTypeGrat = other.gaugeTypeGrat;
this->gaugeTypeMcp = other.gaugeTypeMcp;
this->gaugeTypeVv = other.gaugeTypeVv;
}
void MeasurementMessage::operator=(MeasurementMessage &other)
{
this->pGrat = other.pGrat;
this->pMCP = other.pMCP;
this->pVV = other.pVV;
this->tDPS = other.tDPS;
this->tDet = other.tDet;
this->vTdetHigh = other.vTdetHigh;
this->vTdetLow = other.vTdetLow;
this->vdetHigh = other.vdetHigh;
this->vdetLow = other.vdetLow;
this->vdpsHigh = other.vdpsHigh;
this->vdpsLow = other.vdpsLow;
this->vfoilHigh = other.vfoilHigh;
this->vfoilLow = other.vfoilLow;
}
qreal pressureFromReading(int16_t measure, int type){
qreal baseVal = measure / 1023.0 * 10.0;
......@@ -21,6 +156,44 @@ qreal pressureFromReading(int16_t measure, int type){
}
}
void setValue(QLabel *w, qreal value){
QString unitFromConfig(int value)
{
switch(value)
{
case 0: return "Bar";
case 1: return "mBar";
case 2: return "µBar";
case 3: return "nBar";
case 4: return "kTorr";
case 5: return "Torr";
case 6: return "mTorr";
case 7: return "µTorr";
case 8: return "nTorr";
default: return "<undefined>";
}
}
qreal factorFromConfig(int value)
{
switch(value)
{
case 0: return 1.0e-3; // Bar
case 1: return 1.0; // mBar
case 2: return 1.0e3; // µBar
case 3: return 1.0e6; // nBar
case 4: return 0.75e-3; // kTorr
case 5: return 0.75; // Torr
case 6: return 0.75e3; // mTorr
case 7: return 0.75e6; // µTorr
case 8: return 0.75e9; // nTorr
default: return 1.0;
}
}
void setValue(QLabel *w, qreal value)
{
w->setText(QString::number(value, 'g', 3));
}
......@@ -3,23 +3,85 @@
#include <QtCore>
#include <QLabel>
enum MsgType {
MsgPing = 0,
MsgState = 1 << 0,
MsgMeasurement = 1 << 1,
MsgError = 1 << 2, // needed?
};
struct Config {
QString networkIP;
int networkPort;
bool displayModeDynamic;
int displayModeDynamic;
int pressureUnitType;
int gaugeTypeGrat;
int gaugeTypeMcp;
int gaugeTypeVv;
bool modeWet;
Config();
Config(Config &other);
Config(Config *other);
void operator=(Config &other);
};
// what ino/client intend
struct StateMessage { // repr: 1 + 1(header) bytes
bool operable;
bool override;
bool error;