Programming Gogo
A book about gogo: a tiny actor VM for microcontrollers, programmed in a subset of Go, with Erlang-style actors, supervisors and ports.
It is written for programmers who know some Go or another C-like language, and assumes no knowledge of embedded programming or Erlang.
Contents
- Preface: what gogo is, what it is good for, and what it is not
- Getting Started:
gogo setup, build the tools, run a program, flash a board - Language Basics: the Go subset, what it leaves out, and what to write instead
- Actors: Spawn, Send, Receive, type switches, Call/Reply, names, timers, actors on other boards (with chapter 20)
- Failure and Recovery: exit reasons, links, monitors, TrapExit, supervisors, “let it crash”
- Memory Model: per-actor heaps and limits, copied messages, mailbox quotas, Bytes and Buffer
- Ports and Peripherals: ports, GPIO, events, debouncing, owners, Handoff
- Keyboard and Power: the
kbdandpowerports - Storage:
/flashand/sd,gogo/fs,gogo/kv,gogo/ringlog - Networking: DHCP, DNS, UDP, TCP, the HTTP client and server, the wall clock (
gogo/sys,gogo/sntp) - Configuration and Devices:
gogo config,sys.Config, Wi-Fi, the two boards - Toolchain Reference:
gogo build,run,dis,flash,monitor,firmware,config,setup - Appendices: library quick reference, exit reasons, error codes, limits, differences from Go, requirement names
- Hot Loading: loading code at run time, modules, supervised upgrades
- IMU and IR: the motion sensor, infrared send and receive, streams (
Chunk,Out,Done) - Audio: the microphone and speaker, recording to WAV files, playing them back
- HTTPS: https URLs, the clock, the keep-alive
http.Client, streaming uploads, the"tls"port - Embedding gogo: running gogo images inside a C program:
libgogo.a, the host table and the latched wake, stepping, posts, host ports, a sandboxed/flash - Text UI: the
"screen"port,gogo/tui: frames, windows, colours, menus, progress bars, status lines, a line editor, keys on both boards - Distributed Actors:
gogo/node: a server node and boards, remote PIDs, names, what a message may hold, lost nodes, security - Best Practices: actors as small machines, messages and
Bytes, backpressure, shared state, Call discipline, restarts, receive order, C or gogo, high-bandwidth streams (audio, sensors, cameras), streaming TCP and HTTP, a checklist - Extending gogo with C: why there is no FFI, port drivers in C (the driver table, events, memory, requirements, optional features, host simulation), a tested driver with generic types, host ports, pitfalls
- Bluetooth LE: the
"ble"port, a peripheral a phone app talks to: services and characteristics, events, values a phone reads, notifications with flow control, nRF Connect, the host simulation, memory and Wi-Fi
Known issues lists gogo’s open limitations, with workarounds.
Examples
Every program in the book is a complete, runnable example in
examples/, one directory per program, named after its
chapter (04-call, 09-kv-boot, …). The output printed in the book is
the real output of the host VM, kept next to each program in
expected.out and expected.err. To run them all and check that the
book is still right:
make vm
go build -o build/gogo ./cmd/gogo
docs/book/examples/run.sh
Chapter 23’s programs need a phone: each has a phone script that
tests/ble plays over gogovm’s Bluetooth socket
(go test ./tests/ble/, part of make test), and run.sh skips them.
Chapter 20’s programs need several VMs: they are the server and boards
of tests/dist/progs, run as separate host
VMs over real TCP by go test ./tests/dist/.
Chapter 18’s C programs are in examples/embed-c/;
make example-embed builds them, runs them and checks that every C
snippet in the chapter is cut from them. Chapter 22’s driver is in
examples/dice-driver/; make example-driver runs it on its test board and checks the chapter’s
snippets against the tree.