RISC-V/V203/usb/scope/software/mainwindow.cpp
2024-11-10 21:33:20 +01:00

201 lines
7.7 KiB
C++

#include <QDesktopWidget>
#include <QFileDialog>
#include <QPainter>
#include <QSettings>
#include "mainwindow.h"
#include "ui_mainwindow.h"
/*
#undef qDebug
#define qDebug(p, ...)
*/
static const char * TrigerChannelTexts [] = {
"Channel A", "Channel B"
};
static const char * TrigerModeTexts [] = {
"Auto", "Normal", "Single"
};
static const char * TrigerEdgeTexts [] = {
"Rising", "Faling"
};
static const char * TrigerSelectTexts [] = {
"Trig.Value", "Trig.Offset", "Marker A", "Marker B", "Time Zoom", "Ofset A", "Ofset B"
};
static const char * TimeBaseTexts [] = {
"2μs", "5μs", "10μs", "20μs", "50μs", "100μs", "200μs","500μs",
"1ms", "2ms", "5ms", "10ms", "20ms", "50ms", "100ms", "200ms",
"500ms", "1s",
};
static const char * VoltageScaleTexts [] = {
"100mV","200mV","500mV",
"1V", "2V", "5V",
"10V","20V","50V",
};
MainWindow::MainWindow (QWidget * parent)
: QMainWindow (parent), firmware(parent) {
ui = new Ui_MainWindow;
ui->setupUi (this);
ui->radio_a->setChecked(true);
ui->Display->setTrigger (firmware.getTrigger());
for (auto & e: TrigerModeTexts) ui->comboMode ->addItem(QString(e));
for (auto & e: TrigerChannelTexts) ui->comboChannel ->addItem(QString(e));
for (auto & e: TrigerEdgeTexts) ui->comboRissing ->addItem(QString(e));
for (auto & e: TrigerSelectTexts) ui->comboValue ->addItem(QString(e));
for (auto & e: TimeBaseTexts) ui->comboTimeRange->addItem(QString(e));
for (auto & e: VoltageScaleTexts) ui->comboVA ->addItem(QString(e));
for (auto & e: VoltageScaleTexts) ui->comboVB ->addItem(QString(e));
setWindowTitle ("Scope");
setWindowIcon (QIcon(":ico"));
//setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
connect (&firmware, SIGNAL (Channels_received(QVector<int>, QVector<int>)),
ui->Display, SLOT (DispChannels (QVector<int>, QVector<int>)));
connect (&firmware, SIGNAL (PaketTriggered()), this, SLOT(PaketTriggered()));
connect (&firmware, SIGNAL (SettingReceived(AllSettings)), this, SLOT(SetSelections(AllSettings)));
connect (ui->Display, SIGNAL(SettingsChanged(int)), &firmware, SLOT(SettingChanged(int)));
connect (ui->buttonStart,SIGNAL(pressed()), this, SLOT(Started()));
connect (ui->actionQuit, SIGNAL(triggered(bool)), this, SLOT(close()));
connect (ui->radio_a, SIGNAL(toggled(bool)), this, SLOT(MarkerChangedA(bool)));
connect (ui->radio_b, SIGNAL(toggled(bool)), this, SLOT(MarkerChangedB(bool)));
connect (ui->radio_c, SIGNAL(toggled(bool)), this, SLOT(MarkerChangedC(bool)));
connect (ui->actionExport_Image, SIGNAL(triggered(bool)), this, SLOT(ExportImage (bool)));
connect (ui->actionSaveSet, SIGNAL(triggered(bool)), this, SLOT(SaveSettings(bool)));
connect (ui->actionRestSet, SIGNAL(triggered(bool)), this, SLOT(RestSettings(bool)));
connect (ui->comboMode, SIGNAL(activated(int)), this, SLOT(SendTrigerMode(int)));
connect (ui->comboRissing, SIGNAL(activated(int)), this, SLOT(SendTrigerEdge(int)));
connect (ui->comboChannel, SIGNAL(activated(int)), this, SLOT(SendTrigerChan(int)));
connect (ui->comboValue, SIGNAL(activated(int)), this, SLOT(TriggerValues (int)));
connect (ui->comboTimeRange, SIGNAL(activated(int)), this, SLOT(TimeBaseRange (int)));
connect (ui->comboVA, SIGNAL(activated(int)), this, SLOT(SendVoltageA (int)));
connect (ui->comboVB, SIGNAL(activated(int)), this, SLOT(SendVoltageB (int)));
ui->comboTimeRange->setCurrentIndex(6);
ui->buttonStart->setEnabled(false);
}
void MainWindow::SetSelections(AllSettings as) {
printf("channel : %d\n", as.part.trg.channel);
printf("mode : %d\n", as.part.trg.mode);
printf("rising : %d\n", as.part.trg.rising);
printf("offset : %d\n", as.part.trg.offset);
printf("value : %d\n", as.part.trg.value);
printf("tim bas : %d\n", as.part.tim);
printf("CHA : %d\n", as.part.asc);
printf("CHB : %d\n", as.part.bsc);
ui->comboChannel ->setCurrentIndex(as.part.trg.channel);
ui->comboMode ->setCurrentIndex(as.part.trg.mode);
ui->comboRissing ->setCurrentIndex(as.part.trg.rising ? 1 : 0);
ui->comboTimeRange->setCurrentIndex(as.part.tim);
ui->comboVA ->setCurrentIndex(as.part.asc);
ui->comboVB ->setCurrentIndex(as.part.bsc);
ui->Display->TimeBaseRange (as.part.tim);
ui->Display->reloadChRange (as.part.asc, as.part.bsc);
ui->Display->SendTrigerChan(as.part.trg.channel);
}
MainWindow::~MainWindow () {
delete ui;
}
void MainWindow::Started() {
firmware.Start();
}
void MainWindow::MarkerChangedA(bool b) {
ui->Display->MarkerChanged (0,b);
}
void MainWindow::MarkerChangedB(bool b) {
ui->Display->MarkerChanged (1,b);
}
void MainWindow::MarkerChangedC(bool b) {
ui->Display->MarkerChanged (2,b);
}
void MainWindow::TriggerValues (int n) {
ui->Display->TriggerValues(n);
}
void MainWindow::SendVoltageA(int n) {
firmware.SendVoltage (0,n);
ui->Display->SendVoltage(0,n);
}
void MainWindow::SendVoltageB(int n) {
firmware.SendVoltage (1,n);
ui->Display->SendVoltage(1,n);
}
void MainWindow::SendTrigerMode (int n) {
if (n == TRIGER_MODE_SINGLE) ui->buttonStart->setEnabled(true);
else ui->buttonStart->setEnabled(false);
/*
if (n == TRIGER_MODE_AUTO) ui->comboTimeRange->setEnabled(true); // BUG - jinak dojde při přepínání k chybě
else ui->comboTimeRange->setEnabled(false); // fixed
*/
firmware.SendTrigerMode(n);
}
void MainWindow::SendTrigerEdge (int n) {
firmware.SendTrigerEdge(n);
}
void MainWindow::SendTrigerChan (int n) {
firmware.SendTrigerChan (n);
ui->Display->SendTrigerChan(n);
}
void MainWindow::TimeBaseRange (int n) {
firmware.SendBaseRange(n);
ui->Display->TimeBaseRange(n);
}
void MainWindow::PaketTriggered () {
statusBar()->showMessage(QString("TRIGER"), 500);
}
void MainWindow::ExportImage (bool) {
QWidget * pw = ui->Display;
QString fileName = QFileDialog::getSaveFileName (this,
tr ("Save File"), ".", "Images Files(*.png)");
if (!fileName.isEmpty()) {
QImage img (pw->width(), pw->height(), QImage::Format_ARGB32_Premultiplied);
QPainter painter(&img);
pw->render (&painter);
img.save (fileName, "png", 0);
}
}
void MainWindow::SaveSettings(bool) {
qDebug ("Save Settings");
QSettings setting ("Kizarm", "Scope");
setting.beginGroup("Trigger");
const TrigerSettings * const ts = firmware.getTrigger();
setting.setValue("Value", ts->value);
setting.setValue("Offset", ts->offset);
setting.setValue("Mode", ts->mode);
setting.setValue("Channel", ts->channel);
setting.setValue("Edge", ts->rising);
setting.endGroup();
setting.beginGroup("Markers");
ui->Display->saveSettings (setting);
setting.endGroup();
}
void MainWindow::RestSettings(bool) {
qDebug ("Restore Settings");
QSettings setting ("Kizarm", "Scope");
setting.beginGroup("Trigger");
TrigerSettings ts;
ts.value = setting.value ("Value").toInt();
ts.offset = setting.value ("Offset").toInt();
ts.mode = static_cast<TRIGER_MODE> (setting.value ("Mode").toInt());
ui->comboMode->setCurrentIndex(ts.mode);
ts.channel = static_cast<ADC_CHANNELS> (setting.value ("Channel").toInt());
ui->comboChannel->setCurrentIndex(ts.channel);
ts.rising = setting.value ("Edge").toBool();
ui->comboRissing->setCurrentIndex(ts.value ? 1 : 0);
memcpy (firmware.getTrigger(), & ts, sizeof(TrigerSettings));
setting.endGroup();
SendTrigerMode (ts.mode);
firmware.SettingChanged(MOVE_VALUE);
firmware.SettingChanged(MOVE_OFSET);
setting.beginGroup("Markers");
ui->Display->restSettings (setting);
const int n = setting.value("TimeBaseIndex").toInt();
TimeBaseRange(n);
ui->comboTimeRange->setCurrentIndex(n);
setting.endGroup();
}