Working TCP subsystem

This commit is contained in:
IMback
2017-11-02 20:17:05 +01:00
parent 88ef0be4a2
commit 08dc57d385
19 changed files with 1679 additions and 76 deletions

View File

@ -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

View File

@ -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();
}

View File

@ -1,11 +1,37 @@
#ifndef ALARMTIME_H
#define ALARMTIME_H
#include <QTime>
#include <QObject>
#include <QRunnable>
#include <QScopedPointer>
#include <QEventLoop>
#include <QTimer>
#include <QProcess>
#include <QDebug>
class AlarmTime
class AlarmTime : public QObject, public QRunnable
{
Q_OBJECT
private:
bool triggerd_ = false;
QTime time_;
QTimer timer;
QScopedPointer<QEventLoop> 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

View File

@ -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 ++;
}
}

View File

@ -1,16 +1,46 @@
#ifndef AMPMANAGER_H
#define AMPMANAGER_H
#include <iostream>
class AmpManager : public QObject
#include <QObject>
#include <QRunnable>
#include <QScopedPointer>
#include <QEventLoop>
#include <QTimer>
#include <QProcess>
#include <QByteArray>
#include <QtDebug>
#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<QEventLoop> loop;
long silenceCount = 0;
Microcontroller *_micro;
int _relayNumber;
QProcess arecord;
bool relayState = false;
};
#endif // AMPMANAGER_H

View File

@ -1,4 +0,0 @@
#ifndef FORMATSTRING_H
#define FORMATSTRING_H
#endif // FORMATSTRING_H

122
main.cpp
View File

@ -1,10 +1,128 @@
#include <QtWidgets/QApplication>
#include <stdio.h>
#include <QDir>
#include <QDebug>
#include <QString>
#include <QSettings>
#include <QSignalMapper>
#include <QTcpSocket>
#include <QtSerialPort/QtSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QCommandLineParser>
#include "alarmtime.h"
#include "microcontroller.h"
#include "mainwindow.h"
#include <QApplication>
#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 "<<host.toStdString()<<':'<<port<<'\n';
microSocket->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 "<<microPort->portName().toStdString()<<". Continueing in demo mode"<<'\n';
else micro.setIODevice(microPort);
}
AmpManager amp(&micro, 0);
MainWindow w(&settings, &micro, 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)), &micro, 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()), &amp, SLOT(run()));
QObject::connect(&w, SIGNAL(signalAmpOff()), &amp, SLOT(abort()));
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
QObject::connect(&w, SIGNAL(showAdvRelayDialog()), &relayDialog, SLOT(show()));
QMetaObject::invokeMethod(&w, "postActivate", Qt::QueuedConnection );
w.show();
return a.exec();

View File

@ -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" );
}

View File

@ -2,8 +2,22 @@
#define MAINWINDOW_H
#include <QMainWindow>
#include <QColorDialog>
#include <unistd.h>
#include <iostream>
#include <stdio.h>
#include <termios.h>
#include <QListWidgetItem>
#include <QSettings>
#include <QTime>
#include <QProcess>
#include <QSignalMapper>
#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

View File

@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
@ -5,20 +6,580 @@
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>859</width>
<height>549</height>
</rect>
</property>
<property name="windowTitle" >
<string>MainWindow</string>
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>50</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>600</width>
<height>197</height>
</size>
</property>
<property name="windowTitle">
<string>SHinterface</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>UVOSicon.bmp</normaloff>UVOSicon.bmp</iconset>
</property>
<widget class="QWidget" name="centralWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_10">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_9">
<property name="topMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>10</number>
</property>
<item>
<widget class="QLabel" name="label_serialRecive">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::Box</enum>
</property>
<property name="text">
<string>SHinterface</string>
</property>
<widget class="QMenuBar" name="menuBar" />
<widget class="QToolBar" name="mainToolBar" />
<widget class="QWidget" name="centralWidget" />
<widget class="QStatusBar" name="statusBar" />
</widget>
<layoutDefault spacing="6" margin="11" />
<pixmapfunction></pixmapfunction>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="groupBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="title">
<string>Relays</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>Audio Amp</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<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_amp">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>On</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_ampAuto">
<property name="text">
<string>Auto</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label_6">
<property name="text">
<string>Bedroom Speakers</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QCheckBox" name="checkBox_bspeaker">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string>On</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_bspeakerAuto">
<property name="text">
<string>Auto</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Infinity Mirror</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<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_inf">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>On</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_infAuto">
<property name="text">
<string>Auto</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Airconditioner</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_aircon">
<property name="text">
<string>On</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="button_advRelay">
<property name="text">
<string>Advanced</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_7">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_nightTime">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Shut Down Time:</string>
</property>
</widget>
</item>
<item>
<widget class="QTimeEdit" name="nightTime">
<property name="correctionMode">
<enum>QAbstractSpinBox::CorrectToNearestValue</enum>
</property>
<property name="showGroupSeparator" stdset="0">
<bool>false</bool>
</property>
<property name="currentSection">
<enum>QDateTimeEdit::HourSection</enum>
</property>
<property name="displayFormat">
<string notr="true">hh:mm</string>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_nightTime">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Enable</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListWidget" name="listWidget_patern">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>16777215</width>
<height>120</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>50</height>
</size>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<spacer name="horizontalSpacer_3">
<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="QPushButton" name="presettApply">
<property name="minimumSize">
<size>
<width>128</width>
<height>32</height>
</size>
</property>
<property name="text">
<string>Apply</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="button_color">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>50</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>0</height>
</size>
</property>
<property name="baseSize">
<size>
<width>0</width>
<height>128</height>
</size>
</property>
<property name="text">
<string>Choose Color</string>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_11">
<property name="topMargin">
<number>10</number>
</property>
<property name="bottomMargin">
<number>10</number>
</property>
<item>
<widget class="QPushButton" name="button_lightsOn">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>50</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>48</height>
</size>
</property>
<property name="text">
<string>ON</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="button_lightsOff">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>50</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>0</width>
<height>48</height>
</size>
</property>
<property name="text">
<string>OFF</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Preferred</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_12">
<item>
<widget class="QLabel" name="label_alarm">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Alarm</string>
</property>
</widget>
</item>
<item>
<widget class="QTimeEdit" name="alarmTime">
<property name="enabled">
<bool>true</bool>
</property>
<property name="correctionMode">
<enum>QAbstractSpinBox::CorrectToNearestValue</enum>
</property>
<property name="dateTime">
<datetime>
<hour>0</hour>
<minute>0</minute>
<second>0</second>
<year>2000</year>
<month>1</month>
<day>1</day>
</datetime>
</property>
<property name="currentSection">
<enum>QDateTimeEdit::HourSection</enum>
</property>
<property name="displayFormat">
<string>hh:mm</string>
</property>
<property name="calendarPopup">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_alarm">
<property name="enabled">
<bool>true</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Enable</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QPushButton" name="button_quit">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string>Quit</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

