Sunday, 6 November 2016

Spanish Spectrum+ 128K USB keyboard and Keypad

This is the third in a series of posts about a Spanish Spectrum+ 128K. The first covered the main Spectrum+ 128K Toastrack, and the second it's plug in keypad.
As I said at the end of the previous article, these were sent to me for USB keyboard conversion, so that's what I will be covering today. I have saved the original boards and those will be repaired in a future post and re-homed in a Spectrum+ 48K case.
A few years ago I had a batch of custom keyboard membranes for the Spectrum+ made. I am running low on those, not sure if I'll order any more as the quote from the manufacturer has tripled, and they were already very expensive. I designed these with a 5x13 matrix and a single 18 way connector, those meant each key was individually detectable. This was useful for the USB keyboard as many of the Spectrum+ membranes would fail due to age, and the Spectrum+ used the same 5x8 key matrix as the original Spectrum, with the extra keys setup to press two keys at once (i.e. the " key was actually symbol shift and P pressed together).
In this case, the original membrane was in good condition, and the owner wanted to use it with an emulator, so it wouldn't need the full keyboard mapping. The 5x8 matrix with double press keys is ideal for use with an emulator, so for this I used one of my old style full width USB keyboard controllers and the original membrane.
That takes care of the keyboard, but what about the keypad? Well, it had been intermittent, so I wasn't sure about using the original controller. I did implement the protocol and had some success communicating with it, but in the end, it wasn't reliable, so time for plan B.
Inside the keypad was a small board with a PIC microcontroller which scanned the keypad and communicated to the main unit via a custom protocol. I looked at a few options, I could just replace the PIC on there with a new one, and emulate the original, but I would be emulating the protocol at both ends. Both ends can have modern micrcontrollers in, which have built in UARTs, so why not used tried and tested RS232, albeit at 5V levels, no need for level shifters.
This is very much a one off, so I build up a replacement on padboard, using at ATmega328P. Let's not get into PIC vs AVR here (both are owned by Microchip these days anyway). I could also have used a PIC or a smaller microcontroller, but I went with that one as it did the job nicely, I prototyped it on an Arduino, and transplanted the chip to the new board.
The board fits neatly into the case, and uses the same cable, 2 wires for power, 2 for serial. I only used serial transmit in the end, but RX was wired up in case it was required. There is a crystal hiding under the chip socket, so it can run good old fashioned asynchronous RS232 57600 baud 8N1.
The new board connected to the existing membrane and with the case closed, it looked just like the original. I decided it would be useful to add a label, just in case this got separate from the USB keyboard and someone tried to plug it into an unmodified Spectrum.
The keyboard scanning code in the new microcontroller on the keypad handles debouncing etc., and just sends a single ASCII character when a key is pressed, so if you press the '1' key, it sends '1' in ASCII.
At the receiving end, I used a socket from a scrap +2 board. I was going to use a standard BT socket, but there is a notch on the side which is different from the BT plug, so I needed an original one. This is mounted on some more padboard and a liberal helping on hotmelt glue to hold it in place.
That connects up to the keyboard controller, and I even got RX and TX the right way around first time. I set the keyboard controller to read the serial port, and when it sees a character to press the appropriate key. I couldn't find any information on using a keypad in the emulator, it doesn't seem to be supported. In discussion with the customer, I went for making it press the keys necessary to generate the symbols on the keys. The key 0-9 press the number keys. The symbols send the key combination required on a Spectrum to get that symbol, so . '/' send symbol shift + V, etc.
That was working fine in notepad etc. on Windows, but the fuse Spectrum emulator didn't see those key presses, only occasionally if I pressed the same key repeatedly. It turns out it needs to see a key held down for a specific period in order to register it, so I set it up to report that key for several scans in a row before clearing it. That seemed to sort it and fuse was happy.
That's all complete and ready to go.

If you want to support this blog, you can donate via Patreon or Paypal, or buy something from my store.