Developing Hongmeng equipment program using python (3-prototype of security system)

Harmonyos technology community 2021-10-29 00:15:33
developing hongmeng equipment program using

        Through the introduction of the previous two posts , I believe you have mastered how to use Python operation GPIO and I2C 了 . So today we can go further , adopt Python Medium GPIO and PWM The interface makes the buzzer sound . Of course , It would be boring to just implement this function , So we can expand , Make something a little more interesting .


Um. ... Think about doing something ?!


After a rummage , I found the expansion board below .


 Use Python Develop Hongmeng equipment program (3- Security system prototype )_ Equipment development

        This is in the development kit of Xiaoxiong paihongmeng “ Intelligent Security ” Expansion board , The board is equipped with an infrared sensor and a buzzer . Based on this board , You can develop a simple security case : When the infrared sensor senses ( Someone is approaching ) when , Trigger the buzzer to give a warning ( A voice ).


        Before we start, we need to introduce a new concept : Pulse width modulation (Pulse Width Modulation), namely :PWM. This is a concept at the hardware circuit level , For the development of the Internet of things, there is no need to study it in depth , Just master the basic principles and apply them to development .


To put it simply ,PWM It is a technology that uses digital signals to output analog signals .


        as everyone knows , Modern computer systems are digitally driven , namely : All computer components work under the alternating driving of high and low levels . If all peripherals are digital , Then it's very simple , direct GPIO Connect to communicate . However , Some peripherals are not digital , It's simulated , Need to receive analog signals ( Such as : Sine wave signal ), So you need a processor to use PWM Technology to realize analog signal .


The basic principle is as follows :

 Use Python Develop Hongmeng equipment program (3- Security system prototype )_ Hongmeng _02

According to the electrician and electronics teacher , If you need to generate such a sine wave , Multiple square waves can be used instead of , The effect is the same .


 Use Python Develop Hongmeng equipment program (3- Security system prototype )_ Equipment development _03

chart a) The sine wave in can be replaced by a square wave with the same amplitude but different width , Pictured b) , As long as these square waves and t The area enclosed by the axis and the original sine wave and t The area enclosed by the shaft is the same .


With the above principle , The next thing to discuss is : How to adjust the equivalent square wave ?


To solve this problem , Need to understand the concept of duty cycle !


The duty cycle is defined as a period of time , The time when the signal is at high level accounts for the percentage of the whole signal period .

 Use Python Develop Hongmeng equipment program (3- Security system prototype )_ Equipment development _04

In the figure above, a cycle is 4ms , The duration of high level in a cycle is 1ms, So the duty cycle is 25% .


After mastering these theoretical things , Then we can consider how to practice : How to achieve PWM The signal ( Square wave signal ) Output ?


        Very lucky ,Hi3861 In the PWM modular , So at the code level, you only need to call the relevant API Interface to PWM The module sets the necessary parameters , You can output the required continuous PWM The signal ( Square wave signal ).

 Use Python Develop Hongmeng equipment program (3- Security system prototype )_Python_05

The interface functions in the above table are clear at a glance , Almost possible “ Open the box ”, The only thing that needs to be explained is start( port, duty, freq ) This function , port Express PWM The output channel number of the signal ,freq Used to control the PWM The output frequency of the signal ,duty Used to specify the duty cycle , The value of the duty cycle is duty and freq The ratio of the .


Ok! With all this bedding , You can use the code !

 Use Python Develop Hongmeng equipment program (3- Security system prototype )_Python_06

Snippet 1 Part of the code first on GPIO_7 Initialization and function setting , The purpose is to receive is1 That is, the sensing signal of the infrared sensor , When Hi3861 Receiving this infrared sensing signal will trigger a callback is1_callback() .Snippet 2 Partial code reuse GPIO_8 Connect it PWM modular , The aim is to PWM Set parameters . Here you can go further to popularize science GPIO and PWM The relationship between : In theory, it can be used directly GPIO Generate the required square wave signal , But it doesn't happen in reality ! The reason is simple , Doing so consumes a lot of processor resources , And the frequency of the generated square wave signal is limited . therefore , Generally, independent PWM modular , Processor passes GPIO Connected to this module , Thus, its parameters can be set . after , The specific work of generating square wave is from PWM The module is finished .


Next , Look at the callback function is1_callback() The implementation of the .

 Use Python Develop Hongmeng equipment program (3- Security system prototype )_Python_07

The code is simple , The core part is the analysis of variables pwm_on marked , namely : When the infrared sensing signal is received, it will pwm_on The assignment is True.


so what ?


Then deal with it elsewhere !

 Use Python Develop Hongmeng equipment program (3- Security system prototype )_Python_08

At the end of the script file , You can see the processing pwm_on by True Code for , namely : When pwm_on by True when , call start() Function promotion 1 Number PWM The channel generates a square wave signal . and 1 Number PWM The channel is connected to a buzzer , The final result of generating square wave signal is to make the buzzer sound .


The overall process is as follows :

 Use Python Develop Hongmeng equipment program (3- Security system prototype )_ Tang zuolin _09 The end result is as follows :

 Use Python Develop Hongmeng equipment program (3- Security system prototype )_ Hongmeng _10

