搜索
bottom↓
回复: 0

《I.MX6U嵌入式Qt开发指南》第二十二章 AP3216C

[复制链接]

出0入234汤圆

发表于 2021-7-20 12:03:10 | 显示全部楼层 |阅读模式
本帖最后由 正点原子 于 2021-8-11 12:24 编辑

1)实验平台:正点原子i.MX6ULL Linux阿尔法开发板
2)  章节摘自【正点原子】《I.MX6U嵌入式Qt开发指南》
3)购买链接:https://item.taobao.com/item.htm?&id=603672744434
4)全套实验源码+手册+视频下载地址:http://www.openedv.com/docs/boards/arm-linux/zdyz-i.mx6ull.html
5)正点原子官方B站:https://space.bilibili.com/394620890
6)
正点原子Linux技术交流群:1027879335 1.png

2.jpg


3.png



第二十二章 AP3216C

       本章是AP3216C实验,介绍如何在Qt应用上获取我们正点原子嵌入式I.MX6ULL Linux开发板上的个三合一环境传感器的数据。注意,既然是从开发板上获取数据,那么需要使用开发板,在正点原子I.MX6ULL ALPHA开发板上有这个AP3216C传感器,MINI底板没有这个AP3216C传感器。不过程序是没有传感器的情况下也是可以运行的,可以在Window/Ubuntu/ARM Linux上运行看看界面效果也可以直接看第22.3小节的程序运行后的效果图。获取数据的原理和第十五章LED章节原理一样都是从开发板获取数据,不同的是第十五章使用QFile这个类直接访问文件,本例介绍另外一种方法,就是使用C语言的open()方法访问数据。本章没有使用很多新的知识,编者于是花时间设计一个新界面,将界面拆分成一段段小知识。让读者明白要设计这么一个界面需要经过哪些步骤。
       更多AP3216C的信息请看【正点原子】I.MX6U用户快速体验V1.x.pdf的第3.20小节。

22.1 资源简介

       在正点原子I.MX6ULL ALPHA开发板底板上有一个三合一环境传感器,也就是在拔码开关旁边的传感器,采用的是II2接口。(注意:I.MX6ULL MINI开发板没有这个传器)。下图为I.MX6ULL ALPHA开发板的三合一环境传感器原理图。
第二十二章 AP3216C710.png

        开发板实物图位置。
第二十二章 AP3216C723.png

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. 1   QT       += core gui
  2. 2
  3. 3   greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  4. 4
  5. 5   CONFIG += c++11
  6. 6
  7. 7   # The following define makes your compiler emit warnings if you use
  8. 8   # any Qt feature that has been marked deprecated (the exact warnings
  9. 9   # depend on your compiler). Please consult the documentation of the
  10. 10  # deprecated API in order to know how to port your code away from it.
  11. 11  DEFINES += QT_DEPRECATED_WARNINGS
  12. 12
  13. 13  # You can also make your code fail to compile if it uses deprecated APIs.
  14. 14  # In order to do so, uncomment the following line.
  15. 15  # You can also select to disable deprecated APIs only up to a certain version of Qt.
  16. 16  #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
  17. 17
  18. 18  SOURCES += \
  19. 19      ap3216c.cpp \
  20. 20      arcgraph.cpp \
  21. 21      glowtext.cpp \
  22. 22      main.cpp \
  23. 23      mainwindow.cpp
  24. 24
  25. 25  HEADERS += \
  26. 26      ap3216c.h \
  27. 27      arcgraph.h \
  28. 28      glowtext.h \
  29. 29      mainwindow.h
  30. 30
  31. 31  # Default rules for deployment.
  32. 32  qnx: target.path = /tmp/${TARGET}/bin
  33. 33  else: unix:!android: target.path = /opt/${TARGET}/bin
  34. 34  !isEmpty(target.path): INSTALLS += target
  35. 35
  36. 36  include(headview/headview.pri)
