-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwidget_meter_curr.cpp
159 lines (140 loc) · 4.59 KB
/
widget_meter_curr.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "widget_meter_curr.h"
#include "ui_widget_meter_curr.h"
#include <QTimer>
#include "logger.h"
widget_meter_curr::widget_meter_curr(amper_dev *ad, QProgressBar *pb, QWidget *parent)
: QWidget(parent)
, ui(new Ui::widget_meter_curr)
, dev(ad)
, bar(pb)
, dti_prop(0)
{
ui->setupUi(this);
timer_start = new QTimer(this);
timer_start->setSingleShot(true);
connect(timer_start, SIGNAL(timeout()), this, SLOT(slot_timer_start()));
model = new model_meter_curr(this);
ui->tableView->setModel(model);
connect(this, SIGNAL(signal_read_data(int)), this, SLOT(slot_read_data(int)), Qt::QueuedConnection);
}
widget_meter_curr::~widget_meter_curr()
{
delete ui;
}
void widget_meter_curr::slot_start_after_ms(int start_delay_ms)
{
timer_start->start(start_delay_ms);
}
void widget_meter_curr::slot_timer_start()
{
if (!dti_prop) {
dti_prop = new DTI_Property;
}
if (dev->getPropertyByName("pM_CURR", (DTI_Property *)dti_prop)) {
emit signal_read_data(0);
return;
}
log_1 << "no DTI_Property.name pM_CURR";
emit signal_read_end(false);
}
void widget_meter_curr::slot_read_data(int shift)
{
if (!dti_prop) return;
DTI_Property * prop = (DTI_Property *)dti_prop;
int communication_len = 100;
uchar buff[communication_len];
if (dev->readData(dev->propertySeg(prop), prop->address + shift, communication_len, buff)) {
if (shift + communication_len < (int)sizeof(curr)) {
bar->setValue(shift * 100 / sizeof(curr));
memmove(((uchar*)&curr) + shift, buff, communication_len);
emit signal_read_data(shift + communication_len);
} else {
bar->setValue(100);
memmove(((uchar*)&curr) + shift, buff, sizeof(curr) - shift);
emit signal_read_end(true);
model->set_data((METER_NEW_DATA *)&curr);
ui->tableView->horizontalHeader()->resizeSections(QHeaderView::ResizeToContents);
}
} else {
emit signal_read_data(shift);
}
}
model_meter_curr::model_meter_curr(QObject *parent)
: QAbstractTableModel(parent)
{
memset(&curr, 0, sizeof(curr));
}
int model_meter_curr::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 8;
}
int model_meter_curr::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return PHASE_MAX;
}
QVariant model_meter_curr::data(const QModelIndex &index, int role) const
{
switch (role) {
case Qt::DisplayRole: {
int phase = index.column();
if (phase >= PHASE_MAX) break;
switch (index.row()) {
case 0: return itgr_show(curr.to_display[phase].Pi / 1000.0);
case 1: return itgr_show(curr.to_display[phase].Qi / 1000.0);
case 2: return itgr_show(curr.to_display[phase].Si / 1000.0);
case 3: return itgr_show(curr.phase[phase].U);
case 4: return itgr_show(curr.phase[phase].Ulin);
case 5: return itgr_show(curr.phase[phase].I);
case 6: return itgr_show(curr.phase[phase].F);
case 7: return itgr_show(curr.phase[phase].cosFi);
default: break;
}
break;
}
case Qt::TextAlignmentRole: return Qt::AlignRight + Qt::AlignVCenter;
default: break;
}
return QVariant();
}
QVariant model_meter_curr::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role != Qt::DisplayRole) return QAbstractTableModel::headerData(section, orientation, role);
if (orientation != Qt::Horizontal) {
switch (section) {
case 0: return tr("Мощность Активная");
case 1: return tr("Мощность Реактивная");
case 2: return tr("Мощность Полная");
case 3: return tr("Напряжение RMS");
case 4: return tr("Напряжение линейное RMS");
case 5: return tr("Ток RMS");
case 6: return tr("Частота сети");
case 7: return tr("cosFi");
default: break;
}
} else {
switch (section) {
case 0: return tr("Σ фаз");
case 1: return tr("Фаза 1");
case 2: return tr("Фаза 2");
case 3: return tr("Фаза 3");
default: break;
}
}
return QVariant();
}
void model_meter_curr::set_data(METER_NEW_DATA *curr)
{
layoutAboutToBeChanged();
if (!curr) {
memset(&this->curr, 0, sizeof(this->curr));
} else {
memmove(&this->curr, curr, sizeof(this->curr));
}
layoutChanged();
}
QVariant model_meter_curr::itgr_show(const float val) const
{
return QString::number(val, 'f', 3);
}