豌豆派/Pi¶
拥有若干器件。
显示内容¶
Pi.
在某个位置显示内容,位置坐标以像素点为单位
参数:
- row (int) – 显示行数:1~64
- column (int) – 显示列数:1~128
- *text (str) – 显示内容,可以是字符串,整数,小数
Examples
# 显示各种不同内容 from wonderbits import Pi # 在第1行第1列显示字符串 'Hello' Pi.print(1, 1, 'Hello') # 在第1行第72列显示浮点型数字 -3.14 Pi.print(1, 72, -3.14) # 在第17行第1列显示变量 temp = 10 temp = temp + 3 Pi.print(17, 1, temp)
画折线图¶
Pi.
draw_chart
(x, y=0)¶ 传入(x,y)坐标以上次传入的坐标为起点,本次坐标为终点画线段。如果是首次使用,则只画单个点传入列表则以列表中的值为纵坐标,自动计算横坐标将折线图展示在屏幕上
参数:
- x (int) – X轴坐标:1~128
- y (int) – Y轴坐标:1~64
Examples
# 根据坐标画一组折线 from wonderbits import Pi import time # 定义一堆点坐标 dots =[(1,40),(10,60),(20,10),(30,80),(50,1)] # 将所有点取出来画折线 for dot in dots: Pi.draw_chart(dot[0],dot[1]) time.sleep(2) Pi.clear() # 定义随机纵坐标列表 from random import randint l = [randint(1,64) for i in range(30)] Pi.draw_chart(l)
在指定坐标画一个点¶
Pi.
draw_dot
(x, y)¶
参数:
- x (int) – X轴坐标:1~128
- y (int) – Y轴坐标:1~64
Examples
# 在屏幕上画一条直线(y=x/2)上的点集合 from wonderbits import Pi for x in range(1, 128): Pi.draw_dot(x, x/2)
通过给定坐标画线段¶
Pi.
draw_line
(head_x, head_y, tail_x, tail_y)¶ 在画线的页使用print函数会导致已经画过的线消失切换到不同的页码在回到画线的页码也会导致已经画过的线消失
参数:
- head_x (int) – 起始点X轴坐标:1~128
- head_y (int) – 起始点Y轴坐标:1~64
- tail_x (int) – 终止点X轴坐标:1~128
- tail_y (int) – 终止点Y轴坐标:1~64
Examples
# 在屏幕上画两条线,将屏幕分为四份 from wonderbits import Pi # 在第1页以 (1, 32) 为起点, (128, 32) 为终点画一条直线 Pi.draw_line(1, 32, 128, 32) # 在第1页以 (64, 1) 为起点, (64, 64) 为终点画一条直线 Pi.draw_line(64, 1, 64, 64)
清除全部显示内容¶
Pi.
clear
(row=None)¶Examples
# 清除所有显示内容 from wonderbits import Pi import time # 在清除之前显示一些文字 Pi.print(1,1,'somthing1...') Pi.print(17,1,'somthing2...') Pi.print(33,1,'somthing3...') #延时等待 time.sleep(2) # 清除显示内容 Pi.clear() Pi.print(49,1,'clear') # 观察实验效果你会发现只剩'clear',因为之前的内容被清除了
设置rgb颜色¶
Pi.
set_rgb
(index, r, g, b)¶
参数:
- index (int) –
选择灯的序号:0~3
0表示控制全部灯- r (int) – 红色:0~255
- g (int) – 绿色:0~255
- b (int) – 蓝色:0~255
Examples
# 设置彩灯在红绿蓝之间变换5次,每个颜色持续0.5秒。最后关闭LED from wonderbits import Pi import time for i in range(5): Pi.set_rgb(1, 10, 0, 0) Pi.set_rgb(2, 0, 10, 0) Pi.set_rgb(3, 0, 0, 10) time.sleep(0.5) Pi.set_rgb(1, 0, 10, 0) Pi.set_rgb(2, 0, 0, 10) Pi.set_rgb(3, 10, 0, 0) time.sleep(0.5) Pi.set_rgb(1, 0, 0, 10) Pi.set_rgb(2, 10, 0, 0) Pi.set_rgb(3, 0, 10, 0) time.sleep(0.5) Pi.set_rgb(1, 0, 0, 0) Pi.set_rgb(2, 0, 0, 0) Pi.set_rgb(3, 0, 0, 0)
设置蜂鸣器(Hz)¶
Pi.
set_buzzer
(frequency)¶ 设置蜂鸣器声音频率(Hz),小于20HZ不会发出声音设置频率为0~19表示关闭蜂鸣器
参数: frequency (int) – 频率:0~20000 Hz Examples
# 设置蜂鸣器音调越来越高 from wonderbits import Pi for f in range(20000): Pi.set_buzzer(f)
播放一个音调¶
Pi.
play_a_note
(frequency, time, block=True)¶ 控制蜂鸣器发出一个音调,并持续一段时间
参数:
- frequency (int) – 频率:20~20000 Hz
- time (float) – 时间: 0.05~60 s
- block (bool) –
阻塞参数:
False: 不阻塞True: 阻塞默认为TrueExamples
# 演奏一首小星星,我不会.... 那就音调越来越高吧 from wonderbits import Pi Pi.play_a_note(800, 0.5) Pi.play_a_note(900, 0.5) Pi.play_a_note(1000, 0.5) Pi.play_a_note(1100, 0.5) Pi.play_a_note(1200, 0.5) Pi.play_a_note(1300, 0.5) Pi.play_a_note(0, 0.5)
按键A是否按下¶
Pi.
is_a_pressed
()¶ 判断按键A是否被按下
返回: True: 按键被按下False: 按键没有被按下返回类型: bool Examples
# 测试按键A和按键B状态,实时显示 from wonderbits import Pi # 在显示屏上实时显示SW1和SW2的状态 while True: Pi.print(1, 1, Pi.is_a_pressed() ) Pi.print(17, 1, Pi.is_b_pressed() )
按键B是否按下¶
Pi.
is_b_pressed
()¶ 判断按键B是否被按下
返回: True: 按键被按下False: 按键没有被按下返回类型: bool Examples
未提供。可以参考 is_a_pressed 的使用案例
获取触点状态¶
Pi.
is_touched
(pad=None)¶ 获取某通道是否被触摸不填写通道参数可以支持检测多个通道,无通道被触摸返回False,有通道被触摸返回以该通道字母组成的字符串
参数: pad (str) – 通道号可以为字符’python’中的任意字母,也可以是大写字母
或者为数字1~6,也可以为数字的字符形式请参考具体例程返回: True: 该通道被触摸False: 该通道没有被触摸返回类型: bool Examples
# 实时显示触摸状态 from wonderbits import Pi while True: Pi.print(1, 1, Pi.is_touched(1)) Pi.print(17, 1, Pi.is_touched('y')) Pi.print(33, 1, Pi.is_touched('T')) Pi.print(49, 1, Pi.is_touched())
获取声音强度值¶
Pi.
get_volume
()¶ 声音强度值代表相对强度,值越大代表声音越响
返回: 声音强度值,范围 0~100 返回类型: float Examples
# 显示音量和亮度 from wonderbits import Pi while True: Pi.print(1, 1, Pi.get_volume()) Pi.print(17, 1, Pi.get_light('A')) Pi.print(33, 1, Pi.get_light('B'))
获取亮度值¶
Pi.
get_light
(index='A')¶ 亮度值代表相对强度,值越大代表亮度越强
参数: index (str) – 选择光敏传感器,只有 ‘A’,’B’ 两个参数 返回: 亮度值,范围 0~100 返回类型: int Examples
未提供。可参考 get_volume 的使用案例
获取温度值¶
Pi.
get_temperature
()¶ 获取温度值(°C)
返回: 温度值,范围 -20~100°C 返回类型: int Examples
# 显示温度、湿度和气压值 from wonderbits import Pi while True: Pi.print(1, 1, Pi.get_temperature()) Pi.print(17, 1, Pi.get_humidity()) Pi.print(33, 1, Pi.get_air_pressure())
获取湿度值¶
Pi.
get_humidity
()¶ 获取湿度值(%RH)
返回: 湿度值,范围 0~100%RH 返回类型: int Examples
未提供。可参考 get_temperature 的使用案例
获取气压值¶
Pi.
get_air_pressure
()¶ 获取气压值(Pa)
返回: 湿度值,范围 0~100%RH 返回类型: int Examples
未提供。可参考 get_temperature 的使用案例
获取倾斜角¶
Pi.
get_angle
(axis='X')¶ 获取某轴倾斜角,单位 °
参数: axis (str) – 填写’x’,’y’可以返回对应的1倾斜角度 返回: 对应轴的倾斜角度,范围 -180~180 ° 返回类型: float Examples
# 倾斜角显示案例 from wonderbits import Pi # 在显示屏上实时显示x轴加速度值 while True: Pi.print(1, 1, Pi.get_angle('x')) Pi.print(17, 1, Pi.get_angle('y'))
获取加速度值¶
Pi.
get_acceleration
(axis=None)¶ 获取某轴加速度值,单位 g
参数: axis (str) – 填写’x’,’y’,’z’可以返回对应的加速度值
不填写返回合加速度值返回: 对应的速度值,范围 -2~2 g 返回类型: float Examples
# 加速度值显示案例 from wonderbits import Pi # 在显示屏上实时显示加速度值 while True: Pi.print(1, 1, Pi.get_acceleration()) Pi.print(17, 1, Pi.get_acceleration('x')) Pi.print(33, 1, Pi.get_acceleration('y')) Pi.print(49, 1, Pi.get_acceleration('z'))
开启射频¶
Pi.
radio_on
(channel=1)¶ 可填写参数设置通信信道,默认为1
参数: channel (int) – 信道参数: 1~13,默认为1 Examples
未提供。可参考 radio_send 的使用案例
发送字符串¶
Pi.
radio_send
(s)¶ 设置为相同信道的豌豆派会接收到字符串
参数: s (str) – 需要发送的字符串 Examples
# 发送示例 from wonderbits import Pi Pi.radio_on(2) # 在显示屏上显示发送的字符串内容,并发送字符串 i = 0 while True: send_str = 'send {}'.format(i) Pi.print(1, 1, send_str) Pi.radio_send(send_str) i = i + 1
接收字符串¶
Pi.
radio_receive
()¶ 接收设置为相同信道的豌豆派发送的字符串若是没有收到新的字符串则会返回空字符串
返回: 接受到的字符串 返回类型: str Examples
# 接收示例 from wonderbits import Pi Pi.radio_on(2) # 在显示屏上显示接收到的字符串内容 while True: rec = Pi.radio_receive() if rec: Pi.print(1, 1, rec)
开启蓝牙¶
Pi.
ble_on
(name=None, hide=False)¶ 可填写参数设置蓝牙名字,默认为’Pi-mfe’手机端连接蓝牙的示例APP请参考:蓝牙app。
参数:
- name (str) – 蓝牙名字: 不填写该参数则从配置信息中查找蓝牙名字
- hide (bool) –
True: 隐藏屏幕显示
False: 开启屏幕显示默认为FalseExamples
未提供。可参考 ble_receive 的使用案例
查询蓝牙连接状态¶
通过蓝牙发送字符串¶
Pi.
ble_send
(s)¶ 已连接豌豆派蓝牙的手机客户端会收到消息。
参数: s (str) – 需要发送的字符串 Examples
# 发送接收示例 from wonderbits import Pi import time #开启蓝牙,默认名字为 Pi-mfe Pi.ble_on() # 打开蓝牙需要10秒左右 # 在显示屏第一行显示收到的字符串内容 # 在显示屏第二行显示发送的字符串内容,并发送字符串 i = 0 while True: if Pi.ble_is_connected(): rec_str = Pi.ble_receive() if rec_str: Pi.print(1, 1, rec_str) send_str = 'send {}'.format(i) Pi.print(17, 1, send_str) Pi.ble_send(send_str) i = i + 1 time.sleep(1)
接收已连接的蓝牙发送来的信息¶
Pi.
ble_receive
()¶ 已连接豌豆派蓝牙的手机客户端会收到消息。
返回: 接受到的字符串 返回类型: str Examples
# 接收回传示例 from wonderbits import Pi #开启蓝牙,默认名字为 Pi-mfe Pi.ble_on() # 打开蓝牙需要10秒左右 # 在显示屏第一行显示收到的字符串内容,并将其发送回去 i = 0 while True: if Pi.ble_is_connected(): rec_str= Pi.ble_receive() if rec_str: Pi.print(1, 1, rec_str) Pi.ble_send(rec_str)
关闭蓝牙¶
Pi.
ble_off
()¶Examples
# 关闭蓝牙示例 from wonderbits import Pi import time #开启蓝牙,名字为 123456 Pi.ble_on('123456') # 这段时间可以在手机APP上搜索到蓝牙 time.sleep(20) # 关闭蓝牙之后将不能搜索到蓝牙 Pi.ble_off()
开启wifi并连接网络¶
Pi.
wifi_connect
(ssid=None, password=None, hide=False)¶
参数:
- ssid (str) – wifi名称:不填写该参数则从配置信息中查找wifi名字和密码
- password (str) – WiFi密码:与wifi名称一起填写或不填
- hide (bool) –
True: 隐藏屏幕显示
False: 开启屏幕显示默认为FalseExamples
# 连接 wifi示例 from wonderbits import Pi import time #连接wifi Pi.wifi_connect('wifi_name', '12345678') # 等待wifi连接成功 while not Pi.wifi_is_connected(): pass # 连接wifi后,打印IP信息 print(Pi.wifi_ifconfig()) # 断开wifi连接 Pi.wifi_disconnect()
断开wifi连接¶
Pi.
wifi_disconnect
()¶Examples
未提供。可参考 wifi_connect 的使用案例
查询wifi连接状态¶
Pi.
wifi_is_connected
()¶
返回: True: wifi已连接False: wifi未连接返回类型: bool Examples
未提供。可参考 wifi_connect 的使用案例
查询IP详情¶
Pi.
wifi_ifconfig
()¶
返回: 如果是未连接状态返回空列表 返回类型: list Examples
未提供。可参考 wifi_connect 的使用案例
获取板载信息¶
Pi.
info_get
(k=None)¶ 可以通过参数获取指定信息,如:’name’,’wifi_ssid’,’wifi_password’等不填写参数返回全部信息
参数: k (str) – 获取指定信息
可以不填写返回: 返回信息,大多数情况为字符串,类型由信息类型决定 返回类型: str Examples
from wonderbits import Pi # 读取板载信息 print(Pi.info_get()) print(Pi.info_get('name')) # 设置板载信息中的'name' Pi.info_set('name', 'wonderbits') # 读取板载信息 print(Pi.info_get()) print(Pi.info_get('name'))