Saturday, 27 April 2013

Arduino Leonardo based ZX81 USB keyboard

Last year, I had a ZX81 case and wanted to turn it into a ZX81 USB keyboard. At the time the option I chose was initially developed on an Arduino Uno, and then built up onto a board using just the ATMega328P. I've build several versions of this now, many of which sat alongside Raspberry PI's inside their ZX81's and ZX Spectrum's, and I'd refined the circuit and the presentation a bit since the orignal.
A couple of days ago, I had a request for more information on the keyboard, and I mentioned that if I were doing it now, I would have looked at using an Arduino Leonardo. These are Ardunios based on the ATMega32U4, which has built in USB support, and there are libraries to turn the Leonardo into a USB Keyboard / Mouse etc. as part of the Arduino IDE.
So I got my self a Leonardo and tried out some of the samples and it seemed to work fine, so I adapted my original code to use the Leonardo USB Keyboard object and tried that out, and it worked remarkably well. One of the things I'd noticed about the Leonardo was that it was available to buy without the headers, and the IO pins were arranged in a way that I could get the 8 and 5 pin headers for the keyboard directly over IO pins 0-7 and 9-13.
So it was simply a case of soldering the keyboard connectors in place where the headers would have been.
Then installing it in the ZX81, on the underside of the top of the case.
And there it is, an incredibly simple way of making a ZX81 USB keyboard. There are 20 available IO pins on the Leonardo, so a similar approach could be used on most matrix keyboards, up to 100 keys. It should also work with any of the other ATMega32U4 based Arduino boards (such as the Micro).
The code is available below, in two versions. The first is the standard keyboard mapping, all the letters and numbers work as usual, and with shift held, the letters are upper-cased and the top row gives escape, ", #, arrow keys, !, and delete. The alternate version has the top acting like that by default and requires shift to get the numbers. This is the version I use to drive my myth tv frontend box, so I can navigate the menus without having to hold shift.
Creative Commons Licence
ZX81 USB Keyboard by Dave Curran is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

Note: I have stopped using Leonardo based boards and moved onto my own custom PCBs and firmware for ZX81, ZX Spectrum and Commodore computers. There seem to be some issues using the Leonardo keyboard library with things like the Fuse emulator. If you plan to use an emulator, either see my Etsy store, or try the older V-USB based design.