复制代码

        从上面的项目pro文件可以看出,本例使用的文件比较多。
        第36行,使用到pri文件,pri文件的语法和pro文件相同,通常它是由pro文件改写得到的,该类型文件类似于 C++中的头文件,可以在pro文件中使用 include 将其包含进来,相当于文件引入,当一个项目文件非常多时,或有些项目文件需要重复使用,为了方便管理就可以使用此方法。
        项目里文件很多,我们一个一个分析,最终我们只需关注“mainwindow.h”和“mainwindow.cpp”文件,程序的主要流程都在这两个文件里。分析完了这两个文件再到其他文件。
        在源文件“mainwindow.h”的代码如下。
  1.    /******************************************************************
  2.     Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
  3.     * @projectName   08_spi_sensor
  4.     * @brief         mainwindow.h
  5.     * @author        Deng Zhimao
  6.     * @email         <a href="mailto:1252699831@qq.com" target="_blank">1252699831@qq.com</a>
  7.     * @net            <a href="www.openedv.com" target="_blank">www.openedv.com</a>
  8.     * @date           2021-05-21
  9.     *******************************************************************/
  10. 1   #ifndef MAINWINDOW_H
  11. 2   #define MAINWINDOW_H
  12. 3
  13. 4   #include <QMainWindow>
  14. 5   #include <QLabel>
  15. 6   #include <QVBoxLayout>
  16. 7   #include <QHBoxLayout>
  17. 8   #include "arcgraph.h"
  18. 9   #include "glowtext.h"
  19. 10  #include "ap3216c.h"
  20. 11  #include "headview/headview.h"
  21. 12  class ArcGraph;
  22. 13  class GlowText;
  23. 14  class Ap3216c;
  24. 15  class HeadView;
  25. 16
  26. 17  class MainWindow : public QMainWindow
  27. 18  {
  28. 19      Q_OBJECT
  29. 20
  30. 21  public:
  31. 22      MainWindow(QWidget *parent = nullptr);
  32. 23      ~MainWindow();
  33. 24
  34. 25  private:
  35. 26      ArcGraph *arcGraph[3];
  36. 27      GlowText *glowText[3];
  37. 28
  38. 29      QVBoxLayout *vBoxLayout;
  39. 30      QHBoxLayout *hBoxLayout[5];
  40. 31
  41. 32      GlowText *test;
  42. 33
  43. 34      /* 容器作用,用于布局 */
  44. 35      QWidget *widget[6];
  45. 36
  46. 37      /* 标签文本 */
  47. 38      QLabel *label[3];
  48. 39
  49. 40      /* ii2传感器类 */
  50. 41      Ap3216c *ap3216c;
  51. 42
  52. 43      /* 视图表头 */
  53. 44      HeadView *headView;
  54. 45
  55. 46  private slots:
  56. 47      /* 获取ap3216传感器数据 */
  57. 48      void getAp3216cData();
  58. 49  };
  59. 50  #endif // MAINWINDOW_H
