|
10 | 10 | #include <QDebug>
|
11 | 11 | #include <QFileDialog>
|
12 | 12 | #include <QFile>
|
| 13 | +#include <QStringList> |
| 14 | +#include <memory> |
13 | 15 |
|
14 | 16 | #define ROW_DATA_SIZE 16
|
| 17 | +#define HEADER_ROW_NUM 1 |
| 18 | +#define HEADER_HEX_COL 1 |
15 | 19 |
|
16 | 20 | static void initBufferTable(QTableWidget *bufTable)
|
17 | 21 | {
|
@@ -187,13 +191,43 @@ void MainWindow::slotProgRead()
|
187 | 191 |
|
188 | 192 | void MainWindow::slotProgWrite()
|
189 | 193 | {
|
190 |
| - uint32_t i; |
191 |
| - uint8_t buf[2048]; |
| 194 | + bool convIsOk; |
| 195 | + QStringList sl; |
| 196 | + std::unique_ptr< uint8_t[] > buf; |
| 197 | + uint32_t bufSize, bufIter = 0; |
| 198 | + uint32_t rowCount = ui->bufferTableWidget->rowCount() - HEADER_ROW_NUM; |
| 199 | + |
| 200 | + if (!rowCount) |
| 201 | + { |
| 202 | + qInfo() << "Buffer is empty"; |
| 203 | + return; |
| 204 | + } |
| 205 | + |
| 206 | + bufSize = rowCount * ROW_DATA_SIZE; |
| 207 | + buf = std::unique_ptr< uint8_t[] >(new (std::nothrow) uint8_t[bufSize]); |
| 208 | + if (!buf.get()) |
| 209 | + { |
| 210 | + qCritical() << "Failed to allocate memory for write buffer"; |
| 211 | + return; |
| 212 | + } |
| 213 | + |
| 214 | + for (uint32_t i = HEADER_ROW_NUM; i <= rowCount; i++) |
| 215 | + { |
| 216 | + sl = ui->bufferTableWidget->item(i, HEADER_HEX_COL)-> |
| 217 | + text().split(QChar(' '), QString::SkipEmptyParts); |
192 | 218 |
|
193 |
| - for (i = 0; i < sizeof(buf); i++) |
194 |
| - buf[i] = i; |
| 219 | + for (int j = 0; j < sl.size(); j++) |
| 220 | + { |
| 221 | + buf[bufIter++] = sl.at(j).toUInt(&convIsOk, 16); |
| 222 | + if (!convIsOk) |
| 223 | + { |
| 224 | + qCritical() << "Failed to convert row item to byte"; |
| 225 | + return; |
| 226 | + } |
| 227 | + } |
| 228 | + } |
195 | 229 |
|
196 |
| - if (prog->writeChip(buf, 0x00000000, sizeof(buf))) |
| 230 | + if (prog->writeChip(buf.get(), 0x00000000, bufIter)) |
197 | 231 | log(tr("Failed to write chip\n"));
|
198 | 232 | else
|
199 | 233 | log(tr("Data has been successfully written\n"));
|
|
0 commit comments