_images/掌控-立2.png

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)[source]

发现所有局域网内的Yeelight灯泡.

Parameters:timeout (int) – 等待回复需要多少秒。发现将总是要花这么长的时间, 因为它不知道当所有的灯泡都响应完毕时。
Returns:字典列表,包含网络中每个灯泡的IP,端口和功能。
class yeelight.Bulb(ip, port=55443, effect='smooth', duration=300, auto_on=False)[source]

YeeLight的控制类.

Parameters:
  • 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.

Return type:bool
Returns: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()[source]

Retrieve and return the properties of the bulb.

This method also updates last_properties when it is called.

Returns:A dictionary of param: value items.
Return type:dict
ensure_on()[source]

Turn the bulb on if it is off.

bulb_type

灯泡类型

返回灯泡类型:White or Color.当尝试在属性已知之前访问时,灯泡类型是未知的。

Returns:灯泡类型.
send_command(method, params=None)[source]

请求信息并返回响应

Parameters:
  • method (str) – control method id
  • params (list) – list of params for the specified method
Returns:

the command response

name

设置或返回设备名字 :return: 返回设备名字

is_on

返回灯泡是否打开 :return:打开则是’on’,关闭侧’off’。

turn_on()[source]

打开灯泡

turn_off()[source]

关闭灯泡

toggle()[source]

反转灯泡状态.

set_rgb(red, green, blue)[source]

设置灯泡的RGB值 :param int red: 红色范围 (0-255) :param int green: 绿色范围 (0-255) :param int blue: 蓝色范围 (0-255)

set_hsv(hue, saturation)[source]

Set the bulb’s HSV value.

Parameters:
  • hue (int) – The hue to set (0-359).
  • saturation (int) – The saturation to set (0-100).
set_color_temp(degrees)[source]

设置灯泡色温

Parameters:degrees (int) – 色温范围(1700-6500).
set_adjust(action, prop)[source]

该方法用于改变智能LED的亮度、CT或颜色,在不知道当前值的情况下.

Parameters:
  • action (str) – 调整方向,可以的值是,’increase’ : 增加指定属性;100’decrease’: 减小指定属性;’circle’: 增加指定的属性,当它达到最大值后,回到最小值.
  • prop (str) – 属性调制. 可以是 “bright” 亮度调整, “ct” 色温调整, “color” 颜色调整.
set_brightness(brightness)[source]

设置灯泡亮度

Parameters:brightness (int) – 亮度范围 (1-100).
start_flow(flow)[source]

Start a flow

Parameters:flow (yeelight.Flow) – the Flow instance to start
cron_add(event_type, value)[source]

Add an event to cron.

cron_get(event_type)[source]

Retrieve an event from cron.

cron_del(event_type)[source]

Remove an event from cron.

exception yeelight.BulbException[source]

一般的 yeelight 异常类

当灯泡通知错误时会引发此异常,例如,当尝试向灯泡发出不支持的命令时。

yeelight示例

examples/yeelight_simple.py
 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>)[source]

I2C通讯控制驱动电机类

Parameters:i2c – I2C实例对象,默认i2c=i2c
set_speed(motor_no, spend)[source]

设置电机速度

Parameters:
  • motor_no (int) – 控制电机编号,可以使用 MOTOR_1, MOTOR_2 ,或者直接写入电机编号。
  • speed (int) – 电机速度,范围-100~100,负值代表反转。
get_speed(motor_no)[source]

返回电机速度

Parameters:motor_no (int) – 控制电机编号,可以使用 MOTOR_1, MOTOR_2,或者直接写入电机编号。
Return type:int
Returns:返回该电机速度
MOTOR_1 = 1

M1电机常量,0x01

MOTOR_2 = 2

M2电机常量,0x02

motor示例

examples/motor_simple.py
 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)