Skip to content

Commit c386eed

Browse files
committed
Refactored chip DB set/get parameter methods
1 parent 5be4f45 commit c386eed

File tree

3 files changed

+64
-13
lines changed

3 files changed

+64
-13
lines changed

qt/chip_db.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,50 @@ void ChipDb::reset()
350350
chipInfoVector.clear();
351351
readFromCvs();
352352
}
353+
354+
ChipInfo *ChipDb::getChipInfo(int chipIndex)
355+
{
356+
return chipIndex >= 0 && chipIndex < chipInfoVector.size() ?
357+
&chipInfoVector[chipIndex] : nullptr;
358+
}
359+
360+
QString ChipDb::getChipName(int chipIndex)
361+
{
362+
ChipInfo *ci = getChipInfo(chipIndex);
363+
364+
return ci ? ci->name : QString();
365+
}
366+
367+
int ChipDb::setChipName(int chipIndex, const QString &name)
368+
{
369+
ChipInfo *ci = getChipInfo(chipIndex);
370+
371+
if (!ci)
372+
return -1;
373+
374+
ci->name = name;
375+
376+
return 0;
377+
}
378+
379+
uint32_t ChipDb::getChipParam(int chipIndex, int paramIndex)
380+
{
381+
ChipInfo *ci = getChipInfo(chipIndex);
382+
383+
if (!ci || paramIndex < 0 || paramIndex > CHIP_PARAM_NUM)
384+
return 0;
385+
386+
return ci->params[paramIndex];
387+
}
388+
389+
int ChipDb::setChipParam(int chipIndex, int paramIndex, uint32_t paramValue)
390+
{
391+
ChipInfo *ci = getChipInfo(chipIndex);
392+
393+
if (!ci || paramIndex < 0 || paramIndex > CHIP_PARAM_NUM)
394+
return -1;
395+
396+
ci->params[paramIndex] = paramValue;
397+
398+
return 0;
399+
}

qt/chip_db.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class ChipDb : public QObject
7474
void readFromCvs();
7575
int readCommentsFromCsv(QFile &dbFile, QString &comments);
7676
void writeToCvs();
77+
ChipInfo *getChipInfo(int chipIndex);
7778

7879
public:
7980
explicit ChipDb(QObject *parent = nullptr);
@@ -98,7 +99,10 @@ class ChipDb : public QObject
9899
int getHexStringFromOptParam(const uint32_t &param, QString &value);
99100
bool isParamValid(uint32_t param, uint32_t min, uint32_t max);
100101
bool isOptParamValid(uint32_t param, uint32_t min, uint32_t max);
101-
ChipInfo *operator[](int index) { return &chipInfoVector[index]; }
102+
QString getChipName(int chipIndex);
103+
int setChipName(int chipIndex, const QString &name);
104+
uint32_t getChipParam(int chipIndex, int paramIndex);
105+
int setChipParam(int chipIndex, int paramIndex, uint32_t paramValue);
102106
};
103107

104108