You can see , When I approach the development board , The infrared sensor immediately sends a sensing signal , Then trigger the buzzer to sound .


WOW! A simple security system is done !



The latest version Python The engine library can support almost all Hongmeng's peripheral interfaces , And basic floating point operations .


Interested students can download the code and files in the attachment and try to program by themselves .


Enjoy It!


Open source code address :  ​

You can click the original link below to download the article attachment

Link to the original text :​ ​​

 ​ Want to know more about Hongmeng , Please visit :​

 ​51CTO Hongmeng technology community built by Huawei official strategic cooperation ​


 Use Python Develop Hongmeng equipment program (3- Security system prototype )_Python_11

本文为[Harmonyos technology community]所创,转载请带上原文链接,感谢

  1. Angry liver half moon! Python learning route + resource summary
  2. 如何用Python编写下列程序
  3. Comment écrire les programmes suivants en python
  4. 熬夜整理了2021年Python最新学习资料,分享给学弟学妹们【大学生必备】
  5. 朋友股票亏惨了,我一怒用Python爬取了证券最新数据
  6. Python爬虫高阶:微店混淆逆向解密
  7. Python爬虫开发学习全教程第二版,爆肝十万字【建议收藏】
  8. 我用Python逆向登录世界上最大的游戏平台,steam加密手段有多高明【内附源码】
  9. 我用Python爬取1000封情书助力室友表白班花,却反转再反转...原来这就是班花的终极秘密!
  10. 我用Python爬取了五千张美女图壁纸,每天一张忘记初恋!
  11. 我Python采集了新榜热门内容,原来这就是别人能成为自媒体大佬的秘密!
  12. 30个Python小游戏,上班摸鱼我能玩一天【内附源码】
  13. 【JS 逆向 AES逆向加密】Python爬虫实战,日子越来越有判头了
  14. python将两个列表进行合并,合并时删除重复元素
  15. J'a i utilisé Python pour accéder aux dernières données sur les titres.
  16. Rester debout tard pour trier les derniers documents d'apprentissage de Python 2021 et les partager avec les étudiants et les jeunes filles [Must for College Students]
  17. python中sklearn版本一直是0.0
  18. La version de sklearn en Python a toujours été 0.0
  19. django 自定义中间件如何忽略部分视图函数
  20. django 自定義中間件如何忽略部分視圖函數
  21. Comment les intergiciels personnalisés Django ignorent certaines fonctions de vue
  22. python map函数的使用
  23. python map函數的使用
  24. Utilisation de la fonction de carte Python
  25. 通过对于Arduino或是Python(包括相关库)的使用,结合自己的日常工作,描述相关知识在工作中起到了什么作用。
  26. 2021最全Python接口测试自动化教程【附学习资料】
  27. Python读取配置文件(config.ini),写入配置文件
  28. Python lit le profil (config.ini), écrit le profil
  29. python 两个数据列表匹配查找
  30. python 兩個數據列錶匹配查找
  31. Recherche de correspondance de deux listes de données Python
  32. python循环结构程序编写题目
  33. 【算法学习】1603. 设计停车系统(java / c / c++ / python / go / rust)
  34. Problèmes de programmation de la structure de la boucle Python
  35. 有人能用python写出代码吗wwww
  36. [apprentissage de l'algorithme] 1603. Conception du système de stationnement (Java / C / C + + / python / go / Rust)
  37. Python中if和else,总是显示无效语法是缩进问题吗?是的话可以详细讲解一下吗?
  38. Python的问题,希望有人帮帮忙
  39. Python的問題,希望有人幫幫忙
  40. J'espère que quelqu'un peut m'aider.
  41. Si et autrement en python, est - ce un problème d'indentation d'afficher toujours une syntaxe invalide? Oui, puis - je vous expliquer en détail?
  42. Quelqu'un peut écrire du Code en python?
  43. Dataframe数据,用pandas.to_excel导出成excel问题
  44. Données du cadre de données, en utilisant Exporter Excel vers un problème Excel
  45. 请问这个过期的要怎么弄呀,Python是下载的3.10版本的
  46. 請問這個過期的要怎麼弄呀,Python是下載的3.10版本的
  47. Qu'est - ce qu'on fait de ce vieux python?
  48. Python数据处理转置问题??
  49. Problèmes de transposition du traitement des données python??
  50. 使用python的pdfkit把html转pdf时,echarts无法显示
  51. python利用3维的0,1矩阵再matplotlib中画图
  52. Pycham developing Django project template tutorial
  53. Libraries commonly used in Python data analysis
  54. Python utilise une matrice tridimensionnelle 0, 1 pour redessiner les graphiques dans matplotlib
  55. 预备知识-python核心用法常用数据分析库(上)
  56. 預備知識-python核心用法常用數據分析庫(上)
  57. 我在python上创建了个web静态服务器,把一个前端页面,放进去然后在静态服务器上访问只出现了前段页面的代码,代码运行正常,这是怎么回事
  58. Preliminary Knowledge - Python Core use Common Data Analysis Library (ⅰ)
  59. 预备知识-python核心用法常用数据分析库(上)
  60. J'a i créé un serveur Web statique sur Python, j'ai mis une page frontale, j'ai accédé au Code sur le serveur statique qui n'avait que des pages frontales, et le Code fonctionnait correctement