caml:bit
is an OCaml library to program BBC micro:bit, that is to run OCaml bytecode directly on the BBC micro:bit.
caml:bit
relies on the OCaml runtime provided by the OMicroB project.
caml:bit
tries to mimic the MicroPython API for the BBC micro:bit, see micro:bit Micropython API.
BBC micro:bit can be programmed in either the block based Microsoft MakeCode IDE or MicroPython.
Blocks are a good way for kids without prior programming experience to start, however become limiting very quickly.
MicroPython is nice, except for the lack of a compilation step: in order to resolve a typo in a varibale name one has to actually flash the device, observe the scrolling (i.e. unreadable) error message on the 5x5 pixel screen, connect the UART, reset the device, reproduce the error, then ta-da! see the full error message in the console and fix the problem.
From this point of view, OCaml has the following benefits:
- It uses syntax similar to Python, so it maintains the level of readability and expressiveness.
- It may have type annotations, but even without them, OCaml always does full static type checking using inferred types, so spots all the typos and more.
- It uses Algebraic Data Types as the core if its type system that combine simple mathematical concepets into powerful static verification tools, so helps prevent many types of logical errors.
- It compiles to compact and efficient bytecode, so delivers very good performance.
Here is a very detailed analysis for the curious Python to OCaml: Retrospective.
Note: This is not kids friendly yet, thus would require an adult support.
- A Linux computer, e.g. Ubuntu 20.04.
- Requirements of OMicroB.
Nice to have:
-
Visual Studio Code with OCaml plugin using Merlin to edit OCaml code.
Note: Yes, there is a complete mess in the pugins with circular references of deprecation, but this one worked nicely for me.
-
QEMU to run examples in the BBC micro:bit emulator.
-
Clone the repository.
$ git clone
-
Configure the library.
./configure
What it does:
- Checks out the
camlbit
branch from a fork of OMicroB with few extra patches. - Runs
configure
for OMicroB. - Runs
make
for OMicroB. - Copies Makefile settings from OMicroB to
caml:bit
.
- Checks out the
-
Build the reference documentation.
make docs
What it does:
- Creates
docs/camlBit
folder withcaml:bit
reference documentation. - Creates
docs/stdlib
folder with the provided version of the OCaml standard library reference documentation.
- Creates
-
Build the examples.
make
-
Run the examples.
Some examples can be run with QEMU, however the real BBC micro:bit should be used to see and make use of all the features.
Copy the resulting
*.hex
file to your BBC micro:bit via USB, see How do I transfer my code onto the micro:bit via USB. -
Create your own programs.
Copy one of the examples and start hacking using e.g. Visual Studio Code.
Consult the generated reference documentation for available OCaml standard library and
caml:bit
library features.