parrot — mPython Expansion Board driver

../../_images/extboard_back.png

PARROT, the mPython Expansion Board is derived from the mPython Board, which is compact and portable. The IO pin expansion board supporting motor drive, voice playback, voice synthesis and other functions can expand 12 IO interfaces and 2 I2C interfaces. The library provides motor drive, LED drive and other functions for controlling the expansion board.

Motor control I2C communication protocol data format:

Type Command motor_no speed(int)
Motor 0x01 0x01/0x02 -100~100

When ‘speed’ is negative, reverse; when positive, forward.


parrot.MOTOR_1

M1电机编号,0x01

parrot.MOTOR_2

M2电机编号,0x02

parrot.set_speed(motor_no, speed)[source]

设置电机速度

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

返回电机速度

Parameters:motor_no (int) – 控制电机编号,可以使用 MOTOR_1, MOTOR_2,或者直接写入电机编号。
Return type:int
Returns:返回该电机速度
parrot.led_on(no, brightness=50)[source]

打开灯。电机接口,除了可以驱动电机,还能做灯的控制。

Parameters:
  • no (int) – 控制电机编号,可以使用 MOTOR_1, MOTOR_2,或者直接写入电机编号。
  • brightness (int) – 设置亮度,范围0~100
parrot.led_off(no)[source]

关闭灯。

Parameters:no (int) – 控制电机编号,可以使用 MOTOR_1, MOTOR_2,或者直接写入电机编号。
parrot.get_battery_level()[source]

获取拓展板的电压值。

Returns:返回电量,单位mV。
class parrot.IR_encode[source]

红外编码类

encode_raw(carry_freq, len, repeat_pos, code, data)[source]

制作任意编码。

Parameters:
  • carry_freq (int) – 载波频率,单位hz。
  • len (int) – 加上循环码后的单次发码的code总数
  • repeat_pos (int) – 循环码位置
  • code (int) – code列表,16个成员,记录8组不同的高低电平波形
  • data (char) – 编码波形数据,最长64字节
Returns:

返回编码后的红外脉冲数据buff

encode_nec(user_code, command_code)[source]

NEC编码。

Parameters:
  • user_code (int) – 用户码
  • command_code (int) – 命令码
Returns:

返回编码后的红外脉冲数据buff

class parrot.IR[source]

红外收发管的抽象类,用于红外发射和学习。

send(buff, repeat_en=0)[source]

发送红外脉冲数据buff

Parameters:
  • buff (bytes) – 可以是 IR_encode.encode_nec 返回的buff。或,学习后的buff。
  • repeat_en (init) – 重复码。用于表示按键常按。当repeat_en为1时,会不断发送重复码,直至 stop_send 停止发送。
stop_send()[source]

停止发送,当发送重复码后,使用该函数来停止发送。

learn(wait=True)[source]

红外学习。默认下wait为True,此时为阻塞函数。learn()开始后,须在5秒内,常按住被学习对象按键。当学习完成后,会返回学习结果,成功True,失败Fail。wait 为 False,则为非阻塞,此时不返回结果。

Parameters:wait (bool) – 是否阻塞。
Returns:bool类型,返回结果
get_learn_data()[source]

返回学习后的,红外编码数据。该函数须在 learn 函数后使用。

Returns:返回编码后的红外脉冲数据buff
Motor drive example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import parrot
from mpython import sleep_ms,sleep

# 可设置速度范围为-100到100。
# 当值为正值时,电机正转;为负值时,电机反转;


while True:

    # 设置正转速度100
    parrot.set_speed(parrot.MOTOR_1,100)   
    parrot.set_speed(parrot.MOTOR_2,100)   
    print("current motor speend: %d,%d" %(parrot.get_speed(parrot.MOTOR_1),parrot.get_speed(parrot.MOTOR_2)))    #获取电机速度
    sleep(5)
    # 设置反转速度100
    parrot.set_speed(parrot.MOTOR_1,-100)   
    parrot.set_speed(parrot.MOTOR_2,-100)   
    print("current motor speend: %d,%d" %(parrot.get_speed(parrot.MOTOR_1),parrot.get_speed(parrot.MOTOR_2)))    #获取电机速度
    sleep(5)
    # 停止  
    parrot.set_speed(parrot.MOTOR_1,0)   
    parrot.set_speed(parrot.MOTOR_2,0)   
    print("current motor speend: %d,%d" %(parrot.get_speed(parrot.MOTOR_1),parrot.get_speed(parrot.MOTOR_2)))    #获取电机速度
    sleep(2)
NEC code Infrared code example
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import parrot
import time

ir_code = parrot.IR_encode()

ir = parrot.IR()
ir_buff = ir_code.encode_nec(1, 85)
while True:
    ir.send(ir_buff, 1)
    time.sleep(3)
    ir.stop_send()
    time.sleep(3)
NEC code Infrared learning example
 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
from mpython import *
import parrot
import time

def on_button_a_pressed(_):
    global data
    ir.learn()
    time.sleep(4)
    if 0 == ir.__get_learn_status():
        data = ir.get_learn_data()
        print(data)
    else:
        print('什么都没学到...')

button_a.event_pressed = on_button_a_pressed

def on_button_b_pressed(_):
    global data
    print(data)
    ir.send(data, 0)

button_b.event_pressed = on_button_b_pressed

ir = parrot.IR()
data = None