
mPython library¶
mPython library是一个为掌控板的标准库提供各类库资源。每个模块或包都可以作为PyPI的独立分发包提供。
mPython-lib包发布在PyPI(Python Package Index)上,这是标准的Python社区包存储库:https://pypi.org/。在PyPI上,您可以搜索与mPython相关的包并阅读其他包信息。 按照惯例,所有mPython-lib包名称都以“mPython-”为前缀格式命名。
可通过以下任一方法进行安装:
将项目中的
xx.py
拷到掌控板文件系统上在掌控板REPL界面中,使用upip安装,步骤如下:
- 前置条件需要掌控板连接网络
- 导入upip模块,执行
import upip
- 执行
upip.install('mPython-xx')
yeelight¶
yeelight API¶
这是自动生成的API文档。把它作为公众的参考项目的API
-
yeelight.
discover_bulbs
(timeout=2)[源代码]¶ 发现所有局域网内的Yeelight灯泡.
参数: timeout (int) -- 等待回复需要多少秒。发现将总是要花这么长的时间, 因为它不知道当所有的灯泡都响应完毕时。 返回: 字典列表,包含网络中每个灯泡的IP,端口和功能。
-
class
yeelight.
Bulb
(ip, port=55443, effect='smooth', duration=300, auto_on=False)[源代码]¶ YeeLight的控制类.
参数: - ip (str) -- 灯泡的IP.
- port (int) -- 连接灯泡的端口号,默认55443.
- effect (str) -- 效果类型."smooth" or "sudden".
- duration (int) -- 效果的持续时间,以毫秒为单位.最小值为30.突然效果会忽略此值.
- auto_on (bool) -- 是否
ensure_on()
在每次操作之前调用以自动打开灯泡,如果它已关闭。这会在每条消息之前更新灯泡的属性, 每个命令会花费一个额外的消息。 如果您担心速率限制,请将其关闭并自行检查。get_properties()
或运行ensure_on()
-
music_mode
¶ Return whether the music mode is active.
返回类型: bool 返回: True if music mode is on, False otherwise.
-
last_properties
¶ The last properties we've seen the bulb have.
This might potentially be out of date, as there's no background listener for the bulb's notifications. To update it, call
get_properties
.
-
get_properties
()[源代码]¶ Retrieve and return the properties of the bulb.
This method also updates
last_properties
when it is called.返回: A dictionary of param: value items. 返回类型: dict
-
bulb_type
¶ 灯泡类型
返回灯泡类型:White or Color.当尝试在属性已知之前访问时,灯泡类型是未知的。
返回: 灯泡类型.
-
send_command
(method, params=None)[源代码]¶ 请求信息并返回响应
参数: - method (str) -- control method id
- params (list) -- list of params for the specified method
返回: the command response
-
name
¶ 设置或返回设备名字 :return: 返回设备名字
-
is_on
¶ 返回灯泡是否打开 :return:打开则是'on',关闭侧'off'。
-
set_rgb
(red, green, blue)[源代码]¶ 设置灯泡的RGB值 :param int red: 红色范围 (0-255) :param int green: 绿色范围 (0-255) :param int blue: 蓝色范围 (0-255)
-
set_hsv
(hue, saturation)[源代码]¶ Set the bulb's HSV value.
参数: - hue (int) -- The hue to set (0-359).
- saturation (int) -- The saturation to set (0-100).
yeelight示例¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | from mpython import *
from yeelight import *
my_wifi = wifi() # 连接到与YeeLight相同的局域网内
my_wifi.connectWiFi("","")
discover_bulbs() # 发现局域网内YeeLight的设备信息
bulb=Bulb("192.168.0.7") # 构建Bulb类用于控制,传入IP参数
bulb.turn_on() # 开灯
sleep(2)
bulb.turn_off() # 关灯
sleep(2)
bulb.toggle() # 翻转
sleep(2)
bulb.set_rgb(255,0,0) # 设置RGB值
bulb.set_brightness(50) # 调节亮度
sleep(2)
bulb.set_hsv(180,100) # 设置HSV值
sleep(2)
|
motor¶
motor是掌控板或micropython Motor library.通过I2C通讯控制电机驱动。
电机控制通讯协议数据:
Type | Command | motor_no | speed(int) |
---|---|---|---|
控制电机 | 0x01 | 0x01/0x02 | -100~100 |
当 'speed' 为负值,反转;当为正值,正转。
motor API¶
这是自动生成的API文档。把它作为公众的参考项目的API
-
class
motor.
Motor
(i2c=<sphinx.ext.autodoc.importer._MockObject object>)[源代码]¶ I2C通讯控制驱动电机类
参数: i2c -- I2C实例对象,默认i2c=i2c -
get_speed
(motor_no)[源代码]¶ 返回电机速度
参数: motor_no (int) -- 控制电机编号,可以使用 MOTOR_1
,MOTOR_2
,或者直接写入电机编号。返回类型: int 返回: 返回该电机速度
-
MOTOR_1
= 1¶ M1电机常量,0x01
-
MOTOR_2
= 2¶ M2电机常量,0x02
-
motor示例¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | from motor import Motor
from mpython import sleep_ms,sleep
# 可设置速度范围为-100到100。
# 当值为正值时,电机正转;为负值时,电机反转;
motor=Motor()
while True:
# 设置正转速度100
motor.set_speed(motor.MOTOR_1,100)
motor.set_speed(motor.MOTOR_2,100)
print("current motor speend: %d,%d" %(motor.get_speed(motor.MOTOR_1),motor.get_speed(motor.MOTOR_2))) #获取电机速度
sleep(5)
# 设置反转速度100
motor.set_speed(motor.MOTOR_1,-100)
motor.set_speed(motor.MOTOR_2,-100)
print("current motor speend: %d,%d" %(motor.get_speed(motor.MOTOR_1),motor.get_speed(motor.MOTOR_2))) #获取电机速度
sleep(5)
# 停止
motor.set_speed(motor.MOTOR_1,0)
motor.set_speed(motor.MOTOR_2,0)
print("current motor speend: %d,%d" %(motor.get_speed(motor.MOTOR_1),motor.get_speed(motor.MOTOR_2))) #获取电机速度
sleep(2)
|