资讯详情

【正点原子Linux连载】第二十二章 AP3216C 摘自【正点原子】I.MX6U嵌入式Qt开发指南V1.0.2

1)实验平台:正原子阿尔法Linux开发板 2)平台采购地址:https://item.taobao.com/item.htm?id=603672744434 3)全套实验源码 手册 视频下载地址:http://www.openedv.com/thread-300792-1-1.html 4)正点原子Linux感兴趣的学生可以加入小组讨论:935446741

第二十二章 AP3216C

本章是AP3216C实验,如何介绍Qt在应用程序中获得正点原子嵌入式I.MX6ULL Linux三合一环境传感器在开发板上的数据。请注意,由于数据是从开发板上获取的,因此需要使用开发板在正点原子上I.MX6ULL ALPHA开发板上有这个AP3216C传感器,MINI底板没有这个AP3216C传感器。然而,本章的实验程序也可以在没有传感器的情况下运行Window/Ubuntu/ARM Linux查看界面效果或直接查看第22.程序运行后行后的效果图。第十五章获取数据的原五章LED章节的原理是从开发板获取数据,区别在于第十五章使用QFile本例介绍了另一种直接访问文件的方法,即使用C语言open()方法访问数据。本章没有使用很多新知识,所以编辑花时间设计一个新界面,将界面分成小知识。让读者了解设计这样一个界面需要什么步骤。 更多AP3216C的信息请看【正点原子】I.MX6U用户体验快V1.x.pdf的第3.20小节。

22.1 资源简介

在正点原子I.MX6ULL ALPHA开发板底板上有一个三合一环境传感器,即拔码开关旁边的传感器I2C(注:I.MX6ULL MINI开发板没有这个传感器)。I.MX6ULL ALPHA开发板三合一环境传感器原理图。 在这里插入图片描述

开发板实物图位置。 

22.2 应用实例

在正点原子I.MX6U出厂系统已经编写好了AP3216C驱动,并注册为杂项设备,可在/sys/class/misc下找到ap3216c节点。我们直接用Qt访问节点文件获取AP3216C传感器数据。读取数据流程解释:数据从驱动层传输到Linux应用层,Qt传感器数据从应用层读取。 项目简介:Qt读取三合一环境传感器的数据。 例08_ii2c_ap3216c_sensor,读取三合一环境传感器的数据(难度:一般)。项目路径为Qt/3/08_ii2c_ap3216c_sensor。 项目文件08_ii2c_ap3216c_sensor.pro文件如下。

1   QT        = core gui 2  3   greaterThan(QT_MAJOR_VERSION, 4): QT  = widgets 4  5   CONFIG  = c 11 6  7   # The following define makes your compiler emit warnings if you use 8   # any Qt feature that has been marked deprecated (the exact warnings 9   # depend on your compiler). Please consult the documentation of the 10  # deprecated API in order to know how to port your code away from it. 11  DEFINES  = QT_DEPRECATED_WARNINGS 12 13  # You can also make your code fail to compile if it uses deprecated APIs. 14  # In order to do so, uncomment the following line. 15  # You can also select to disable deprecated APIs only up to a certain version of Qt. 16  #DEFINES  = QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0 17 18  SOURCES  = \ 19      ap3216c.cpp \ 20      arcgraph.cpp \ 21      glowtext.cpp \ 22      main.cpp \ 23      mainwindow.cpp 24 25  HEADERS  = \ 26      ap3216c.h \ 27 arcgraph.h \ 28 glowtext.h \ 29 mainwindow.h 30 31 # Default rules for deployment. 32 qnx: target.path = /tmp/$${ 
          TARGET}/bin 33 else: unix:!android: target.path = /opt/$${ 
          TARGET}/bin 34 !isEmpty(target.path): INSTALLS += target 35 36 include(headview/headview.pri) 
