Wednesday 5 February 2014

Getting the keyboard to work

I really wanted to integrate the Pi into the Spectrum, as opposed to the other way around, which consequently is quite difficult.

The keyboard membrane functions to short out two pins to generate the keystrokes.  The Spectrum uses 5 and 8 pin connectors, therefore 5x8 = 40 key combinations are possible.  The first job was to integrate the spectrum membrane into a USB keyboard which could be used with the Pi.



Once this was joined together a complete key-map can be determined by pressing the keys on the membrane and seeing what keystrokes are generated by the Pi and putting them into a spreadsheet for reference :


The greyed out keys are what the Spectrum keyboard 'should' print, the red keys are what actually prints on the screen.  As can be seen the map is a complete mess, ??? denotes a function of the Japanese standard keyboard of all thing.  No matter, all that is needed is the key codes from this reference.

When a key is pressed, the computer simply reads it as a code.  For example key A may be code 65, B is code 66, C is code 67 and so on.  All that needs to be done is tell the computer code 65 no longer means A, it means something else.  It's a bit like taking the keys off the keyboard and rearranging them, but doing it on a software level.

This is where things get extremely tricky...

Everyone I have seen at this stage use a program on the Pi called xmodmap to fudge the keyboard.  It has to be run as a script or manually on every boot and takes a long time for the Pi to compile, from what I have seen it also does not fully emulate the Spectrum's keyboard nor does it run in Linux's command line interface (shell).  I wanted a more professional feel to my Spectrum Pi so I wanted to totally edit or create a key map so it worked on all levels.

This is a lot harder than it seems and there is scarce documentation on the subject requiring less than a computer degree to make sense of !

Firstly I tried to re-program the existing generic keyboard map the Pi uses, unfortunately this has no provision for a SHIFT + Function key and I need this to generate some capital letters and symbols.  I also struggled to define some of the Japanese function keys stated above and it looked like this method not going to be possible as a lot of people had stated on forums.

However, not to be beaten, using another 'model' (eg the Commodore Amiga) of key maps allowed me to generate the functions I wanted.  This is also handy as it still preserves the old generic model in case you want to go back to them later.

There are three files that need to be worked on here :


1)  /usr/share/X11/xkb/keycodes
This is where the MODELS are stored.  A model defines what code refers to what location on the keyboard.  If this sounds complicated, that's because it is.  For example, when a key is pressed code 23 may be generated, the MODEL file changes this code into an alias...in this case AD01.



2) /usr/share/X11/xkb/symbols
This is where the LAYOUTS are stored.  A layout defines what alias (defined in the keycode file) makes what key to print on the screen.  In the above example code 23 = AD01 = letter Q.




The idea is the model should be regarded as a type of keyboard, and the layout regarded as the region or language.  The whole process is long winded and complicated, especially when you're working it out with no instructions !
It also has one last twist to the tale.  The reason why people said it was impossible to re-map keyboards by editing these two files is because they missed a quirk in the last file...

3)  /etc/default/keyboard
This file tells the Pi what model and layout to use.  It is imperative to understand that if these values do not change in this file, then a new keyboard will NOT be compiled.  People were editing the model and layout files and wondering why nothing had changed, believing it didn't work.  However, by studying the Pi's startup process you could tell that when you changed the 'keyboard' file the Pi took longer to boot.  This lead me to believe that in order to generate new keyboard maps the file names of the model and layout would have to be changed in the 'keyboard' file every time you made a change / edit.




This is indeed the case, the easy way was to make a re-named clone of the model and layout files and alternate between them when testing, that way the Pi would always re-generate the keyboard maps using your trial and error values each time.

Confused ??!!
I'd be annoyed if you weren't !
It's extremely hard for me to put this into words, it's long winded and a weird way of programming the keyboard if you ask me, BUT it does work.  Through this you can generate the Spectrum ZX keyboard very accurately, it loads instantly and is widely customisable.

No comments:

Post a Comment