I TeX Therefore I Toast

2012-10-29 by . 2 comments

Post to Twitter

Introduction

TeXthusiast
You should use TeX! It can do anything!
TeXptic
What anything?
TeXthusiast
Yes! Anything. TeX is Turing-complete, you know.
TeXptic
Okay. Get TeX to make me some toast.

More Fruit

Despite the fact that I clearly have no imagination when it comes to blog titles, this post has nothing to do with Apple products (except being written on one). But Apples are not the only fruit! As the participants at this year’s UK-TUG meeting will know, the new fruit on the block is the Raspberry.

Raspberries are at the opposite end of the spectrum to Apples. Far from being in slimline sleek boxes, with a Raspberry you have to build your own. Everything is exposed. Everything.

Its full name is a Raspberry Pi and it is a small computer without a box. It costs about 30 quid, a bit more if you buy a kit with a few optional extras (all you really need is an SD card and a power source). It tends to run Linux (though there may be other OSes available) and was developed with education in mind.

For me, the really important part — the characteristic part — is the little extra on the motherboard: the GPIO pins. It stands for General Purpose Input and Output and it is what makes a Pi a Pi. Everything else is just a cheap Linux computer: good, but not special. The GPIO pins make it really something.

The point of these pins is that you can connect them to circuits and you can write to and read from them. So you could set a pin to HIGH to turn on a light, or read a pin to see if a button has been pushed which connects it to 0V.

From that, it is very easy to see the possibilities. Hook up a small electromagnet and you could launch a small cart down a slope and measure the time it takes to get to the bottom. Your data goes straight into the computer ready for further analysis. But unlike those prebuilt probes and sensors that come with their fancy software, you understand exactly what is going on with your data at all times because you built the hardware and you wrote the software.

To Toast, Perhaps A Muffin

(English, of course.)

What has that all to do with toast? Imagine a simple switching circuit where the Raspberry Pi controls some external device. It could turn it on, wait a bit, then turn it off again. With a bit of programming it could adjust the time according to some simple parameters, and even time it so that it was ready for your breakfast.

The circuit below is a simplified version (laid out for a breadboard).

The Raspberry Pi can set the GPIO 1 pin to either HIGH (3.3V) or LOW (0V). (Technically, it can also be left in a FLOATING state leading some to claim that it is using a ternary logic. This is nonsense, of course.) If it is set to LOW then the transistor triggers, allowing current to flow to the LED. (Note that the Pi is low-powered so shouldn’t power anything directly.) If HIGH then not. A simple switch.

TeX on Toast

What does this have to do with TeX? To control the pins we need to write programs. The “simple” language that the Pi works with is python in that all the relevant libraries for python are already installed. Of course, there is also C/C++. But I have no desire to learn yet another programming language. As far as programming goes my current language is, by a strange coincidence, lua. So I went looking for a lua solution. What I found was a C++ library (called wiringPi) designed for working with the pins (a huge advantage of it was that it made it possible to work with the pins with always using sudo). With a bit of help from swig, I got lua bindings and so could write simple control programs in lua.

To quote Tom Lehrer: “You’re way ahead of me.”. It’s a short step from lua to LuaTeX. Wrapping the lua commands in TeX was the work of a moment. Getting LuaTeX to work is a little trickier but thankfully Simon Dales has already done the hard work and you can download his binaries from TUG. With that in place, and the requisite lua module and LaTeX package, it is now (theoretically) possible to write:

\documentclass{article}
\usepackage{toast}
\begin{document}
\toast{bread}
\end{document}

and get a nice slice of toast at the end.

Lest it seem a bit too fanciful, the only part missing is that I’ve not (yet) been allowed to dismantle the toaster to connect it to the Pi. I have compiled a TeX document which made an LED light for half a second. The actual document was:

\documentclass{article}
\usepackage{wiringPi}
\newcommand\makeToast{\wPiWritePin{1}{0}\wPiSleep{1.5}\wPiWritePin{1}{1}}
\begin{document}
\makeToast
\end{document}

Make Your Own Toast

You will need:

  1. A Raspberry Pi

  2. A Toaster

  3. Some basic electronic stuff

  4. My (small) modifications to the wiringPi library (available as a git repository from my website)

  5. My wiringpi-sys.lua module (available as a bzr repository from my website) which includes a basic LuaLaTeX package.

  6. Marmite.

Pi In The Sky?

What’s the point? In terms of controlling the GPIO pins directly from TeX? Probably not a lot. (Though it is fun to think of a program that not only gathers the data and proccesses it but also typesets the results nicely.) Mathematics progresses in part by taking something to extremes, looking for the odd, the weird, the stuff which makes you say, “Why would anyone ever consider doing that?”. So think of this as a mathematician doing what he does best. Who knows what might be the end result. Perhaps a toaster that could talk to you, ask you what you wanted, and produce a nicely typeset document detailing all of the toast you’ve eaten that week. Wouldn’t that be a good thing?

‘Course it would.

Filed under LaTeX

2 Comments

Subscribe to comments with RSS.

  • […] Stacey wrote about using TeX to make toast, or about the fun of using TeX to program the GPIO interface of a Raspberry Pi to make LEDs […]

  • Leave a comment

    Log in
    with Stack Exchange
    or