从上面的项目pro文件可以看出,本例使用的文件比较多。
第36行,使用到pri文件,pri文件的语法和pro文件相同,通常它是由pro文件改写得到的,该类型文件类似于 C++中的头文件,可以在pro文件中使用 include 将其包含进来,相当于文件引入,当一个项目文件非常多时,或有些项目文件需要重复使用,为了方便管理就可以使用此方法。
项目里文件很多,我们一个一个分析,最终我们只需关注“mainwindow.h”和“mainwindow.cpp”文件,程序的主要流程都在这两个文件里。分析完了这两个文件再到其他文件。
在源文件“mainwindow.h”的代码如下。
    /****************************************************************** Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved. * @projectName 08_spi_sensor * @brief mainwindow.h * @author Deng Zhimao * @email 1252699831@qq.com * @net www.openedv.com * @date 2021-05-21 *******************************************************************/
1   #ifndef MAINWINDOW_H
2   #define MAINWINDOW_H
3 
4   #include <QMainWindow>
5   #include <QLabel>
6   #include <QVBoxLayout>
7   #include <QHBoxLayout>
8   #include "arcgraph.h"
9   #include "glowtext.h"
10  #include "ap3216c.h"
11  #include "headview/headview.h"
12  class ArcGraph;
13  class GlowText;
14  class Ap3216c;
15  class HeadView;
16
17  class MainWindow : public QMainWindow
18  { 
        
19      Q_OBJECT
20
21  public:
22      MainWindow(QWidget *parent = nullptr);
23      ~MainWindow();
24
25  private:
26      ArcGraph *arcGraph[3];
27      GlowText *glowText[3];
28
29      QVBoxLayout *vBoxLayout;
30      QHBoxLayout *hBoxLayout[5];
31
32      GlowText *test;
33
34      /* 容器作用,用于布局 */
35      QWidget *widget[6];
36
37      /* 标签文本 */
38      QLabel *label[3];
39
40      /* i2C传感器类 */
41      Ap3216c *ap3216c;
42
43      /* 视图表头 */
44      HeadView *headView;
45
46  private slots:
47      /* 获取ap3216传感器数据 */
48      void getAp3216cData();
49  };
50  #endif // MAINWINDOW_H
在“mainwindow.h”的头文件里,我们看到使用了ArcGraph、GlowText、Ap3216c和HeadView自定义的类。它们是蓝色科技弧形视图、发光文本、Ap3216c类和视图表头。不同的类分开来写这样可以很方便地管理我们的项目。这些类在后面已经贴上代码和图加上一些解释方便给读者查阅。“mainwindow.h”头文件的解释就到这里了。
	“mainwindow.cpp”文件主要承担着布局及数据显示的功能。
    /****************************************************************** Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved. * @projectName 08_spi_sensor * @brief mainwindow.cpp * @author Deng Zhimao * @email 1252699831@qq.com * @net www.openedv.com * @date 2021-05-21 *******************************************************************/
