如何自訂 PXT平台 及 增加 library

動機:是否可以自行客製 PXT平台 及 自編其積木?!

準備環境
Windows 10 / MacBook Pro筆電

實作步驟
1.MacOS X平台
按照網頁 ( http://siever.info/home/author/bsiever/ ) 指示安裝即可成功,如下:

  • Installation / Running

i.Install Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

ii.Install Node.js (using homebrew)
brew install node

iii.Install Yotta
brew tap ARMmbed/homebrew-formulae
brew install python cmake ninja arm-none-eabi-gcc
pip install yotta

iv.Install srecord
brew install srecord

v.Clone the pxt repo. (Replace the repo URL with the URL of a fork if desired)
git clone https://github.com/Microsoft/pxt-microbit

vi.Change your directory to be within the cloned repo. For the above:
cd pxt-microbit

vii.Install pxt
npm install -g pxt
npm install

viii.Run the local pxt server.
pxt serve


  • Adding a Library

i.Stop the pxt server (if running)
Control + C

ii.Create the sub directory in ROOT/libs. Ex:
cd pxt-microbit/libs
git clone https://github.com/Microsoft/pxt-bluetooth-temperature-sensor
mv pxt-bluetooth-temperature-sensor bluetooth-temperature-sensor

iii.Add the new directory to bluetooth-temperature-sensor in pxt-microbit/pxtarget.json
...
"bundleddirs": [
... ,
"libs/bluetooth-temperature-sensor"
],
...

iv.If needed, update the pxt.json of the new library. For example, for the above update the path of the dependencies:
...
"dependencies": {
"core": "file:../core",
"bluetooth": "file:../bluetooth"
...

v.Restart the server to build it.

2.Windows 10平台
基本上按照上述網頁指示安裝即可成功(部分指令不同),如下:

  • Installation / Running

i.Install Node.js
請至網站 ( https://nodejs.org/en/ ) 下載並安裝 (8.9.1 LTS)

ii.Install Yotta
請至網站 ( https://mbed-media.mbed.com/filer_public/2f/0b/2f0b924c-1fac-4907-989b-f2afe3f5785e/yotta_install_v023.exe ) 下載並安裝

iii.Install srecord
請至網站 ( https://sourceforge.net/projects/srecord/files/srecord-win32/1.64/ ) 下載並安裝

iv.Clone the pxt repo. (Replace the repo URL with the URL of a fork if desired)
git clone https://github.com/Microsoft/pxt-microbit

v.Change your directory to be within the cloned repo. For the above:
cd pxt-microbit

vi.Install pxt
npm install -g pxt
npm install

vii.Run the local pxt server.
pxt serve


  • Adding a Library

i.Stop the pxt server (if running)
按 Control + C

ii.Create the sub directory in ROOT/libs. Ex:
cd pxt-microbit/libs
git clone https://github.com/Microsoft/pxt-bluetooth-temperature-sensor
ren pxt-bluetooth-temperature-sensor bluetooth-temperature-sensor

iii.Add the new directory to bluetooth-temperature-sensor in pxt-microbit/pxtarget.json
...
"bundleddirs": [
... ,
"libs/bluetooth-temperature-sensor"
],
...

iv.If needed, update the pxt.json of the new library. For example, for the above update the path of the dependencies:
...
"dependencies": {
"core": "file:../core",
"bluetooth": "file:../bluetooth"
...

v.Restart the server to build it.


  • 增加自訂blocks積木

i.先到新目錄: cd \pxt-microbit\libs\bluetooth-temperature-sensor 編輯 pxt.json
...
   "files": [
        "README.md",
        "extension.cpp",
        "TemperatureSensorService.cpp",
        "TemperatureSensorService.h",
        "shims.d.ts",
        "enums.d.ts",
        "custom.ts"
    ],
...

ii.再於新目錄,自編積木(示範碼) custom.ts,如下


iii.重新執行自訂pxt server
回到 \pxt-microbit 目錄
pxt serve

參攷畫面如下


iv.依據上述網頁 https://www.hackster.io/pelikhan/custom-ble-services-with-micro-bit-6c9879 指示修改相關程式,並執行 pxt deploy to flash .hex 到 micro:bit 上,如下圖

v.重新執行自訂pxt server
回到 \pxt-microbit 目錄
pxt serve

畫面如下


後記:關於自定積木可以整合成 Package,如果想要申請 MakeCode Package,可至 https://docs.google.com/forms/d/1doV-go5lYiIe3ISTzGiuQAD2xa7-j7-KZq8NbF8bIV8/viewform?edit_requested=truemicro:bit Javascript Blocks editor package/organisation approval )


感謝
1.Bill Siever, JUNE 21, 2017, PXT FOR THE MICRO:BIT LOCALLY ON MACOS, http://siever.info/home/author/bsiever/
2.Peli de Halleux, April 12, 2017, Custom BLE Services with Micro:bit, https://www.hackster.io/pelikhan/custom-ble-services-with-micro-bit-6c9879

留言