on textChanged() and on_toggeled() handlers for serial encoding

This commit is contained in:
mmehari 2024-03-23 05:03:51 +01:00
parent 3907e4d180
commit 7383ea0785
4 changed files with 163 additions and 1 deletions

View File

@ -93,6 +93,7 @@ void SingleChannelController::waveformName(QString newName)
dataStringCurrent += strcspn(dataStringCurrent, "\t") + 1;
m_data.samples[i] = static_cast<uint8_t>(dummy);
}
m_data.repeat_forever = true;
free(dataString);
fclose(fptr);
@ -111,6 +112,7 @@ void SingleChannelController::freqUpdate(double newFreq)
{
qDebug() << "newFreq = " << newFreq;
m_data.freq = newFreq;
m_data.repeat_forever = true;
notifyUpdate(this);
}
@ -118,6 +120,7 @@ void SingleChannelController::amplitudeUpdate(double newAmplitude)
{
qDebug() << "newAmplitude = " << newAmplitude;
m_data.amplitude = newAmplitude;
m_data.repeat_forever = true;
notifyUpdate(this);
}
@ -125,6 +128,20 @@ void SingleChannelController::offsetUpdate(double newOffset)
{
qDebug() << "newOffset = " << newOffset;
m_data.offset = newOffset;
m_data.repeat_forever = true;
notifyUpdate(this);
}
void SingleChannelController::txuartUpdate(int baudRate, std::vector<uint8_t> samples)
{
// Update txUart data
int length = samples.size();
m_data.samples.resize(length);
m_data.samples = samples;
m_data.freq = baudRate/length;
m_data.divisibility = 1;
m_data.repeat_forever = false;
notifyUpdate(this);
}
@ -186,6 +203,11 @@ void DualChannelController::offsetUpdate(ChannelID channelID, double newOffset)
getChannelController(channelID)->offsetUpdate(newOffset);
}
void DualChannelController::txuartUpdate(ChannelID channelID, int baudRate, std::vector<uint8_t> samples)
{
getChannelController(channelID)->txuartUpdate(baudRate, samples);
}
void DualChannelController::waveformName_CH1(QString newName)
{

View File

@ -24,6 +24,7 @@ enum class ChannelID
struct ChannelData
{
std::vector<uint8_t> samples;
bool repeat_forever;
int divisibility;
double freq = 1000.0;
double amplitude = 0.0;
@ -47,6 +48,7 @@ public slots:
void freqUpdate(double newFreq);
void amplitudeUpdate(double newAmplitude);
void offsetUpdate(double newOffset);
void txuartUpdate(int baudRate, std::vector<uint8_t> samples);
private:
ChannelData m_data;
@ -60,6 +62,7 @@ public:
public:
SingleChannelController* getChannelController(ChannelID channelID);
void txuartUpdate(ChannelID channelID, int baudRate, std::vector<uint8_t> samples);
signals:
void functionGenToUpdate(ChannelID channel, SingleChannelController* fGenControl);

View File

@ -88,7 +88,7 @@ MainWindow::MainWindow(QWidget *parent) :
// layout->addWidget(sizeGrip, 0,0,1,1,Qt::AlignBottom | Qt::AlignLeft);
// }
for (const auto & console : {ui->console1, ui->console2})
for (const auto & console : {ui->console1, ui->console2, ui->txuart})
{
QFont font("Monospace");
font.setStyleHint(QFont::Monospace);
@ -99,6 +99,7 @@ MainWindow::MainWindow(QWidget *parent) :
ui->kickstartIsoButton->setVisible(0);
ui->console1->setVisible(0);
ui->console2->setVisible(0);
ui->txuart->setVisible(0);
#endif
ui->timeBaseSlider->setVisible(0);
@ -2572,3 +2573,133 @@ void MainWindow::on_actionFrequency_Spectrum_triggered(bool checked)
else
MAX_WINDOW_SIZE = 10;
}
std::vector<uint8_t> MainWindow::uartEncode(const QString& text, UartParity parity)
{
std::vector<uint8_t> uartData;
int i;
// Prepend Initialization data
for (i = 0; i < 300; i++)
uartData.push_back(255);
for (const QChar& character : text)
{
// Convert character to ASCII value
int ascii = character.toLatin1();
// Append start bit
uartData.push_back(0);
// Append data bits (least significant bit first)
for (i = 0; i < 8; i++)
{
if((ascii >> i) & 1)
uartData.push_back(255);
else
uartData.push_back(0);
}
if(parity == UartParity::Even || parity == UartParity::Odd)
{
// Calculate and append parity bit
int numOneBits = 0;
for (bool bit : uartData)
{
if (bit)
numOneBits++;
}
if(parity == UartParity::Even)
{
if(numOneBits % 2 == 0)
uartData.push_back(0);
else
uartData.push_back(255);
}
else
{
if(numOneBits % 2 == 0)
uartData.push_back(255);
else
uartData.push_back(0);
}
}
// Append stop bit
uartData.push_back(255);
}
return uartData;
}
void MainWindow::on_serialEncodingCheck_CH1_toggled(bool checked)
{
int baudRate_CH1;
UartParity parity_CH1;
std::vector<uint8_t> data;
// If uart encoding is enabled
if(checked)
{
// Enable uart decoding
ui->serialDecodingCheck_CH1->setChecked(true);
/* transmit IDLE signal */
// txuart parameters
baudRate_CH1 = ui->controller_iso->baudRate_CH1;
parity_CH1 = ui->controller_iso->parity_CH1;
// Encode txuart data
data = uartEncode("\r\n", parity_CH1);
// Transmit txuart data
using functionGen::ChannelID;
ui->controller_fg->txuartUpdate(ChannelID::CH1, baudRate_CH1, data);
}
}
void MainWindow::on_txuart_textChanged()
{
QString text, new_char;
int baudRate_CH1;
UartParity parity_CH1;
std::vector<uint8_t> data;
// Retrieve text displayed
text = ui->txuart->toPlainText();
// Check if key pressed in backspace
if (text.length() == prev_text.length()-1)
new_char = '\b';
else
new_char = text.right(1);
// txuart parameters
baudRate_CH1 = ui->controller_iso->baudRate_CH1;
parity_CH1 = ui->controller_iso->parity_CH1;
// Encode txuart data
if (new_char == '\n')
new_char = "\r\n";
data = uartEncode(new_char, parity_CH1);
// Transmit txuart data
using functionGen::ChannelID;
ui->controller_fg->txuartUpdate(ChannelID::CH1, baudRate_CH1, data);
// Check if the last character is newline
if (new_char == '\r')
{
// Clear txuart screen
ui->txuart->clear();
// Update previous text
prev_text = "";
}
else
{
// Update previous text
prev_text = text;
}
}

View File

@ -228,6 +228,9 @@ private slots:
void on_actionShow_Debug_Console_triggered(bool checked);
void on_actionFrequency_Spectrum_triggered(bool checked);
void on_serialEncodingCheck_CH1_toggled(bool checked);
void on_txuart_textChanged();
private:
//Generic Vars
Ui::MainWindow *ui;
@ -250,6 +253,7 @@ private:
QPalette defaultPalette;
QString defaultStyleName;
QString prev_text;
//Generic Functions
void initialisePlot();
@ -258,6 +262,8 @@ private:
void initShortcuts();
void readSettingsFile();
void setDarkMode(bool dark);
std::vector<uint8_t> uartEncode(const QString& line, UartParity parity);
std::vector<uint8_t> resampler(std::vector<uint8_t>, int input_baudRate, int output_baudRate);
//Shortcut pointers
QActionGroup *gainGroup;