1   #include "mainwindow.h"
2   #include <QDebug>
3   MainWindow::MainWindow(QWidget *parent)
4       : QMainWindow(parent)
5   { 
        
6       this->resize(800, 480);
7       this->setStyleSheet("background:#011753");
8  
9       for (int i = 0; i < 6; i++)
10          widget[i] = new QWidget();
11 
12      for (int i = 0; i < 3; i++)
13          arcGraph[i] = new ArcGraph();
14 
15      for (int i = 0; i < 5; i++)
16          hBoxLayout[i] = new QHBoxLayout();
17 
18      headView = new HeadView();
19 
20      QFont font;
21      font.setPixelSize(18);
22 
23      QPalette pal;
24      pal.setColor(QPalette::WindowText, Qt::white);
25 
26      QStringList list;
27      list<<"环境光强度:"<<"接近距离:"<<"红外强度:";
28      for (int i = 0; i < 3; i++) { 
        
29          label[i] = new QLabel();
30          glowText[i] = new GlowText();
31          glowText[i]->setMinimumWidth(30);
32          label[i]->setText(list[i]);
33          label[i]->setFont(font);
34          label[i]->setPalette(pal);
35          label[i]->adjustSize();
36      }
37 
38      vBoxLayout = new QVBoxLayout();
39 
40      /* 垂直布局,将主窗体为上下两部分,方便布局 */
41      vBoxLayout->addWidget(headView);
42      vBoxLayout->addWidget(widget[1]);
43      vBoxLayout->addWidget(widget[2]);
44      widget[0]->setLayout(vBoxLayout);
45 
46      /* 主布局设置为widget[0] */
47      setCentralWidget(widget[0]);
48 
49      /* 设置widget[1]的高度,不会随界面的大小而变化 */
50      widget[2]->setFixedHeight(150);
51 
52      /* 三个蓝色科技感弧形图布局,采用水平布局 */
53      hBoxLayout[0]->addWidget(arcGraph[0]);
54      hBoxLayout[0]->addWidget(arcGraph[1]);
55      hBoxLayout[0]->addWidget(arcGraph[2]);
56      widget[1]->setLayout(hBoxLayout[0]);
57 
58      /* 数据文字容器水平布局, */
59      hBoxLayout[1]->addWidget(widget[3]);
60      hBoxLayout[1]->addWidget(widget[4]);
61      hBoxLayout[1]->addWidget(widget[5]);
62      hBoxLayout[1]->setContentsMargins(0, 40, 0, 0);
63 
64      widget[2]->setLayout(hBoxLayout[1]);
65 
66      /* als布局 */
67      hBoxLayout[2]->addWidget(label[0]);
68      hBoxLayout[2]->addWidget(glowText[0]);
69      hBoxLayout[2]->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
70      widget[3]->setLayout(hBoxLayout[2]);
71 
72      /* ps布局 */
73      hBoxLayout[3]->addWidget(label[1]);
74      hBoxLayout[3]->addWidget(glowText[1]);
75      hBoxLayout[3]->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
76      widget[4]->setLayout(hBoxLayout[3]);
77 
78      /* ir布局 */
79      hBoxLayout[4]->addWidget(label[2]);
80      hBoxLayout[4]->addWidget(glowText[2]);
81      hBoxLayout[4]->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
82      widget[5]->setLayout(hBoxLayout[4]);
83 
84      ap3216c = new Ap3216c(this);
85      /* 只能在开发板上开启获取数据,Ubuntu上是没有ap3216c传感器的 */
86  #if __arm__
87      ap3216c->setCapture(true);
88  #endif
89 
90      connect(ap3216c, SIGNAL(ap3216cDataChanged()),
91              this, SLOT(getAp3216cData()));
92  }
93 
94  MainWindow::~MainWindow()
95  { 
        
96  }
97 
98  void MainWindow::getAp3216cData()
99  { 
        
100     static QString als = ap3216c->alsData();
101     if (als != ap3216c->alsData()) { 
        
102         als = ap3216c->alsData();
103         arcGraph[0]->setangleLength(als.toUInt() * 360 / 65535);
104     }
105
106     static QString ps = ap3216c->psData();
107     if (ps != ap3216c->psData()) { 
        
108         ps = ap3216c->psData();
109         arcGraph[1]->setangleLength(ps.toUInt() * 360 / 1023);
110     }
111
112     static QString ir = ap3216c->irData();
113     if (ir != ap3216c->irData()) { 
        
114         ir = ap3216c->irData();
115         arcGraph[2]->setangleLength(ir.toUInt() * 360 / 1023);
116     }
117
118     glowText[0]->setTextData(als);
119     glowText[1]->setTextData(ps);
120     glowText[2]->setTextData(ir);
121 }
第98行之前都是一些布局及变量声明使用的内容。
第98~121行,若收到Ap3216c类发送过来的信号,则显示数据。显示在发数据在ArcGraph和GlowText类上。其中ArcGraph是一个弧形视图,通过setangleLength()方法,设置传入弧的角度大小,就可以画出一段弧,用图形的方式显示给用户看,比数字更直观。其中65535和1023数值的由来是环境光传感器具有16位的分辨率,接近传感器和红外传感器具有10位分辨率。也就是2的16次方减一,与2的10次方减一。
主要的流程基本介绍完,我们开始分步介绍ArcGraph、GlowText、Ap3216c和HeadView类。
HeadView类主要功能是显示如下的一个表头。可以很方便地移植到其他项目里用。下图的背景颜色可忽略。实际这个类的背景颜色是透明的。

