Monday, April 24, 2017

USB Mouse for the ZX81 and ZXpand (Part 1)

A USB mouse on a ZX81 sounds a little mad and yet given the wealth of plug and play electronics available in 2017, it's a comparatively painless task to implement. To be clear I'm talking about a digital mouse, one that uses an Atari style joystick port, where proportional mouse movements are converted to digital signals.

Why a digital mouse? While not being a true proportional mouse implantation, it is a path taken by at least one 8bit computer, the Commodore 64 and it's 1351 Mouse back in the 1980s. As an added bonus this also means the mouse interface should work on all 8bit systems that support the Atari protocol. At the end of the day it's smarter to build on an existing solid and supported base such as Charlie Robsons'  ZXpand with its digital joystick implementation, than it is to implement a non supported (by existing software) standalone proportional mouse interface; at least for now.

So now it's down to how to get this thing to work, and my prototyping weapon of choice is an Arduino UNO armed with a USB host shield. The USB host shield really is the key to the project, it comes with an extensive Arduino / C++ library ready to plug in, leaving the casual developer or hobbiest free to solve other non-USB related hardware and software puzzles.


Stage 1: Getting a Test up and Running


Testing a 4006 IC vs a Diode
As we will be leveraging the ZXpands joystick port for interfacing our mouse (and Adruino) with, we need to trigger all mouse movements by simulating joystick button presses. To achieve this the correct signal lines on the joystick port must be pulled low (pulled to ground). We also need to insure that we don't unduly damage the ZX81 / ZXpand in the process of doing so. Ideally we'd wish to partially electrically separate the Arduino from the ZXpand (partially, as we will eventually be drawing power from the joystick port), so that unintentional voltage are not shot in the wrong direction, particularly during testing phase. There are numerous ways ways to achieve this, I tested 2 options.

Option 1: Use Some Diodes.  Diodes can be placed with the cathode to Arduino output pins, and anodes to the joystick port lines. Our Arduino can then set the input to 0v when we wish to trigger a simulated push button action, and there will be no possibility of feeding +5v back to the ZXpand / ZX81.

Option 2: Use a 4066 IC bilateral Switch. This is the slightly more complicated method, though not by much. A bilateral switch can be used to act as Joystick buttons. Power is applied to the IC switch and 2 connected lines are switched on, much in the same way a relay works. There is an easy to follow tutorial to be found on the subject at Brainy Bits. Th e main downside to this method is that in order to trigger the 5 required buttons on the joystick port (up, down, left, right and fire), we would need 2 ICs and all that extra routing on a PCB that that would require.

I wired up the 4066 IC switch for one input and implemented the diode method on an other input, needles to say both of the above variations worked, producing the same results of simulating a button press / movement on the ZXpand Joystick port. I decided to go with the diode method for ease of implementation. If however we were in a position were we might be dealing with different voltage levels, such as a Teensy or Pro Micro with 3.3volts and the ZX81 with 5volts, the switch would be the better option here.


Stage 2: A DIY Cut-down Arduino Clone


If one of the aims of this project is to make something a little more permanent, then using a Uno for the end solution is not the ideal. I could just design a simple shield for the UNO, however the USB host shield will still be required, and with the addition of an extra shield on top, would leave the whole project a little unwieldy for what it actually achieves.

It seems apt to mention here before proceeding, there is such a beast as a Adruino UNO and USB host in one easy to use package. Freetronics makes the USBDroid, this could be a perfect solution for proof of concept stages, however to use this in a semi-permanent ZX81 mouse interface seems a bit of a waste.

Bare-bones Breadboard Arduino (and shiny lights)
Anyways, the last little blurb aside, the intention is to mount a USB shield on a Arduino clone board, one with all the other required elements built in. All parts, from the diodes to connectors for the the joystick port and a through port for an actual digital joystick. As I've never built one of these before, I thought it best to build a bare-bones cut down Arduino on a breadboard first, then hook that up a USB shield for some more testing.

At the risk of sounding like a paid advocate for Freetronics, I used their easy to follow Tutorial - build your own "breadboard Arduino", as the basis for the rest of the design. The rest of the design consisting of connecting to USB host shield and the ZXpands joystick port.

There are a number of Arduino pins that must be connected to the USB shield, INT, SS, SCK, MOSI, MISO and RESET. Also required are  a 5v power source (which we will eventually tap from the ZXpands joystick port) and a 3.3v input. To generate the 3.3v a down voltage regulator circuit needs to be added to the end design.

For full details on the USB Host Shield, there is an extensive manual and resources available on the Circuits at Home website. I can't recommend that highly enough.

Stage 3: Build a Prototype Mouse Interface PCB


Mouse PCB. Note the USB Host Shield Mounts on Top.
Lastly on the hardware side of thing I wanted to build a demo PCB board to bring everything together. I employed the help of Fritzing for the PCB design. Fritzing is not the most feature rich of programs, however it is approachable and presents a low learning curve to the casual user, perfect for these small projects.

I wanted the end PCB to be easily producible and assemblable at home. As such I designed the board to be primarily one sided, any tracks that had to be placed on the topside are straight. The final (or ongoing) Fritzing files will be available for download. if anybody plans to make the PCB at home, only the bottom layer needs to be etched and copper wire strands can be used for the linking via's. If preferred, the boards can be fully double sided for usage by PCB fabrication houses or the DIY experts.

I've covered the home PCB making process I use in a previous article back when I first started this blog. If you're interested refer to Creating A PCB For Mounting The Keyboard Controller.


Next Time: Software and Other Bits


Coming up in Part 2, I'll look into the software side of the project, post the schematics, code and parts list for building the ZX81 USB mouse interface. For now feel free to have one or more views of the teaser video on You Tube.