Extension that modifies the simulator

I was wondering if it is possible to write an extension that modifies the simulator? I use some extensions that allow students to program different sensors and I would like them to be able to test their programs by choosing different sensor values in the simulator (like the light sensor on the micro:bit) before they load the program on to the micro:bit. This is especially important since school is going to most likely be virtual this fall.

Hi Alex,

I’ve been investigating this concept for a while. Someone else may have a better answer, but I was considering using the “Experimental” Editor Extensions feature, which would require explicit effort for users to opt-in to using, and it would not be as visible as the built-in interactions, like the light sensor.

My basic idea is to allow users to enter a table of times and periods to simulate the sensor in the extension frame. For example, they may indicate the sensor shows 20 for 2 seconds, then 22 for 4 seconds, then 26 for 12 seconds, then 22 for 2 seconds. A total of 20 seconds of values are entered and they’d repeat every 20 seconds. Of course, if you’re just setting a specific value rather than a “schedule”, you could do so with a GUI instead.

Details about these types of extensions are here: https://makecode.com/extensions/extensions
and there’s an example extension here: https://github.com/samelhusseini/pxt-editor-extension-sample

I hope someone else can share a better approach!

For security reasons, it is not currently possible to inject any javascript into the simulator. In a nutshel, it’s not trivial right now.

Here are a few ways I can think of making this work:

  • fork github.com/microsoft/pxt-microbit and implement additional behaviors in the pxt simulator itself. This is potentially complicated as our codebase is rather large. Send us the pull request! :slight_smile:
  • embed MakeCode as a widget in your page and communicate to the simulator via iframe messages. You will receive messages for serial, radio packets.

Just to check…PRs to update simulation behavior for specific extensions may be accepted? (That would be great!)

How would I go about sending text between the sim and my own process in the host frame?

@toysonlocation can you give a little background on what you’re trying to do? Are you embedding the simulator in a website?

Thanks for the reply. We want to let students develop for a robot car in a simulated environment rather than force them to have the real microbit on hand. We would like to simulate the car in its own virtual 2D env, and have that virtual env informed by output from the sim, and feed back to the sim from the virtual env via sensors. I have wrapped servo extensions with custom blocks to present the same API, and would hope to do the same with the sensors “somehow”. So, there are really three things I’m looking at:

  1. real time text data to and from the sim
  2. an external view to process the stream (from the sim) and send back signals (to the sim)
  3. a way to wrap the return signals in the sensor components so the student won’t need to swap them out with “real” sensor components for the real microbit.

I would recommend looking at the jacdac extension.

1 Like

Hi TGO, thanks for the link. Notifications from this board were going into my spam folder, or else I would have written sooner. The most important thing is that we can data from the sim, and write back to it via fake sensors. I’ll have a look at Jacdac!