复制代码

        在“mainwindow.h”的头文件里,我们看到使用了ArcGraph、GlowText、Ap3216c和HeadView自定义的类。它们是蓝色科技弧形视图,发光文本、Ap3216c类和视频表头图。不同的类分开来写这样可以很方便地管理我们的项目。这些类在后面已经贴上代码和图加上一些解释方便给读者查阅。“mainwindow.h”头文件的解释就到这里了。
        “mainwindow.cpp”文件主要承担着布局及数据显示的功能。
  1. <font size="2">    /******************************************************************
  2.     Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
  3.     * @projectName   08_spi_sensor
  4.     * @brief         mainwindow.cpp
  5.     * @author        Deng Zhimao
  6.     * @email         <a href="mailto:1252699831@qq.com" target="_blank" style="">1252699831@qq.com</a>
  7.     * @net            <a href="www.openedv.com" target="_blank" style="">www.openedv.com</a>
  8.     * @date           2021-05-21
  9.     *******************************************************************/
  10. 1   #include "mainwindow.h"
  11. 2   #include <QDebug>
  12. 3   MainWindow::MainWindow(QWidget *parent)
  13. 4       : QMainWindow(parent)
  14. 5   {
  15. 6       this->resize(800, 480);
  16. 7       this->setStyleSheet("background:#011753");
  17. 8  
  18. 9       for (int i = 0; i < 6; i++)
  19. 10          widget = new QWidget();
  20. 11
  21. 12      for (int i = 0; i < 3; i++)
  22. <span style="font-style: italic;"><span style="font-style: normal;">13          arcGraph = new ArcGraph();
  23. 14
  24. 15      for (int i = 0; i < 5; i++)
  25. 16          hBoxLayout</span><span style="font-style: normal;"> = new QHBoxLayout();
  26. 17
  27. 18      headView = new HeadView();
  28. 19
  29. 20      QFont font;
  30. 21      font.setPixelSize(18);
  31. 22
  32. 23      QPalette pal;
  33. 24      pal.setColor(QPalette::WindowText, Qt::white);
  34. 25
  35. 26      QStringList list;
  36. 27      list<<"环境光强度:"<<"接近距离:"<<"红外强度:";
  37. 28      for (int i = 0; i < 3; i++) {
  38. 29          label</span><span style="font-style: normal;"> = new QLabel();
  39. 30          glowText</span><span style="font-style: normal;"> = new GlowText();
  40. 31          glowText</span><span style="font-style: normal;">->setMinimumWidth(30);
  41. 32          label</span><span style="font-style: normal;">->setText(list</span><span style="font-style: normal;">);
  42. 33          label</span><span style="font-style: normal;">->setFont(font);
  43. 34          label</span><span style="font-style: normal;">->setPalette(pal);
  44. 35          label</span><span style="font-style: normal;">->adjustSize();
  45. 36      }
  46. 37
  47. 38      vBoxLayout = new QVBoxLayout();
  48. 39
  49. 40      /* 垂直布局,将主窗体为上下两部分,方便布局 */
  50. 41      vBoxLayout->addWidget(headView);
  51. 42      vBoxLayout->addWidget(widget[1]);
  52. 43      vBoxLayout->addWidget(widget[2]);
  53. 44      widget[0]->setLayout(vBoxLayout);
  54. 45
  55. 46      /* 主布局设置为widget[0] */
  56. 47      setCentralWidget(widget[0]);
  57. 48
  58. 49      /* 设置widget[1]的高度,不会随界面的大小而变化 */
  59. 50      widget[2]->setFixedHeight(150);
  60. 51
  61. 52      /* 三个蓝色科技感弧形图布局,采用水平布局 */
  62. 53      hBoxLayout[0]->addWidget(arcGraph[0]);
  63. 54      hBoxLayout[0]->addWidget(arcGraph[1]);
  64. 55      hBoxLayout[0]->addWidget(arcGraph[2]);
  65. 56      widget[1]->setLayout(hBoxLayout[0]);
  66. 57
  67. 58      /* 数据文字容器水平布局, */
  68. 59      hBoxLayout[1]->addWidget(widget[3]);
  69. 60      hBoxLayout[1]->addWidget(widget[4]);
  70. 61      hBoxLayout[1]->addWidget(widget[5]);
  71. 62      hBoxLayout[1]->setContentsMargins(0, 40, 0, 0);
  72. 63
  73. 64      widget[2]->setLayout(hBoxLayout[1]);
  74. 65
  75. 66      /* als布局 */
  76. 67      hBoxLayout[2]->addWidget(label[0]);
  77. 68      hBoxLayout[2]->addWidget(glowText[0]);
  78. 69      hBoxLayout[2]->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
  79. 70      widget[3]->setLayout(hBoxLayout[2]);
  80. 71
  81. 72      /* ps布局 */
  82. 73      hBoxLayout[3]->addWidget(label[1]);
  83. 74      hBoxLayout[3]->addWidget(glowText[1]);
  84. 75      hBoxLayout[3]->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
  85. 76      widget[4]->setLayout(hBoxLayout[3]);
  86. 77
  87. 78      /* ir布局 */
  88. 79      hBoxLayout[4]->addWidget(label[2]);
  89. 80      hBoxLayout[4]->addWidget(glowText[2]);
  90. 81      hBoxLayout[4]->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
  91. 82      widget[5]->setLayout(hBoxLayout[4]);
  92. 83
  93. 84      ap3216c = new Ap3216c(this);
  94. 85      /* 只能在开发板上开启获取数据,Ubuntu上是没有ap3216c传感器的 */
  95. 86  #if __arm__
  96. 87      ap3216c->setCapture(true);
  97. 88  #endif
  98. 89
  99. 90      connect(ap3216c, SIGNAL(ap3216cDataChanged()),
  100. 91              this, SLOT(getAp3216cData()));
  101. 92  }
  102. 93
  103. 94  MainWindow::~MainWindow()
  104. 95  {
  105. 96  }
  106. 97
  107. 98  void MainWindow::getAp3216cData()
  108. 99  {
  109. 100     static QString als = ap3216c->alsData();
  110. 101     if (als != ap3216c->alsData()) {
  111. 102         als = ap3216c->alsData();
  112. 103         arcGraph[0]->setangleLength(als.toUInt() * 360 / 65535);
  113. 104     }
  114. 105
  115. 106     static QString ps = ap3216c->psData();
  116. 107     if (ps != ap3216c->psData()) {
  117. 108         ps = ap3216c->psData();
  118. 109         arcGraph[1]->setangleLength(ps.toUInt() * 360 / 1023);
  119. 110     }
  120. 111
  121. 112     static QString ir = ap3216c->irData();
  122. 113     if (ir != ap3216c->irData()) {
  123. 114         ir = ap3216c->irData();
  124. 115         arcGraph[2]->setangleLength(ir.toUInt() * 360 / 1023);
  125. 116     }
  126. 117
  127. 118     glowText[0]->setTextData(als);
  128. 119     glowText[1]->setTextData(ps);
  129. 120     glowText[2]->setTextData(ir);
  130. 121 }</span></span></font>
复制代码

        第98行之前都是一些布局及变量声明使用的内容,这些都不会再解释,因为我们已经说过很多次了。
        第98~121行,若收到Ap3216c类发送过来的信号,则显示数据。显示在发数据在ArcGraph和GlowText类上。其中ArcGraph是一个弧形视图,通过setangleLength()方法,设置传入弧的角度大小,就可以画出一段弧,用图形的方式显示给用户看,比数字更直观。其中65535和1023数值的由来是环境光传感器具有16位的分辨率,接近传感器和红外传感器具有10位分辨率。也就是2的16次减一,与2的10次方减一。
        主要的流程基本介绍完,我们开始分步介绍ArcGraph、GlowText、Ap3216c和HeadView类。
        HeadView类主要功能是显示如下的一个表头。可以很方便的移植到其他项目里用。下图的背景颜色可忽略。实际这个类的背景颜色是透明的。
第二十二章 AP3216C8719.png

