replace Arecord with Qtmultimedia (requiers pulse?)

This commit is contained in:
IMback
2017-11-19 22:08:59 +01:00
parent a82a9459bc
commit 1dd7aca03b
16 changed files with 329 additions and 167 deletions

View File

@ -4,9 +4,7 @@
# #
#------------------------------------------------- #-------------------------------------------------
QT += core gui widgets network serialport QT += core gui widgets network serialport multimedia
greaterThan(QT_MAJOR_VERSION, 4): QT +=
TARGET = SHinterface TARGET = SHinterface
TEMPLATE = app TEMPLATE = app
@ -21,14 +19,22 @@ DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp mainwindow.cpp ampmanager.cpp alarmtime.cpp \ SOURCES += main.cpp mainwindow.cpp ampmanager.cpp alarmtime.cpp \
microcontroller.cpp \ microcontroller.cpp \
relaydialog.cpp relaydialog.cpp \
alarmsettingsdialog.cpp \
power.cpp
HEADERS += mainwindow.h \ HEADERS += mainwindow.h \
ampmanager.h \ ampmanager.h \
relay.h \ relay.h \
alarmtime.h \ alarmtime.h \
microcontroller.h \ microcontroller.h \
relaydialog.h relaydialog.h \
alarmsettingsdialog.h \
power.h
FORMS += mainwindow.ui \ FORMS += mainwindow.ui \
relaydialog.ui relaydialog.ui \
alarmsettingsdialog.ui
RESOURCES += \
resources.qrc

View File

@ -1,38 +1,109 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"> <ui version="4.0">
<author/>
<comment/>
<exportmacro/>
<class>AlarmSettingsDialog</class> <class>AlarmSettingsDialog</class>
<widget name="AlarmSettingsDialog" class="QDialog"> <widget class="QDialog" name="AlarmSettingsDialog">
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>423</width>
<height>300</height> <height>281</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Dialog</string> <string>Dialog</string>
</property> </property>
<widget name="buttonBox" class="QDialogButtonBox"> <layout class="QVBoxLayout" name="verticalLayout">
<property name="geometry"> <item>
<rect> <widget class="QLabel" name="label">
<x>30</x> <property name="text">
<y>240</y> <string>Sound File:</string>
<width>341</width> </property>
<height>32</height> </widget>
</rect> </item>
</property> <item>
<property name="orientation"> <layout class="QHBoxLayout" name="horizontalLayout">
<enum>Qt::Horizontal</enum> <property name="leftMargin">
</property> <number>0</number>
<property name="standardButtons"> </property>
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set> <property name="topMargin">
</property> <number>0</number>
</widget> </property>
<item>
<widget class="QLineEdit" name="lineEdit">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Change</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<property name="topMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Sunrise</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="checkBox">
<property name="text">
<string>Enable</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget> </widget>
<pixmapfunction/>
<resources/> <resources/>
<connections> <connections>
<connection> <connection>

View File

