2 Star 2 Fork 0

MAMAMA / scratchlink-server

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

ScratchLink Server

📕开发环境python 3.7.6

Scratch 3.0与BLE设备连接时要经过ScratchLink代理,两者其实是通过websocket连接,地址为wss://device-manager.scratch.mit.edu:20110/scratch/ble。通过修改C:\Windows\System32\drivers\etc中的HOSTS文件,增加以下内容将域名重定向到localhost

127.0.0.1 device-manager.scratch.mit.edu

用cherrypy和ws4py在本地建立websocket服务
用tornado建立websocket服务,截获Scratch软件的消息。主要难点在于Scratch软件是通过https(wss)进行连接,在本地建立https服务没有太好的方法,目前查到可以用mkcert实现。需要先安装windows的包管理器choco

choco install mkcert
mkcert -install
mkcert device-manager.scratch.mit.edu

生成证书后在server的配置中加上:

server = httpserver.HTTPServer(scratchlink_parser, ssl_options={
        "certfile": "device-manager.scratch.mit.edu.pem",
        "keyfile": "device-manager.scratch.mit.edu-key.pem",
})

安装依赖包并运行

pip install -r package.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
python scratchlink.py

可以继承ScratchLink类来处理scratch的microbit积木中display和display_text的消息,从而实现自己的扩展,比如把消息转发到串口,就可以控制其他外设。使用display_text更加直观,但是效率较低,因为microbit显示文字的速度比较慢,所以scratch做了等待。使用display的效率比较高,但是用的是LED阵列的编码,需要自己去根据scratch上面LED的图案来设置解码。下面是一个使用scratchlink库的demo

import scratchlink
import colorlog
import logging

class MyParser(scratchlink.ScratchBleParser):
    def on_display_text(self, txt):
        print('I recieve: '+ txt)

    def on_display(self, bytelist):
        for line in self.led_matrix:
            for led in line:
                print(led, end=' ')
            print('')

if __name__ == '__main__':
    # 设置打印的日志级别,默认INFO,可以改为DEBUG看到更多信息
    colorlog.getLogger('scratchlink').setLevel(logging.INFO)
    scratchlink.ScratchLink(MyParser)

调试记录

2020-03-19

❌遇到一个bug,建立的wss:websocket服务消息有一条滞后,也就是客户端发送A,B两条消息,服务端只收到A,等客户端发送C消息时,服务端才收到B。目前不知道如何解决。

2020-03-19 night

更换为tornado框架后完美解决上面的bug

MIT License Copyright (c) 2020 MAMAMA Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

ScratchLink Microbit模块的python代理 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/MAMAMAisused/scratchlink-server.git
git@gitee.com:MAMAMAisused/scratchlink-server.git
MAMAMAisused
scratchlink-server
scratchlink-server
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891