Anelok: scripted simulations

Werner Almesberger werner at
Sun Nov 8 23:46:30 UTC 2015

The simulator has learned some new tricks: now simulations can be
scripted, e.g., to implement unit or regression tests.

It all started with a bug that's been around for a while and that
I finally wanted gone. That bug was that Anelok would not drop
the authentication even if idle for a very long time. The fix is

Now, how do you test this ? Having to wait more than five minutes
for each test is a bit annoying. And so would be having to change
the timeouts to a shorter value and then changing them back.

The solution is of course to use the simulator and make it run in
accelerated virtual time, not real time. Tweaking time is easy, as
countless Hollywood-made documentaries on time travel illustrate.

Since I also wanted to have better access to the display content,
I first upgraded from SDL 1.2 to SDL 2.0. That turned out to be
more tricky than expected since

- in SDL 1.2, everything you can draw on is a "surface", while
  SDL 2.0 introduces also "renderer" and "texture",

- I used the drawing functions from SDL_gfx, which work on surfaces
  in 1.2 but on the renderer in 2.0,

- the renderer is basically write-only, i.e., one can't show some
  content, then modify it and show the modification. Instead, one
  has to redraw from scratch each time.

- Anelok's GUI middleware often works like this, but not always.
  Worse, the simulator draws a red disc on top of the display
  content when the LED is on, which is quite incompatible with
  the write-only concept of the renderer.

So in the end I dropped SDL_gfx and kept the old mechanism using
surfaces, just replacing the optimized SDL_gfx primitives with
SDL_FillRect, and having the surface on top of texture and
renderer, as SDL 2.0 requires.

In the end it turned out that I didn't need any of the new features
of SDL 2.0, but in the long run, it's better to have upgraded
instead of depending on the largely unmaintained 1.2.

The simulator currently accepts commands to

- press or release buttons (top, middle, bottom, release),

- advance simulated time (wait), and

- dump the display content or to compare it with a file (dump,

Details can be found at the end of

Since basic user actions typically involve several simulator commands,
I defined them as cpp macros in

I don't find the all-caps macros too pretty, but the naming is
at least reasonable systematic.

This is what the regression tests for the above bug look like:

As an added benefit, the simulator can now be used to automate
the taking of screenshots, like those in

- Werner

More information about the discussion mailing list