“headview.h”头文件内容如下。文件比较简单,不再解释。
  1.     /******************************************************************
  2.     Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
  3.     * @projectName   headview
  4.     * @brief         headview.h
  5.     * @author        Deng Zhimao
  6.     * @email         <a href="mailto:1252699831@qq.com" target="_blank">1252699831@qq.com</a>
  7.     * @net            <a href="www.openedv.com" target="_blank">www.openedv.com</a>
  8.     * @date           2021-05-22
  9.     *******************************************************************/
  10. 1   #ifndef HEADVIEW_H
  11. 2   #define HEADVIEW_H
  12. 3
  13. 4   #include <QWidget>
  14. 5   #include <QLabel>
  15. 6   #include <QVBoxLayout>
  16. 7   #include <QHBoxLayout>
  17. 8
  18. 9
  19. 10  class HeadView : public QWidget
  20. 11  {
  21. 12      Q_OBJECT
  22. 13
  23. 14  public:
  24. 15      HeadView(QWidget *parent = nullptr);
  25. 16      ~HeadView();
  26. 17
  27. 18  private:
  28. 19      QWidget *widget;
  29. 20      QLabel *textLabel;
  30. 21      QWidget *iconWidget;
  31. 22      QWidget *lineWidget;
  32. 23
  33. 24      QHBoxLayout *hBoxLayout;
  34. 25      QVBoxLayout *vBoxLayout;
  35. 26  };
  36. 27  #endif // HEADVIEW_H
复制代码

“headview.cpp”源文件内容如下。
  
  1.   /******************************************************************
  2.     Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
  3.     * @projectName   headview
  4.     * @brief         headview.cpp
  5.     * @author        Deng Zhimao
  6.     * @email         <a href="mailto:1252699831@qq.com" target="_blank">1252699831@qq.com</a>
  7.     * @net            <a href="www.openedv.com" target="_blank">www.openedv.com</a>
  8.     * @date           2021-05-22
  9.     *******************************************************************/
  10. 1   #include "headview.h"
  11. 2
  12. 3   HeadView::HeadView(QWidget *parent)
  13. 4       : QWidget(parent)
  14. 5   {
  15. 6       this->setAttribute(Qt::WA_TranslucentBackground, true);
  16. 7       widget = new QWidget();
  17. 8
  18. 9       iconWidget = new QWidget(this);
  19. 10      iconWidget->setFixedSize(48, 48);
  20. 11      iconWidget->setStyleSheet("background:url(:/images/dataviewicon.png)");
  21. 12
  22. 13      textLabel = new QLabel(this);
  23. 14      textLabel->setFixedSize(200, 48);
  24. 15      textLabel->setText("数据可视化情况");
  25. 16      textLabel->setStyleSheet("QLabel {font-size: 20px; color: white}");
  26. 17
  27. 18      lineWidget = new QWidget(this);
  28. 19      lineWidget->setFixedHeight(2);
  29. 20      lineWidget->setStyleSheet("QWidget {background: #eeeeeeee}");
  30. 21
  31. 22      vBoxLayout = new QVBoxLayout();
  32. 23      vBoxLayout->addWidget(widget);
  33. 24      vBoxLayout->addWidget(lineWidget);
  34. 25      vBoxLayout->setContentsMargins(0, 0, 0, 0);
  35. 26
  36. 27      hBoxLayout = new QHBoxLayout();
  37. 28      hBoxLayout->addWidget(iconWidget);
  38. 29      hBoxLayout->addWidget(textLabel);
  39. 30      hBoxLayout->setContentsMargins(0, 0, 0, 0);
  40. 31      hBoxLayout->setAlignment(Qt::AlignLeft);
  41. 32      widget->setLayout(hBoxLayout);
  42. 33
  43. 34      this->setLayout(vBoxLayout);
  44. 35      this->adjustSize();
  45. 36      this->setMaximumHeight(48);
  46. 37  }
  47. 38
  48. 39  HeadView::~HeadView()
  49. 40  {
  50. 41  }
复制代码

        ArcGraph类是一个蓝色科技感弧形视图,这里运用了QPainter为画图,在第九章画图章节我们已经了解过QPainter的内容了。不详细解释。ArcGraph类实现的效果如下。(注背景实际上是透明的)。
第二十二章 AP3216C11553.png

“arcgraph.h”头文件内容如下。
  1.     /******************************************************************
  2.     Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
  3.     * @projectName   08_spi_sensor
  4.     * @brief         arcgraph.h
  5.     * @author        Deng Zhimao
  6.     * @email         <a href="mailto:1252699831@qq.com" target="_blank">1252699831@qq.com</a>
  7.     * @net            <a href="www.openedv.com" target="_blank">www.openedv.com</a>
  8.     * @date           2021-05-21
  9.     *******************************************************************/
  10. 1   #ifndef ARCGRAPH_H
  11. 2   #define ARCGRAPH_H
  12. 3
  13. 4   #include <QWidget>
  14. 5   #include <QPainter>
  15. 6   #include <QPaintEvent>
  16. 7
  17. 8   /* 蓝色科技感弧形视图 */
  18. 9   class ArcGraph : public QWidget
  19. 10  {
  20. 11      Q_OBJECT
  21. 12
  22. 13  public:
  23. 14      ArcGraph(QWidget *parent = nullptr);
  24. 15      ~ArcGraph();
  25. 16
  26. 17      void setstartAngle(int);
  27. 18      void setangleLength(int);
  28. 19
  29. 20  private:
  30. 21      void paintEvent(QPaintEvent *event);
  31. 22      int startAngle;
  32. 23      int angleLength;
  33. 24  };
  34. 25  #endif // ARCGRAPH_H
