Building an extension for MakeCode for micro:bits
Building a block
//% blockId="bitwise_not" block="~ %a"
export function bitwiseNot(a: number){
return(~a)
}
The key thing to look at here is the block="~ %a" text; this defines how the block looks. It is telling the MakeCode editor to display a tilde (~) followed by a parameter called a. The user can type a number into the parameter's white circle, or another block can be inserted into the circle.
Your blocks need to be written inside a namespace - which will be the name of your extension:
namespace BitwiseLogic {
(block code goes here)
}
Here's a view of the playground and the micro:bit editor:
Building your extension
Tip! You may need to force-refresh your browser (typically Ctrl F5) occasionally to make sure you are seeing any changes made in other tools like the text editor or GitHub.
Once you have tested your block you can copy the code into a text file. You can call it main.ts (ts stands for Typescript), but you don't have to.
I would advise uploading that file to Github - you will have to do that at some stage, and it's best to learn to use it sooner rather than later! Here is a good guide to get started.
I have found some problems with setting up a repo (short for repository - that is just a name for a bunch of files) so that it is compatible with MakeCode. The best way IMHO is to:
- create a new project in the MakeCode editor
- name it: pxt-your-extension-name, e.g. pxt-BitwiseLogic
- save it to GitHub using the blue/white button at the bottom of the screen
- this creates a number of files, many of which you don't need. For now, just use main.ts to save your block code in.
Testing your extension
You can keep copying your main.ts into the playground on the left-hand side, and then build some test programs using blocks on the right-hand side, but another way to do it is to use the normal MakeCode editor and access your extension via GitHub. Of course, once your extension has been approved, it will appear in the official Extensions list.
You can do your testing in MakeCode by creating a new project - I recommend first going to Settings (the gear wheel) and doing a Reset from the drop-down menu.
Then, in the list of blocks, click on Advanced and scroll down to Extensions. Now paste in the URL of your Github repo:
Now your extension package will pop up:
Now you can start to design your test code!
.
Comments
Post a Comment