qt/chip_db_table_model.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ QVariant ChipDbTableModel::data(const QModelIndex &index, int role) const
3232
switch (column)
3333
{
3434
case CHIP_PARAM_NAME:
35-
return (*chipDb)[index.row()]->name;
35+
return chipDb->getChipName(index.row());
3636
case CHIP_PARAM_PAGE_SIZE:
3737
case CHIP_PARAM_BLOCK_SIZE:
3838
case CHIP_PARAM_TOTAL_SIZE:
3939
case CHIP_PARAM_SPARE_SIZE:
40-
chipDb->getHexStringFromParam((*chipDb)[index.row()]->params[column],
40+
chipDb->getHexStringFromParam(chipDb->getChipParam(index.row(), column),
4141
paramStr);
4242
return paramStr;
4343
case CHIP_PARAM_T_CS:
@@ -57,21 +57,21 @@ QVariant ChipDbTableModel::data(const QModelIndex &index, int role) const
5757
case CHIP_PARAM_ROW_CYCLES:
5858
case CHIP_PARAM_COL_CYCLES:
5959
case CHIP_PARAM_BB_MARK_OFF:
60-
return (*chipDb)[index.row()]->params[column];
60+
return chipDb->getChipParam(index.row(), column);
6161
case CHIP_PARAM_READ1_CMD:
6262
case CHIP_PARAM_READ_ID_CMD:
6363
case CHIP_PARAM_RESET_CMD:
6464
case CHIP_PARAM_WRITE1_CMD:
6565
case CHIP_PARAM_ERASE1_CMD:
6666
case CHIP_PARAM_STATUS_CMD:
67-
chipDb->getHexStringFromParam((*chipDb)[index.row()]->params[column],
67+
chipDb->getHexStringFromParam(chipDb->getChipParam(index.row(), column),
6868
paramStr);
6969
return paramStr;
7070
case CHIP_PARAM_READ2_CMD:
7171
case CHIP_PARAM_WRITE2_CMD:
7272
case CHIP_PARAM_ERASE2_CMD:
73-
chipDb->getHexStringFromOptParam((*chipDb)[index.row()]->params[column],
74-
paramStr);
73+
chipDb->getHexStringFromOptParam(chipDb->getChipParam(index.row(),
74+
column), paramStr);
7575
return paramStr;
7676
}
7777

@@ -209,15 +209,15 @@ bool ChipDbTableModel::setData(const QModelIndex &index, const QVariant &value,
209209
switch (index.column())
210210
{
211211
case CHIP_PARAM_NAME:
212-
(*chipDb)[index.row()]->name = value.toString();
212+
chipDb->setChipName(index.row(), value.toString());
213213
return true;
214214
case CHIP_PARAM_PAGE_SIZE:
215215
case CHIP_PARAM_BLOCK_SIZE:
216216
case CHIP_PARAM_TOTAL_SIZE:
217217
case CHIP_PARAM_SPARE_SIZE:
218218
if (chipDb->getParamFromHexString(value.toString(), paramVal))
219219
return false;
220-
(*chipDb)[index.row()]->params[index.column()] = paramVal;
220+
chipDb->setChipParam(index.row(), index.column(), paramVal);
221221
return true;
222222
case CHIP_PARAM_T_CS:
223223
case CHIP_PARAM_T_CLS:
@@ -236,7 +236,7 @@ bool ChipDbTableModel::setData(const QModelIndex &index, const QVariant &value,
236236
case CHIP_PARAM_BB_MARK_OFF:
237237
if (chipDb->getParamFromString(value.toString(), paramVal))
238238
return false;
239-
(*chipDb)[index.row()]->params[index.column()] = paramVal;
239+
chipDb->setChipParam(index.row(), index.column(), paramVal);
240240
return true;
241241
case CHIP_PARAM_ROW_CYCLES:
242242
case CHIP_PARAM_COL_CYCLES:
@@ -247,7 +247,7 @@ bool ChipDbTableModel::setData(const QModelIndex &index, const QVariant &value,
247247
{
248248
return false;
249249
}
250-
(*chipDb)[index.row()]->params[index.column()] = paramVal;
250+
chipDb->setChipParam(index.row(), index.column(), paramVal);
251251
return true;
252252
case CHIP_PARAM_READ1_CMD:
253253
case CHIP_PARAM_READ_ID_CMD:
@@ -259,7 +259,7 @@ bool ChipDbTableModel::setData(const QModelIndex &index, const QVariant &value,
259259
return false;
260260
if (!chipDb->isParamValid(paramVal, 0x00, 0xFF))
261261
return false;
262-
(*chipDb)[index.row()]->params[index.column()] = paramVal;
262+
chipDb->setChipParam(index.row(), index.column(), paramVal);
263263
return true;
264264
case CHIP_PARAM_READ2_CMD:
265265
case CHIP_PARAM_WRITE2_CMD:
@@ -268,7 +268,7 @@ bool ChipDbTableModel::setData(const QModelIndex &index, const QVariant &value,
268268
return false;
269269
if (!chipDb->isOptParamValid(paramVal, 0x00, 0xFF))
270270
return false;
271-
(*chipDb)[index.row()]->params[index.column()] = paramVal;
271+
chipDb->setChipParam(index.row(), index.column(), paramVal);
272272
return true;
273273
}
274274

0 commit comments

Comments
 (0)