DE BLAUWE SCHICHT on the WORLD WIDE WEB

Main

Tinkering
    ETOPO1
    QL Keys (1)
    QL Keys (2)
    Atari2USB
    MOS 6581 (1)
    MOS 6581 (2)
    Indy battery
    C64 and BT

Writing

Pictures

Retrogaming

About

The Commodore 64 keyboard as a Bluetooth HID (for an iPad)

(Note: In the meantime, the BlueSMiRF HID Bluetooth module mentioned below appears to have become difficult to obtain. I don't know of a replacement (yet); for now I will leave this page as it is for the perusal of the curious.)

Introduction

The Commodore 64 is an iconic eight-bit computer that was introduced in the early 1980's. Partly because of an aggressive marketing campaign this machine attained great popularity and was produced in large numbers for about ten years in various incarnations, the most well-known being the "bread bin". This was a time when computers like these were known as "home computers" and entire (printed!) magazines - not to mention whole tribes of followers - were dedicated to the machines of a single manufacturer.

c64 x 2
The iPad and the Commodore 64: convenience and aesthetics, respectively.

One of the specimens in my possession (if you are as old as I am you probably have at least 1.5 C64's in your collection) is a nice unyellowed C-type. The C64C is a cost-reduced version with a case resembling the equally amazing multi-mode Commodore 128 - see the photograph above. Some while ago it occured to me that the C64 might make a nice iPad keyboard and stand all in one. I imagined that it wouldn't be too hard to do and went to work. There was one constraint: the C64 to be used was in good condition and it should remain possible to return it to its original state.

The central components used are an ATMEL 328p microcontroller and a BlueSMiRF Bluetooth HID module. The ATMEL was programmed using the Arduino development tools.

Interfacing with the keyboard matrix

The keyboard consists of a matrix of eight rows and eight columns, with a key on each crossing. When a key is pressed, the crossing becomes an intersection and a row is joined to a column. The 8+8=16 lines (plus two others for the RESTORE key) are connected to CN1 on the C64 main board. A very useful illustration of the matrix lay-out can be found in the Commodore 64 Service Manual.

Illustration of the C64 keyboard matrix
The C64 keyboard matrix. Taken from the Commodore 64 Service
Manual (click for larger picture).

Detecting pressed keys is fairly straightforward. The keyboard cable is removed and attached to the ATMEL microcontroller as shown in the schematic below. The column lines are attached to the microcontroller's outputs D9-D12 and (reconfigured) A0-A3; the row lines are connected to D2-D8 and D13 (with the built-in pull-up resistors switched on). There is one extra line for the RESTORE key, which has a special function and effect on the C64, but is scanned by the microcontroller as a normal key on pin A4.

Detected keypresses and releases are mapped to matching Bluetooth codes and sent to a BlueSMiRF HID Bluetooth module.

C64 to Bluetooth schematic
C64 keyboard to Bluetooth schematic. Illustration drawn with Dia (click to enlarge).

The circuit is powered by a 9V battery, using a 7805 voltage regulator to obtain 5V. The C64 power LED is wired to the device as well.

Prototype C64 to Bluetooth adapter
C64 keyboard to Bluetooth prototype. The keyboard is to be connected to the row of headers. Protruding on the left is the Bluetooth module.

Configuring and using the BlueSMiRF Bluetooth module

The BlueSmirf HID is initially set to TTY mode. In order to have it work as an HID, a SparkFun FTDI Basic Breakout Board is used to change settings. The FTDI board provides a USB to 5V or 3.3V serial interface, so that one can communicate with the Bluetooth module using a serial console, such as Hyperterminal in Windows or the CLI command "screen" in Linux. The following steps are to be made:

  • First, establish a connection (using hyperterm on windows, or screen /dev/serial/by-id/ 115200 on linux).
  • Second, put the modem in command mode by typing $$$ (pressing the enter key is not necessary in this case).
  • To place the modem in HID mode, enter
    s~,6 <enter>
  • In order to make the modem discoverable by iOS devices, enter
    s,0 <enter>
  • Change its name to something appropriate (was RNBT_26C4)
    sn,C64BT <enter>
  • Reboot the device and place it in data mode
    r,1 <enter>

BlueSMiRF BT module
The BlueSMiRF Bluetooth HID (center), with a TTL
to USB adapter (top) and colorful wires (bottom).

The BlueSMiRF should now be ready to use. Wiring the module to the microcontroller involves powering it with the same 5V used for the ATMEL and connecting "RTS-O" to "CTS-I". Pin "RX-I" is connected to the TX pin on the ATMEL and the "TX-O" is left open, as communication is one-way only.

Keyboard mapping

The firmware running on the microcontroller is currently tailored for iOS devices, as that's all the tablet and smartphone hardware I have in my possession. The age difference between the C64 and iPad keyboards turned out to be not too great to bridge; it's mostly a matter of wiring the lay-out in software. Some adaptations and augmentations are necessary (if only to be able to get curly braces or a backslash). Note that this is not a fully featured n-key rollover implementation; it suffices for iPad however.

The "CBM" key is of course a logical choice for the "Command" or "GUI" key, but this does not feature very prominently in iOS, although it can be found on both Apple's and third-party Bluetooth keyboards sold for the iPad - those offered by LogiTech, for example. The Alt modifier key is more important; it is used to enter diacriticals and foreign or rarely used symbols. The "Restore" key is assigned to that function. The C64 keyboard already features Control and Shift keys, and these are naturally used in that capacity.

The Shift keys on the C64 keyboard matrix are wired separately (unlike those on the Apple iie or Sinclair QL). This is used for some much-needed fine-grained modification of the cursor and Clear/Home keys. The right shift modifies the direction of the cursor keys, entirely in the style of the C64 OS itself. The left shift key enables select, in the manner of modern keyboards. Left shift + right shift + cursor left results in "move and select one character to the right of the current cursor position". Normal keys behave non-discriminantly with regard to the shift keys.

The software has been written for the popular Arduino Uno using the Arduino development software. After prototyping was done, the ATMEL 328p was extracted and placed on an experimenting board (see photo above).

C64 to Bluetooth sketch for Arduino

The table below gives an overview of some special keys and key combinations. Note the assignments of the function keys and the modifier-and-key combination needed for some modern OS-related keys. Other keys will behave as their marking would suggest (although the graphical symbols are not supported).

Keys Effect
---------------------- --------------------------------
CBM Command
RESTORE Option
Shift+INST/DEL Delete
Lshift+arrow Select text
Lshift+Rshift+arrow Select text in reverse direction
CLR/HOME Send cursor to begin of line
Rshift+CLR/HOME Send cursor to end of line
Lshift+CLR/HOME Select to begin of line
Lshift+Rshift+CLR/HOME Select to end of line
CBM+M \ (backslash)
CBM+B | (pipe)
CBM+F _ (underscore)
CBM+J ` (backtick)
CBM+S ~ (HOME is where the HEART is)
Lshift+: or Lshift+; [ or ]
Rshift+: or Rshift+; { or }
F1/F2 Play toggle/Volume toggle
F3/F4 Forward track/Volume up
F5/F6 Backward track/Volume down
F7/F8 Home/Keyboard toggle
RUN STOP Escape
Arrow left TAB
SHIFT LOCK Shift lock (not Caps lock!)

Assembly and a sort of stand thing, too

Once the soldering and prototyping is done, the board can be fitted inside the C64. It turns out there is a narrow space between the rear edge of the keyboard and the top case, and that both battery and electronics fit there very well indeed. The wiring is tamed using office tape.

C64 to Bluetooth adapter in place
The battery and the electronics fit snugly under the rear edge of the keyboard.

The flat rear surface on the C64C provides an area to set the iPad, provided some way can be found to keep it upright. In an electronics shop I found some used "slotted" heat sinks that would serve for this purpose. I like heats sinks; they simply look cool (note: there is a pun nearby).

No permanent changes were to be made to the case; no screws could be used or new holes made. I found that the heat sinks could be fixed in place using thin metal wire that fits through the grille on the flat rear section of the C64C. The slot where the tablet would sit was then padded with thin rubber sheet to prevent scratches on the aluminum exterior.

A microswitch is mounted to one of the two heat sinks, which is wired to the battery. By placing the iPad the switch is pressed down, activating the electronics.

2nd hand heat sink Mounted on top of the C64C, with switch Fixed with wire through the grille
From left to right: heat sink used to stand up a tablet; mounted on the rear of the C64C, with switch; fixed on the underside of the top case using thin metal wire.

This construction is of course not necessary; any normal iPad stand could do the job just as well. I just liked the notion of the Bluetooth keyboard being turned on by placing the tablet in its stand. Another earlier and untried idea was to modify the rear of the machine, detaching the rear and employing a flip-up mechanism.

To do

The possibility to keep the keyboard connected to the C64 main board, so that the former can be used in two modes. However, in unpowered state the 6526 CIA doesn't play nice with the current incarnation of the scanning circuit, so that needs some thinking. Also some means is required for the microcontroller to detect whether the computer is turned on, and in that case to stop it scanning the matrix in order to let the C64 have priority there.

Credits/useful URLs

  • I got the BlueSMiRF HID device from Sparkfun.com, as well as its datasheet.
  • The Commodore 64 Service Manual can be found in several places on the Internet; try Project 64 for instance.

(Originally written 2014/03/04)

Back