复制代码

“arcgraph.cpp”源文件内容如下。
   
  1. /******************************************************************
  2.     Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
  3.     * @projectName   08_ii2_ap3216c_sensor
  4.     * @brief         arcgraph.cpp
  5.     * @author        Deng Zhimao
  6.     * @email         <a href="mailto:1252699831@qq.com" target="_blank">1252699831@qq.com</a>
  7.     * @net            <a href="www.openedv.com" target="_blank">www.openedv.com</a>
  8.     * @date           2021-05-22
  9.     *******************************************************************/
  10. 1   #include "arcgraph.h"
  11. 2  
  12. 3   ArcGraph::ArcGraph(QWidget *parent)
  13. 4       : QWidget(parent),
  14. 5         startAngle(90),
  15. 6         angleLength(100)
  16. 7   {
  17. 8       this->setMinimumSize(100, 100);
  18. 9       setAttribute(Qt::WA_TranslucentBackground, true);
  19. 10  }
  20. 11
  21. 12  ArcGraph::~ArcGraph()
  22. 13  {
  23. 14  }
  24. 15
  25. 16  void ArcGraph::setstartAngle(int angle)
  26. 17  {
  27. 18      startAngle = angle;
  28. 19      this->repaint();
  29. 20  }
  30. 21
  31. 22  void ArcGraph::setangleLength(int length)
  32. 23  {
  33. 24      angleLength = length;
  34. 25      this->repaint();
  35. 26  }
  36. 27
  37. 28  void ArcGraph::paintEvent(QPaintEvent *event)
  38. 29  {
  39. 30      QPainter painter(this);
  40. 31
  41. 32      /* 保存状态 */
  42. 33      painter.save();
  43. 34
  44. 35      /* 设置抗锯齿 */
  45. 36      painter.setRenderHints(QPainter::Antialiasing, true);
  46. 37
  47. 38      /* 最外层的圆 */
  48. 39      QRect drawRect = event->rect();
  49. 40      QRadialGradient gradient1(drawRect.center(),
  50. 41                                drawRect.width() / 2,
  51. 42                                drawRect.center());
  52. 43      gradient1.setColorAt(0, Qt::transparent);
  53. 44      gradient1.setColorAt(0.5, Qt::transparent);
  54. 45      gradient1.setColorAt(0.51, QColor("#00237f"));
  55. 46      gradient1.setColorAt(0.58, QColor("#00237f"));
  56. 47      gradient1.setColorAt(0.59, Qt::transparent);
  57. 48      gradient1.setColorAt(1, Qt::transparent);
  58. 49      painter.setBrush(gradient1);
  59. 50      painter.setPen(Qt::NoPen);
  60. 51      painter.drawEllipse(drawRect);
  61. 52
  62. 53      /* 里层的圆 */
  63. 54      QRadialGradient gradient2(drawRect.center(),
  64. 55                                drawRect.width() / 2,
  65. 56                                drawRect.center());
  66. 57      gradient2.setColorAt(0, Qt::transparent);
  67. 58      gradient2.setColorAt(0.420, Qt::transparent);
  68. 59      gradient2.setColorAt(0.421, QColor("#885881e3"));
  69. 60      gradient2.setColorAt(0.430, QColor("#5881e3"));
  70. 61      gradient2.setColorAt(0.440, QColor("#885881e3"));
  71. 62      gradient2.setColorAt(0.441, Qt::transparent);
  72. 63      gradient2.setColorAt(1, Qt::transparent);
  73. 64      painter.setBrush(gradient2);
  74. 65      painter.setPen(Qt::NoPen);
  75. 66      painter.drawEllipse(drawRect);
  76. 67
  77. 68      /* 数字 */
  78. 69      QFont font;
  79. 70      font.setPixelSize(drawRect.width() / 10);
  80. 71      painter.setPen(Qt::white);
  81. 72      painter.setFont(font);
  82. 73      painter.drawText(drawRect, Qt::AlignCenter,
  83. 74                       QString::number(angleLength * 100 / 360) + "%");
  84. 75
  85. 76      /* 发光背景圆 */
  86. 77      painter.translate(drawRect.width() >> 1, drawRect.height() >> 1);
  87. 78      int radius = drawRect.width() / 2;
  88. 79      /* radius<< 1(左移1位)相当于radius*2 */
  89. 80      QRectF rect(-radius, -radius, radius << 1, radius << 1);
  90. 81
  91. 82      QRadialGradient gradient3(0, 0, radius);
  92. 83      gradient3.setColorAt(0, Qt::transparent);
  93. 84      gradient3.setColorAt(0.42, Qt::transparent);
  94. 85      gradient3.setColorAt(0.51, QColor("#500194d3"));
  95. 86      gradient3.setColorAt(0.55, QColor("#22c1f3f9"));
  96. 87      gradient3.setColorAt(0.58, QColor("#500194d3"));
  97. 88      gradient3.setColorAt(0.68, Qt::transparent);
  98. 89      gradient3.setColorAt(1.0, Qt::transparent);
  99. 90      painter.setBrush(gradient3);
  100. 91      QPainterPath path1;
  101. 92      path1.arcTo(rect, startAngle, -angleLength);
  102. 93      painter.setPen(Qt::NoPen);
  103. 94      painter.drawPath(path1);
  104. 95
  105. 96      /* 发光圆/弧 */
  106. 97      QRadialGradient gradient4(0, 0, radius);
  107. 98      gradient4.setColorAt(0, Qt::transparent);
  108. 99      gradient4.setColorAt(0.49, Qt::transparent);
  109. 100     gradient4.setColorAt(0.50, QColor("#4bf3f9"));
  110. 101     gradient4.setColorAt(0.59, QColor("#4bf3f9"));
  111. 102     gradient4.setColorAt(0.60, Qt::transparent);
  112. 103     gradient4.setColorAt(1.0, Qt::transparent);
  113. 104     painter.setBrush(gradient4);
  114. 105     QPainterPath path2;
  115. 106     path2.arcTo(rect, startAngle, -angleLength);
  116. 107     painter.setPen(Qt::NoPen);
  117. 108     painter.drawPath(path2);
  118. 109
  119. 110     /* 恢复状态 */
  120. 111     painter.restore();
  121. 112
  122. 113     /* 设置事件对象的accept标志 */
  123. 114     event->accept();
  124. 115 }