@ -1,19 +1,28 @@
#include "ampmanager.h" #include "ampmanager.h"
AmpManager::AmpManager(Microcontroller *micro, int relayNumber, QObject *parent) : QObject(parent), _micro(micro), _relayNumber(relayNumber) AmpManager::AmpManager(Microcontroller *micro, int relayNumber, QAudioDeviceInfo device, QObject *parent) : QObject(parent), _micro(micro), _relayNumber(relayNumber)
{ {
silenceCount = 0; QAudioFormat format;
format.setSampleRate(8000);
format.setChannelCount(1);
format.setSampleSize(8);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::UnSignedInt);
_audioDevice = new QAudioInput(device/*, format*/);
} }
AmpManager::~AmpManager() AmpManager::~AmpManager()
{ {
abort(); abort();
delete _audioDevice;
} }
void AmpManager::run() void AmpManager::run()
{ {
abort(); abort();
arecord.start( "arecord -D front -" ); _ioDevice = _audioDevice->start();
loop.reset(new QEventLoop); loop.reset(new QEventLoop);
QTimer timer; QTimer timer;
connect(&timer, SIGNAL(timeout()), this, SLOT(doTick())); connect(&timer, SIGNAL(timeout()), this, SLOT(doTick()));
@ -29,18 +38,17 @@ void AmpManager::run()
void AmpManager::abort() void AmpManager::abort()
{ {
if (!loop.isNull()){ if (!loop.isNull())loop->quit();
loop->quit(); if(_audioDevice->error() == 0)_audioDevice->stop();
} if(_ioDevice != nullptr)delete _ioDevice;
if(arecord.state() == QProcess::Running)arecord.close();
qDebug()<<"Stop Auto Amp Manager\n"; qDebug()<<"Stop Auto Amp Manager\n";
} }
void AmpManager::doTick() void AmpManager::doTick()
{ {
if(arecord.state() == QProcess::Running) if(_audioDevice->error() == 0 && _ioDevice != nullptr)
{ {
QByteArray buffer = arecord.readAllStandardOutput(); QByteArray buffer = _ioDevice->readAll();
for(long i = 0; i < buffer.size(); i++) for(long i = 0; i < buffer.size(); i++)
{ {
if((uint8_t) buffer.at(i) != 128) if((uint8_t) buffer.at(i) != 128)
@ -60,6 +68,6 @@ void AmpManager::doTick()
_micro->relayOn(_relayNumber); _micro->relayOn(_relayNumber);
relayState = true; relayState = true;
} }
silenceCount ++; silenceCount++;
} }
} }

View File

@ -8,9 +8,11 @@
#include <QScopedPointer> #include <QScopedPointer>
#include <QEventLoop> #include <QEventLoop>
#include <QTimer> #include <QTimer>
#include <QProcess>
#include <QByteArray> #include <QByteArray>
#include <QtDebug> #include <QtDebug>
#include <QAudioInput>
#include <QAudioDeviceInfo>
#include <QIODevice>
#include "microcontroller.h" #include "microcontroller.h"
@ -19,7 +21,7 @@ class AmpManager : public QObject, public QRunnable
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit AmpManager(Microcontroller *micro, int relayNumber, QObject *parent = 0); explicit AmpManager(Microcontroller *micro, int relayNumber, QAudioDeviceInfo device, QObject *parent = 0);
~AmpManager(); ~AmpManager();
@ -36,8 +38,8 @@ private:
Microcontroller *_micro; Microcontroller *_micro;
int _relayNumber; int _relayNumber;
QAudioInput* _audioDevice;
QProcess arecord; QIODevice* _ioDevice = nullptr;
bool relayState = false; bool relayState = false;

View File

@ -11,11 +11,13 @@
#include <QCommandLineParser> #include <QCommandLineParser>
#include "alarmtime.h" #include "alarmtime.h"
#include "microcontroller.h" #include "microcontroller.h"
#include "mainwindow.h" #include "mainwindow.h"
#include "relaydialog.h" #include "relaydialog.h"
#include "ampmanager.h" #include "ampmanager.h"
#include "power.h"
#define BAUD QSerialPort::Baud38400 #define BAUD QSerialPort::Baud38400
@ -46,12 +48,14 @@ int main(int argc, char *argv[])
parser.addOption(serialOption); parser.addOption(serialOption);
QCommandLineOption baudOption(QStringList() << "b" << "baud", QCoreApplication::translate("main", "Set Baud Rate")); QCommandLineOption baudOption(QStringList() << "b" << "baud", QCoreApplication::translate("main", "Set Baud Rate"));
parser.addOption(baudOption); parser.addOption(baudOption);
QCommandLineOption secondaryOption(QStringList() << "s" << "secondary", QCoreApplication::translate("main", "Set if instance is not main instance")); QCommandLineOption secondaryOption(QStringList() << "e" << "secondary", QCoreApplication::translate("main", "Set if instance is not main instance"));
parser.addOption(secondaryOption); parser.addOption(secondaryOption);
parser.process(a); parser.process(a);
QSettings settings; QSettings settings;
//connect to microcontoler //connect to microcontoler
Microcontroller micro; Microcontroller micro;
if(parser.isSet(tcpOption)) if(parser.isSet(tcpOption))
@ -85,23 +89,35 @@ int main(int argc, char *argv[])
else micro.setIODevice(microPort); else micro.setIODevice(microPort);
} }
AmpManager amp(&micro, 0); Power power(&settings, &micro);
AmpManager amp(&micro, 0, QAudioDeviceInfo::defaultInputDevice());
MainWindow w(&settings, &micro, parser.isSet(secondaryOption)); MainWindow w(&settings, &micro, parser.isSet(secondaryOption));
QObject::connect(&micro, SIGNAL(textRecived(QString)), &w, SLOT(changeHeaderLableText(QString)));
QObject::connect(&micro, SIGNAL(relayStateChanged(std::vector<bool>)), &w, SLOT(relayStateChanged(std::vector<bool>)));
RelayDialog relayDialog(&micro); RelayDialog relayDialog(&micro);
QObject::connect(&micro, SIGNAL(relayStateChanged(std::vector<bool>)), &relayDialog, SLOT(relayStateChanged(std::vector<bool>)));
//Alarms
AlarmTime almNight(settings.value("nightTime").toTime());
AlarmTime *almAlarm = new AlarmTime(settings.value("alarmTime").toTime());
QSignalMapper signalMapper;
if(!parser.isSet(secondaryOption)) if(!parser.isSet(secondaryOption))
{ {
//Alarms
AlarmTime almNight(settings.value("nightTime").toTime()); QObject::connect(&almNight, SIGNAL(trigger()), &power, SLOT(syncoff()));
QObject::connect(&almNight, SIGNAL(trigger()), &w, SLOT(slotSyncoff()));
QObject::connect(&w, SIGNAL(signalAlmNightChanged(QTime)), &almNight, SLOT(changeTime(QTime))); QObject::connect(&w, SIGNAL(signalAlmNightChanged(QTime)), &almNight, SLOT(changeTime(QTime)));
QObject::connect(&w, SIGNAL(signalAlmNightStateChanged(int)), &almNight, SLOT(runOrAbort(int))); QObject::connect(&w, SIGNAL(signalAlmNightStateChanged(int)), &almNight, SLOT(runOrAbort(int)));
//QMetaObject::invokeMethod(&almNight, "run", Qt::QueuedConnection ); //QMetaObject::invokeMethod(&almNight, "run", Qt::QueuedConnection );
AlarmTime *almAlarm = new AlarmTime(settings.value("alarmTime").toTime());
QSignalMapper signalMapper;
QObject::connect(almAlarm, SIGNAL(trigger()), &signalMapper, SLOT(map())); QObject::connect(almAlarm, SIGNAL(trigger()), &signalMapper, SLOT(map()));
signalMapper.setMapping(almAlarm, 4); signalMapper.setMapping(almAlarm, 4);
QObject::connect(&signalMapper, SIGNAL(mapped(int)), &micro, SLOT(setPattern(int))); QObject::connect(&signalMapper, SIGNAL(mapped(int)), &micro, SLOT(setPattern(int)));
@ -115,13 +131,11 @@ int main(int argc, char *argv[])
QMetaObject::invokeMethod(&amp, "run", Qt::QueuedConnection ); QMetaObject::invokeMethod(&amp, "run", Qt::QueuedConnection );
} }
//serial display
QObject::connect(&micro, SIGNAL(textRecived(QString)), &w, SLOT(changeHeaderLableText(QString)));
QMetaObject::invokeMethod(&micro, "run", Qt::QueuedConnection );
//Advanced Relays //Advanced Relays
QObject::connect(&w, SIGNAL(showAdvRelayDialog()), &relayDialog, SLOT(show())); QObject::connect(&w, SIGNAL(showAdvRelayDialog()), &relayDialog, SLOT(show()));
QMetaObject::invokeMethod(&micro, "run", Qt::QueuedConnection );
QMetaObject::invokeMethod(&w, "postActivate", Qt::QueuedConnection ); QMetaObject::invokeMethod(&w, "postActivate", Qt::QueuedConnection );
w.show(); w.show();

