From 08dc57d3852d1daf0e2a7f9e6e13a3afc22766f3 Mon Sep 17 00:00:00 2001 From: IMback Date: Thu, 2 Nov 2017 20:17:05 +0100 Subject: [PATCH] Working TCP subsystem --- SHinterface.pro | 23 +- alarmtime.cpp | 52 +++- alarmtime.h | 34 ++- ampmanager.cpp | 63 ++++- ampmanager.h | 38 ++- formatstring.h | 4 - main.cpp | 122 ++++++++- mainwindow.cpp | 192 ++++++++++++++- mainwindow.h | 72 +++++- mainwindow.ui | 585 +++++++++++++++++++++++++++++++++++++++++++- microcontroller.cpp | 109 ++++++++- microcontroller.h | 44 +++- relay.h | 16 +- relaydialog.cpp | 1 + relaydialog.ui | 250 +++++++++++++++++-- serial_io.cpp | 67 +++++ serial_io.h | 17 ++ serialwatcher.cpp | 32 ++- serialwatcher.h | 34 ++- 19 files changed, 1679 insertions(+), 76 deletions(-) delete mode 100644 formatstring.h diff --git a/SHinterface.pro b/SHinterface.pro index 10970ec..6f0a2cb 100644 --- a/SHinterface.pro +++ b/SHinterface.pro @@ -4,9 +4,9 @@ # #------------------------------------------------- -QT += core gui +QT += core gui widgets network serialport -greaterThan(QT_MAJOR_VERSION, 4): QT += widgets +greaterThan(QT_MAJOR_VERSION, 4): QT += TARGET = SHinterface TEMPLATE = app @@ -17,15 +17,18 @@ TEMPLATE = app # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS -# You can also make your code fail to compile if you use deprecated APIs. -# In order to do so, uncomment the following line. -# You can also select to disable deprecated APIs only up to a certain version of Qt. -#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 -SOURCES += main.cpp\ - mainwindow.cpp +SOURCES += main.cpp mainwindow.cpp ampmanager.cpp alarmtime.cpp \ + microcontroller.cpp \ + relaydialog.cpp -HEADERS += mainwindow.h +HEADERS += mainwindow.h \ + ampmanager.h \ + relay.h \ + alarmtime.h \ + microcontroller.h \ + relaydialog.h -FORMS += mainwindow.ui +FORMS += mainwindow.ui \ + relaydialog.ui diff --git a/alarmtime.cpp b/alarmtime.cpp index 29bd0df..f55d4ea 100644 --- a/alarmtime.cpp +++ b/alarmtime.cpp @@ -1,6 +1,56 @@ #include "alarmtime.h" -AlarmTime::AlarmTime() +AlarmTime::AlarmTime(const QTime time, QObject *parent) : QObject(parent), time_(time) { + connect(&timer, SIGNAL(timeout()), this, SLOT(doTick())); + timer.setInterval(500); } + +AlarmTime::~AlarmTime() +{ + abort(); +} + +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"; +} + +void AlarmTime::doTick() +{ + if(time_.hour() == QTime::currentTime().hour() && time_.minute() == QTime::currentTime().minute() && triggerd_==false ) + { + qDebug()<<"Trigger\n"; + triggerd_=true; + trigger(); + } + else if( time_.hour() != QTime::currentTime().hour() ) triggerd_=false; +} + +void AlarmTime::changeTime(QTime time) +{ + time_=time; + qDebug()<<"Time Changed\n"; +} + +void AlarmTime::runOrAbort(int state) +{ + state ? run() : abort(); +} diff --git a/alarmtime.h b/alarmtime.h index 89d91bb..4c06a28 100644 --- a/alarmtime.h +++ b/alarmtime.h @@ -1,11 +1,37 @@ #ifndef ALARMTIME_H #define ALARMTIME_H +#include +#include +#include +#include +#include +#include +#include +#include - -class AlarmTime +class AlarmTime : public QObject, public QRunnable { + Q_OBJECT +private: + + bool triggerd_ = false; + QTime time_; + QTimer timer; + QScopedPointer loop; + public: - AlarmTime(); + explicit AlarmTime(const QTime time = QTime::currentTime(), QObject *parent = 0); + ~AlarmTime(); + +signals: + void trigger(); + +public slots: + void run(); + void abort(); + void runOrAbort(int state); + void doTick(); + void changeTime(QTime time); }; -#endif // ALARMTIME_H \ No newline at end of file +#endif // ALARMTIME_H diff --git a/ampmanager.cpp b/ampmanager.cpp index abeca71..777623c 100644 --- a/ampmanager.cpp +++ b/ampmanager.cpp @@ -1,6 +1,65 @@ #include "ampmanager.h" -AmpManager::AmpManager(QObject *parent) : QObject(parent) +AmpManager::AmpManager(Microcontroller *micro, int relayNumber, QObject *parent) : QObject(parent), _micro(micro), _relayNumber(relayNumber) { - + silenceCount = 0; +} + +AmpManager::~AmpManager() +{ + abort(); +} + +void AmpManager::run() +{ + abort(); + arecord.start( "arecord -D front -" ); + loop.reset(new QEventLoop); + QTimer timer; + connect(&timer, SIGNAL(timeout()), this, SLOT(doTick())); + timer.setInterval(500); + timer.start(); + + qDebug()<<"Start Auto Amp Manager\n"; + _micro->relayOn(_relayNumber); + relayState = true; + loop->exec(); +} + + +void AmpManager::abort() +{ + if (!loop.isNull()){ + loop->quit(); + } + if(arecord.state() == QProcess::Running)arecord.close(); + qDebug()<<"Stop Auto Amp Manager\n"; +} + +void AmpManager::doTick() +{ + if(arecord.state() == QProcess::Running) + { + QByteArray buffer = arecord.readAllStandardOutput(); + for(long i = 0; i < buffer.size(); i++) + { + if((uint8_t) buffer.at(i) != 128) + { + silenceCount = 0; + } + } + if(silenceCount > 40 && relayState) + { + std::cout<<"Auto off Amp\n"; + _micro->relayOff(_relayNumber); + relayState = false; + } + else if(silenceCount == 0 && !relayState) + { + std::cout<<"Auto on Amp\n"; + _micro->relayOn(_relayNumber); + relayState = true; + } + silenceCount ++; + } } diff --git a/ampmanager.h b/ampmanager.h index 98297ab..4d50ef2 100644 --- a/ampmanager.h +++ b/ampmanager.h @@ -1,16 +1,46 @@ #ifndef AMPMANAGER_H #define AMPMANAGER_H +#include -class AmpManager : public QObject +#include +#include +#include +#include +#include +#include +#include +#include + +#include "microcontroller.h" + + +class AmpManager : public QObject, public QRunnable { Q_OBJECT public: - explicit AmpManager(QObject *parent = 0); + explicit AmpManager(Microcontroller *micro, int relayNumber, QObject *parent = 0); + ~AmpManager(); + -signals: public slots: + void run(); + void abort(); + void doTick(); + +private: + QScopedPointer loop; + + long silenceCount = 0; + Microcontroller *_micro; + int _relayNumber; + + + QProcess arecord; + + bool relayState = false; + }; -#endif // AMPMANAGER_H \ No newline at end of file +#endif // AMPMANAGER_H diff --git a/formatstring.h b/formatstring.h deleted file mode 100644 index c96daa3..0000000 --- a/formatstring.h +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef FORMATSTRING_H -#define FORMATSTRING_H - -#endif // FORMATSTRING_H diff --git a/main.cpp b/main.cpp index b48f94e..02cb0a4 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,128 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#include "alarmtime.h" +#include "microcontroller.h" #include "mainwindow.h" -#include +#include "relaydialog.h" +#include "ampmanager.h" + +#define BAUD QSerialPort::Baud38400 int main(int argc, char *argv[]) { QApplication a(argc, argv); - MainWindow w; + + //set info + QCoreApplication::setOrganizationName("UVOS"); + QCoreApplication::setOrganizationDomain("uvos.xyz"); + QCoreApplication::setApplicationName("SHinterface"); + QCoreApplication::setApplicationVersion("0.2"); + + QDir::setCurrent(a.applicationDirPath()); + + //parse comand line + QCommandLineParser parser; + parser.setApplicationDescription("Smart Home Interface"); + parser.addHelpOption(); + parser.addVersionOption(); + QCommandLineOption tcpOption(QStringList() << "t" << "tcp", QCoreApplication::translate("main", "Use Tcp connection")); + parser.addOption(tcpOption); + QCommandLineOption hostOption(QStringList() << "H" << "host", QCoreApplication::translate("main", "Set server host ip addres"), "adress"); + parser.addOption(hostOption); + QCommandLineOption portOption(QStringList() << "p" << "port", QCoreApplication::translate("main", "Set server Port in TCP mode or Serial port in serial mode"), "port"); + parser.addOption(portOption); + QCommandLineOption serialOption(QStringList() << "s" << "serial", QCoreApplication::translate("main", "Use serial connection")); + parser.addOption(serialOption); + QCommandLineOption baudOption(QStringList() << "b" << "baud", QCoreApplication::translate("main", "Set Baud Rate")); + parser.addOption(baudOption); + QCommandLineOption secondaryOption(QStringList() << "s" << "secondary", QCoreApplication::translate("main", "Set if instance is not main instance")); + parser.addOption(secondaryOption); + parser.process(a); + + QSettings settings; + + //connect to microcontoler + Microcontroller micro; + if(parser.isSet(tcpOption)) + { + QTcpSocket* microSocket = new QTcpSocket; + + int port = 6856; + if(parser.isSet(portOption)) port = parser.value(portOption).toInt(); + + QString host("127.0.0.1"); + if(parser.isSet(hostOption)) host = parser.value(hostOption); + std::cout<<"connecting to "<connectToHost(host, port, QIODevice::ReadWrite); + if(!microSocket->waitForConnected(1000)) + { + std::cout<<"Can not connect to to Server.\n"; + return 1; + } + micro.setIODevice(microSocket); + } + else + { + QSerialPort* microPort = new QSerialPort; + if(parser.isSet(portOption)) microPort->setPortName(parser.value(portOption)); + else microPort->setPortName("ttyUSB0"); + + if(parser.isSet(portOption)) microPort->setBaudRate(parser.value(baudOption).toInt()); + else microPort->setBaudRate(BAUD); + + if(!microPort->open(QIODevice::ReadWrite)) std::cout<<"Can not open serial port "<portName().toStdString()<<". Continueing in demo mode"<<'\n'; + else micro.setIODevice(microPort); + } + + AmpManager amp(µ, 0); + + MainWindow w(&settings, µ, parser.isSet(secondaryOption)); + + RelayDialog relayDialog; + + if(!parser.isSet(secondaryOption)) + { + //Alarms + AlarmTime almNight(settings.value("nightTime").toTime()); + QObject::connect(&almNight, SIGNAL(trigger()), &w, SLOT(slotSyncoff())); + 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 ); + + AlarmTime *almAlarm = new AlarmTime(settings.value("alarmTime").toTime()); + QSignalMapper signalMapper; + 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 ); + + //Amplifyer + QObject::connect(&w, SIGNAL(signalAmpOn()), &, SLOT(run())); + QObject::connect(&w, SIGNAL(signalAmpOff()), &, SLOT(abort())); + QMetaObject::invokeMethod(&, "run", Qt::QueuedConnection ); + } + + //serial display + QObject::connect(µ, SIGNAL(textRecived(QString)), &w, SLOT(changeHeaderLableText(QString))); + QMetaObject::invokeMethod(µ, "run", Qt::QueuedConnection ); + + //Advanced Relays + QObject::connect(&w, SIGNAL(showAdvRelayDialog()), &relayDialog, SLOT(show())); + + QMetaObject::invokeMethod(&w, "postActivate", Qt::QueuedConnection ); w.show(); return a.exec(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 49d64fc..4a9d4a4 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -1,14 +1,202 @@ #include "mainwindow.h" #include "ui_mainwindow.h" -MainWindow::MainWindow(QWidget *parent) : +MainWindow::MainWindow(QSettings *settings, Microcontroller *micro , bool isRemoteMode , QWidget *parent) : QMainWindow(parent), - ui(new Ui::MainWindow) + 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())); + connect(&colorChooser, SIGNAL(currentColorChanged(const QColor)), this, SLOT(slotChangedRgb(const QColor))); + connect(ui->button_lightsOn, SIGNAL(clicked()), _micro, SLOT(rgbOn())); + connect(ui->button_lightsOff, SIGNAL(clicked()), _micro, SLOT(rgbOff())); + connect(ui->button_quit, SIGNAL(clicked()), this, SLOT(close())); + connect(ui->button_color, SIGNAL(clicked()), &colorChooser, SLOT(show())); + + new QListWidgetItem(tr("Pattern 0 Solid"), ui->listWidget_patern); + new QListWidgetItem(tr("Pattern 1"), ui->listWidget_patern); + new QListWidgetItem(tr("Pattern 2 Alarm"), ui->listWidget_patern); + new QListWidgetItem(tr("Pattern 3"), ui->listWidget_patern); + new QListWidgetItem(tr("Pattern 4 Sunrise"), ui->listWidget_patern); + + + //Amp + if(!isRemoteMode)connect(ui->checkBox_ampAuto, SIGNAL(stateChanged(int)), this, SLOT(slotAmpChkbtn(int))); + connect(ui->checkBox_amp, SIGNAL(stateChanged(int)), this, SLOT(slotAmpToggle(int))); + + + //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))); + + + //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))); + + + //Airconditioner + connect(ui->checkBox_aircon, SIGNAL(stateChanged(int)), this, SLOT(slotAirconToggle(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 + connect(ui->button_advRelay, SIGNAL(clicked()), this, SIGNAL(showAdvRelayDialog())); +} + +void MainWindow::remoteMode() +{ + ui->alarmTime->setEnabled(false); + ui->checkBox_alarm->setEnabled(false); + ui->label_alarm->setEnabled(false); + + ui->nightTime->setEnabled(false); + ui->checkBox_nightTime->setEnabled(false); + ui->label_nightTime->setEnabled(false); + + ui->checkBox_ampAuto->setEnabled(false); + ui->checkBox_bspeakerAuto->setEnabled(false); + ui->checkBox_amp->setEnabled(true); + ui->checkBox_bspeaker->setEnabled(true); + ui->checkBox_inf->setEnabled(true); + ui->checkBox_infAuto->setEnabled(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; } + +void MainWindow::slotChangedRgb(const QColor color) +{ + _micro->changeRgbColor(color); + if( ui->checkBox_infAuto->isChecked() ) + { + if( color.redF() < 0.2 && color.greenF() < 0.2 && color.blueF() > 0.8 ) + { + qDebug()<<"Auto turn on inf mirror\n"; + slotInfMirrorToggle(true); + } + else slotInfMirrorToggle(false); + } +} + + +void MainWindow::slotApplyPreset() +{ + if(ui->listWidget_patern->selectedItems().count() == 1) _micro->setPattern(ui->listWidget_patern->currentRow()); +} + +void MainWindow::slotAmpToggle(int state) +{ + _micro->relayToggle(state, 0); +} + +void MainWindow::slotBSpeakerToggle(int state) +{ + _micro->relayToggle(state, 1); +} + +void MainWindow::slotBSpeakerAutoToggle(int state) +{ + ui->checkBox_bspeaker->setEnabled(!state); +} + +void MainWindow::slotInfMirrorToggle(int state) +{ + _micro->relayToggle(state, 2); +} + +void MainWindow::slotInfMirrorAutoToggle(int state) +{ + ui->checkBox_inf->setEnabled(!state); + if(!state) + { + slotInfMirrorToggle(ui->checkBox_inf->isChecked()); + } +} + + +void MainWindow::slotAirconToggle(int state) +{ + _micro->relayToggle(state, 3); +} + +void MainWindow::slotChangedAlarmTime(const QTime time) +{ + _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); +} + +void MainWindow::slotAmpChkbtn(int state) +{ + ui->checkBox_amp->setEnabled(!state); + if(state) + { + signalAmpOn(); + } + else + { + signalAmpOff(); + slotAmpToggle(ui->checkBox_amp->checkState()); + } +} + +void MainWindow::changeHeaderLableText(const QString 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" ); +} diff --git a/mainwindow.h b/mainwindow.h index a3948a9..c1506aa 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -2,8 +2,22 @@ #define MAINWINDOW_H #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "alarmtime.h" +#include "microcontroller.h" -namespace Ui { + +namespace Ui +{ class MainWindow; } @@ -12,11 +26,65 @@ class MainWindow : public QMainWindow Q_OBJECT public: - explicit MainWindow(QWidget *parent = 0); + explicit MainWindow(QSettings *settings, Microcontroller *micro, bool isRemoteMode = false, QWidget *parent = 0); ~MainWindow(); + + private: Ui::MainWindow *ui; + + QColorDialog colorChooser; + + QSettings *_settings; + + Microcontroller *_micro; + + void remoteMode(); + +signals: + + void signalAmpOn(); + void signalAmpOff(); + + void signalAlmNightStateChanged(int state); + void signalAlmNightChanged(const QTime time); + + void signalAlmAlarmStateChanged(int state); + void signalAlmAlarmChanged(const QTime time); + + void showAdvRelayDialog(); + +private slots: + + void postActivate(); + + //RGB + void slotChangedRgb(const QColor color); + void slotApplyPreset(); + void slotAmpChkbtn(int state); + + void changeHeaderLableText(const QString string); + + //Relays + void slotAmpToggle(int state); + void slotBSpeakerToggle(int state); + void slotBSpeakerAutoToggle(int state); + void slotInfMirrorToggle(int state); + void slotInfMirrorAutoToggle(int state); + void slotAirconToggle(int state); + + //Alarm + void slotChangedAlarmTime(const QTime time); + void saveAlarmState(int state); + + //Night + void slotChangedNightTime(const QTime time); + + //syncoff + + void slotSyncoff(); + }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 6050363..4c9f48b 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -1,24 +1,585 @@ + MainWindow - - + + 0 0 - 400 - 300 + 859 + 549 - - MainWindow + + + 0 + 50 + - - - - + + + 600 + 197 + + + + SHinterface + + + + UVOSicon.bmpUVOSicon.bmp + + + + + 0 + 0 + + + + Qt::LeftToRight + + + false + + + + + + + + + + 0 + + + 10 + + + + + + 0 + 0 + + + + QFrame::Box + + + SHinterface + + + + + + + + + + 0 + 0 + + + + Relays + + + + + + + + Audio Amp + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + false + + + On + + + + + + + Auto + + + true + + + + + + + + + Qt::Horizontal + + + + + + + + + Bedroom Speakers + + + + + + + Qt::Horizontal + + + + 20 + 20 + + + + + + + + true + + + On + + + true + + + + + + + Auto + + + false + + + + + + + + + Qt::Horizontal + + + + + + + + + Infinity Mirror + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + false + + + On + + + + + + + Auto + + + true + + + + + + + + + Qt::Horizontal + + + + + + + + + Airconditioner + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + On + + + + + + + + + Advanced + + + + + + + + + + QLayout::SetDefaultConstraint + + + 0 + + + 0 + + + + + true + + + + 0 + 0 + + + + Shut Down Time: + + + + + + + QAbstractSpinBox::CorrectToNearestValue + + + false + + + QDateTimeEdit::HourSection + + + hh:mm + + + + + + + + 0 + 0 + + + + Enable + + + true + + + + + + + + + + + + + + + + 0 + 0 + + + + + 0 + 0 + + + + + 16777215 + 120 + + + + + 0 + 50 + + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 128 + 32 + + + + Apply + + + + + + + + + + 0 + 50 + + + + + 0 + 0 + + + + + 0 + 128 + + + + Choose Color + + + + + + + 10 + + + 10 + + + + + + 0 + 50 + + + + + 0 + 48 + + + + ON + + + + + + + + 0 + 50 + + + + + 0 + 48 + + + + OFF + + + + + + + + + Qt::Vertical + + + QSizePolicy::Preferred + + + + 20 + 20 + + + + + + + + + + + 0 + 0 + + + + Alarm + + + + + + + true + + + QAbstractSpinBox::CorrectToNearestValue + + + + 0 + 0 + 0 + 2000 + 1 + 1 + + + + QDateTimeEdit::HourSection + + + hh:mm + + + false + + + + + + + true + + + + 0 + 0 + + + + Enable + + + + + + + + + + 0 + 0 + + + + Qt::RightToLeft + + + Quit + + + + + + + - - + diff --git a/microcontroller.cpp b/microcontroller.cpp index 9c8e14e..0c3cdb0 100644 --- a/microcontroller.cpp +++ b/microcontroller.cpp @@ -1,6 +1,113 @@ #include "microcontroller.h" +Microcontroller::Microcontroller(QIODevice* port): _port(port) +{ +} + Microcontroller::Microcontroller() { - +} + +Microcontroller::~Microcontroller() +{ + if(_port != nullptr) delete _port; +} + +void Microcontroller::setIODevice(QIODevice *port) +{ + _port = port; +} + +void Microcontroller::relayToggle(int state, int relay) +{ + char buffer[8]; + int length = sprintf(buffer, "%d \n", relay); + if(_port != nullptr) state ? _port->write("relay on ", sizeof("relay on ")-1) : _port->write("relay off ", sizeof("relay off ")-1); + state ? std::cout<<"relay on " : std::cout<<"relay off "; + std::cout<write(buffer, length); +} + +void Microcontroller::rgbOn() +{ + if(_port != nullptr) _port->write("rgb on\n", sizeof("rgb on\n")-1); +} + +void Microcontroller::rgbOff() +{ + if(_port != nullptr) _port->write( "rgb off\n", sizeof("rgb off\n")-1); +} + +void Microcontroller::changeRgbColor(const QColor color) +{ + char buffer[64]; + int length = sprintf(buffer, "rgb set %03d %03d %03d \n", color.red(), color.green(), color.blue() ); + if(_port != nullptr) _port->write(buffer, length); + std::cout<write("rgb pattern ", sizeof("rgb pattern ")-1); + int length = sprintf(buffer, "%d \n", pattern); + _port->write(buffer, length); +} + +void Microcontroller::relayOn(int relay) +{ + relayToggle(true, relay); +} + +void Microcontroller::relayOff(int relay) +{ + relayToggle(false, relay); +} + +bool Microcontroller::connected() +{ + if(_port != nullptr) return _port->isOpen(); + else return false; +} + +void Microcontroller::run() +{ + abort(); + + loop.reset(new QEventLoop); + QTimer timer; + connect(&timer, SIGNAL(timeout()), this, SLOT(doTick())); + timer.setInterval(500); + timer.start(); + + loop->exec(); +} + + +void Microcontroller::abort() +{ + if (!loop.isNull()) + { + loop->quit(); + } +} + +void Microcontroller::doTick() +{ + if(_port != nullptr) + { + char charBuf; + while(_port->getChar(&charBuf)) + { + _buffer.push_back(charBuf); + std::cout< -class Microcontroller +#include +#include +#include +#include +#include +#include +#include +#include + +class Microcontroller: public QObject { + Q_OBJECT +private: + + QIODevice* _port = nullptr; + + QScopedPointer loop; + QString _buffer; + public: + Microcontroller(QIODevice* port); Microcontroller(); + ~Microcontroller(); + bool connected(); + void setIODevice(QIODevice* port); + +public slots: + void rgbOn(); + void rgbOff(); + void changeRgbColor(const QColor color); + void setPattern(int pattern); + + void relayToggle(int state, int id); + void relayOn(int relay); + void relayOff(int relay); + + void run(); + void abort(); + void doTick(); + +signals: + void textRecived(const QString string); + }; -#endif // MICROCONTROLLER_H \ No newline at end of file +#endif // MICROCONTROLLER_H diff --git a/relay.h b/relay.h index 3e3e755..b9d5f87 100644 --- a/relay.h +++ b/relay.h @@ -1,4 +1,14 @@ -#ifndef RELAY_H -#define RELAY_H +#pragma once + +#include "serial_io.h" + +static void relay_toggle(bool state, int id, int serial) +{ + 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<setupUi(this); + } RelayDialog::~RelayDialog() diff --git a/relaydialog.ui b/relaydialog.ui index 2c38028..a6b5ccd 100644 --- a/relaydialog.ui +++ b/relaydialog.ui @@ -1,38 +1,242 @@ + - - - RelayDialog 0 0 - 400 - 300 + 358 + 277 - Dialog + Advanced Relays - - - - 30 - 240 - 341 - 32 - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - + + + + + Advanced + + + + + + + + Night Relays + + + + + + + Qt::Horizontal + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + On + + + + + + + + + Qt::Horizontal + + + + + + + + + Test Equitment + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + On + + + + + + + + + Qt::Horizontal + + + + + + + + + 3Dator + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + On + + + + + + + + + Qt::Horizontal + + + + + + + + + Aux 2 + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + On + + + + + + + + + + + + 0 + + + 0 + + + + + Enterance Watch + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Inside + + + true + + + + + + + Watch + + + true + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Ok + + + + - diff --git a/serial_io.cpp b/serial_io.cpp index e69de29..2d34100 100644 --- a/serial_io.cpp +++ b/serial_io.cpp @@ -0,0 +1,67 @@ +#include "serial_io.h" + +struct termios toptions;; + +void sWrite(int port, char string[], size_t length) +{ + if(port != -1) write(port, string, length); + else std::cout< +#include +#include +#include + +#define BAUDRATE B38400 +#define DEVICE "/dev/ttyUSB0" + +void sWrite(int port, char string[], size_t length); + +void sWrite(int port, const char string[], size_t length); + +ssize_t sRead(int port, void *buf, size_t count); + + +int serialport_init(); + #endif // SERIAL_H diff --git a/serialwatcher.cpp b/serialwatcher.cpp index 36d97a8..e30185b 100644 --- a/serialwatcher.cpp +++ b/serialwatcher.cpp @@ -1,6 +1,36 @@ #include "serialwatcher.h" -SerialWatcher::SerialWatcher() +void SerialWatcher::run() { + abort(); + loop.reset(new QEventLoop); + QTimer timer; + connect(&timer, SIGNAL(timeout()), this, SLOT(doTick())); + timer.setInterval(500); + timer.start(); + + loop->exec(); +} + + +void SerialWatcher::abort() +{ + if (!loop.isNull()){ + loop->quit(); + } +} + +void SerialWatcher::doTick() +{ + char charBuf; + while(sRead(_serial, &charBuf, 1) == 1) + { + _buffer.push_back(charBuf); + if( _buffer.endsWith('\n') ) + { + textRecived(_buffer); + _buffer.clear(); + } + } } diff --git a/serialwatcher.h b/serialwatcher.h index 4f3c1f7..4b2f64b 100644 --- a/serialwatcher.h +++ b/serialwatcher.h @@ -1,11 +1,39 @@ #ifndef SERIALWATCHER_H #define SERIALWATCHER_H +#include "serial_io.h" +#define BUFFER_SIZE 128 -class SerialWatcher +#include +#include +#include +#include +#include +#include + +class SerialWatcher: public QObject, public QRunnable { + Q_OBJECT +private: + + QScopedPointer loop; + int _serial; + + QString _buffer; + public: - SerialWatcher(); + explicit SerialWatcher(int serial, QObject *parent = 0); + ~SerialWatcher(); + +signals: + void textRecived(const QString string); + +public slots: + void run(); + void abort(); + + void doTick(); }; -#endif // SERIALWATCHER_H \ No newline at end of file + +#endif // SERIALWATCHER_H