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)); MainWindow w(&settings, &micro, parser.isSet(secondaryOption));
RelayDialog relayDialog; RelayDialog relayDialog(&micro);
if(!parser.isSet(secondaryOption)) if(!parser.isSet(secondaryOption))
{ {

View File

@ -2,6 +2,7 @@
Microcontroller::Microcontroller(QIODevice* port): _port(port) Microcontroller::Microcontroller(QIODevice* port): _port(port)
{ {
getState();
} }
Microcontroller::Microcontroller() Microcontroller::Microcontroller()
@ -16,6 +17,7 @@ Microcontroller::~Microcontroller()
void Microcontroller::setIODevice(QIODevice *port) void Microcontroller::setIODevice(QIODevice *port)
{ {
_port = port; _port = port;
getState();
} }
void Microcontroller::relayToggle(int state, int relay) void Microcontroller::relayToggle(int state, int relay)
@ -84,6 +86,11 @@ void Microcontroller::run()
loop->exec(); loop->exec();
} }
void Microcontroller::getState()
{
if(_port != nullptr) _port->write("relay state\n", sizeof("relay state\n"));
}
void Microcontroller::abort() 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() void Microcontroller::doTick()
{ {
if(_port != nullptr) if(_port != nullptr)
@ -101,10 +131,12 @@ void Microcontroller::doTick()
while(_port->getChar(&charBuf)) while(_port->getChar(&charBuf))
{ {
_buffer.push_back(charBuf); _buffer.push_back(charBuf);
std::cout<<charBuf<<std::endl;
if( _buffer.endsWith('\n') ) if( _buffer.endsWith('\n') )
{ {
if(_buffer.size() > 2 && _buffer[0] == "S" && _buffer[1] == "T")
{
processMicroReturn();
}
textRecived(_buffer); textRecived(_buffer);
_buffer.clear(); _buffer.clear();
} }

View File

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

View File

@ -1,15 +1,43 @@
#include "relaydialog.h" #include "relaydialog.h"
#include "ui_relaydialog.h" #include "ui_relaydialog.h"
RelayDialog::RelayDialog(QWidget *parent) : RelayDialog::RelayDialog(Microcontroller *micro, QWidget *parent) :
QDialog(parent), QDialog(parent),
ui(new Ui::RelayDialog) ui(new Ui::RelayDialog),
_micro(micro)
{ {
ui->setupUi(this); 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() RelayDialog::~RelayDialog()
{ {
delete ui; 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 #define RELAYDIALOG_H
#include <QDialog> #include <QDialog>
#include <QList>
#include <vector>
#include <QAbstractButton>
#include "microcontroller.h"
#define STARTING_RELAY 4
namespace Ui { namespace Ui {
class RelayDialog; class RelayDialog;
@ -12,11 +18,22 @@ class RelayDialog : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit RelayDialog(QWidget *parent = 0); explicit RelayDialog(Microcontroller *micro, QWidget *parent = 0);
~RelayDialog(); ~RelayDialog();
private: private:
std::vector<QAbstractButton*> _relayCheckBoxes;
Ui::RelayDialog *ui; Ui::RelayDialog *ui;
Microcontroller *_micro;
private slots:
void relayCheckBoxToggeled(bool checked);
public slots:
void relayStateChanged(std::vector<bool> relayStates);
}; };
#endif // RELAYDIALOG_H #endif // RELAYDIALOG_H

View File

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