View File

@ -19,7 +19,7 @@ MainWindow::MainWindow(QSettings *settings, Microcontroller *micro , bool isRemo
//RGB Leds //RGB Leds
connect(ui->presettApply, SIGNAL(clicked()), this, SLOT(slotApplyPreset())); connect(ui->presettApply, SIGNAL(clicked()), this, SLOT(slotApplyPreset()));
connect(&colorChooser, SIGNAL(currentColorChanged(const QColor)), this, SLOT(slotChangedRgb(const QColor))); connect(&colorChooser, SIGNAL(colorSelected(const QColor)), this, SLOT(slotChangedRgb(const QColor)));
connect(ui->button_lightsOn, SIGNAL(clicked()), _micro, SLOT(rgbOn())); connect(ui->button_lightsOn, SIGNAL(clicked()), _micro, SLOT(rgbOn()));
connect(ui->button_lightsOff, SIGNAL(clicked()), _micro, SLOT(rgbOff())); connect(ui->button_lightsOff, SIGNAL(clicked()), _micro, SLOT(rgbOff()));
connect(ui->button_quit, SIGNAL(clicked()), this, SLOT(close())); connect(ui->button_quit, SIGNAL(clicked()), this, SLOT(close()));
@ -32,24 +32,25 @@ MainWindow::MainWindow(QSettings *settings, Microcontroller *micro , bool isRemo
new QListWidgetItem(tr("Pattern 4 Sunrise"), ui->listWidget_patern); new QListWidgetItem(tr("Pattern 4 Sunrise"), ui->listWidget_patern);
//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);
for(unsigned int i = 0; i < _relayCheckBoxes.size(); i++) connect(_relayCheckBoxes[i], SIGNAL(stateChanged(int)), this, SLOT(relayCheckBoxToggeled(int)));
//Amp //Amp
if(!isRemoteMode)connect(ui->checkBox_ampAuto, SIGNAL(stateChanged(int)), this, SLOT(slotAmpChkbtn(int))); if(!isRemoteMode)connect(ui->checkBox_ampAuto, SIGNAL(stateChanged(int)), this, SLOT(slotAmpAutoToggle(int)));
connect(ui->checkBox_amp, SIGNAL(stateChanged(int)), this, SLOT(slotAmpToggle(int)));
//Bedroom Speakers //Bedroom Speakers
connect(ui->checkBox_bspeaker, SIGNAL(stateChanged(int)), this, SLOT(slotBSpeakerToggle(int)));
if(!isRemoteMode)connect(ui->checkBox_bspeakerAuto, SIGNAL(stateChanged(int)), this, SLOT(slotBSpeakerAutoToggle(int))); if(!isRemoteMode)connect(ui->checkBox_bspeakerAuto, SIGNAL(stateChanged(int)), this, SLOT(slotBSpeakerAutoToggle(int)));
//Infinity Mirror //Infinity Mirror
connect(ui->checkBox_inf, SIGNAL(stateChanged(int)), this, SLOT(slotInfMirrorToggle(int)));
if(!isRemoteMode)connect(ui->checkBox_infAuto, SIGNAL(stateChanged(int)), this, SLOT(slotInfMirrorAutoToggle(int))); if(!isRemoteMode)connect(ui->checkBox_infAuto, SIGNAL(stateChanged(int)), this, SLOT(slotInfMirrorAutoToggle(int)));
//Airconditioner
connect(ui->checkBox_aircon, SIGNAL(stateChanged(int)), this, SLOT(slotAirconToggle(int)));
if(!isRemoteMode) if(!isRemoteMode)
{ {
//Alarm //Alarm
@ -72,18 +73,21 @@ void MainWindow::remoteMode()
{ {
ui->alarmTime->setEnabled(false); ui->alarmTime->setEnabled(false);
ui->checkBox_alarm->setEnabled(false); ui->checkBox_alarm->setEnabled(false);
ui->label_alarm->setEnabled(false); ui->pushButton_alarm->setEnabled(false);
ui->nightTime->setEnabled(false); ui->nightTime->setEnabled(false);
ui->checkBox_nightTime->setEnabled(false); ui->checkBox_nightTime->setEnabled(false);
ui->label_nightTime->setEnabled(false); ui->label_nightTime->setEnabled(false);
ui->checkBox_ampAuto->setEnabled(false); ui->checkBox_ampAuto->setEnabled(false);
ui->checkBox_ampAuto->setChecked(false);
ui->checkBox_bspeakerAuto->setEnabled(false); ui->checkBox_bspeakerAuto->setEnabled(false);
ui->checkBox_amp->setEnabled(true); ui->checkBox_amp->setEnabled(true);
ui->checkBox_bspeaker->setEnabled(true); ui->checkBox_bspeaker->setEnabled(true);
ui->checkBox_inf->setEnabled(true); ui->checkBox_inf->setEnabled(true);
ui->checkBox_inf->setChecked(false);
ui->checkBox_infAuto->setEnabled(false); ui->checkBox_infAuto->setEnabled(false);
ui->checkBox_infAuto->setChecked(false);
} }
void MainWindow::postActivate() void MainWindow::postActivate()
@ -106,9 +110,9 @@ void MainWindow::slotChangedRgb(const QColor color)
if( color.redF() < 0.2 && color.greenF() < 0.2 && color.blueF() > 0.8 ) if( color.redF() < 0.2 && color.greenF() < 0.2 && color.blueF() > 0.8 )
{ {
qDebug()<<"Auto turn on inf mirror\n"; qDebug()<<"Auto turn on inf mirror\n";
slotInfMirrorToggle(true); _micro->relayOn(2);
} }
else slotInfMirrorToggle(false); else _micro->relayOff(2);
} }
} }
@ -118,14 +122,20 @@ void MainWindow::slotApplyPreset()
if(ui->listWidget_patern->selectedItems().count() == 1) _micro->setPattern(ui->listWidget_patern->currentRow()); if(ui->listWidget_patern->selectedItems().count() == 1) _micro->setPattern(ui->listWidget_patern->currentRow());
} }
void MainWindow::slotAmpToggle(int state) void MainWindow::relayCheckBoxToggeled(int state)
{ {
_micro->relayToggle(state, 0); for(unsigned int i = 0; i < _relayCheckBoxes.size(); i++)
if(_relayCheckBoxes[i] == sender()) _micro->relayToggle(state, i);
} }
void MainWindow::slotBSpeakerToggle(int state) void MainWindow::relayStateChanged(std::vector<bool> relayStates)
{ {
_micro->relayToggle(state, 1); if(relayStates.size() >= 4) for(unsigned int i = 0; i < _relayCheckBoxes.size(); i++)
{
_relayCheckBoxes[i]->blockSignals(true);
_relayCheckBoxes[i]->setChecked(relayStates[i]);
_relayCheckBoxes[i]->blockSignals(false);
}
} }
void MainWindow::slotBSpeakerAutoToggle(int state) void MainWindow::slotBSpeakerAutoToggle(int state)
@ -133,26 +143,15 @@ void MainWindow::slotBSpeakerAutoToggle(int state)
ui->checkBox_bspeaker->setEnabled(!state); ui->checkBox_bspeaker->setEnabled(!state);
} }
void MainWindow::slotInfMirrorToggle(int state)
{
_micro->relayToggle(state, 2);
}
void MainWindow::slotInfMirrorAutoToggle(int state) void MainWindow::slotInfMirrorAutoToggle(int state)
{ {
ui->checkBox_inf->setEnabled(!state); ui->checkBox_inf->setEnabled(!state);
if(!state) if(!state)
{ {
slotInfMirrorToggle(ui->checkBox_inf->isChecked()); _micro->relayToggle(ui->checkBox_inf->isChecked(), 2);
} }
} }
void MainWindow::slotAirconToggle(int state)
{
_micro->relayToggle(state, 3);
}
void MainWindow::slotChangedAlarmTime(const QTime time) void MainWindow::slotChangedAlarmTime(const QTime time)
{ {
_settings->setValue("alarmTime", time); _settings->setValue("alarmTime", time);
@ -170,7 +169,7 @@ void MainWindow::slotChangedNightTime(const QTime time)
signalAlmNightChanged(time); signalAlmNightChanged(time);
} }
void MainWindow::slotAmpChkbtn(int state) void MainWindow::slotAmpAutoToggle(int state)
{ {
ui->checkBox_amp->setEnabled(!state); ui->checkBox_amp->setEnabled(!state);
if(state) if(state)
@ -180,7 +179,7 @@ void MainWindow::slotAmpChkbtn(int state)
else else
{ {
signalAmpOff(); signalAmpOff();
slotAmpToggle(ui->checkBox_amp->checkState()); _micro->relayToggle(ui->checkBox_amp->checkState(), 0);
} }
} }
@ -188,15 +187,3 @@ void MainWindow::changeHeaderLableText(const QString string)
{ {
ui->label_serialRecive->setText(string); ui->label_serialRecive->setText(string);
} }
void MainWindow::slotSyncoff()
{
qDebug()<<"Power Off on alarm\n";
_settings->sync();
slotAmpToggle(false);
slotBSpeakerToggle(false);
slotInfMirrorToggle(false);
slotAirconToggle(false);
QProcess::execute ( "syncoff" );
}

