fantestwindow.cpp 7.13 KB
#include "fantestwindow.h"
#include "ui_fantestwindow.h"

#include "soundplayer.h"

FanTestWindow::FanTestWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::FanTestWindow)
{
    ui->setupUi(this);

    ui->clockContainer->setParent(ui->upperStack);
    setAttribute(Qt::WA_DeleteOnClose);

    foreach (QPushButton *button, findChildren<QPushButton *>())
        connect(button, &QPushButton::pressed, SoundPlayer::playClick);

    udp = UdpHandler::getInstance();
    connect(udp, SIGNAL(changed()), this, SLOT(onDataChanged()));

    currentFan = Fan1;
    direction1 = direction2 = ClockWise;
    rpm1 = rpm2 = 500;

    udp->set(TG_OVEN_MODE, 4);
    udp->turnOn(TG_MANUAL_FAN1);
    udp->turnOn(TG_MANUAL_FAN2);
    udp->set(TG_FAN1_RPM, rpm1);
    udp->set(TG_FAN2_RPM, rpm2);
    udp->set(TG_FAN1_RPM, direction1);
    udp->set(TG_FAN2_RPM, direction2);

    onDataChanged();
}

FanTestWindow::~FanTestWindow()
{
    delete ui;
}

void FanTestWindow::onDataChanged()
{
    if (udp->convection1())
        ui->start1Button->setText("STOP");
    else
        ui->start1Button->setText("START");

    if (udp->convection2())
        ui->start2Button->setText("STOP");
    else
        ui->start2Button->setText("START");

    int rpm;
    Direction dir;

    QString unselectedStyle("color: white");
    QString selectedStyle("color: yellow");

    ui->motorLabel1->setStyleSheet(unselectedStyle);
    ui->motorLabel2->setStyleSheet(unselectedStyle);
    ui->directionLabelLeft->setStyleSheet(unselectedStyle);
    ui->directionLabelRight->setStyleSheet(unselectedStyle);
    ui->rpmLabel500->setStyleSheet(unselectedStyle);
    ui->rpmLabel725->setStyleSheet(unselectedStyle);
    ui->rpmLabel950->setStyleSheet(unselectedStyle);
    ui->rpmLabel1175->setStyleSheet(unselectedStyle);
    ui->rpmLabel1400->setStyleSheet(unselectedStyle);

    switch (currentFan)
    {
    case Fan1:
        rpm = rpm1;
        dir = direction1;
        ui->fanLabel->setPixmap(QPixmap(":/images/config/service/fan_sel_1.png"));
        ui->motorLabel1->setStyleSheet(selectedStyle);
        break;
    case Fan2:
        rpm = rpm2;
        dir = direction2;
        ui->fanLabel->setPixmap(QPixmap(":/images/config/service/fan_sel_2.png"));
        ui->motorLabel2->setStyleSheet(selectedStyle);
        break;
    default:
        rpm = 500;
        dir = ClockWise;
        ui->fanLabel->setPixmap(QPixmap(":/images/config/service/fan_sel_1.png"));
        ui->motorLabel1->setStyleSheet(selectedStyle);
    }

    switch (rpm)
    {
    case 500:
        ui->rpmLabel->setPixmap(QPixmap(":/images/config/service/fan_level_1.png"));
        ui->rpmLabel500->setStyleSheet(selectedStyle);
        break;
    case 725:
        ui->rpmLabel->setPixmap(QPixmap(":/images/config/service/fan_level_2.png"));
        ui->rpmLabel725->setStyleSheet(selectedStyle);
        break;
    case 950:
        ui->rpmLabel->setPixmap(QPixmap(":/images/config/service/fan_level_3.png"));
        ui->rpmLabel950->setStyleSheet(selectedStyle);
        break;
    case 1175:
        ui->rpmLabel->setPixmap(QPixmap(":/images/config/service/fan_level_4.png"));
        ui->rpmLabel1175->setStyleSheet(selectedStyle);
        break;
    case 1400:
        ui->rpmLabel->setPixmap(QPixmap(":/images/config/service/fan_level_5.png"));
        ui->rpmLabel1400->setStyleSheet(selectedStyle);
        break;
    default:
        ui->rpmLabel->setPixmap(QPixmap(":/images/config/service/fan_level_1.png"));
        ui->rpmLabel500->setStyleSheet(selectedStyle);
    }

    switch (dir)
    {
    case ClockWise:
        ui->directionLabel->setPixmap(QPixmap(":/images/config/service/fan_dir_cw.png"));
        ui->directionLabelRight->setStyleSheet(selectedStyle);
        break;
    case CounterClockWise:
        ui->directionLabel->setPixmap(QPixmap(":/images/config/service/fan_dir_ccw.png"));
        ui->directionLabelLeft->setStyleSheet(selectedStyle);
        break;
    default:
        ui->directionLabel->setPixmap(QPixmap(":/images/config/service/fan_dir_cw.png"));
        ui->directionLabelRight->setStyleSheet(selectedStyle);
    }

    QString buf;
    ui->motor1RpmLabel->setText(buf.sprintf("%d rpm", rpm1));
    ui->motor2RpmLabel->setText(buf.sprintf("%d rpm", rpm2));
}

