Microsoft MakeCode

Accessing the pins in C?

Erm, why am I finding it totally impossible to get an extension to compile when I try to access the Microbit pins in the C code in extension.cpp? I’m completely baffled by this? What is wrong with pins.spiWrite(0) or pins.digitalWritePin(DigitalPin.P1, 0) for example?

That are TypeScript apis and are not meant to be used in C. Why do you want to write C/C++?

Because I want to do some data manipulation and write a buffer to SPI and also toggle a digital output pin, and I’m interested to see if the C compiles as better optimised code than the Typescript.

I also observe that the driver extension for the WS2812b device (used by Neopixel and others) has an ARM assembler routine for pusing a buffer to the output pins and presumably there is a reason for going to the trouble of doing it in assembler?

Online I discovered things such as uBit.io.P0.setDigitalValue(x) or uBit.io.pin[0].setDigitalValue(x) (or something like that) as allegedly existing in C for the Microbit but that doesn’t appeart to be valid either. Are these routines available or not?

You can use our Buffer type in typescript to manipulate it. Use the indexer [] and those will be compiled to ARM instructions so the overhead is minimal.

In general, C++ is not recommended as it triggers C++ compilation in the cloud. If you want to do C++, you’ll be better of building locally and using the CLI to get incremental build. Otherwise you’ll have to wait a minute on each build. For C++ APIs, use https://lancaster-university.github.io/microbit-docs/

I looked at the Lancaster pages but was left none the wiser. I’m aware that in most cases the Typescript will be compiled to efficient ARM code but I am, it seems, deprived of any opportunity to experiment and see for myself what is possible. I’m aware that some Microbit extensions include a bit of C in a cpp file or even some ARM code in an asm. I’m also not sure I understand the function of a double colon and when that is required.

You replied to me a while back when I commented on the C code for the Pimoroni Scrollbit which has C code for fetching font data, and has shims to push those functions into the Typescript, which I now understand.

With the limited number of examples available to me it’s not at all obvious why the example code (digital pins) I got from the Lancaster website don’t compile. It was a test function with one line of code! I’m not writing a stand-alone program so I doubt I need to do Microbit uBit; uBit.init(); inside an extension.

So, for me to learn and to have the pleasure of understanding how the C and TS fit together, is there anyone who can explain how to execute Microbit functions using C, according to what the Lancaster site claims is possible?

The shim annotation tells the linker which function to use when compiling down to C++ or Browser JavaScript. You’ll see that shim a::b annotation point to a c++ function somewhere in our codebase.

This might help https://makecode.com/simshim

Hmm, I’m afraid it’s only partially helpful and I spotted that document earlier. We discussed shims here a few weeks ago when I was starting on the learning curve, e.g the shim for the enhanced createImage() field editor in the beta release which you put in. It’s not the shims notion itself that I’m struggling with.

What, for example, the documentation doesn’t tell me is why there is a shim.d.ts file for an extenions which contains nothing but declare namespace whatever { } as is true of the first example I looked at - the Pimoroni scrollbit. When the documentation mentions auto-generated shim files I don’t know what relevance this has to what I’ve been exposed to so far. What do I infer from the fact that the Neopixel extension doesn’t appear to have a “declare” but the Scrollbit does, for example?

It’s also not clear to me what the purpose of enums.d.ts files is and whether there are circumstances in which I would need one, or under what circumstances it is auto-generated or not.

There was also the question of things such as //% parts="ledmatrix" for which I could find no documentation until it was explained on this forum that it was defunct when I asked.

With all this “clutter” but not knowing whether, for what I want to achieve, it’s actually essential rather than just clutter, it is actually rather difficult learning the ropes, I’m afraid to say.

Oh, and there is an empty document here: https://makecode.com/cpp2ts

part is very alive and specifies the breadboard rendering engine which “parts” are needed. This is also used to generate breadboarding wiring instructions (feature that is not live).

The CLI will generate the shims.d.ts if there are c++ function with //% annotations. It will generate enum a.d.ts if there are C++ enums with //% annotations. Generation does not happen in the browser.