“headview.h”头文件内容如下。文件比较简单,不再解释。

    /****************************************************************** Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved. * @projectName headview * @brief headview.h * @author Deng Zhimao * @email 1252699831@qq.com * @net www.openedv.com * @date 2021-05-22 *******************************************************************/
1   #ifndef HEADVIEW_H
2   #define HEADVIEW_H
3 
4   #include <QWidget>
5   #include <QLabel>
6   #include <QVBoxLayout>
7   #include <QHBoxLayout>
8 
9 
10  class HeadView : public QWidget
11  { 
        
12      Q_OBJECT
13
14  public:
15      HeadView(QWidget *parent = nullptr);
16      ~HeadView();
17
18  private:
19      QWidget *widget;
20      QLabel *textLabel;
21      QWidget *iconWidget;
22      QWidget *lineWidget;
23
24      QHBoxLayout *hBoxLayout;
25      QVBoxLayout *vBoxLayout;
26  };
27  #endif // HEADVIEW_H

“headview.cpp”源文件内容如下。

    /****************************************************************** Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved. * @projectName headview * @brief headview.cpp * @author Deng Zhimao * @email 1252699831@qq.com * @net www.openedv.com * @date 2021-05-22 *******************************************************************/
1   #include "headview.h"
2 
3   HeadView::HeadView(QWidget *parent)
4       : QWidget(parent)
5   { 
        
6       this->setAttribute(Qt::WA_TranslucentBackground, true);
7       widget = new QWidget();
8 
9       iconWidget = new QWidget(this);
10      iconWidget->setFixedSize(48, 48);
11      iconWidget->setStyleSheet("background:url(:/images/dataviewicon.png)");
12
13      textLabel = new QLabel(this);
14      textLabel->setFixedSize(200, 48);
15      textLabel->setText("数据可视化情况");
16      textLabel->setStyleSheet("QLabel {font-size: 20px; color: white}");
17
18      lineWidget = new QWidget(this);
19      lineWidget->setFixedHeight(2);
20      lineWidget->setStyleSheet("QWidget {background: #eeeeeeee}");
21
22      vBoxLayout = new QVBoxLayout();
23      vBoxLayout->addWidget(widget);
24      vBoxLayout->addWidget(lineWidget);
25      vBoxLayout->setContentsMargins(0, 0, 0, 0);
26
27      hBoxLayout = new QHBoxLayout();
28      hBoxLayout->addWidget(iconWidget);
29      hBoxLayout->addWidget(textLabel);
30      hBoxLayout->setContentsMargins(0, 0, 0, 0);
31      hBoxLayout->setAlignment(Qt::AlignLeft);
32      widget->setLayout(hBoxLayout);
33
34      this->setLayout(vBoxLayout);
35      this->adjustSize();
36      this->setMaximumHeight(48);
37  }
38
39  HeadView::~HeadView()
40  { 
        
41  }
ArcGraph类是一个蓝色科技感弧形视图,这里运用了QPainter为画图,在第九章画图章节我们已经了解过QPainter的内容了。不详细解释。ArcGraph类实现的效果如下。(注背景实际上是透明的)。
“arcgraph.h”头文件内容如下。
    /****************************************************************** Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved. * @projectName 08_spi_sensor * @brief arcgraph.h * @author Deng Zhimao * @email 1252699831@qq.com * @net www.openedv.com * @date 2021-05-21 *******************************************************************/
1   #ifndef ARCGRAPH_H
2   #define ARCGRAPH_H
3 
4   #include <QWidget>
5   #include <QPainter>
6   #include <QPaintEvent>
7 
8   /* 蓝色科技感弧形视图 */
9   class ArcGraph : public QWidget
10  { 
        
11      Q_OBJECT
12
13  public:
14      ArcGraph(QWidget *parent = nullptr); 

标签: exact传感器红外传感器e3f3n传感器e3x

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

锐单商城 - 一站式电子元器件采购平台