View File

@ -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<<buffer;
if(_port != nullptr) _port->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<<buffer;
}
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);
}
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<<charBuf<<std::endl;
if( _buffer.endsWith('\n') )
{
textRecived(_buffer);
_buffer.clear();
}
}
}
}

View File

@ -1,11 +1,51 @@
#ifndef MICROCONTROLLER_H
#define MICROCONTROLLER_H
#include <iostream>
class Microcontroller
#include <QObject>
#include <QColor>
#include <QIODevice>
#include <QString>
#include <QRunnable>
#include <QScopedPointer>
#include <QEventLoop>
#include <QTimer>
class Microcontroller: public QObject
{
Q_OBJECT
private:
QIODevice* _port = nullptr;
QScopedPointer<QEventLoop> 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

16
relay.h
View File

@ -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<<buffer;
sWrite(serial, buffer, length);
}
#endif // RELAY_H

View File

@ -6,6 +6,7 @@ RelayDialog::RelayDialog(QWidget *parent) :
ui(new Ui::RelayDialog)
{
ui->setupUi(this);
}
RelayDialog::~RelayDialog()

View File

@ -1,38 +1,242 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<author/>
<comment/>
<exportmacro/>
<class>RelayDialog</class>
<widget class="QDialog" name="RelayDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
<width>358</width>
<height>277</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
<string>Advanced Relays</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Advanced</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Night Relays</string>
</property>
</widget>
</item>
<item>
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</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_3">
<property name="text">
<string>On</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Test Equitment</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<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_4">
<property name="text">
<string>On</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>3Dator</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<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_5">
<property name="text">
<string>On</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="Line" name="line_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_4">
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Aux 2</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<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_6">
<property name="text">
<string>On</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_5">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string>Enterance Watch</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_5">
<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>Inside</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="checkBox_2">
<property name="text">
<string>Watch</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
<rect>
<x>30</x>
<y>240</y>
<width>341</width>
<height>32</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<pixmapfunction/>
<resources/>
<connections>
<connection>

View File

@ -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<<string;
}
void sWrite(int port, const char string[], size_t length)
{
if(port != -1) write(port, string, length);
else std::cout<<string;
}
ssize_t sRead(int port, void *buf, size_t count)
{
return (port != -1) ? read(port, buf, count) : 0;
}
int serialport_init()
{
int fd;
system("stty -F /dev/ttyUSB0 38400 sane -echo");
fd = open(DEVICE, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("init_serialport: Unable to open port ");
return -1;
}
if (tcgetattr(fd, &toptions) < 0)
{
perror("init_serialport: Couldn't get term attributes");
return -1;
}
speed_t brate = BAUDRATE;
cfsetispeed(&toptions, brate);
cfsetospeed(&toptions, brate);
// 8N1
toptions.c_cflag &= ~PARENB;
toptions.c_cflag &= ~CSTOPB;
toptions.c_cflag &= ~CSIZE;
toptions.c_cflag |= CS8;
// no flow control
toptions.c_cflag &= ~CRTSCTS;
toptions.c_cflag |= CREAD | CLOCAL | IGNPAR; // turn on READ & ignore ctrl lines
toptions.c_iflag &= ~(IXON | IXOFF | IXANY); // turn off s/w flow ctrl
toptions.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOE); // make raw
toptions.c_oflag &= ~OPOST; // make raw
// see: http://unixwiz.net/techtips/termios-vmin-vtime.html
toptions.c_cc[VMIN] = 0;
toptions.c_cc[VTIME] = 20;
fcntl(fd, F_SETFL, FNDELAY);
if( tcsetattr(fd, TCSANOW, &toptions) < 0)
{
perror("init_serialport: Couldn't set term attributes");
return -1;
}
return fd;
}

View File

@ -1,4 +1,21 @@
#ifndef SERIAL_H
#define SERIAL_H
#include <termios.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#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

View File

@ -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();
}
}
}

View File

@ -1,11 +1,39 @@
#ifndef SERIALWATCHER_H
#define SERIALWATCHER_H
#include "serial_io.h"
#define BUFFER_SIZE 128
class SerialWatcher
#include<QString>
#include<QObject>
#include<QRunnable>
#include<QScopedPointer>
#include<QEventLoop>
#include<QTimer>
class SerialWatcher: public QObject, public QRunnable
{
Q_OBJECT
private:
QScopedPointer<QEventLoop> 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