Skip to content

Commit 1b45988

Browse files
committed
qt: output read buffer to buffer table
1 parent cc3dbdb commit 1b45988

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

qt/main_window.cpp

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#define HEADER_ADDRESS_WIDTH 80
2525
#define HEADER_HEX_WIDTH 340
2626

27+
#define START_ADDRESS 0x00000000
28+
2729
static void initBufferTable(QTableWidget *bufTable)
2830
{
2931
QTableWidgetItem *addressHeaderItem, *hexHeaderItem, *anciiHeaderItem;
@@ -90,12 +92,31 @@ void MainWindow::log(QString logMsg)
9092
ui->logTextEdit->insertPlainText(logMsg);
9193
}
9294

95+
void MainWindow::insertBufferRow(quint8 *readBuf, quint32 size, quint32 rowNum,
96+
quint32 address)
97+
{
98+
QString addressString, hexString;
99+
100+
ui->bufferTableWidget->insertRow(rowNum);
101+
102+
for (uint32_t i = 0; i < size; i++)
103+
hexString.append(QString().sprintf("%02X ", readBuf[i]));
104+
105+
addressString.sprintf("0x%08X", address);
106+
107+
ui->bufferTableWidget->setItem(rowNum, HEADER_ADDRESS_COL,
108+
new QTableWidgetItem(addressString));
109+
ui->bufferTableWidget->setItem(rowNum, HEADER_HEX_COL,
110+
new QTableWidgetItem(hexString));
111+
ui->bufferTableWidget->setItem(rowNum, HEADER_ANCII_COL,
112+
new QTableWidgetItem("................"));
113+
}
114+
93115
void MainWindow::slotFileOpen()
94116
{
95117
qint64 ret;
96-
QString addressString, hexString;
97118
quint8 readBuf[ROW_DATA_SIZE] = {};
98-
quint32 rowNum = 1, address = 0;
119+
quint32 rowNum = HEADER_ROW_NUM, address = START_ADDRESS;
99120
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), ".",
100121
tr("Binary Files (*)"));
101122

@@ -110,23 +131,13 @@ void MainWindow::slotFileOpen()
110131
return;
111132
}
112133

134+
/* Reset buffer table */
135+
ui->bufferTableWidget->setRowCount(HEADER_ROW_NUM);
136+
113137
while ((ret = file.read((char *)readBuf, ROW_DATA_SIZE)) > 0)
114138
{
115-
ui->bufferTableWidget->insertRow(rowNum);
116-
117-
hexString.clear();
118-
for (int i = 0; i < ret; i++)
119-
hexString.append(QString().sprintf("%02X ", readBuf[i]));
120-
121-
addressString.sprintf("0x%08X", address);
139+
insertBufferRow(readBuf, ret, rowNum, address);
122140
address += ret;
123-
124-
ui->bufferTableWidget->setItem(rowNum, HEADER_ADDRESS_COL,
125-
new QTableWidgetItem(addressString));
126-
ui->bufferTableWidget->setItem(rowNum, HEADER_HEX_COL,
127-
new QTableWidgetItem(hexString));
128-
ui->bufferTableWidget->setItem(rowNum, HEADER_ANCII_COL,
129-
new QTableWidgetItem("................"));
130141
rowNum++;
131142
}
132143

@@ -180,20 +191,43 @@ void MainWindow::slotProgErase()
180191
QByteArray ba = ui->chipSelectComboBox->currentText().toLatin1();
181192
ChipInfo *chipInfo = getChipInfoByName(ba.data());
182193

183-
if (prog->eraseChip(0x00000000, chipInfo->size))
194+
if (prog->eraseChip(START_ADDRESS, chipInfo->size))
184195
log(tr("Failed to erase chip\n"));
185196
else
186197
log(tr("Chip has been erased successfully\n"));
187198
}
188199

189200
void MainWindow::slotProgRead()
190201
{
191-
uint8_t buf[2048];
202+
uint32_t rowNum = HEADER_ROW_NUM, address = START_ADDRESS;
203+
QByteArray ba = ui->chipSelectComboBox->currentText().toLatin1();
204+
ChipInfo *chipInfo = getChipInfoByName(ba.data());
205+
std::unique_ptr< uint8_t[] > buf = std::unique_ptr< uint8_t[] >
206+
(new (std::nothrow) uint8_t[chipInfo->size]);
207+
208+
if (!buf.get())
209+
{
210+
qCritical() << "Failed to allocate momory for read buffer";
211+
return;
212+
}
213+
214+
/* Reset buffer table */
215+
ui->bufferTableWidget->setRowCount(HEADER_ROW_NUM);
192216

193-
if (prog->readChip(buf, 0x00000000, sizeof(buf)))
217+
if (prog->readChip(buf.get(), START_ADDRESS, chipInfo->size))
218+
{
194219
log(tr("Failed to read chip\n"));
220+
return;
221+
}
195222
else
196223
log(tr("Data has been successfully read\n"));
224+
225+
for (uint32_t i = 0; i < chipInfo->size; i += ROW_DATA_SIZE)
226+
{
227+
insertBufferRow(buf.get() + i, ROW_DATA_SIZE, rowNum, address);
228+
rowNum++;
229+
address += ROW_DATA_SIZE;
230+
}
197231
}
198232

199233
void MainWindow::slotProgWrite()
@@ -234,7 +268,7 @@ void MainWindow::slotProgWrite()
234268
}
235269
}
236270

237-
if (prog->writeChip(buf.get(), 0x00000000, bufIter))
271+
if (prog->writeChip(buf.get(), START_ADDRESS, bufIter))
238272
log(tr("Failed to write chip\n"));
239273
else
240274
log(tr("Data has been successfully written\n"));

qt/main_window.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class MainWindow : public QMainWindow
2727
private:
2828
Ui::MainWindow *ui;
2929

30+
void insertBufferRow(quint8 *readBuf, quint32 size, quint32 rowNum,
31+
quint32 address);
32+
3033
public slots:
3134
void slotFileOpen();
3235
void slotProgConnect();

0 commit comments

Comments
 (0)