1 个稳定版本

1.0.0 2021 年 8 月 22 日

#499WebAssembly

MIT 许可证

100KB
3.5K SLoC

PixelSpark 无线 LED 协议 (PWLP) 服务器

构建

常规构建

cargo build

适用于 Raspberry Pi

cargo build --target=arm-unknown-linux-musleabi --features=raspberrypi 

为了简化在 Raspberry Pi 上的构建和上传,请使用 build_pi.sh。将以下内容添加到您的 SSH 配置文件 (~/.ssh/config)

Host rpi
	HostName raspberrypi.local
	User pi
	IdentityFile ~/.ssh/id_rsa
	StrictHostKeyChecking no
	UserKnownHostsFile /dev/null

在将您的 SSH 公钥 (~/.ssh/id_rsa.pub) 添加到 Pi 的 ~/.ssh/authorized_keys 后,您将能够无密码上传。如果您还没有 SSH 密钥,请运行 ssh-keygen

WASM

cargo install wasm-pack
wasm-pack build --target=web --release -- --no-default-features --features=wasm

有关使用示例,请参阅 index.html。要测试

npm install -g http-server
http-server

程序

二进制文件将包括几个默认程序作为二进制文件;这些位于 src/programs 文件夹中,可以使用 ./generate_programs.sh 重新构建

  • off.{txt, bin}:发送到灯带的程序,用于关闭
  • default_serve.{txt, bin}:在设置/命令行未指定其他程序时的默认程序

用法

# Compile a script
cargo run -- compile test/random.txt test/random.bin

# Test run a script
cat test/random.txt | cargo run -- run

# Serve programs to devices (configure using config.toml)
cargo run -- serve

# Run a client (configure using config.toml)
cargo run -- client

# Run a program
cargo run -- run --binary test/clock.bin

# Run a program on an actual strip with 100 LEDs (SPI bus 0 SS 0) on a Raspberry
cargo run -- run --binary --hardware -l 100 test/clock.bin

# Run a program on an actual strip with 100 LEDs connected to SPI bus 1 slave select 1 on a Raspberry
cargo run -- run --binary --hardware --bus 1 --ss 1 -l 100 test/clock.bin

协议

PLWP 协议定义了消息格式以及指令架构。脚本编译为此架构,并使用消息格式传输到设备,设备将执行它们。

有关更多信息,请参阅 protocol.md

脚本语言

示例脚本可以在 test 文件夹中找到。

语句

连续语句由分号 (";") 分隔。支持的构造

  • if(比较) {语句}
  • loop { statements }:无限循环 statements
  • for(var=expression) { statements }:从 expression 开始计数变量 var 递减到 1(包含),例如 for(n=5) 将循环 n=5, 4, 3, 2, 1。
  • 注释和空白
    • /*可以跨越多行*/
    • // 单行注释(应在 \n 结束)
    • \r\n\t 都是空白字符
  • 特殊命令
    • yield
  • 用户命令
    • get_pixel(index):获取当前像素值(可能尚未绘制);格式为 0x00BBGGRR
    • set_pixel(i, r, g, b):将索引为 i 的像素设置为颜色 (r, g, b)
    • random(max):返回一个介于 0 和 max 之间的随机数,包含 max
    • get_length:返回条带长度
    • get_precise_time:返回单调时间,单位为毫秒。在确定性模式下,使用指令数量来返回近似时间。
    • get_wall_time:返回自 Unix 纪元时间以来经过的秒数(可能在未来会溢出)。
  • 编译器内嵌函数
    • rgb(r, g, b) 转换为 (r & 0xFF) | (g & 0xFF) << 8 | (b & 0xFF) << 16
    • red(c) 转换为 c & 0xFF
    • green(c) 转换为 (c >> 8) & 0xFF
    • blue(c) 转换为 (c >> 16) & 0xFF

表达式

支持的操作符

  • 算术:a+ba/ba*ba-ba%b
  • 二进制: a|b, a&b, a^b (XOR)
  • 一元: !a
  • 比较: a==b, a!=b, a<b, a>b, a<=b, a>=b

API

GET /

获取服务器状态。可用于健康检查。

{}

GET /devices

返回当前或之前连接的设备列表。

{
  "devices": {
    "aa-bb-cc-dd-ee-ff": {
      "address": "1.2.3.4:5678",
      "program": [10, 11, 12, ...]
    }
  }
}

GET /devices/<mac>

返回特定设备的信息

{
  "address": "1.2.3.4:5678",
  "program": [10, 11, 12, ...]
}

GET /devices/<mac>/<program_name>

向设备发送内置程序。内置程序名称

{}

许可证

MIT

依赖

~9–21MB
~289K SLoC