View File

@ -3,15 +3,10 @@
#include <QMainWindow> #include <QMainWindow>
#include <QColorDialog> #include <QColorDialog>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <termios.h>
#include <QListWidgetItem> #include <QListWidgetItem>
#include <QSettings> #include <QSettings>
#include <QTime> #include <QTime>
#include <QProcess> #include <vector>
#include <QSignalMapper>
#include "alarmtime.h" #include "alarmtime.h"
#include "microcontroller.h" #include "microcontroller.h"
@ -40,6 +35,8 @@ private:
Microcontroller *_micro; Microcontroller *_micro;
std::vector<QAbstractButton*> _relayCheckBoxes;
void remoteMode(); void remoteMode();
signals: signals:
@ -62,17 +59,16 @@ private slots:
//RGB //RGB
void slotChangedRgb(const QColor color); void slotChangedRgb(const QColor color);
void slotApplyPreset(); void slotApplyPreset();
void slotAmpChkbtn(int state);
void changeHeaderLableText(const QString string); void changeHeaderLableText(const QString string);
//Relays //relays
void slotAmpToggle(int state); void relayCheckBoxToggeled(int state);
void slotBSpeakerToggle(int state);
//Automation
void slotAmpAutoToggle(int state);
void slotBSpeakerAutoToggle(int state); void slotBSpeakerAutoToggle(int state);
void slotInfMirrorToggle(int state);
void slotInfMirrorAutoToggle(int state); void slotInfMirrorAutoToggle(int state);
void slotAirconToggle(int state);
//Alarm //Alarm
void slotChangedAlarmTime(const QTime time); void slotChangedAlarmTime(const QTime time);
@ -81,9 +77,10 @@ private slots:
//Night //Night
void slotChangedNightTime(const QTime time); void slotChangedNightTime(const QTime time);
//syncoff
void slotSyncoff(); public slots:
void relayStateChanged(std::vector<bool> relayStates);
}; };

