Relay dialog hooked up

This commit is contained in:
IMback
2017-11-07 20:26:16 +01:00
parent 08dc57d385
commit 2c46e6ffb6
6 changed files with 102 additions and 13 deletions

View File

@ -89,7 +89,7 @@ int main(int argc, char *argv[])
MainWindow w(&settings, &micro, parser.isSet(secondaryOption));
RelayDialog relayDialog;
RelayDialog relayDialog(&micro);
if(!parser.isSet(secondaryOption))
{

View File

@ -2,6 +2,7 @@
Microcontroller::Microcontroller(QIODevice* port): _port(port)
{
getState();
}
Microcontroller::Microcontroller()
@ -16,6 +17,7 @@ Microcontroller::~Microcontroller()
void Microcontroller::setIODevice(QIODevice *port)
{
_port = port;
getState();
}
void Microcontroller::relayToggle(int state, int relay)
@ -84,6 +86,11 @@ void Microcontroller::run()
loop->exec();
}
void Microcontroller::getState()
{
if(_port != nullptr) _port->write("relay state\n", sizeof("relay state\n"));
}
void Microcontroller::abort()
{
@ -93,6 +100,29 @@ void Microcontroller::abort()
}
}
void Microcontroller::processMicroReturn()
{
QString workbuff = _buffer;
workbuff.remove(0, 2);
QStringList workbufList = workbuff.split(',');
int numberOfRelays = workbufList[0].toInt();
if(workbufList.size() >= numberOfRelays+1)
{
bool hasChanged = false;
_relayStates.resize(numberOfRelays, false);
for(int i = 0; i < numberOfRelays; i++)
{
if(_relayStates[i] != (bool)workbufList[i+1].toInt())
{
_relayStates[i] = (bool)workbufList[i+1].toInt();
hasChanged = true;
}
}
if(hasChanged)relayStateChanged(_relayStates);
}
}
void Microcontroller::doTick()
{
if(_port != nullptr)
@ -101,10 +131,12 @@ void Microcontroller::doTick()
while(_port->getChar(&charBuf))
{
_buffer.push_back(charBuf);
std::cout<<charBuf<<std::endl;
if( _buffer.endsWith('\n') )
{
if(_buffer.size() > 2 && _buffer[0] == "S" && _buffer[1] == "T")
{
processMicroReturn();
}
textRecived(_buffer);
_buffer.clear();
}

View File

@ -11,17 +11,22 @@
#include <QScopedPointer>
#include <QEventLoop>
#include <QTimer>
#include <vector>
class Microcontroller: public QObject
{
Q_OBJECT
private:
std::vector<bool> _relayStates; //ugh vector of bools
QIODevice* _port = nullptr;
QScopedPointer<QEventLoop> loop;
QString _buffer;
void processMicroReturn();
void getState();
public:
Microcontroller(QIODevice* port);
Microcontroller();
@ -45,6 +50,7 @@ public slots:
signals:
void textRecived(const QString string);
void relayStateChanged(std::vector<bool> relayStates);
};

View File

@ -1,15 +1,43 @@
#include "relaydialog.h"
#include "ui_relaydialog.h"
RelayDialog::RelayDialog(QWidget *parent) :
RelayDialog::RelayDialog(Microcontroller *micro, QWidget *parent) :
QDialog(parent),
ui(new Ui::RelayDialog)
ui(new Ui::RelayDialog),
_micro(micro)
{
ui->setupUi(this);
_relayCheckBoxes.push_back(ui->checkBox_R0);
_relayCheckBoxes.push_back(ui->checkBox_R1);
_relayCheckBoxes.push_back(ui->checkBox_R2);
_relayCheckBoxes.push_back(ui->checkBox_R3);
for(unsigned int i = 0; i < _relayCheckBoxes.size(); i++) connect(_relayCheckBoxes[i], SIGNAL(toggled(bool)), this, SLOT(relayCheckBoxToggeled(bool)));
_micro->relayOn(STARTING_RELAY);
_micro->relayOn(STARTING_RELAY+1);
}
RelayDialog::~RelayDialog()
{
delete ui;
}
void RelayDialog::relayCheckBoxToggeled(bool checked)
{
for(unsigned int i = 0; i < _relayCheckBoxes.size(); i++)
{
if(_relayCheckBoxes[i] == sender())
{
checked ? _micro->relayOn(i+4) : _micro->relayOff(i+4);
}
}
}
void RelayDialog::relayStateChanged(std::vector<bool> relayStates)
{
if(relayStates.size() >= 8) for(unsigned int i = 0; i < _relayCheckBoxes.size(); i++) _relayCheckBoxes[i]->setChecked(relayStates[i+4]);
}

View File

@ -2,6 +2,12 @@
#define RELAYDIALOG_H
#include <QDialog>
#include <QList>
#include <vector>
#include <QAbstractButton>
#include "microcontroller.h"
#define STARTING_RELAY 4
namespace Ui {
class RelayDialog;
@ -12,11 +18,22 @@ class RelayDialog : public QDialog
Q_OBJECT
public:
explicit RelayDialog(QWidget *parent = 0);
explicit RelayDialog(Microcontroller *micro, QWidget *parent = 0);
~RelayDialog();
private:
std::vector<QAbstractButton*> _relayCheckBoxes;
Ui::RelayDialog *ui;
Microcontroller *_micro;
private slots:
void relayCheckBoxToggeled(bool checked);
public slots:
void relayStateChanged(std::vector<bool> relayStates);
};
#endif // RELAYDIALOG_H

View File

@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>358</width>
<height>277</height>
<height>279</height>
</rect>
</property>
<property name="windowTitle">
@ -50,10 +50,13 @@
</spacer>
</item>
<item>
<widget class="QCheckBox" name="checkBox_3">
<widget class="QCheckBox" name="checkBox_R0">
<property name="text">
<string>On</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
@ -66,7 +69,7 @@
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<layout class="QHBoxLayout" name="horizontalLayout_R2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
@ -88,10 +91,13 @@
</spacer>
</item>
<item>
<widget class="QCheckBox" name="checkBox_4">
<widget class="QCheckBox" name="checkBox_R1">
<property name="text">
<string>On</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
@ -126,7 +132,7 @@
</spacer>
</item>
<item>
<widget class="QCheckBox" name="checkBox_5">
<widget class="QCheckBox" name="checkBox_R2">
<property name="text">
<string>On</string>
</property>
@ -146,7 +152,7 @@
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Aux 2</string>
<string>Aux 1</string>
</property>
</widget>
</item>
@ -164,7 +170,7 @@
</spacer>
</item>
<item>
<widget class="QCheckBox" name="checkBox_6">
<widget class="QCheckBox" name="checkBox_R3">
<property name="text">
<string>On</string>
</property>