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