Lesson plan

Modern C++ for microcontrollers lesson plan

The parts of modern C++ that make firmware safer and cost nothing on the chip, measured on an ESP32-C3: constexpr, enum class, std::array, RAII, lambdas and templates without bloat. Ends with a zero-cost pin driver that races digitalWrite.

4 chapters, 7 lessons, about 5 hours, on the ESP32-C3-DevKitM-1. Every lesson runs in the browser, with nothing to install.

The unit

Objectives and materials

By the end, students can

  • Values and names checked at build time.
  • Arrays that know their size, and cleanup you cannot forget.
  • Lambdas, templates, and what they really cost.
  • A pin driver with no run-time cost.

Materials for the whole unit

  • ESP32-C3-DevKitM-1
  • Full-size breadboard
  • LED x 2
  • Pushbutton
  • Resistor x 2

Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.

Chapter 1. About 1 h 25 min

Let the compiler work

Values and names checked at build time.

LessonTimeWhat students do
constexprWork done by the compiler, not the chip: a Fibonacci number that costs nothing at run time against the 21891 calls it takes live, a lookup table built at build time, and static_assert guarding a blink rate.45 min
  1. Run it: the compiler did the math
  2. The same sum at run time
  3. A table built at build time
  4. Ask the compiler for more
  5. A blink rate checked at build time
  6. Let static_assert say no
enum classAn airlock state machine with names that stay in their scope, never turn into an int by accident, fit in one byte, and make the compiler check every switch.40 min
  1. Run it: a one-byte state
  2. What a plain enum gets wrong
  3. Cycle the airlock
  4. Add a state
  5. Why it matters

Materials

  • ESP32-C3-DevKitM-1
  • Full-size breadboard
  • LED
  • Pushbutton
  • Resistor

Check for understanding

Each student finishes "enum class" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Why it matters".

Chapter 2. About 1 h 30 min

Safe by construction

Arrays that know their size, and cleanup you cannot forget.

LessonTimeWhat students do
std::arrayA fixed array that knows its size and costs nothing extra. No decay to a pointer in a function, range-for, std::sort, and what Mokxi's small standard library does instead of throwing.45 min
  1. Run it: temperatures in a std::array
  2. It costs nothing
  3. No decay
  4. at() without exceptions
  5. One number to change
  6. Sort it
RAII guardsA constructor takes, a destructor gives back. A lamp guard that survives an early return, a scoped timer that reports itself, and a hand-written version that leaves the lamp on.45 min
  1. Run it: a guard in braces
  2. The early return
  3. A timer that reports itself
  4. Time the transmit
  5. Fix the legacy send
  6. RAII in the wild

Materials

  • ESP32-C3-DevKitM-1
  • Full-size breadboard
  • LED
  • Pushbutton
  • Resistor

Check for understanding

Each student finishes "RAII guards" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "RAII in the wild".

Chapter 3. About 1 h 20 min

Generic code

Lambdas, templates, and what they really cost.

LessonTimeWhat students do
LambdasLittle functions written where they are used: a task table of captureless lambdas, a sort by distance from the Sun, captures by value and by reference, and what each one costs in bytes.40 min
  1. Run it: sorted by a lambda
  2. A task table
  3. Captures and their cost
  4. Flip the order
  5. Add a task
Templates without bloatAn array template of your own in eight lines, ring buffers of three sizes, and what every new size costs in the build log. Then the shared-base trick, measured: less code, a little more RAM.40 min
  1. Run it: an array of your own
  2. Three sizes, three copies
  3. One more size
  4. Share the code
  5. Rules for templates on a chip

Materials

  • ESP32-C3-DevKitM-1
  • Full-size breadboard
  • LED
  • Pushbutton
  • Resistor

Check for understanding

Each student finishes "Templates without bloat" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Rules for templates on a chip".

Chapter 4. About 45 min

Project

A pin driver with no run-time cost.

LessonTimeWhat students do
Project: a zero-cost pin driverA pin driver where the pin number is part of the type: every call is one register write, it races digitalWrite and wins by more than ten times, and you add the input half and a blinker built from templates.45 min
  1. Run it: OutputPin<8>
  2. The race
  3. Nothing to store
  4. Write the input half
  5. Faster heartbeat
  6. Your version

Materials

  • ESP32-C3-DevKitM-1
  • Full-size breadboard
  • LED x 2
  • Pushbutton
  • Resistor x 2

Check for understanding

Each student finishes "Project: a zero-cost pin driver" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.

Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Your version".

Your class

Teach this unit as a class

Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.

The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.