//-----------------------------------------------------------------------------
// Copyright 2007 Jonathan Westhues
//
// This file is part of LDmicro.
//
// LDmicro is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// LDmicro is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with LDmicro. If not, see .
//------
//
// Dialog for setting the overall PLC parameters. Mostly this relates to
// timing; to set up the timers we need to know the desired cycle time,
// which is configurable, plus the MCU clock (i.e. crystal frequency).
// Jonathan Westhues, Nov 2004
//-----------------------------------------------------------------------------
#include
#include
#include
#include
//#include
#include "ldmicro.h"
static QDialog* ConfDialog;
static QLineEdit* CrystalTextbox;
static QLineEdit* CycleTextbox;
static QLineEdit* BaudTextbox;
static QDialogButtonBox* ButtonBox;
static LONG_PTR PrevCrystalProc;
static LONG_PTR PrevCycleProc;
static LONG_PTR PrevBaudProc;
QGridLayout* ConfGrid;
static inline void DestroyWindow()
{
delete ConfDialog;
delete CrystalTextbox;
delete CycleTextbox;
delete BaudTextbox;
delete ButtonBox;
delete ConfGrid;
}
static void MakeControls(void)
{
// Creating text labels
QLabel* textLabel = new QLabel(_("Cycle Time (ms):"));
QLabel* textLabel2 = new QLabel(_("Crystal Frequency (MHz):"));
QLabel* textLabel3 = new QLabel(_("UART Baud Rate (bps):"));
CycleTextbox = new QLineEdit();
CrystalTextbox = new QLineEdit();
BaudTextbox = new QLineEdit();
CycleTextbox->setValidator(
new QRegExpValidator(
QRegExp("[0-9]+")));
CrystalTextbox->setValidator(
new QRegExpValidator(
QRegExp("[0-9]+")));
BaudTextbox->setValidator(
new QRegExpValidator(
QRegExp("[0-9]+")));
if(!UartFunctionUsed()) {
BaudTextbox->setEnabled(FALSE);
textLabel3->setEnabled(FALSE);
}
if(Prog.mcu && (Prog.mcu->whichIsa == ISA_ANSIC ||
Prog.mcu->whichIsa == ISA_INTERPRETED))
{
CrystalTextbox->setEnabled(FALSE);
textLabel2->setEnabled(FALSE);
}
ButtonBox = new QDialogButtonBox(QDialogButtonBox::Ok
| QDialogButtonBox::Cancel, Qt::Vertical,
ConfDialog);
char explanation[1024] = "";
if(UartFunctionUsed()) {
if(Prog.mcu && Prog.mcu->uartNeeds.rxPin != 0) {
sprintf(explanation,
_("Serial (UART) will use pins %d and %d.\r\n\r\n"),
Prog.mcu->uartNeeds.rxPin, Prog.mcu->uartNeeds.txPin);
}
else {
strcpy(explanation,
_("Please select a micro with a UART.\r\n\r\n"));
}
}
else {
strcpy(explanation, _("No serial instructions (UART Send/UART Receive) "
"are in use; add one to program before setting baud rate.\r\n\r\n")
);
}
strcat(explanation,
_("The cycle time for the 'PLC' runtime generated by LDmicro is user-"
"configurable. Very short cycle times may not be achievable due "
"to processor speed constraints, and very long cycle times may not "
"be achievable due to hardware overflows. Cycle times between 10 ms "
"and 100 ms will usually be practical.\r\n\r\n"
"The compiler must know what speed crystal you are using with the "
"micro to convert between timing in clock cycles and timing in "
"seconds. A 4 MHz to 20 MHz crystal is typical; check the speed "
"grade of the part you are using to determine the maximum allowable "
"clock speed before choosing a crystal."));
QLabel* textLabel4 = new QLabel(explanation);
textLabel4->setAlignment(Qt::AlignJustify);
textLabel4->setMaximumWidth(310);
textLabel4->setWordWrap(TRUE);
// Creating required containers for packing
ConfGrid->addWidget(textLabel, 0,0);
ConfGrid->addWidget(CycleTextbox, 0,1);
ConfGrid->addWidget(ButtonBox, 0, 2, 0, 2);
ConfGrid->addWidget(textLabel2, 1, 0);
ConfGrid->addWidget(CrystalTextbox, 1, 1);
ConfGrid->addWidget(textLabel3, 2, 0);
ConfGrid->addWidget(BaudTextbox, 2, 1);
ConfGrid->addWidget(textLabel4, 3, 0, 2, 0,
Qt::AlignJustify);
NiceFont(ConfDialog);
CycleTextbox->setFocus();
ConfDialog->adjustSize();
QObject::connect(ButtonBox, SIGNAL(accepted()), ConfDialog, SLOT(accept()));
QObject::connect(ButtonBox, SIGNAL(rejected()), ConfDialog, SLOT(reject()));
}
void ShowConfDialog(void)
{
// The window's height will be resized later, to fit the explanation text.
ConfDialog = CreateWindowClient(_("PLC Configuration"),
100, 100, 0, 0, MainWindow);
ConfGrid = new QGridLayout(ConfDialog);
MakeControls();
char buf[16];
sprintf(buf, "%.1f", (Prog.cycleTime / 1000.0));
CycleTextbox->setText(buf);
sprintf(buf, "%.6f", Prog.mcuClock / 1e6);
CrystalTextbox->setText(buf);
sprintf(buf, "%d", Prog.baudRate);
BaudTextbox->setText(buf);
int ret = ConfDialog->exec();
switch(ret)
{
case QDialog::Accepted:
{
char buf[16];
strncpy(buf, CycleTextbox->text().toStdString().c_str(),16);
Prog.cycleTime = (int)(1000*atof(buf) + 0.5);
if(Prog.cycleTime == 0) {
Error(_("Zero cycle time not valid; resetting to 10 ms."));
Prog.cycleTime = 10000;
}
strncpy(buf, CrystalTextbox->text().toStdString().c_str(),16);
Prog.mcuClock = (int)(1e6*atof(buf) + 0.5);
strncpy(buf, BaudTextbox->text().toStdString().c_str(),16);
Prog.baudRate = atoi(buf);
}
break;
case QDialog::Rejected:
break;
}
}