## Introduction ##
: You should use TeX! It can do anything!
: What *anything?*
: Yes! Anything. TeX is Turing-complete, you know.
: 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](http://www.raspberrypi.org/).
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`)](https://projects.drogon.net/raspberry-pi/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](http://www.tug.org/texlive/custom-bin.html). With that in place, and the requisite lua module and LaTeX package, it is now (theoretically) possible to write:
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:
## Make Your Own Toast ##
You will need:
1. A Raspberry Pi
1. A Toaster
1. Some basic electronic stuff
1. My (small) modifications to the `wiringPi` library (available as a `git` repository from [my website](http://www.math.ntnu.no/~stacey/code/wiringPi))
1. My `wiringpi-sys.lua` module (available as a `bzr` repository from [my website](http://www.math.ntnu.no/~stacey/code/wiringpi-sys)) which includes a basic LuaLaTeX package.
## 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.](http://youtu.be/LRq_SAuQDec)