Microsoft MakeCode

Couple of questions making a PXT

I am looking at making a PXT to wrap a javascript library - was tossing up between this and doing my own blockly. Basically I am experimenting with a 3d asset generator to make 3d printable objects.

I have gotten pretty far quickly, which is great but I am a bit stuck and going round in circles in the documentation.

  1. I want every new application to have “on start”
  • how do I make sure a block is always on the code editor
  • how do I trigger code based off “on start”
  1. When I do pxt serve, half the icons are missing, especially the ones in the “Simulator Toolbar” for play, stop and debug. Is this something I will have to make on my own or is this just a bug running locally on my machine?
  2. Am I able to control the size of the Simulator window to be a bit taller?

Thanks for any help!
JFo

The answer to question 2 is I had a typescript error.
When typescript errors occur you fall back to an unthemed thing - which means you can’t see the icons.

The base pxtsim.d.ts had errors so I added off “skipLibCheck”: true in pxt-sample/sim/tsconfig.json. This quieted down these errors:

../node_modules/pxt-core/built/pxtsim.d.ts(256,13): error TS1086: An accessor cannot be declared in an ambient context.
../node_modules/pxt-core/built/pxtsim.d.ts(1010,13): error TS1086: An accessor cannot be declared in an ambient context.
../node_modules/pxt-core/built/pxtsim.d.ts(1049,13): error TS1086: An accessor cannot be declared in an ambient context.
../node_modules/pxt-core/built/pxtsim.d.ts(1294,21): error TS1086: An accessor cannot be declared in an ambient context.
../node_modules/pxt-core/built/pxtsim.d.ts(1294,21): error TS7033: Property 'stoppedClass' implicitly has type 'any', because its get accessor lacks a return type annotation.
../node_modules/pxt-core/built/pxtsim.d.ts(1295,21): error TS1086: An accessor cannot be declared in an ambient context.
../node_modules/pxt-core/built/pxtsim.d.ts(1295,21): error TS7033: Property 'invalidatedClass' implicitly has type 'any', because its get accessor lacks a return type annotation.
INTERNAL ERROR: Error: Exit code: 2 from cd sim; node ../node_modules/typescript/bin/tsc
    at ChildProcess.<anonymous> (/Users/jessica/Documents/Me3d/KhanCoding/pxt-sample/node_modules/pxt-core/built/nodeutil.js:85:24)
    at ChildProcess.emit (events.js:223:5)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)

Answer to 1:

  • You can customize the starting blocks by changing libs/blocksprj/main.blocks in your target repo. The one for arcade is here
  • To make it so that on-start can never be deleted, enable the option onStartUnDeletable in the runtime settings in pxtarget.json

Answer to 3:

  • In the simulator settings in pxtarget.json, there is an aspectRatio setting that you can use to make the sim taller
1 Like

Also, on-start is just a wrapper for the “top level” code in main.ts. Any code that’s placed in there should run at the start of the program. If you’re trying to make your own custom on-start event that you can control, (e.g. myNamespace.onStart(() => {})), then you should define your own function and remove the onStart block entirely. You can make any block undeletable by setting this option:

//% undeletable=1

and you can make your custom block show up in the default project by editing that main.blocks file I mentioned in my above comment. For example, if you set the blockId of your function to be blockId=myCustomOnStart then your main.blocks should look like this:

<xml xmlns="http://www.w3.org/1999/xhtml">
  <variables></variables>
  <block type="myCustomOnStart" x="0" y="0"></block>
</xml>
1 Like

Hello @jfo8000 and @richard! I’m a begginer as well and I’m building my own PXT. I would like to know if:

  1. Does PXT allow accessing the C++ code behind the blocks?
  2. I would like to have my own set of blocks, with their customised C++ code, is that possible in any way?

Thanks!

You can do so by developing an extension: https://makecode.com/extensions

However, you should ask yourself if you need to use C++ for your extension. If possible, it is better to implement everything in TypeScript. That will make your code more portable, debuggable, and will allow your code to run in the simulator.

For examples of extensions that use C++, check out pxt-common-packages.

May I ask what you want to make your own editor for? If it’s just to support a new board, consider adding it to maker instead.

Thanks for the reply @richard.

You are right, and maybe what I want to do is easier than I thought. I would like to build a centrifuge. I’m using the board Adafruit feather M4 and my goal is to run the motor of the centrifuge by using the blocks in PXT.
The block I want to create will have 2 different variables the user can modify: Time and speed. An example could be: image
So, how can I “link” the block input given by the user with the hardware (centrifuge motor)? I know the question may sound very general, but any tip or help would be fantastic for me to start working on it.

Thanks

Great! The Adafruit Feather M4 express is one of the boards supported on maker.makecode.com

For defining blocks, check out our docs here. Every block corresponds to a TypeScript function which you can use to implement all of the communication with the centrifuge.

How does the board communicate with the centrifuge? We have typescript APIs for i2c and spi under the pins namespace along with all of the usual analog and digital read/write.

Hi @richard! Thanks for your answer, it’s really helpful.
To run the centrifuge I use the library Servo.h.
This is the code:

The brushless motor attached to the pin 8 can be run like a servo motor, where 0 is the minimum power and 180 the maximum.
Any tip to create my TypeScript function to implement this code?
How can the typeScript code read the inputs Speed and time from the block? (see block image above)

Thanks!

@peli any advice for using servos with makecode?

Have you looked at the servo library for ideas? https://github.com/microsoft/pxt-common-packages/blob/master/libs/servo/servo.ts

@peli @richard I’m struggling with the addition of my blocks to my local editor. I’ve got a MakeCode Maker editor type and, after the installation, I have 3 folders: pxt, pxt-common-packages and pxt-maker. In which folder and file should I paste my blocks code to visualize them in the editor? Am I doing something wrong?

Thanks!

@dwagga are you creating a fork of the maker editor? or just adding a few blocks to it. If you just need to add a few blocks, you can add them without forking the editor. See https://arcade.makecode.com/blocks/custom

@peli You are right, I don’t need to create a fork. I’ve been trying to do it through an extension: creating the blocks in the file main.ts of the extension and adding it to the project. Is that correct? However, after adding it, don’t see the blocks in the editor. What do you think?
Moreover, how can I make an extension compatible with a specific board? For example, when I try to add the extension in a project that has an Adafruit Feather M4 Express, it says:

Extension adafruit-feather-m4-express is incompatible with pxt-centrifuge2. Remove adafruit-feather-m4-express and add pxt-centrifuge2?

Do you have a repo we can look at?

Hello @peli @richard. Yes, my extension is: https://github.com/Dwagga/pxt-centrifuge2
I got an error when it tries to build. Could you please take a look? I’m simply trying to add two basic blocks, made with https://makecode.com/playground. Maybe there is something wrong with the dependencies.

Thank you!

The extension loads for me when i add it as an extension in maker.
image

Right, it works. However I got this error, should I worry about it?

Thanks!

Just a followup on this one. I tried undeleteable, but because I hadn’t removed onStart from the blocks list, the user could drag a second one out and then not be able to delete. So I gave up and just went half way with the onStart starting block.

Am pretty happy with the compromise, though.
JFo