复制代码

        GlowText类是发光文字效果类,模拟出文字发光效果。可以看到下图的数字100微微发着青色的光,(文档可能效果不是很明显)在Qt C++里想要实现这种效果需要自己实现。通过重写类或者自己设计程序实现。GlowText是编者设计发光效果的类。
第二十二章 AP3216C17008.png

“glowtext.h”头文件内容如下。
  1.    /******************************************************************
  2.     Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
  3.     * @projectName   GlowText
  4.     * @brief         glowtext.h
  5.     * @author        Deng Zhimao
  6.     * @email         <a href="mailto:1252699831@qq.com" target="_blank">1252699831@qq.com</a>
  7.     * @net            <a href="www.openedv.com" target="_blank">www.openedv.com</a>
  8.     * @date           2021-05-21
  9.     *******************************************************************/
  10. 1   #ifndef GLOWTEXT_H
  11. 2   #define GLOWTEXT_H
  12. 3
  13. 4   #include <QWidget>
  14. 5   #include <QLabel>
  15. 6
  16. 7   class GlowText : public QWidget
  17. 8   {
  18. 9       Q_OBJECT
  19. 10
  20. 11  public:
  21. 12      GlowText(QWidget *parent = nullptr);
  22. 13      ~GlowText();
  23. 14
  24. 15      void setTextColor(QColor);
  25. 16      void setFontSize(int);
  26. 17      void setTextData(QString);
  27. 18
  28. 19  private:
  29. 20      /* 文本背景 */
  30. 21      QLabel *textLabelbg;
  31. 22
  32. 23      /* 文本标签 */
  33. 24      QLabel *textLabel;
  34. 25
  35. 26      /* 字体颜色 */
  36. 27      QColor textColor;
  37. 28
  38. 29      /* 文本字体大小 */
  39. 30      int fontSize;
  40. 31
  41. 32      /* 文本内容 */
  42. 33      QString textData;
  43. 34  };
  44. 35  #endif // GLOWTEXT_H
复制代码