void FanTestWindow::on_rpmButton_clicked()
{
    int *target;
    switch (currentFan)
    {
    case Fan1:
        target = &rpm1;
        break;
    case Fan2:
        target = &rpm2;
        break;
    default:
        return;
    }

    switch (*target)
    {
    case 500:
        *target = 725;
        break;
    case 725:
        *target = 950;
        break;
    case 950:
        *target = 1175;
        break;
    case 1175:
        *target = 1400;
        break;
    case 1400:
        *target = 500;
        break;
    default:
        *target = 500;
    }

    setRpm(currentFan, *target);

    onDataChanged();
}

void FanTestWindow::on_directionButton_clicked()
{
    Direction *target;
    switch (currentFan)
    {
    case Fan1:
        target = &direction1;
        break;
    case Fan2:
        target = &direction2;
        break;
    default:
        return;
    }

    switch (*target)
    {
    case ClockWise:
        *target = CounterClockWise;
        break;
    case CounterClockWise:
        *target = ClockWise;
        break;
    default:
        return;
    }

    setDirection(currentFan, *target);

    onDataChanged();
}

void FanTestWindow::on_motorButton_clicked()
{
    switch (currentFan)
    {
    case Fan1:
        currentFan = Fan2;
        break;
    case Fan2:
        currentFan = Fan1;
        break;
    default:
        currentFan = Fan1;
    }

    onDataChanged();
}

void FanTestWindow::on_start1Button_clicked()
{
    if (udp->convection1())
        stop(Fan1);
    else
        start(Fan1);
}

void FanTestWindow::on_start2Button_clicked()
{
    if (udp->convection2())
        stop(Fan2);
    else
        start(Fan2);
}

void FanTestWindow::on_backButton_clicked()
{
    stop(Fan1);
    stop(Fan2);

    udp->set(TG_FAN1_RPM, 500);
    udp->set(TG_FAN2_RPM, 500);
    udp->set(TG_FAN1_DIRECTOIN, 0);
    udp->set(TG_FAN2_DIRECTOIN, 0);
    udp->turnOff(TG_MANUAL_FAN1);
    udp->turnOff(TG_MANUAL_FAN2);

    deleteLater();
}

void FanTestWindow::setRpm(Fan fan, int rpm)
{
    switch (fan)
    {
    case Fan1:
        udp->set(TG_FAN1_RPM, rpm);
        break;
    case Fan2:
        udp->set(TG_FAN2_RPM, rpm);
        break;
    }
}

void FanTestWindow::setDirection(Fan fan, Direction direction)
{
    int dir;
    switch (direction)
    {
    case ClockWise:
        dir = 0;
        break;
    case CounterClockWise:
        dir = 1;
        break;
    default:
        return;
    }

    switch (fan)
    {
    case Fan1:
        udp->set(TG_FAN1_DIRECTOIN, dir);
        break;
    case Fan2:
        udp->set(TG_FAN2_DIRECTOIN, dir);
        break;
    }
}

void FanTestWindow::start(Fan fan)
{
    switch (fan)
    {
    case Fan1:
        udp->turnOn(TG_FAN1_MANUAL);
        break;
    case Fan2:
        udp->turnOn(TG_FAN2_MANUAL);
        break;
    }
}

void FanTestWindow::stop(Fan fan)
{
    switch (fan)
    {
    case Fan1:
        udp->turnOff(TG_FAN1_MANUAL);
        break;
    case Fan2:
        udp->turnOff(TG_FAN2_MANUAL);
        break;
    }
}