init branch
This commit is contained in:
@ -1,14 +1,50 @@
|
||||
#include "alarmsettingsdialog.h"
|
||||
#include "ui_alarmsettingsdialog.h"
|
||||
|
||||
AlarmSettingsDialog::AlarmSettingsDialog(QWidget *parent) :
|
||||
QDialog(parent),
|
||||
ui(new Ui::AlarmSettingsDialog)
|
||||
#include <QFileDialog>
|
||||
|
||||
AlarmSettingsDialog::AlarmSettingsDialog(AlarmTime* almNight, AlarmTime* almAlarm, QSettings* settings, QWidget *parent): QDialog(parent), ui(new Ui::AlarmSettingsDialog), almNight_(almNight), almAlarm_(almAlarm), settings_(settings)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
//restore settings
|
||||
ui->checkBox_Alarm->setChecked(settings_->value("Alarms/alarmOn").toBool());
|
||||
ui->checkBox_Sunrise->setChecked(settings_->value("Alarms/sunrise").toBool());
|
||||
ui->timeEdit_Shutdown->setTime(settings_->value("Alarms/shutdownTime").toTime());
|
||||
ui->timeEdit_Alarm->setTime(settings_->value("Alarms/alarmTime").toTime());
|
||||
ui->lineEdit->setText(settings_->value("Alarms/alarmSoundFile").toString());
|
||||
|
||||
connect(ui->pushButton_changeFile, SIGNAL(clicked()), this, SLOT(showFileChooser()));
|
||||
}
|
||||
|
||||
AlarmSettingsDialog::~AlarmSettingsDialog()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void AlarmSettingsDialog::accept()
|
||||
{
|
||||
//store settings
|
||||
settings_->setValue("Alarms/alarmOn", ui->checkBox_Alarm->checkState());
|
||||
settings_->setValue("Alarms/sunrise", ui->checkBox_Sunrise->checkState());
|
||||
settings_->setValue("Alarms/shutdownTime", ui->timeEdit_Shutdown->time());
|
||||
settings_->setValue("Alarms/alarmTime", ui->timeEdit_Alarm->time());
|
||||
settings_->setValue("Alarms/alarmSoundFile", ui->lineEdit->text());
|
||||
|
||||
//send signals
|
||||
signalAlarmSoundFile(ui->lineEdit->text());
|
||||
signalSunrise(ui->checkBox_Sunrise->checkState());
|
||||
|
||||
//modify alarm objects
|
||||
almAlarm_->changeTime(ui->timeEdit_Alarm->time());
|
||||
almNight_->changeTime(ui->timeEdit_Shutdown->time());
|
||||
|
||||
QDialog::accept();
|
||||
}
|
||||
|
||||
void AlarmSettingsDialog::showFileChooser()
|
||||
{
|
||||
ui->lineEdit->setText(QFileDialog::getOpenFileName(this));
|
||||
}
|
||||
|
||||
|
||||
|
@ -2,6 +2,11 @@
|
||||
#define ALARMSETTINGSDIALOG_H
|
||||
|
||||
#include <QDialog>
|
||||
#include <QTime>
|
||||
#include <QString>
|
||||
#include <QSettings>
|
||||
|
||||
#include "alarmtime.h"
|
||||
|
||||
namespace Ui {
|
||||
class AlarmSettingsDialog;
|
||||
@ -11,10 +16,26 @@ class AlarmSettingsDialog : public QDialog
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
AlarmTime* almNight_;
|
||||
AlarmTime* almAlarm_;
|
||||
QSettings* settings_;
|
||||
|
||||
|
||||
public:
|
||||
explicit AlarmSettingsDialog(QWidget *parent = 0);
|
||||
explicit AlarmSettingsDialog(AlarmTime* almNight, AlarmTime* almAlarm, QSettings* settings, QWidget* parent = nullptr);
|
||||
~AlarmSettingsDialog();
|
||||
|
||||
signals:
|
||||
void signalAlarmSoundFile(QString fileName);
|
||||
void signalSunrise(bool enabled);
|
||||
|
||||
public slots:
|
||||
void accept();
|
||||
|
||||
private slots:
|
||||
void showFileChooser();
|
||||
|
||||
|
||||
private:
|
||||
Ui::AlarmSettingsDialog *ui;
|
||||
};
|
||||
|
@ -3,8 +3,7 @@
|
||||
AlarmTime::AlarmTime(const QTime time, QObject *parent) : QObject(parent), time_(time)
|
||||
{
|
||||
connect(&timer, SIGNAL(timeout()), this, SLOT(doTick()));
|
||||
timer.setInterval(500);
|
||||
|
||||
timer.setInterval(1000);
|
||||
}
|
||||
|
||||
AlarmTime::~AlarmTime()
|
||||
@ -15,21 +14,16 @@ AlarmTime::~AlarmTime()
|
||||
void AlarmTime::run()
|
||||
{
|
||||
abort();
|
||||
loop.reset(new QEventLoop);
|
||||
|
||||
timer.start();
|
||||
|
||||
qDebug()<<"Start Alarm Time Manager\n";
|
||||
loop->exec();
|
||||
}
|
||||
|
||||
|
||||
void AlarmTime::abort()
|
||||
{
|
||||
timer.stop();
|
||||
if (!loop.isNull()){
|
||||
loop->quit();
|
||||
}
|
||||
qDebug()<<"Stop Alarm Time Manager\n";
|
||||
}
|
||||
|
||||
@ -37,7 +31,7 @@ void AlarmTime::doTick()
|
||||
{
|
||||
if(time_.hour() == QTime::currentTime().hour() && time_.minute() == QTime::currentTime().minute() && triggerd_==false )
|
||||
{
|
||||
qDebug()<<"Trigger\n";
|
||||
qDebug()<<"Trigger\n";
|
||||
triggerd_=true;
|
||||
trigger();
|
||||
}
|
||||
|
@ -17,10 +17,9 @@ private:
|
||||
bool triggerd_ = false;
|
||||
QTime time_;
|
||||
QTimer timer;
|
||||
QScopedPointer<QEventLoop> loop;
|
||||
|
||||
public:
|
||||
explicit AlarmTime(const QTime time = QTime::currentTime(), QObject *parent = 0);
|
||||
explicit AlarmTime(const QTime time = QTime::currentTime(), QObject *parent = nullptr);
|
||||
~AlarmTime();
|
||||
|
||||
signals:
|
||||
|
@ -15,11 +15,11 @@
|
||||
#include "microcontroller.h"
|
||||
|
||||
|
||||
class AmpManager : public QObject, public QRunnable
|
||||
class AmpManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit AmpManager(Microcontroller *micro, int relayNumber, QObject *parent = 0);
|
||||
explicit AmpManager(Microcontroller *micro, int relayNumber, QObject *parent = nullptr);
|
||||
~AmpManager();
|
||||
|
||||
|
||||
@ -27,6 +27,8 @@ public:
|
||||
public slots:
|
||||
void run();
|
||||
void abort();
|
||||
|
||||
private slots:
|
||||
void doTick();
|
||||
|
||||
private:
|
||||
|
52
main.cpp
52
main.cpp
@ -17,7 +17,8 @@
|
||||
#include "mainwindow.h"
|
||||
#include "relaydialog.h"
|
||||
#include "ampmanager.h"
|
||||
#include "power.h"
|
||||
#include "alarmactions.h"
|
||||
#include "alarmsettingsdialog.h"
|
||||
|
||||
#define BAUD QSerialPort::Baud38400
|
||||
|
||||
@ -29,7 +30,7 @@ int main(int argc, char *argv[])
|
||||
QCoreApplication::setOrganizationName("UVOS");
|
||||
QCoreApplication::setOrganizationDomain("uvos.xyz");
|
||||
QCoreApplication::setApplicationName("SHinterface");
|
||||
QCoreApplication::setApplicationVersion("0.2");
|
||||
QCoreApplication::setApplicationVersion("0.5");
|
||||
|
||||
QDir::setCurrent(a.applicationDirPath());
|
||||
|
||||
@ -54,8 +55,6 @@ int main(int argc, char *argv[])
|
||||
|
||||
QSettings settings;
|
||||
|
||||
|
||||
|
||||
//connect to microcontoler
|
||||
Microcontroller micro;
|
||||
if(parser.isSet(tcpOption))
|
||||
@ -89,41 +88,38 @@ int main(int argc, char *argv[])
|
||||
else micro.setIODevice(microPort);
|
||||
}
|
||||
|
||||
Power power(&settings, µ);
|
||||
|
||||
|
||||
|
||||
AlarmActions alarmActions(&settings, µ);
|
||||
|
||||
AmpManager amp(µ, 0);
|
||||
|
||||
MainWindow w(&settings, µ, parser.isSet(secondaryOption));
|
||||
QObject::connect(µ, SIGNAL(textRecived(QString)), &w, SLOT(changeHeaderLableText(QString)));
|
||||
QObject::connect(µ, SIGNAL(relayStateChanged(std::vector<bool>)), &w, SLOT(relayStateChanged(std::vector<bool>)));
|
||||
|
||||
RelayDialog relayDialog(µ);
|
||||
QObject::connect(µ, SIGNAL(relayStateChanged(std::vector<bool>)), &relayDialog, SLOT(relayStateChanged(std::vector<bool>)));
|
||||
|
||||
//Alarms
|
||||
AlarmTime almNight(settings.value("nightTime").toTime());
|
||||
AlarmTime almAlarm(settings.value("alarmTime").toTime());
|
||||
|
||||
AlarmTime *almAlarm = new AlarmTime(settings.value("alarmTime").toTime());
|
||||
QSignalMapper signalMapper;
|
||||
//mainwindow
|
||||
MainWindow w(µ, parser.isSet(secondaryOption));
|
||||
QObject::connect(µ, SIGNAL(textRecived(QString)), &w, SLOT(changeHeaderLableText(QString)));
|
||||
QObject::connect(µ, SIGNAL(relayStateChanged(std::vector<bool>)), &w, SLOT(relayStateChanged(std::vector<bool>)));
|
||||
QObject::connect(µ, SIGNAL(auxStateChanged(int)), &w, SLOT(auxStateChanged(int)));
|
||||
|
||||
//dialogs
|
||||
AlarmSettingsDialog alarmDialog(&almNight, &almAlarm, &settings, &w);
|
||||
|
||||
RelayDialog relayDialog(µ, &w);
|
||||
QObject::connect(µ, SIGNAL(relayStateChanged(std::vector<bool>)), &relayDialog, SLOT(relayStateChanged(std::vector<bool>)));
|
||||
|
||||
if(!parser.isSet(secondaryOption))
|
||||
{
|
||||
|
||||
QObject::connect(&almNight, SIGNAL(trigger()), &power, SLOT(syncoff()));
|
||||
QObject::connect(&almNight, SIGNAL(trigger()), &alarmActions, SLOT(syncoff()));
|
||||
QObject::connect(&w, SIGNAL(signalAlmNightChanged(QTime)), &almNight, SLOT(changeTime(QTime)));
|
||||
QObject::connect(&w, SIGNAL(signalAlmNightStateChanged(int)), &almNight, SLOT(runOrAbort(int)));
|
||||
//QMetaObject::invokeMethod(&almNight, "run", Qt::QueuedConnection );
|
||||
almNight.run();
|
||||
|
||||
|
||||
QObject::connect(almAlarm, SIGNAL(trigger()), &signalMapper, SLOT(map()));
|
||||
signalMapper.setMapping(almAlarm, 4);
|
||||
QObject::connect(&signalMapper, SIGNAL(mapped(int)), µ, SLOT(setPattern(int)));
|
||||
QObject::connect(&w, SIGNAL(signalAlmAlarmChanged(QTime)), almAlarm, SLOT(changeTime(QTime)));
|
||||
QObject::connect(&w, SIGNAL(signalAlmAlarmStateChanged(int)), almAlarm, SLOT(runOrAbort(int)));
|
||||
//QMetaObject::invokeMethod(almAlarm, "run", Qt::QueuedConnection );
|
||||
QObject::connect(&almAlarm, &AlarmTime::trigger, µ, &Microcontroller::startSunrise);
|
||||
QObject::connect(&w, SIGNAL(signalAlmAlarmChanged(QTime)), &almAlarm, SLOT(changeTime(QTime)));
|
||||
QObject::connect(&w, SIGNAL(signalAlmAlarmStateChanged(int)), &almAlarm, SLOT(runOrAbort(int)));
|
||||
almAlarm.run();
|
||||
|
||||
//Amplifyer
|
||||
QObject::connect(&w, SIGNAL(signalAmpOn()), &, SLOT(run()));
|
||||
@ -131,12 +127,12 @@ int main(int argc, char *argv[])
|
||||
QMetaObject::invokeMethod(&, "run", Qt::QueuedConnection );
|
||||
}
|
||||
|
||||
//Advanced Relays
|
||||
//show dialogs
|
||||
QObject::connect(&w, SIGNAL(showAdvRelayDialog()), &relayDialog, SLOT(show()));
|
||||
QObject::connect(&w, &MainWindow::showAlmSettingsDialog, &alarmDialog, &AlarmSettingsDialog::show);
|
||||
|
||||
QMetaObject::invokeMethod(µ, "run", Qt::QueuedConnection );
|
||||
|
||||
QMetaObject::invokeMethod(&w, "postActivate", Qt::QueuedConnection );
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
|
@ -1,21 +1,16 @@
|
||||
#include "mainwindow.h"
|
||||
#include "ui_mainwindow.h"
|
||||
|
||||
MainWindow::MainWindow(QSettings *settings, Microcontroller *micro , bool isRemoteMode , QWidget *parent) :
|
||||
MainWindow::MainWindow(Microcontroller *micro , bool isRemoteMode , QWidget *parent) :
|
||||
QMainWindow(parent),
|
||||
ui(new Ui::MainWindow),
|
||||
colorChooser(this),
|
||||
_settings(settings),
|
||||
_micro(micro)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
if(!_micro->connected()) ui->label_serialRecive->setText("No IO Port! Debug only.");
|
||||
|
||||
//Settings
|
||||
ui->alarmTime->setTime(_settings->value("alarmTime").toTime());
|
||||
ui->nightTime->setTime(_settings->value("nightTime").toTime());
|
||||
ui->checkBox_alarm->setChecked(_settings->value("alarmOn").toBool());
|
||||
|
||||
//RGB Leds
|
||||
connect(ui->presettApply, SIGNAL(clicked()), this, SLOT(slotApplyPreset()));
|
||||
@ -32,11 +27,16 @@ MainWindow::MainWindow(QSettings *settings, Microcontroller *micro , bool isRemo
|
||||
new QListWidgetItem(tr("Pattern 4 Sunrise"), ui->listWidget_patern);
|
||||
|
||||
|
||||
//Desk light
|
||||
|
||||
connect(ui->horizontalSlider_deskLight, &QAbstractSlider::valueChanged, _micro, [this](int value){ _micro->setAuxPwm(value); });
|
||||
|
||||
//Relays
|
||||
_relayCheckBoxes.push_back(ui->checkBox_amp);
|
||||
_relayCheckBoxes.push_back(ui->checkBox_bspeaker);
|
||||
_relayCheckBoxes.push_back(ui->checkBox_inf);
|
||||
_relayCheckBoxes.push_back(ui->checkBox_aircon);
|
||||
_relayCheckBoxes.push_back(ui->checkBox_SolderingIorn);
|
||||
_relayCheckBoxes.push_back(ui->checkBox_testEquitmpent);
|
||||
|
||||
for(unsigned int i = 0; i < _relayCheckBoxes.size(); i++) connect(_relayCheckBoxes[i], SIGNAL(stateChanged(int)), this, SLOT(relayCheckBoxToggeled(int)));
|
||||
|
||||
@ -47,37 +47,17 @@ MainWindow::MainWindow(QSettings *settings, Microcontroller *micro , bool isRemo
|
||||
//Bedroom Speakers
|
||||
if(!isRemoteMode)connect(ui->checkBox_bspeakerAuto, SIGNAL(stateChanged(int)), this, SLOT(slotBSpeakerAutoToggle(int)));
|
||||
|
||||
|
||||
//Infinity Mirror
|
||||
if(!isRemoteMode)connect(ui->checkBox_infAuto, SIGNAL(stateChanged(int)), this, SLOT(slotInfMirrorAutoToggle(int)));
|
||||
|
||||
if(!isRemoteMode)
|
||||
{
|
||||
//Alarm
|
||||
connect(ui->alarmTime, SIGNAL(timeChanged(QTime)), this, SLOT(slotChangedAlarmTime(QTime)));
|
||||
connect(ui->checkBox_alarm, SIGNAL(stateChanged(int)), this, SIGNAL(signalAlmAlarmStateChanged(int)));
|
||||
connect(ui->checkBox_alarm, SIGNAL(stateChanged(int)), this, SLOT(saveAlarmState(int)));
|
||||
|
||||
|
||||
|
||||
//Night Time
|
||||
connect(ui->nightTime, SIGNAL(timeChanged(QTime)), this, SLOT(slotChangedNightTime(QTime)));
|
||||
connect(ui->checkBox_nightTime, SIGNAL(stateChanged(int)), this, SIGNAL(signalAlmNightStateChanged(int)));
|
||||
}
|
||||
else remoteMode();
|
||||
//adv relays
|
||||
|
||||
//dialogs
|
||||
connect(ui->button_advRelay, SIGNAL(clicked()), this, SIGNAL(showAdvRelayDialog()));
|
||||
connect(ui->pushButton_alarms, SIGNAL(clicked()), this, SIGNAL(showAlmSettingsDialog()));
|
||||
}
|
||||
|
||||
void MainWindow::remoteMode()
|
||||
{
|
||||
ui->alarmTime->setEnabled(false);
|
||||
ui->checkBox_alarm->setEnabled(false);
|
||||
ui->pushButton_alarm->setEnabled(false);
|
||||
|
||||
ui->nightTime->setEnabled(false);
|
||||
ui->checkBox_nightTime->setEnabled(false);
|
||||
ui->label_nightTime->setEnabled(false);
|
||||
|
||||
ui->checkBox_ampAuto->setEnabled(false);
|
||||
ui->checkBox_ampAuto->setChecked(false);
|
||||
@ -90,15 +70,8 @@ void MainWindow::remoteMode()
|
||||
ui->checkBox_infAuto->setChecked(false);
|
||||
}
|
||||
|
||||
void MainWindow::postActivate()
|
||||
{
|
||||
QMetaObject::invokeMethod( this, "signalAlmNightStateChanged", Qt::QueuedConnection, Q_ARG(int, ui->checkBox_nightTime->checkState()) );
|
||||
signalAlmAlarmStateChanged(ui->checkBox_alarm->checkState());
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
{
|
||||
_settings->sync();
|
||||
delete ui;
|
||||
}
|
||||
|
||||
@ -145,7 +118,7 @@ void MainWindow::slotBSpeakerAutoToggle(int state)
|
||||
|
||||
void MainWindow::slotDoorOpenTimeout()
|
||||
{
|
||||
ui->checkBox_doorOpen->setChecked(true);
|
||||
//ui->checkBox_doorOpen->setChecked(true);
|
||||
}
|
||||
|
||||
void MainWindow::slotInfMirrorAutoToggle(int state)
|
||||
@ -157,21 +130,11 @@ void MainWindow::slotInfMirrorAutoToggle(int state)
|
||||
}
|
||||
}
|
||||
|
||||
void MainWindow::slotChangedAlarmTime(const QTime time)
|
||||
void MainWindow::auxStateChanged(int value)
|
||||
{
|
||||
_settings->setValue("alarmTime", time);
|
||||
signalAlmAlarmChanged(time);
|
||||
}
|
||||
|
||||
void MainWindow::saveAlarmState(int state)
|
||||
{
|
||||
_settings->setValue("alarmOn", state);
|
||||
}
|
||||
|
||||
void MainWindow::slotChangedNightTime(const QTime time)
|
||||
{
|
||||
_settings->setValue("nightTime", time);
|
||||
signalAlmNightChanged(time);
|
||||
ui->horizontalSlider_deskLight->blockSignals(true);
|
||||
ui->horizontalSlider_deskLight->setValue(value);
|
||||
ui->horizontalSlider_deskLight->blockSignals(false);
|
||||
}
|
||||
|
||||
void MainWindow::slotAmpAutoToggle(int state)
|
||||
|
21
mainwindow.h
21
mainwindow.h
@ -4,7 +4,6 @@
|
||||
#include <QMainWindow>
|
||||
#include <QColorDialog>
|
||||
#include <QListWidgetItem>
|
||||
#include <QSettings>
|
||||
#include <QTime>
|
||||
#include <vector>
|
||||
#include "alarmtime.h"
|
||||
@ -21,7 +20,7 @@ class MainWindow : public QMainWindow
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit MainWindow(QSettings *settings, Microcontroller *micro, bool isRemoteMode = false, QWidget *parent = 0);
|
||||
explicit MainWindow(Microcontroller *micro, bool isRemoteMode = false, QWidget *parent = nullptr);
|
||||
~MainWindow();
|
||||
|
||||
|
||||
@ -31,8 +30,6 @@ private:
|
||||
|
||||
QColorDialog colorChooser;
|
||||
|
||||
QSettings *_settings;
|
||||
|
||||
Microcontroller *_micro;
|
||||
|
||||
std::vector<QAbstractButton*> _relayCheckBoxes;
|
||||
@ -44,18 +41,12 @@ signals:
|
||||
void signalAmpOn();
|
||||
void signalAmpOff();
|
||||
|
||||
void signalAlmNightStateChanged(int state);
|
||||
void signalAlmNightChanged(const QTime time);
|
||||
|
||||
void signalAlmAlarmStateChanged(int state);
|
||||
void signalAlmAlarmChanged(const QTime time);
|
||||
void showAlmSettingsDialog();
|
||||
|
||||
void showAdvRelayDialog();
|
||||
|
||||
private slots:
|
||||
|
||||
void postActivate();
|
||||
|
||||
//RGB
|
||||
void slotChangedRgb(const QColor color);
|
||||
void slotApplyPreset();
|
||||
@ -70,19 +61,13 @@ private slots:
|
||||
void slotBSpeakerAutoToggle(int state);
|
||||
void slotInfMirrorAutoToggle(int state);
|
||||
|
||||
//Alarm
|
||||
void slotChangedAlarmTime(const QTime time);
|
||||
void saveAlarmState(int state);
|
||||
|
||||
//Night
|
||||
void slotChangedNightTime(const QTime time);
|
||||
|
||||
//door
|
||||
void slotDoorOpenTimeout();
|
||||
|
||||
public slots:
|
||||
|
||||
void relayStateChanged(std::vector<bool> relayStates);
|
||||
void auxStateChanged(int value);
|
||||
|
||||
};
|
||||
|
||||
|
@ -42,13 +42,37 @@ void Microcontroller::changeRgbColor(const QColor color)
|
||||
std::cout<<buffer;
|
||||
}
|
||||
|
||||
void Microcontroller::setAuxPwm(int duty)
|
||||
{
|
||||
if(duty != _auxState)
|
||||
{
|
||||
if(duty > 1)
|
||||
{
|
||||
if(_auxState <= 1 && _port != nullptr) _port->write("aux on\n");
|
||||
char buffer[64];
|
||||
int length = sprintf(buffer, "aux set %03d \n", duty );
|
||||
if(_port != nullptr) _port->write(buffer, length);
|
||||
}
|
||||
else if(_port != nullptr)_port->write("aux off\n");
|
||||
}
|
||||
_auxState = duty;
|
||||
}
|
||||
|
||||
void Microcontroller::setPattern(int pattern)
|
||||
{
|
||||
char buffer[4];
|
||||
std::cout<<"pattern apply\n";
|
||||
_port->write("rgb pattern ", sizeof("rgb pattern ")-1);
|
||||
int length = sprintf(buffer, "%d \n", pattern);
|
||||
_port->write(buffer, length);
|
||||
if(_port != nullptr)
|
||||
{
|
||||
char buffer[4];
|
||||
std::cout<<"pattern apply\n";
|
||||
_port->write("rgb pattern ", sizeof("rgb pattern ")-1);
|
||||
int length = sprintf(buffer, "%d \n", pattern);
|
||||
_port->write(buffer, length);
|
||||
}
|
||||
}
|
||||
|
||||
void Microcontroller::startSunrise()
|
||||
{
|
||||
setPattern(4);
|
||||
}
|
||||
|
||||
bool Microcontroller::connected()
|
||||
@ -72,9 +96,13 @@ void Microcontroller::run()
|
||||
|
||||
void Microcontroller::requestState()
|
||||
{
|
||||
if(_port != nullptr) _port->write("relay state\n", sizeof("relay state\n"));
|
||||
if(_port != nullptr) _port->write("state\n", sizeof("state\n"));
|
||||
}
|
||||
|
||||
void Microcontroller::requestRelayList()
|
||||
{
|
||||
if(_port != nullptr) _port->write("relay list\n", sizeof("relay list\n"));
|
||||
}
|
||||
|
||||
void Microcontroller::abort()
|
||||
{
|
||||
@ -121,20 +149,27 @@ void Microcontroller::processMicroReturn()
|
||||
|
||||
workbuff.remove(0, 2);
|
||||
QStringList workbufList = workbuff.split(',');
|
||||
int numberOfRelays = workbufList[0].toInt();
|
||||
if(workbufList.size() >= numberOfRelays+1)
|
||||
|
||||
//aux state
|
||||
if(workbufList[0].toInt() != _auxState)
|
||||
{
|
||||
_auxState = workbufList[0].toInt();
|
||||
auxStateChanged(_auxState);
|
||||
}
|
||||
|
||||
//relay state
|
||||
uint_fast8_t numberOfRelays = workbufList[1].toInt();
|
||||
if(workbufList.size() >= numberOfRelays+2)
|
||||
{
|
||||
bool hasChanged = false;
|
||||
_relayStates.resize(numberOfRelays, false);
|
||||
for(int i = 0; i < numberOfRelays; i++)
|
||||
for(uint_fast8_t i = 0; i < numberOfRelays; i++)
|
||||
{
|
||||
if(_relayStates[i] != (bool)workbufList[i+1].toInt())
|
||||
if(_relayStates[i] != static_cast<bool>(workbufList[i+2].toInt()))
|
||||
{
|
||||
_relayStates[i] = (bool)workbufList[i+1].toInt();
|
||||
hasChanged = true;
|
||||
_relayStates[i] = static_cast<bool>(workbufList[i+2].toInt());
|
||||
relayStateChanged(Relay(this, i));
|
||||
}
|
||||
}
|
||||
if(hasChanged)relayStateChanged(_relayStates);
|
||||
}
|
||||
}
|
||||
else if(workbuff.contains("Door Open Warning"))
|
||||
@ -143,7 +178,7 @@ void Microcontroller::processMicroReturn()
|
||||
}
|
||||
else if(workbuff.size() > 2 && workbuff[0]=='D' && workbuff[1]=='2')
|
||||
{
|
||||
if(workbuff[3] == 'O') doorOpen(1);
|
||||
if(workbuff[3] == 'O') doorOpened(1);
|
||||
else if(workbuff[3] == 'C') doorClosed(1);
|
||||
}
|
||||
}
|
||||
|
@ -13,13 +13,21 @@
|
||||
#include <QTimer>
|
||||
#include <QAbstractButton>
|
||||
#include <vector>
|
||||
#include "relay.h"
|
||||
|
||||
class Microcontroller: public QObject
|
||||
class Microcontroller : public QObject
|
||||
{
|
||||
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
|
||||
static constexpr char AMP_RELAY = 0;
|
||||
|
||||
private:
|
||||
|
||||
std::vector<bool> _relayStates; //ugh vector of bools
|
||||
int _auxState = 0;
|
||||
QIODevice* _port = nullptr;
|
||||
|
||||
QScopedPointer<QEventLoop> loop;
|
||||
@ -41,10 +49,15 @@ public slots:
|
||||
void rgbOff();
|
||||
void changeRgbColor(const QColor color);
|
||||
void setPattern(int pattern);
|
||||
void startSunrise();
|
||||
|
||||
void requestRelayList();
|
||||
|
||||
void setAuxPwm(int duty);
|
||||
|
||||
void relayToggle(int state, int id);
|
||||
void relayOn(int relay);
|
||||
void relayOff(int relay);
|
||||
void relayToggle(bool state, int id);
|
||||
|
||||
void run();
|
||||
void abort();
|
||||
@ -52,7 +65,9 @@ public slots:
|
||||
|
||||
signals:
|
||||
void textRecived(const QString string);
|
||||
void relayStateChanged(std::vector<bool> relayStates);
|
||||
void relayStateChanged(Relay relay);
|
||||
void auxStateChanged(int value);
|
||||
void gotRelayList(std::vector<Relay> relays);
|
||||
void doorOpenTimeout();
|
||||
void doorOpened(int id);
|
||||
void doorClosed(int id);
|
||||
|
48
relay.h
48
relay.h
@ -1,14 +1,42 @@
|
||||
#pragma once
|
||||
#ifndef RELAY_H
|
||||
#define RELAY_H
|
||||
|
||||
#include "serial_io.h"
|
||||
#include<stdint.h>
|
||||
#include<QObject>
|
||||
#include<QString>
|
||||
#include<vector>
|
||||
#include<memory>
|
||||
|
||||
static void relay_toggle(bool state, int id, int serial)
|
||||
#include"actor.h"
|
||||
|
||||
class Microcontroller;
|
||||
|
||||
class Relay : public QObject
|
||||
{
|
||||
char buffer[8];
|
||||
int length = sprintf(buffer, "%d \n", id);
|
||||
state ? write(serial, "relay on ", sizeof("relay on ")-1) : write(serial, "relay off ", sizeof("relay off ")-1);
|
||||
state ? std::cout<<"relay on " : std::cout<<"relay off ";
|
||||
std::cout<<buffer;
|
||||
sWrite(serial, buffer, length);
|
||||
}
|
||||
Q_OBJECT
|
||||
private:
|
||||
Microcontroller* micro_;
|
||||
QString name_;
|
||||
bool state_ = false;
|
||||
uint8_t id_;
|
||||
uint16_t address_;
|
||||
std::vector< std::unique_ptr<Actor> > actors_;
|
||||
bool actorsActive_ = true;
|
||||
|
||||
public slots:
|
||||
void on();
|
||||
void off();
|
||||
void toggle();
|
||||
|
||||
public:
|
||||
Relay(Microcontroller* micro, uint8_t id, QString name = "", uint16_t address = 0, QObject *parent = nullptr);
|
||||
void addActor(std::unique_ptr<Actor> actor);
|
||||
void setState(bool state);
|
||||
bool hasActors();
|
||||
void setActorsActive(bool in);
|
||||
QString getName();
|
||||
void setName(QString name);
|
||||
std::vector< std::unique_ptr<Actor> >* getActors();
|
||||
~Relay();
|
||||
};
|
||||
#endif // RELAY_H
|
||||
|
@ -10,13 +10,10 @@ RelayDialog::RelayDialog(Microcontroller *micro, QWidget *parent) :
|
||||
|
||||
_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(stateChanged(int)), this, SLOT(relayCheckBoxToggeled(int)));
|
||||
|
||||
_micro->relayOn(STARTING_RELAY);
|
||||
_micro->relayOn(STARTING_RELAY+1);
|
||||
|
||||
}
|
||||
|
||||
@ -29,12 +26,24 @@ RelayDialog::~RelayDialog()
|
||||
void RelayDialog::relayCheckBoxToggeled(int state)
|
||||
{
|
||||
for(unsigned int i = 0; i < _relayCheckBoxes.size(); i++)
|
||||
if(_relayCheckBoxes[i] == sender())_micro->relayToggle(state, i+STARTING_RELAY);
|
||||
{
|
||||
|
||||
if(_relayCheckBoxes[i] == sender())
|
||||
{
|
||||
std::cerr<<state<<" "<<i<<std::endl;
|
||||
if(state == 2 && i == 0)
|
||||
{
|
||||
std::cerr<<"primeing lambda\n";
|
||||
QTimer::singleShot(3600000, _micro, [this](){ _micro->relayOff(STARTING_RELAY); _relayCheckBoxes[0]->setChecked(false);});
|
||||
}
|
||||
_micro->relayToggle(state, i+STARTING_RELAY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RelayDialog::relayStateChanged(std::vector<bool> relayStates)
|
||||
{
|
||||
if(relayStates.size() >= STARTING_RELAY+4) for(unsigned int i = 0; i < _relayCheckBoxes.size(); i++)
|
||||
if(relayStates.size() > STARTING_RELAY) for(unsigned int i = 0; i < relayStates.size()-STARTING_RELAY; i++)
|
||||
{
|
||||
_relayCheckBoxes[i]->blockSignals(true);
|
||||
_relayCheckBoxes[i]->setChecked(relayStates[i+STARTING_RELAY]);
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include <QAbstractButton>
|
||||
#include "microcontroller.h"
|
||||
|
||||
#define STARTING_RELAY 4
|
||||
#define STARTING_RELAY 5
|
||||
|
||||
namespace Ui {
|
||||
class RelayDialog;
|
||||
|
Reference in New Issue
Block a user