LedBag¶
介绍¶
可编程书包(也兼容可编程相框、音响...)
demo¶
hello world¶
from codelab_adapter.led_bag import LedBag
bag = LedBag()
bag.connect() # 默认连接 jupyterlab 模拟器,输入书包id可连接到书包: bag.connect(DEVICE_ID)
# 点亮第一个led
bag.set_pixel(0, 0, 'red') # bag.set_pixel(0, 0, (255,0,0))
LedBag API¶
可以在 Adapter notebooks 里的 hello_LedBag.ipynb
做实验
控制像素¶
color = (255, 0, 0)
# 设置像素
bag.set_pixel(0, 0, color)
# 查询像素, renturn RGB tuple
bag.get_pixel(0, 0)
清理画布¶
bag.clear()
show¶
bag.show()
全屏颜色¶
color = (255, 0, 0) # red (RGB), 通过调整 RGB 的数值调整亮度
bag.set_color(color)
显示字符¶
# 显示 emoji 字符
bag.display_emoji('🐳')
# 英文字符
bag.display_text('hello world')
# 中文字符串
bag.display_text_zh('早上好', color=(255,0,0))
# 显示中文字符
bag.display_char_zh('早', color=(255,0,0))
硬件特殊功能¶
# 内置效果
bag.show_visualization(1) # 参数: 特效 ID
# 记分板
bag.show_scoreboard(3, 2)
# 显示时钟
bag.show_clock()
# show design
bag.show_design()
保存、加载图片¶
# 保存当前图像
bag.save(name="test")
# 加载图像(自动转为16x16格式)
bag.load('test.png')
断开蓝牙¶
bag.close()
动画(Animation) API¶
from codelab_adapter.led_bag import Animation
animation = Animation()
# bag set pixel ...
animation.add_frame(bag) # 把 bag 当前状态(bag._img)作为 animation 的一帧
# bag set pixel ...
animation.add_frame(bag)
# 在模拟器里展示动画
animation.show(duration=0.1)
# 将动画同步到书包
animation.show(to_bag=bag)
# 保存为gif
animation.save(name='hello')
# 加载 gif, 建议使用手机 APP 设计动图。 也可参看动图网站 https://giphy.com/,或者试试表情包
animation.load('./hello.gif')
# 查看、修改某一帧
animation.show_frame(FRAME_ID)
animation.remove_frame(FRAME_ID)
积木说明¶
需要先下载插件: node_ledbag.py
进阶¶
游戏平台¶
与像素交互
# import pynput
import time
from codelab_adapter.led_bag import LedBag
import ipywidgets as widgets
from IPython.display import display
bag = LedBag() #
bag.connect()
block_location = (0, 0)
button_up = widgets.Button(description="上")
button_down = widgets.Button(description="下")
button_left = widgets.Button(description="左")
button_right = widgets.Button(description="右")
output = widgets.Output()
vbox = widgets.VBox([button_up, button_down])
hbox = widgets.HBox([button_left, vbox, button_right])
display(hbox, output)
def move(direction):
global block_location
bag.clear()
if direction == "上":
x, y = block_location
block_location = (max(x-1, 0), y)
bag.set_pixel(block_location[0], block_location[1], 'red')
if direction == "下":
x, y = block_location
block_location = (min(x+1, 15), y)
bag.set_pixel(block_location[0], block_location[1], 'red')
if direction == "左":
x, y = block_location
block_location = (x, max(y-1, 0))
bag.set_pixel(block_location[0], block_location[1], 'red')
if direction == "右":
x, y = block_location
block_location = (x, min(y+1, 15))
bag.set_pixel(block_location[0], block_location[1], 'red')
def on_button_clicked(b):
global block_location
with output:
move(b.description)
button_up.on_click(on_button_clicked)
button_down.on_click(on_button_clicked)
button_left.on_click(on_button_clicked)
button_right.on_click(on_button_clicked)
连接游戏手柄: ...
FAQ¶
发现设备¶
import nest_asyncio
nest_asyncio.apply()
import bluetooth
result = bluetooth.discover_devices(lookup_names=True)
print(result)
OverflowError¶
提醒
4.9.6 以上版本没有这个问题
一张图片最多包含 62 种颜色。
如果你想让每个像素随机变化,可使用以下代码:
import random
from codelab_adapter.led_bag import LedBag
bag = LedBag()
bag.connect('xxx')
bag.clear()
color_set = set() # 颜色集合(不重复)
while len(color_set) <= 62:
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
color = (r, g, b)
if color not in color_set:
color_set.add(color)
for i in range(16):
for j in range(16):
bag.set_pixel(i,j, random.choice(list(color_set)))