-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOscilloscopeWidget.cpp
More file actions
326 lines (274 loc) · 9.75 KB
/
OscilloscopeWidget.cpp
File metadata and controls
326 lines (274 loc) · 9.75 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "OscilloscopeWidget.h"
#include <QPainter>
#include <QPainterPath>
#include <QtMath>
OscilloscopeWidget::OscilloscopeWidget(QWidget *parent)
: QWidget(parent),
m_labelFont("Consolas", 8),
m_valueFont("Consolas", 9)
{
setMinimumHeight(150);
m_refreshTimer = new QTimer(this);
m_refreshTimer->setInterval(33); // ~30fps
connect(m_refreshTimer, &QTimer::timeout, this, &OscilloscopeWidget::onRefreshTimer);
m_refreshTimer->start();
}
void OscilloscopeWidget::appendSamples(const QVector<uint16_t> &samples) {
m_data.append(samples);
// 方案 A:增大线性缓冲区,保留足够历史数据供触发搜索
if (m_data.size() > MAX_BUFFER_POINTS) {
int removeCount = m_data.size() - MAX_BUFFER_POINTS;
m_data.erase(m_data.begin(), m_data.begin() + removeCount);
}
// 触发检测(仅 Non-Auto 模式且尚未触发时)
if (m_triggerMode != Auto && !m_triggered) {
int tp = findTriggerPoint();
if (tp >= 0) {
m_displayStartIdx = tp;
m_triggered = true;
}
}
m_dataDirty = true;
}
void OscilloscopeWidget::onRefreshTimer() {
if (m_dataDirty) {
m_dataDirty = false;
m_offscreenDirty = true;
update();
}
}
void OscilloscopeWidget::clearData() {
m_data.clear();
m_triggered = false;
m_displayStartIdx = -1;
if (m_triggerMode == Single)
m_armed = false;
m_offscreenDirty = true;
m_dataDirty = false;
update();
}
void OscilloscopeWidget::setMaxDisplayPoints(int count) {
m_maxDisplayPoints = qMax(50, count);
m_offscreenDirty = true;
}
void OscilloscopeWidget::setVoltageRange(double minV, double maxV) {
m_minVoltage = minV;
m_maxVoltage = maxV;
m_bgDirty = true;
m_offscreenDirty = true;
}
void OscilloscopeWidget::setTriggerMode(TriggerMode mode) {
m_triggerMode = mode;
m_triggered = false;
m_displayStartIdx = -1;
if (mode == Single)
m_armed = true;
m_offscreenDirty = true;
}
void OscilloscopeWidget::setTriggerEdge(TriggerEdge edge) {
m_triggerEdge = edge;
m_triggered = false;
m_displayStartIdx = -1;
m_offscreenDirty = true;
}
void OscilloscopeWidget::setTriggerLevel(double voltage) {
m_triggerLevel = qBound(m_minVoltage, voltage, m_maxVoltage);
m_triggered = false;
m_displayStartIdx = -1;
m_offscreenDirty = true;
}
void OscilloscopeWidget::armTrigger() {
m_armed = true;
m_triggered = false;
m_displayStartIdx = -1;
m_offscreenDirty = true;
}
uint16_t OscilloscopeWidget::voltageToAdc(double voltage) const {
double ratio = (voltage - m_minVoltage) / (m_maxVoltage - m_minVoltage);
ratio = qBound(0.0, ratio, 1.0);
return static_cast<uint16_t>(ratio * 65535.0);
}
int OscilloscopeWidget::findTriggerPoint() const {
if (m_data.size() < 2)
return -1;
uint16_t level = voltageToAdc(m_triggerLevel);
// 从最新数据向前搜索,找到最近的触发点
int searchStart = qMax(0, m_data.size() - MAX_BUFFER_POINTS);
for (int i = m_data.size() - 1; i > searchStart; i--) {
bool match = false;
switch (m_triggerEdge) {
case Rising:
match = (m_data[i - 1] < level && m_data[i] >= level);
break;
case Falling:
match = (m_data[i - 1] >= level && m_data[i] < level);
break;
case Both:
match = ((m_data[i - 1] < level && m_data[i] >= level) ||
(m_data[i - 1] >= level && m_data[i] < level));
break;
}
if (match)
return i;
}
return -1;
}
void OscilloscopeWidget::resizeEvent(QResizeEvent *event) {
QWidget::resizeEvent(event);
m_bgDirty = true;
m_offscreenDirty = true;
}
void OscilloscopeWidget::rebuildBackground() {
int w = width() - LEFT_MARGIN - RIGHT_MARGIN;
int h = height() - TOP_MARGIN - BOTTOM_MARGIN;
if (w <= 0 || h <= 0) return;
m_background = QPixmap(size() * devicePixelRatioF());
m_background.setDevicePixelRatio(devicePixelRatioF());
m_background.fill(Qt::transparent);
QPainter p(&m_background);
p.setRenderHint(QPainter::Antialiasing, false);
QRectF plotArea(LEFT_MARGIN, TOP_MARGIN, w, h);
// 背景
p.fillRect(rect(), QColor("#11111b"));
p.fillRect(plotArea, QColor("#0d0d18"));
// 网格线
QPen gridPen(QColor("#1e1e2e"), 1);
QPen gridPenMajor(QColor("#2a2a3c"), 1);
// 水平网格 (电压)
int vDivs = 8;
for (int i = 0; i <= vDivs; i++) {
double y = TOP_MARGIN + (h * i) / vDivs;
p.setPen(i == 0 || i == vDivs ? gridPenMajor : gridPen);
p.drawLine(QPointF(LEFT_MARGIN, y), QPointF(LEFT_MARGIN + w, y));
}
// 垂直网格
int hDivs = 10;
for (int i = 0; i <= hDivs; i++) {
double x = LEFT_MARGIN + (w * i) / hDivs;
p.setPen(i == 0 || i == hDivs ? gridPenMajor : gridPen);
p.drawLine(QPointF(x, TOP_MARGIN), QPointF(x, TOP_MARGIN + h));
}
// Y轴标签
p.setPen(QColor("#6c7086"));
p.setFont(m_labelFont);
for (int i = 0; i <= vDivs; i++) {
double voltage = m_maxVoltage - (m_maxVoltage - m_minVoltage) * i / vDivs;
double y = TOP_MARGIN + (h * i) / vDivs;
p.drawText(QRectF(0, y - 10, LEFT_MARGIN - 4, 20),
Qt::AlignRight | Qt::AlignVCenter,
QString("%1V").arg(voltage, 4, 'f', 2));
}
// X轴标签(固定文本部分)
p.drawText(QRectF(LEFT_MARGIN, TOP_MARGIN + h + 2, w, BOTTOM_MARGIN - 2),
Qt::AlignLeft | Qt::AlignTop, QString("Sample 0"));
// 边框
p.setPen(QPen(QColor("#313244"), 1));
p.setBrush(Qt::NoBrush);
p.drawRect(plotArea);
m_bgDirty = false;
}
void OscilloscopeWidget::rebuildOffscreen() {
int w = width() - LEFT_MARGIN - RIGHT_MARGIN;
int h = height() - TOP_MARGIN - BOTTOM_MARGIN;
if (w <= 0 || h <= 0) return;
// 按需重建背景层
if (m_bgDirty) {
rebuildBackground();
}
m_offscreen = QPixmap(size() * devicePixelRatioF());
m_offscreen.setDevicePixelRatio(devicePixelRatioF());
// 先铺背景
{
QPainter p(&m_offscreen);
p.drawPixmap(0, 0, m_background);
}
QPainter p(&m_offscreen);
p.setRenderHint(QPainter::Antialiasing, false);
const double adcMax = 65535.0;
// 触发电平参考线
{
double trigRatio = (m_triggerLevel - m_minVoltage) / (m_maxVoltage - m_minVoltage);
trigRatio = qBound(0.0, trigRatio, 1.0);
double trigY = TOP_MARGIN + h * (1.0 - trigRatio);
QPen trigPen(QColor(TRIGGER_RGB), 1, Qt::DashLine);
p.setPen(trigPen);
p.drawLine(QPointF(LEFT_MARGIN, trigY), QPointF(LEFT_MARGIN + w, trigY));
// 触发电平标签
p.setFont(m_labelFont);
p.setPen(QColor(TRIGGER_RGB));
p.drawText(QRectF(0, trigY - 10, LEFT_MARGIN - 4, 20),
Qt::AlignRight | Qt::AlignVCenter,
QString("T:%1V").arg(m_triggerLevel, 4, 'f', 2));
}
// 触发状态指示
if (m_triggerMode != Auto) {
p.setFont(m_valueFont);
if (m_triggered) {
p.setPen(QColor("#a6e3a1")); // 绿色 = 已触发
p.drawText(LEFT_MARGIN + w - 80, TOP_MARGIN + 17, "TRIG'D");
} else {
p.setPen(QColor("#f9e2af")); // 黄色 = 等待触发
p.drawText(LEFT_MARGIN + w - 80, TOP_MARGIN + 17, "WAIT");
}
}
// 计算显示起点
int startIdx;
if (m_triggerMode == Auto || !m_triggered) {
// Auto 模式或未触发:自由滚动
startIdx = qMax(0, m_data.size() - m_maxDisplayPoints);
} else {
// Normal/Single 且已触发:从触发点开始
startIdx = m_displayStartIdx;
}
int displayCount = qMin(m_maxDisplayPoints, m_data.size() - startIdx);
// X轴动态标签
if (!m_data.isEmpty()) {
p.setPen(QColor("#6c7086"));
p.setFont(m_labelFont);
p.drawText(QRectF(LEFT_MARGIN, TOP_MARGIN + h + 2, w, BOTTOM_MARGIN - 2),
Qt::AlignRight | Qt::AlignTop,
QString("Sample %1").arg(startIdx + displayCount - 1));
}
// 绘制波形
if (displayCount > 0) {
// 波形路径
QPainterPath wavePath;
for (int i = 0; i < displayCount; i++) {
double x = LEFT_MARGIN + (w * i) / qMax(1, displayCount - 1);
double voltage = (m_data[startIdx + i] / adcMax)
* (m_maxVoltage - m_minVoltage) + m_minVoltage;
voltage = qBound(m_minVoltage, voltage, m_maxVoltage);
double ratio = (voltage - m_minVoltage) / (m_maxVoltage - m_minVoltage);
double y = TOP_MARGIN + h * (1.0 - ratio);
if (i == 0) wavePath.moveTo(x, y);
else wavePath.lineTo(x, y);
}
// 填充区域
QPainterPath fill = wavePath;
fill.lineTo(LEFT_MARGIN + w, TOP_MARGIN + h);
fill.lineTo(LEFT_MARGIN, TOP_MARGIN + h);
fill.closeSubpath();
QColor waveColor(WAVE_RGB);
QColor fillColor = waveColor;
fillColor.setAlpha(25);
p.fillPath(fill, fillColor);
// 画线
p.setPen(QPen(waveColor, 1.5));
p.drawPath(wavePath);
// 当前值显示
p.setFont(m_valueFont);
uint16_t lastVal = m_data[startIdx + displayCount - 1];
double lastVoltage = (lastVal / adcMax) * (m_maxVoltage - m_minVoltage) + m_minVoltage;
p.setPen(QColor(WAVE_RGB));
p.drawText(LEFT_MARGIN + 5, TOP_MARGIN + 17,
QString("ADC: %1V").arg(lastVoltage, 5, 'f', 3));
}
m_offscreenDirty = false;
}
void OscilloscopeWidget::paintEvent(QPaintEvent *) {
if (m_offscreenDirty) {
rebuildOffscreen();
}
QPainter p(this);
p.drawPixmap(0, 0, m_offscreen);
}