View File

@ -23,11 +23,11 @@
</size> </size>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>SHinterface</string> <string>Smart Home Interface</string>
</property> </property>
<property name="windowIcon"> <property name="windowIcon">
<iconset> <iconset resource="resources.qrc">
<normaloff>UVOSicon.bmp</normaloff>UVOSicon.bmp</iconset> <normaloff>:/images/UVOSicon.bmp</normaloff>:/images/UVOSicon.bmp</iconset>
</property> </property>
<widget class="QWidget" name="centralWidget"> <widget class="QWidget" name="centralWidget">
<property name="sizePolicy"> <property name="sizePolicy">
@ -421,6 +421,12 @@
<height>0</height> <height>0</height>
</size> </size>
</property> </property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>80</height>
</size>
</property>
<property name="baseSize"> <property name="baseSize">
<size> <size>
<width>0</width> <width>0</width>
@ -499,12 +505,9 @@
<item> <item>
<layout class="QHBoxLayout" name="horizontalLayout_12"> <layout class="QHBoxLayout" name="horizontalLayout_12">
<item> <item>
<widget class="QLabel" name="label_alarm"> <widget class="QPushButton" name="pushButton_alarm">
<property name="sizePolicy"> <property name="enabled">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred"> <bool>true</bool>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property> </property>
<property name="text"> <property name="text">
<string>Alarm</string> <string>Alarm</string>
@ -580,6 +583,8 @@
</widget> </widget>
</widget> </widget>
<layoutdefault spacing="6" margin="11"/> <layoutdefault spacing="6" margin="11"/>
<resources/> <resources>
<include location="resources.qrc"/>
</resources>
<connections/> <connections/>
</ui> </ui>