“glowtext.cpp”源文件内容如下。
  1.    /******************************************************************
  2.     Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
  3.     * @projectName   GlowText
  4.     * @brief         glowtext.cpp
  5.     * @author        Deng Zhimao
  6.     * @email         <a href="mailto:1252699831@qq.com" target="_blank">1252699831@qq.com</a>
  7.     * @net            <a href="www.openedv.com" target="_blank">www.openedv.com</a>
  8.     * @date           2021-05-21
  9.     *******************************************************************/
  10. 1   #include "glowtext.h"
  11. 2   #include <QDebug>
  12. 3   #include <QGraphicsBlurEffect>
  13. 4
  14. 5   GlowText::GlowText(QWidget *parent)
  15. 6       : QWidget(parent),
  16. 7         textColor("#4bf3f9"),
  17. 8         fontSize(18),
  18. 9         textData("100")
  19. 10  {
  20. 11      QFont font;
  21. 12      font.setPixelSize(fontSize);
  22. 13      QPalette pal;
  23. 14      pal.setColor(QPalette::WindowText, textColor);
  24. 15      textLabelbg = new QLabel(this);
  25. 16      textLabelbg->setAttribute(Qt::WA_TranslucentBackground, true);
  26. 17      textLabelbg->setPalette(pal);
  27. 18      textLabelbg->setFont(font);
  28. 19      textLabelbg->setText(textData);
  29. 20      textLabelbg->setAlignment(Qt::AlignCenter);
  30. 21
  31. 22      /* 设置模糊特效 */
  32. 23      QGraphicsBlurEffect *ef = new QGraphicsBlurEffect();
  33. 24      ef->setBlurRadius(25);
  34. 25      ef->setBlurHints(QGraphicsBlurEffect::QualityHint);
  35. 26      textLabelbg->setGraphicsEffect(ef);
  36. 27
  37. 28      textLabel = new QLabel(this);
  38. 29      textLabel->setAttribute(Qt::WA_TranslucentBackground, true);
  39. 30      textLabel->setPalette(pal);
  40. 31      textLabel->setFont(font);
  41. 32      textLabel->setText(textData);
  42. 33      textLabel->setAlignment(Qt::AlignCenter);
  43. 34      textLabelbg->adjustSize();
  44. 35      textLabel->adjustSize();
  45. 36
  46. 37      this->resize(textLabel->size().width() + 10,
  47. 38                   textLabel->size().height() + 10);
  48. 39      /* 背景透明化 */
  49. 40      this->setAttribute(Qt::WA_TranslucentBackground, true);
  50. 41  }
  51. 42
  52. 43  GlowText::~GlowText()
  53. 44  {
  54. 45  }
  55. 46
  56. 47  void GlowText::setTextColor(QColor color)
  57. 48  {
  58. 49      QPalette pal;
  59. 50      pal.setColor(QPalette::WindowText, color);
  60. 51      textLabelbg->setPalette(pal);
  61. 52      textLabel->setPalette(pal);
  62. 53  }
  63. 54
  64. 55  void GlowText::setFontSize(int size)
  65. 56  {
  66. 57      QFont font;
  67. 58      font.setPixelSize(size);
  68. 59
  69. 60      textLabelbg->setFont(font);
  70. 61      textLabel->setFont(font);
  71. 62
  72. 63      textLabel->adjustSize();
  73. 64      textLabelbg->adjustSize();
  74. 65      this->resize(textLabel->size().width() + 10,
  75. 66                   textLabel->size().height() + 10);
  76. 67  }
  77. 68
  78. 69  void GlowText::setTextData(QString text)
  79. 70  {
  80. 71      textLabelbg->setText(text);
  81. 72      textLabel->setText(text);
  82. 73
  83. 74      textLabel->adjustSize();
  84. 75      textLabelbg->adjustSize();
  85. 76      this->resize(textLabel->size().width() + 10,
  86. 77                   textLabel->size().height() + 10);
  87. 78  }
复制代码

        Ap3216c类的作用就是从驱动层提供给Linux应用层的接口获取数据。
“ap3216c.h”头文件内容如下。
  1.    /******************************************************************
  2.     Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
  3.     * @projectName   sensor
  4.     * @brief         ap3216c.h
  5.     * @author        Deng Zhimao
  6.     * @email         <a href="mailto:1252699831@qq.com" target="_blank">1252699831@qq.com</a>
  7.     * @net            <a href="www.openedv.com" target="_blank">www.openedv.com</a>
  8.     * @date           2020-07-10
  9.     *******************************************************************/
  10. 1   #ifndef AP3216C_H
  11. 2   #define AP3216C_H
  12. 3
  13. 4   #include <QObject>
  14. 5   #include <QTimer>
  15. 6
  16. 7   class Ap3216c : public QObject
  17. 8   {
  18. 9       Q_OBJECT
  19. 10
  20. 11  public:
  21. 12      explicit Ap3216c(QObject *parent = 0);
  22. 13      ~Ap3216c();
  23. 14
  24. 15      Q_INVOKABLE void setCapture(bool str);
  25. 16
  26. 17      QString alsData();
  27. 18      QString psData();
  28. 19      QString irData();
  29. 20
  30. 21  private:
  31. 22      QTimer *timer;
  32. 23      QString alsdata;
  33. 24      QString psdata;
  34. 25      QString irdata;
  35. 26
  36. 27      QString readAlsData();
  37. 28      QString readPsData();
  38. 29      QString readIrData();
  39. 30
  40. 31      Q_PROPERTY(QString alsData READ alsData NOTIFY ap3216cDataChanged)
  41. 32      Q_PROPERTY(QString psData READ psData NOTIFY ap3216cDataChanged)
  42. 33      Q_PROPERTY(QString irData READ irData NOTIFY ap3216cDataChanged)
  43. 34
  44. 35  public slots:
  45. 36      void timer_timeout();
  46. 37
  47. 38  signals:
  48. 39      void ap3216cDataChanged();
  49. 40
  50. 41  };
  51. 42
  52. 43  #endif // AP3216C_H
复制代码

