Code Cookbook

Using functions in PXT

QUESTION How do I use functions in PXT? How do I reuse code in PXT ANSWER 1. make a little blocks program that beats a heart… 2. go into java script view to view ‘under the covers’ (press the JAVASCRIPT button at the top of the editor) 3. move the beating code into a beat() function with a bit of cut and paste and typing 4. go back to blocks view and see what it has done (press the BLOCKS […]

Receiving radio data from PXT within Python

PROBLEM I want to send a text string from PXT to Python. Why do I get ‘junk’ at the start of a radio message sent by PXT? Python not receiving radio message correctly. CAUSE The PXT radio blocks add some additional binary information at the start of the radio message. When received by Python, this binary data can cause your program to crash. SOLUTION You need to step over the first 12 bytes of any radio message transmitted by PXT, […]

Build a new Image() from a buffer

PROBLEM I want to display an image from a pre-prepared buffer. SOLUTION from microbit import * i = Image(5,5,b’\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08′) display.show(i) NOTES 1. The documentation for Image() is here: http://microbit-micropython.readthedocs.io/en/latest/image.html?#classes 2. You can’t use keyword arguments with this function. 3. The buffer parameter must support the buffer protocol – i.e. a bytearray or a string of bytes. You can’t pass a Python list to this method.

How to use an updated MicroPython image

QUESTION How do I use a more recent MicroPython image to generate my scripts? I have a patched .hex image how do I use this to load Python onto the micro:bit? Updating MicroPython ANSWER go to: https://github.com/bbcmicrobit/PythonEditor press the CLONE OR DOWNLOAD button Choose the DOWNLOAD ZIP option This will download a complete web based python editor. Unzip this on your computer – you should now have a PythonEditor folder get a patched .hex file that we sent you rename […]

Storing and retrieving values in non volatile data

PROBLEM How do I store non volatile data in MicroPython? How do I create, read, and write files in MicroPython? How do I store a variable in non volatile data and retrieve it again on reset? How do I store a variable so that it is remembered when the micro:bit is turned off? How do I stop my variables being lost when I power off the micro:bit? KEYWORDS nonvolatile, non volatile, non-volatile, variable, persistent DISCUSSION The MicroPython language has a […]

All for Joomla All for Webmasters