python for linkiot - readthedocs.org
TRANSCRIPT
![Page 1: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/1.jpg)
Python for LinkIoTRelease 1.0.0
Nov 27, 2019
![Page 2: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/2.jpg)
![Page 3: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/3.jpg)
API and Usage
1 LinkIoT 简介 1
2 安装 MicroPython 3
3 安装 Mu 编辑器 17
4 LinkIoT 模块简介 19
5 可编程 Led 模块 21
6 可编程 Button 模块 23
7 可编程加速度传感器模块 25
8 可编程 Lcd 模块 27
i
![Page 4: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/4.jpg)
ii
![Page 5: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/5.jpg)
CHAPTER 1
LinkIoT 简介
1
![Page 6: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/6.jpg)
Python for LinkIoT, Release 1.0.0
2 Chapter 1. LinkIoT 简介
![Page 7: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/7.jpg)
CHAPTER 2
安装 MicroPython
2.1 安装 HBFlash
LinkIoT 支持 Arduino、Scratch 和 MicroPython 编程。当我们需要使用 LinkIoT 进行 MicroPython 编程或者某些特殊情况下(如固件升级、固件异常等情况)需要重新更新 LinkIoT 的 Python 固件时,就需要对LinkIoT 重新写入固件。那么就可以使用 HBFlash 应用程序方便快捷的刷入最新 LinkIoT 官方固件。
Note: HBFlash 软件每次打开都会下载最新官方 LinkIoT 的 MicroPython 固件。
2.1.1 HBFlash Windows 版本
HBFlash Windows 点击 HBFlash.exe 下载。
2.1.2 HBFlash MacOS 版本
HBFlash MacOS 点击 HBFlash.pkg 下载,运行 HBFlash.pkg 进行安装。
MacOS 系统提示无法打开 HBFlash.pkg
当 MacOS 系统提示无法打开“HBFlash.pkg”,因为它来自身份不明的开发者。如下图所示,需要修改系统设置打开 HBFlash.pkg 进行安装。
3
![Page 8: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/8.jpg)
Python for LinkIoT, Release 1.0.0
打开系统偏好设置-安全与隐私-通用界面,选择“仍要打开”
4 Chapter 2. 安装 MicroPython
![Page 9: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/9.jpg)
Python for LinkIoT, Release 1.0.0
2.1. 安装 HBFlash 5
![Page 10: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/10.jpg)
Python for LinkIoT, Release 1.0.0
安装 HBFlash.pkg
6 Chapter 2. 安装 MicroPython
![Page 11: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/11.jpg)
Python for LinkIoT, Release 1.0.0
2.1. 安装 HBFlash 7
![Page 12: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/12.jpg)
Python for LinkIoT, Release 1.0.0
8 Chapter 2. 安装 MicroPython
![Page 13: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/13.jpg)
Python for LinkIoT, Release 1.0.0
2.1. 安装 HBFlash 9
![Page 14: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/14.jpg)
Python for LinkIoT, Release 1.0.0
2.2 刷写 LinkIoT MicroPython 固件
使用 HBFlash 对 LinkIoT 刷写所支持的 MicroPython 固件。将 HBLinkIoT 通过 USB 线插入电脑,打开HBFlash 应用程序,选择对应的 USB 串口,进行下载刷入固件。
10 Chapter 2. 安装 MicroPython
![Page 15: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/15.jpg)
Python for LinkIoT, Release 1.0.0
2.2. 刷写 LinkIoT MicroPython 固件 11
![Page 16: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/16.jpg)
Python for LinkIoT, Release 1.0.0
12 Chapter 2. 安装 MicroPython
![Page 17: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/17.jpg)
Python for LinkIoT, Release 1.0.0
2.2. 刷写 LinkIoT MicroPython 固件 13
![Page 18: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/18.jpg)
Python for LinkIoT, Release 1.0.0
14 Chapter 2. 安装 MicroPython
![Page 19: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/19.jpg)
Python for LinkIoT, Release 1.0.0
2.2. 刷写 LinkIoT MicroPython 固件 15
![Page 20: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/20.jpg)
Python for LinkIoT, Release 1.0.0
16 Chapter 2. 安装 MicroPython
![Page 21: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/21.jpg)
CHAPTER 3
安装 Mu 编辑器
17
![Page 22: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/22.jpg)
Python for LinkIoT, Release 1.0.0
18 Chapter 3. 安装 Mu 编辑器
![Page 23: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/23.jpg)
CHAPTER 4
LinkIoT 模块简介
LinkIoT 支持 LCD、姿态传感器、Button 和 Led 控制。
Import LinkIoT:
1 from linkiot import *
19
![Page 24: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/24.jpg)
Python for LinkIoT, Release 1.0.0
20 Chapter 4. LinkIoT 模块简介
![Page 25: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/25.jpg)
CHAPTER 5
可编程 Led 模块
LinkIoT 具有一颗可编程控制 Led 灯,引用 LinkIoT 模块即可直接控制使用。
5.1 控制 Led 亮/灭
linkiot.setLed(value) 参数 value 是布尔型,若 value 为 True,则 Led 灯亮,否则 Led 灭。
示例 1:
1 import time2 from linkiot import *3
4 ledStatus = False5
6 while True:7 linkiot.setLed(ledStatus)8 ledStatus = not ledStatus9 utime.sleep_ms(500)
5.2 控制 Led 灯亮度
linkiot.setLedBrightness(value) 参数 value 是数字类型,可设置 0~100。
21
![Page 26: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/26.jpg)
Python for LinkIoT, Release 1.0.0
示例 2:
1 import time2 from linkiot import *3
4 ledBrightness = 05
6 while True:7 if ledBrightness > 100:8 ledBrightness = 09
10 linkiot.setLedBrightness(ledBrightness)11 ledBrightness += 1012 utime.sleep_ms(100)
22 Chapter 5. 可编程 Led 模块
![Page 27: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/27.jpg)
CHAPTER 6
可编程 Button 模块
LinkIoT 具有可编程按钮 button。
6.1 按钮按下状态
linkiot.button.wasPressed() 方法返回按钮当前是否按下
linkiot.button.wasReleased() 方法返回按钮是否释放状态即未按下状态
示例 1:
1 import time2 from linkiot import *3
4 while True:5 if linkiot.button.wasPressed():6 print("Button was pressed")7
8 if linkiot.button.wasReleased():9 print("Button was Released")
10
11 utime.sleep(0.1)
23
![Page 28: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/28.jpg)
Python for LinkIoT, Release 1.0.0
6.2 按钮回调方法
示例 2:
1 import time2 from linkiot import *3
4 def on_wasPressed():5 print("Button was pressed")6
7 def on_wasReleased():8 print("Button was Released")9
10 linkiot.button.wasPressed(on_wasPressed)11 linkiot.button.wasReleased(on_wasReleased)
24 Chapter 6. 可编程 Button 模块
![Page 29: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/29.jpg)
CHAPTER 7
可编程加速度传感器模块
LinkIoT 具有加速度传感器,可以获取姿态 X 、Y、Z 方向的数值和检测是否摇晃状态。
7.1 更新加速度传感器
linkiot.updateAttitude() updateAttitude() 方法更新加速度传感器数值,再获取传感器的数值和状态前,必须先使用该方法,才能获取当前传感器实际数值。
7.2 X Y Z 方向加速度数值
linkiot.accX accX 属性返回当前 X 方向加速度数值
linkiot.accY accY 属性返回当前 Y 方向加速度数值
linkiot.accZ accZ 属性返回当前 Z 方向加速度数值
示例 1:
1 import time2 from linkiot import *3
4 while True:5 linkiot.updateAttitude()6 print("x = " + str(linkiot.accX))
(continues on next page)
25
![Page 30: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/30.jpg)
Python for LinkIoT, Release 1.0.0
(continued from previous page)
7 print("y = " + str(linkiot.accY))8 print("z = " + str(linkiot.accZ))9 utime.sleep(0.3)
7.3 俯仰角(Pitch angle)和翻滚角(Roll angle)
linkiot.anglePitch anglePitch 属性返回俯仰角角度
linkiot.angleRoll angleRoll 属性返回翻滚角角度
示例 2:
1 import time2 from linkiot import *3
4 while True:5 linkiot.updateAttitude()6 print("Pitch angle = " + str(linkiot.anglePitch))7 print("Roll angle = " + str(linkiot.angleRoll))8 utime.sleep(0.2)
7.4 振动状态
linkiot.wasShaked wasShaked 属性返回当前是否振动
示例 3:
1 import time2 from linkiot import *3
4 while True:5 linkiot.updateAttitude()6 if linkiot.wasShaked:7 print("Shaked")8 utime.sleep(0.1)
26 Chapter 7. 可编程加速度传感器模块
![Page 31: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/31.jpg)
CHAPTER 8
可编程 Lcd 模块
LinkIoT 具有可编程 Lcd,可实现画点、线、矩形、圆等基本图像。
8.1 Colors
在 Lcd 编程中会使用颜色,可以使用 24 位整型数值,每种颜色 8 位。例如,0xFF0000 就是红色,0xFF00就是绿色。
定义了一些颜色常量供快捷选择使用:
27
![Page 32: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/32.jpg)
Python for LinkIoT, Release 1.0.0
常量 颜色 数值
TFT_BLACK 黑色 0TFT_NAVY 海蓝色 128TFT_BLUE 蓝色 255TFT_DARKGREEN 深绿色 32768TFT_DARKCYAN 深青色 32896TFT_GREEN 绿色 62580TFT_CYAN 青色 65535TFT_MAROON 栗色 8388608TFT_PURPLE 紫色 8388736TFT_OLIVE 橄榄绿 8421376TFT_DARKGREY 深灰色 8421504TFT_GREENYELLOW 黄绿色 11336748TFT_LIGHTGREY 亮灰色 12632256TFT_RED 红色 16515072TFT_MAGENTA 洋红色 16515327TFT_ORANGE 橙色 16557056TFT_PINK 粉红色 16564426TFT_YELLOW 黄色 16579584TFT_WHITE 白色 16579836
8.2 Fonts
字体常量: TFT_FONT_Default, TFT_DEJAVU18_FONT, TFT_DEJAVU24_FONT,TFT_UBUNTU16_FONT, TFT_COMIC24_FONT, TFT_MINYA24_FONT,TFT_TOONEY32_FONT, TFT_SMALL_FONT, TFT_DEF_SMALL_FONT,TFT_FONT_7SEG, TFT_USER_FONT
8.3 Lcd Methods
8.3.1 画像素点
linkiot.Lcd.pixel(x, y [,color])
设置坐标点 (x, y) 的像素颜色,颜色参数 color 为颜色值,选填参数。
8.3.2 画直线段
linkiot.Lcd.line(x, y, x1, y1 [, color])
28 Chapter 8. 可编程 Lcd 模块
![Page 33: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/33.jpg)
Python for LinkIoT, Release 1.0.0
画坐标点 (x, y) 到坐标点 (x1, y1) 的直线,颜色参数 color 为线条颜色值,选填参数。
8.3.3 画三角形
linkiot.Lcd.triangle(x, y, x1, y1, x2, y2 [,color,fillcolor])
画坐标 (x, y), (x1, y1) 和 (x2, y2) 三点相连的三角形,颜色参数 color 和 fillcolor 选填,分别为三角形颜色和填充颜色。
8.3.4 画矩形
linkiot.Lcd.rect(x, y, width, height [,color, fillcolor])
以坐标 (x, y) 为左上角顶点,width 作为宽,height 作为高,画矩形,颜色参数 color 和 fillcolor 选填,分别为矩形颜色和填充颜色。
8.3.5 画圆形
linkiot.Lcd.circle(x, y, r [, color, fillcolor])
以坐标 (x, y) 为圆心,r 为圆半径画圆,颜色参数 color 和 fillcolor 选填,分别为圆形的颜色和填充颜色。
8.3.6 画椭圆形
linkiot.Lcd.ellipse(x, y, rx, ry [,color, fillcolor])
以坐标 (x, y) 为圆心,x 方向半径为 rx,y 方向半径为 ry 画椭圆,颜色参数 color 和 fillcolor 选填,分别为椭圆颜色和填充颜色。
8.3.7 显示文本
linkiot.Lcd.text(x, y, text [, color])
显示屏输入文本显示,以 (x, y) 坐标为文本起点,text 为文本内容,颜色参数 color 选填,为文本颜色。
8.3.8 设置字体
linkiot.Lcd.font(font)
设置 linkiot 显示屏文本字体,参数 font 选用 Fonts 字体常量。
8.3. Lcd Methods 29
![Page 34: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/34.jpg)
Python for LinkIoT, Release 1.0.0
8.3.9 设置背景色
linkiot.Lcd.setbg(color)
设置显示屏背景颜色。
8.3.10 清屏
linkiot.Lcd.clear()
清空显示屏显示内容
8.3.11 设置屏幕方向
linkiot.Lcd.setRotation(value)
设置屏幕显示方向,使用默认常量设置。方向常量:TFT_PORTRAIT ,TFT_LANDSCAPE,TFT_PORTRAIT_FLIP,TFT_LANDSCAPE_FLIP 。
示例 1:
1 import machine, utime2 from linkiot import *3
4
5
6 def rotateScreen():7 if linkiot.accX > 9.0:8 linkiot.Lcd.setRotation(TFT_LANDSCAPE)9 elif linkiot.accX < -9.0:
10 linkiot.Lcd.setRotation(TFT_LANDSCAPE_FLIP)11 elif linkiot.accY > 9.0:12 linkiot.Lcd.setRotation(TFT_PORTRAIT_FLIP)13 elif linkiot.accY < -9.0:14 linkiot.Lcd.setRotation(TFT_PORTRAIT)15
16 ledStatus = False17
18 def on_wasPressed():19 print("button pressed")20
21 linkiot.button.wasPressed(on_wasPressed)22
(continues on next page)
30 Chapter 8. 可编程 Lcd 模块
![Page 35: Python for LinkIoT - readthedocs.org](https://reader033.vdocuments.mx/reader033/viewer/2022041710/62539a5bf098b431cf2f809d/html5/thumbnails/35.jpg)
Python for LinkIoT, Release 1.0.0
(continued from previous page)
23 while True:24 linkiot.updateAttitude()25 rotateScreen()26
27 if linkiot.wasShaked:28 print("shaked")29
30 linkiot.Lcd.clear()31 linkiot.Lcd.text(0,0,str(linkiot.accX), TFT_GREEN)32 linkiot.Lcd.text(0,25,str(linkiot.accY), TFT_BLUE)33 linkiot.Lcd.text(0,50,str(linkiot.accZ), TFT_PURPLE)34 linkiot.Lcd.text(0,75,str(linkiot.anglePitch), TFT_RED)35 linkiot.Lcd.text(0,100,str(linkiot.angleRoll), TFT_YELLOW)36
37 linkiot.setLed(ledStatus)38 ledStatus = not ledStatus39 utime.sleep_ms(200)
8.3. Lcd Methods 31