“ap3216c.cpp”源文件内容如下。
  1.     /******************************************************************
  2.     Copyright © Deng Zhimao Co., Ltd. 1990-2021. All rights reserved.
  3.     * @projectName   sensor
  4.     * @brief         ap3216c.cpp
  5.     * @author        Deng Zhimao
  6.     * @email         <a href="mailto:1252699831@qq.com" target="_blank">1252699831@qq.com</a>
  7.     * @net            <a href="www.openedv.com" target="_blank">www.openedv.com</a>
  8.     * @date           2020-07-10
  9.     *******************************************************************/
  10. 1   #include "ap3216c.h"
  11. 2   #include <stdio.h>
  12. 3   #include <string.h>
  13. 4   #include <sys/types.h>
  14. 5   #include <sys/stat.h>
  15. 6   #include <fcntl.h>
  16. 7   #include <unistd.h>
  17. 8   #include <QDebug>
  18. 9  
  19. 10  Ap3216c::Ap3216c(QObject *parent) : QObject (parent)
  20. 11  {
  21. 12      timer = new QTimer();
  22. 13      connect(timer, SIGNAL(timeout()), this, SLOT(timer_timeout()));
  23. 14  }
  24. 15
  25. 16  Ap3216c::~Ap3216c()
  26. 17  {
  27. 18
  28. 19  }
  29. 20
  30. 21  void Ap3216c::timer_timeout()
  31. 22  {
  32. 23      alsdata = readAlsData();
  33. 24      psdata = readPsData();
  34. 25      irdata = readIrData();
  35. 26      emit ap3216cDataChanged();
  36. 27  }
  37. 28
  38. 29  QString Ap3216c::readIrData()
  39. 30  {
  40. 31      char const *filename = "/sys/class/misc/ap3216c/ir";
  41. 32      int err = 0;
  42. 33      int fd;
  43. 34      char buf[10];
  44. 35
  45. 36      fd = open(filename, O_RDONLY);
  46. 37      if(fd < 0) {
  47. 38          close(fd);
  48. 39          return "open file error!";
  49. 40      }
  50. 41
  51. 42      err = read(fd, buf, sizeof(buf));
  52. 43      if (err < 0) {
  53. 44          close(fd);
  54. 45          return "read data error!";
  55. 46      }
  56. 47      close(fd);
  57. 48
  58. 49      QString irValue = buf;
  59. 50      QStringList list = irValue.split("\n");
  60. 51      return list[0];
  61. 52  }
  62. 53
  63. 54  QString Ap3216c::readPsData()
  64. 55  {
  65. 56      char const *filename = "/sys/class/misc/ap3216c/ps";
  66. 57      int err = 0;
  67. 58      int fd;
  68. 59      char buf[10];
  69. 60
  70. 61      fd = open(filename, O_RDONLY);
  71. 62      if(fd < 0) {
  72. 63          close(fd);
  73. 64          return "open file error!";
  74. 65      }
  75. 66
  76. 67      err = read(fd, buf, sizeof(buf));
  77. 68      if (err < 0) {
  78. 69          close(fd);
  79. 70          return "read data error!";
  80. 71      }
  81. 72      close(fd);
  82. 73
  83. 74      QString psValue = buf;
  84. 75      QStringList list = psValue.split("\n");
  85. 76      return list[0];
  86. 77  }
  87. 78
  88. 79  QString Ap3216c::readAlsData()
  89. 80  {
  90. 81      char const *filename = "/sys/class/misc/ap3216c/als";
  91. 82      int err = 0;
  92. 83      int fd;
  93. 84      char buf[10];
  94. 85
  95. 86      fd = open(filename, O_RDONLY);
  96. 87      if(fd < 0) {
  97. 88          close(fd);
  98. 89          return "open file error!";
  99. 90      }
  100. 91
  101. 92      err = read(fd, buf, sizeof(buf));
  102. 93      if (err < 0) {
  103. 94          close(fd);
  104. 95          return "read data error!";
  105. 96      }
  106. 97      close(fd);
  107. 98
  108. 99      QString alsValue = buf;
  109. 100     QStringList list = alsValue.split("\n");
  110. 101     return list[0];
  111. 102 }
  112. 103
  113. 104 QString Ap3216c::alsData()
  114. 105 {
  115. 106     return alsdata;
  116. 107 }
  117. 108
  118. 109 QString Ap3216c::irData()
  119. 110 {
  120. 111     return irdata;
  121. 112 }
  122. 113
  123. 114 QString Ap3216c::psData()
  124. 115 {
  125. 116     return psdata;
  126. 117 }
  127. 118
  128. 119 void Ap3216c::setCapture(bool str)
  129. 120 {
  130. 121     if(str)
  131. 122         timer->start(500);
  132. 123     else
  133. 124         timer->stop();
  134. 125 }
复制代码

        上面采用C语言的接口访问节点文件的方法来获取数据。需要包含C语言的头文件,如第1~7行。
        总结,从上面来看,设计一个界面的代码往往比实现这个读取数据的功能复杂多。所以还是应证了那句编者说的话,“美化界面比功能实现要耗时”!
22.3 程序运行效果
        Ubuntu运行效果图如下(下图为初始化数据)。要想获取传感器数据必须使用正点原子I.MX6ULL APLPA开发板,交叉编译到开发板上运行!
        在正点原子I.MX6ULL APLPA开发板上运行的情况时,当我们用手接近三合一环境传感器时,界面上的数据会发生变化,数据默认设置为500ms采集一次。

第二十二章 AP3216C25646.png

阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-26 07:10

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表