THIS MODULE ONLY WORKS WITH PYTHON 2.7 or 3.3+.
A utility for flashing the BBC micro:bit with Python scripts and the MicroPython runtime. You pronounce the name of this utility “micro-flash”. ;-)
It provides two services:
- A library of functions to programatically create a hex file and flash it onto a BBC micro:bit.
- A command line utility called uflash that will flash Python scripts onto a BBC micro:bit.
Several essential operations are implemented:
- Encode Python into the hex format.
- Embed the resulting hexified Python into the MicroPython runtime hex.
- Extract an encoded Python script from a MicroPython hex file.
- Discover the connected micro:bit.
- Copy the resulting hex onto the micro:bit, thus flashing the device.
- Specify the MicroPython runtime hex in which to embed your Python code.
To install simply type:
$ pip install uflash
...and the package will download from PyPI. If you wish to upgrade to the latest version, use the following command:
$ pip install --no-cache --upgrade uflash
NB: You must use a USB data cable to connect the micro:bit to your computer (some cables are power only). You’re in good shape if, when plugged in, the micro:bit appears as a USB storage device on your file system.
Linux users: For uflash to work you must ensure the micro:bit is mounted as a USB storage device. Usually this is done automatically. If not you’ve probably configured automounting to be off. If that’s the case, we assume you have the technical knowledge to mount the device yourself or to install the required kernel modules if they’re missing. Default installs of popular Linux distros “should just work” (tm) out of the box given a default install.
To read help simply type:
$ uflash --help
$ uflash -h
If you type the command on its own then uflash will attempt to find a connected BBC micro:bit and flash an unmodified default version of the MicroPython runtime onto it:
$ uflash Flashing Python to: /media/ntoll/MICROBIT/micropython.hex
To flash a version of the MicroPython runtime with a specified script embedded within it (so that script is run when the BBC micro:bit boots up) then pass the path to the Python script in as the first argument to the command:
$ uflash my_script.py Flashing Python to: /media/ntoll/MICROBIT/micropython.hex
You can let uflash watch for changes of your script. It will be flashed automatically every time you save it:
$ uflash -w my_script.py
$ uflash --watch my_script.py
At this point uflash will try to automatically detect the path to the device. However, if you have several devices plugged in and/or know what the path on the filesystem to the BBC micro:bit already is, you can specify this as a second argument to the command:
$ uflash myscript.py /media/ntoll/MICROBIT Flashing Python to: /media/ntoll/MICROBIT/micropython.hex
You can even flash multiple devices at once:
$ uflash myscript.py /media/ntoll/MICROBIT /media/ntoll/MICROBIT1 Flashing Python to: /media/ntoll/MICROBIT/micropython.hex Flashing Python to: /media/ntoll/MICROBIT1/micropython.hex
To extract a Python script from a hex file use the “-e” flag like this:
$ uflash -e something.hex myscript.py
This will save the Python script recovered from “something.hex” into the file “myscript.py”. If you don’t supply a target the recovered script will emit to stdout.
If you’re developing MicroPython and have a custom runtime hex file you can specify that uflash use it instead of the built-in version of MicroPython in the following way:
$ uflash -r firmware.hex
$ uflash --runtime=firmware.hex
The source code is hosted in GitHub. Please feel free to fork the repository. Assuming you have Git installed you can download the code from the canonical repository with the following command:
$ git clone https://github.com/ntoll/uflash.git
Ensure you have the correct dependencies for development installed by creating a virtualenv and running:
$ pip install -r requirements.txt
To locally install your development version of the module into a virtualenv, run the following command:
$ python setup.py develop
There is a Makefile that helps with most of the common workflows associated
with development. Typing
make on its own will list the options thus:
$ make There is no default Makefile target right now. Try: make clean - reset the project and remove auto-generated assets. make pyflakes - run the PyFlakes code checker. make pep8 - run the PEP8 style checker. make test - run the test suite. make coverage - view a report on test coverage. make check - run all the checkers and tests. make package - create a deployable package for the project. make publish - publish the project to PyPI. make docs - run sphinx to create project documentation.
Contributing to uFlash¶
Hey! Many thanks for wanting to improve uFlash.
Contributions are welcome without prejudice from anyone irrespective of age, gender, religion, race or sexuality. If you’re thinking, “but they don’t mean me”, then we especially mean YOU. Good quality code and engagement with respect, humour and intelligence wins every time.
- If you’re from a background which isn’t well-represented in most geeky groups, get involved - we want to help you make a difference.
- If you’re from a background which is well-represented in most geeky groups, get involved - we want your help making a difference.
- If you’re worried about not being technical enough, get involved - your fresh perspective will be invaluable.
- If you think you’re an imposter, get involved.
- If your day job isn’t code, get involved.
- This isn’t a group of experts, just people. Get involved!
- We are interested in educational, social and technical problems. If you are too, get involved.
- This is a new community. No-one knows what they are doing, so, get involved.
We expect contributors to follow the Python Software Foundation’s Code of Conduct: https://www.python.org/psf/codeofconduct/
Feedback may be given for contributions and, where necessary, changes will be politely requested and discussed with the originating author. Respectful yet robust argument is most welcome.
Finally, contributions are subject to the following caveat: the contribution was created by the contributor who, by submitting the contribution, is confirming that they have the authority to submit the contribution and place it under the license as defined in the LICENSE file found within this repository.
- Your code should be commented in plain English (British spelling).
- If your contribution is for a major block of work and you’ve not done so already, add yourself to the AUTHORS file following the convention found therein.
- You MUST include tests. We have 100% test coverage.
- Have fun!
This module contains functions for turning a Python script into a .hex file and flashing it onto a BBC micro:bit.
Given a string representing the MicroPython runtime hex, will embed a string representing a hex encoded Python script into it.
Returns a string representation of the resulting combination.
Will raise a ValueError if the runtime_hex is missing.
If the python_hex is missing, it will return the unmodified runtime_hex.
Given a path_to_hex file this function will attempt to extract the embedded script from it and save it either to output_path or stdout
Given a hex file containing the MicroPython runtime and an embedded Python script, will extract the original Python script.
Returns a string containing the original embedded script.
Returns a path on the filesystem that represents the plugged in BBC micro:bit that is to be flashed. If no micro:bit is found, it returns None.
Works on Linux, OSX and Windows. Will raise a NotImplementedError exception if run on any other operating system.
flash(path_to_python=None, paths_to_microbits=None, path_to_runtime=None)¶
Given a path to a Python file will attempt to create a hex file and then flash it onto the referenced BBC micro:bit.
If the path_to_python is unspecified it will simply flash the unmodified MicroPython runtime onto the device.
If paths_to_microbits is unspecified it will attempt to find the device’s path on the filesystem automatically.
If the path_to_runtime is unspecified it will use the built in version of the MicroPython runtime. This feature is useful if a custom build of MicroPython is available.
If the automatic discovery fails, then it will raise an IOError.
Returns a string representation of the version information of this project.
Takes the byte content of a Python script and returns a hex encoded version of it.
Based on the hexlify script in the microbit-micropython repository.
Entry point for the command line tool ‘uflash’.
Will print help text if the optional first argument is “help”. Otherwise it will ensure the optional first argument ends in ”.py” (the source Python script).
An optional second argument is used to reference the path to the micro:bit device. Any more arguments are ignored.
Exceptions are caught and printed for the user.
Given a string representation of a hex file, this function copies it to the specified path thus causing the device mounted at that point to be flashed.
If the hex_file is empty it will raise a ValueError.
If the filename at the end of the path does not end in ‘.hex’ it will raise a ValueError.
Compatibility for 2 & 3 str()
Takes a hexlified script and turns it back into a string of Python code.
- Refactor hex extraction to not depend on extended address record before script (thanks Carlos).
- Refactor tox tests to fix Windows related Gremlin (thanks again, Carlos).
- Watch for changes in a script. Automatically flash on save.
- Update runtime to include latest bug fixes and inclusion of input() builtin.
- Detecting drives on Windows 10 no longer causes pop-ups in certain situations.
- Documentation updates.
- Add support for flash multiple microbits.
- Update runtime to include audio and speech modules.
- Update runtime to include the new radio module.
- Update runtime to include file system related changes.
- Runtime updated to version 1.0 of MicroPython for the BBC micro:bit.
- Runtime update to fix display related bug.
- Runtime update to latest version of the DAL (swaps pins 4 and 5).
- Runtime update to fix error reporting bug.
- Documentation update.
- Help text update.
- Add ability to specify a MicroPython runtime to use.
- Test fixes.
- Updated to latest version of MicroPython runtime.
- Works with Python 2.7 (thanks to @Funkyhat).
- Updated to the latest build of MicroPython for the BBC micro:bit.
- Minor refactoring and updates to the test suite due to MicroPython updates.
- Minor code refactor.
- Documentation update.
- Feature complete.
- Comprehensive test suite - 100% coverage.
- Tested on Linux and Windows.
- Access via the “uflash” command.
- Initial release. Basic functionality.
Copyright (c) 2015-2016 Nicholas H.Tollervey and others.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.