View File

@ -1,24 +1,6 @@
#include "microcontroller.h" #include "microcontroller.h"
Microcontroller::Microcontroller(QIODevice* port): _port(port) //relays
{
getState();
}
Microcontroller::Microcontroller()
{
}
Microcontroller::~Microcontroller()
{
if(_port != nullptr) delete _port;
}
void Microcontroller::setIODevice(QIODevice *port)
{
_port = port;
getState();
}
void Microcontroller::relayToggle(int state, int relay) void Microcontroller::relayToggle(int state, int relay)
{ {
@ -30,6 +12,18 @@ void Microcontroller::relayToggle(int state, int relay)
if(_port != nullptr) _port->write(buffer, length); if(_port != nullptr) _port->write(buffer, length);
} }
void Microcontroller::relayOn(int relay)
{
relayToggle(true, relay);
}
void Microcontroller::relayOff(int relay)
{
relayToggle(false, relay);
}
//rgb lights
void Microcontroller::rgbOn() void Microcontroller::rgbOn()
{ {
if(_port != nullptr) _port->write("rgb on\n", sizeof("rgb on\n")-1); if(_port != nullptr) _port->write("rgb on\n", sizeof("rgb on\n")-1);
@ -57,16 +51,6 @@ void Microcontroller::setPattern(int pattern)
_port->write(buffer, length); _port->write(buffer, length);
} }
void Microcontroller::relayOn(int relay)
{
relayToggle(true, relay);
}
void Microcontroller::relayOff(int relay)
{
relayToggle(false, relay);
}
bool Microcontroller::connected() bool Microcontroller::connected()
{ {
if(_port != nullptr) return _port->isOpen(); if(_port != nullptr) return _port->isOpen();
@ -86,7 +70,7 @@ void Microcontroller::run()
loop->exec(); loop->exec();
} }
void Microcontroller::getState() void Microcontroller::requestState()
{ {
if(_port != nullptr) _port->write("relay state\n", sizeof("relay state\n")); if(_port != nullptr) _port->write("relay state\n", sizeof("relay state\n"));
} }
@ -100,6 +84,34 @@ void Microcontroller::abort()
} }
} }
std::vector<bool> Microcontroller::getLastState()
{
return _relayStates;
}
//housekeeping
Microcontroller::Microcontroller(QIODevice* port): _port(port)
{
requestState();
}
Microcontroller::Microcontroller()
{
}
Microcontroller::~Microcontroller()
{
if(_port != nullptr) delete _port;
}
void Microcontroller::setIODevice(QIODevice *port)
{
_port = port;
requestState();
}
void Microcontroller::processMicroReturn() void Microcontroller::processMicroReturn()
{ {
QString workbuff = _buffer; QString workbuff = _buffer;

View File

@ -11,6 +11,7 @@
#include <QScopedPointer> #include <QScopedPointer>
#include <QEventLoop> #include <QEventLoop>
#include <QTimer> #include <QTimer>
#include <QAbstractButton>
#include <vector> #include <vector>
class Microcontroller: public QObject class Microcontroller: public QObject
@ -25,7 +26,7 @@ private:
QString _buffer; QString _buffer;
void processMicroReturn(); void processMicroReturn();
void getState(); void requestState();
public: public:
Microcontroller(QIODevice* port); Microcontroller(QIODevice* port);
@ -33,6 +34,7 @@ public:
~Microcontroller(); ~Microcontroller();
bool connected(); bool connected();
void setIODevice(QIODevice* port); void setIODevice(QIODevice* port);
std::vector<bool> getLastState();
public slots: public slots:
void rgbOn(); void rgbOn();

View File

@ -1,6 +1,14 @@
#include "power.h" #include "power.h"
#include <QProcess>
Power::Power(QObject *parent) : QObject(parent) Power::Power(QSettings *settings, Microcontroller* micro, QObject *parent) : QObject(parent), _micro(micro), _settings(settings)
{ {
} }
void Power::syncoff()
{
_settings->sync();
for(unsigned int i = 0; i < _micro->getLastState().size(); i++) _micro->relayOff(i);
QProcess::execute ( "syncoff" );
}

11
power.h
View File

@ -2,16 +2,25 @@
#define POWER_H #define POWER_H
#include <QObject> #include <QObject>
#include <QSettings>
#include "microcontroller.h"
class Power : public QObject class Power : public QObject
{ {
private:
Q_OBJECT Q_OBJECT
Microcontroller* _micro;
QSettings* _settings;
public: public:
explicit Power(QObject *parent = nullptr); explicit Power(QSettings* settings, Microcontroller* micro, QObject *parent = nullptr);
signals: signals:
public slots: public slots:
void syncoff();
}; };
#endif // POWER_H #endif // POWER_H

View File

@ -13,7 +13,7 @@ RelayDialog::RelayDialog(Microcontroller *micro, QWidget *parent) :
_relayCheckBoxes.push_back(ui->checkBox_R2); _relayCheckBoxes.push_back(ui->checkBox_R2);
_relayCheckBoxes.push_back(ui->checkBox_R3); _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))); 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);
_micro->relayOn(STARTING_RELAY+1); _micro->relayOn(STARTING_RELAY+1);
@ -26,18 +26,18 @@ RelayDialog::~RelayDialog()
} }
void RelayDialog::relayCheckBoxToggeled(bool checked) void RelayDialog::relayCheckBoxToggeled(int state)
{ {
for(unsigned int i = 0; i < _relayCheckBoxes.size(); i++) for(unsigned int i = 0; i < _relayCheckBoxes.size(); i++)
{ if(_relayCheckBoxes[i] == sender())_micro->relayToggle(state, i+STARTING_RELAY);
if(_relayCheckBoxes[i] == sender())
{
checked ? _micro->relayOn(i+4) : _micro->relayOff(i+4);
}
}
} }
void RelayDialog::relayStateChanged(std::vector<bool> relayStates) 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]); if(relayStates.size() >= STARTING_RELAY+4) for(unsigned int i = 0; i < _relayCheckBoxes.size(); i++)
{
_relayCheckBoxes[i]->blockSignals(true);
_relayCheckBoxes[i]->setChecked(relayStates[i+STARTING_RELAY]);
_relayCheckBoxes[i]->blockSignals(false);
}
} }

View File

@ -30,7 +30,7 @@ private:
Microcontroller *_micro; Microcontroller *_micro;
private slots: private slots:
void relayCheckBoxToggeled(bool checked); void relayCheckBoxToggeled(int state);
public slots: public slots:
void relayStateChanged(std::vector<bool> relayStates); void relayStateChanged(std::vector<bool> relayStates);

View File

@ -152,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 1</string> <string>3040</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -178,6 +178,44 @@
</item> </item>
</layout> </layout>
</item> </item>
<item>
<widget class="Line" name="line_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Aux</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_6">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="checkBox_3">
<property name="text">
<string>On</string>
</property>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
</item> </item>

View File

@ -1,2 +1,5 @@
<!DOCTYPE RCC> <RCC>
<RCC version="1.0"/> <qresource prefix="/images">
<file>UVOSicon.bmp</file>
</qresource>
</RCC>