1 #include "widget.h"
2 #include "ui_widget.h"
3 #include <QTimer>
4 #include <QDebug>
5 #include "adcinterface.h"
6 #include <QPaintEvent>
7 #include <QPainter>
8
Widget(QWidget * parent)9 Widget::Widget(QWidget *parent)
10 : IWidget(parent)
11 , ui(new Ui::Widget)
12 {
13 ui->setupUi(this);
14
15 m_adcs << new ADCInterface("/dev/input/event2")
16 << new ADCInterface("/dev/input/event3")
17 << new ADCInterface("/dev/input/event4")
18 << new ADCInterface("/dev/input/event5");
19
20 for(int i=0; i<m_adcs.size(); i++)
21 {
22 ADCInterface *adc =m_adcs[i];
23 connect(adc, &ADCInterface::readRead, this, [=, adc, i](int v){
24 m_values[i] =v;
25 });
26 adc->open();
27 }
28
29
30 QList<QLabel*> showValueLbls ;
31 showValueLbls <<ui->adc0Lbl
32 <<ui->adc1Lbl
33 << ui->adc2Lbl
34 << ui->adc3Lbl;
35 QList<QLabel*> showVoltagelbls ;
36 showVoltagelbls <<ui->adc0Lblv
37 <<ui->adc1Lblv
38 << ui->adc2Lblv
39 << ui->adc3Lblv;
40 m_values<<0 <<0 <<0<<0;
41 m_timer = new QTimer(this);
42 connect(m_timer, &QTimer::timeout, this, [=, showValueLbls, showVoltagelbls](){
43 for(int i=0; i<m_values.size(); i++)
44 {
45 int v = m_values[i];
46 QLabel *pValueLbl = showValueLbls[i];
47 QLabel *pVoltageLbl = showVoltagelbls[i];
48 pValueLbl->setText(QString::number(v));
49 pVoltageLbl->setText(QString::number(v*1.8/4096,'g', 3));
50 }
51 });
52 m_timer->start(100);
53
54 }
55
~Widget()56 Widget::~Widget()
57 {
58 m_timer->stop();
59 foreach (ADCInterface *adc, m_adcs) {
60 adc->close();
61 delete adc;
62 }
63 m_adcs.clear();
64 delete ui;
65 }
66
id()67 QString Widget::id()
68 {
69 return "adc";
70 }
71
paintEvent(QPaintEvent * e)72 void Widget::paintEvent(QPaintEvent *e)
73 {
74 QPainter painter(this);
75 QRect rect0(-1,ui->adc0Lbl->y(), this->width()+1, ui->adc0Lbl->height());
76 QRect rect1(-1,ui->adc1Lbl->y(), this->width()+1, ui->adc1Lbl->height());
77 QRect rect2(-1,ui->adc2Lbl->y(), this->width()+1, ui->adc2Lbl->height());
78 QRect rect3(-1,ui->adc3Lbl->y(), this->width()+1, ui->adc3Lbl->height());
79
80 QColor color(222, 222,222);
81 painter.fillRect(rect0, color);
82 painter.fillRect(rect1, Qt::white);
83 painter.fillRect(rect2, color);
84 painter.fillRect(rect3, Qt::white);
85
86 QWidget::paintEvent(e);
87 }
88