This project allows a ZX Spectrum keyboard to interface with a Raspberry Pi via GPIO pins. By leveraging the GPIO capabilities of the Raspberry Pi, the keyboard's rows and columns can be scanned to detect key presses and simulate key events on the Pi. The project also supports two operational modes: normal
and symbol
, enabling the use of special characters.
- Interface a ZX Spectrum keyboard to a Raspberry Pi.
- Map key presses to Linux input events using the
uinput
module. - Toggle between
normal
andsymbol
modes for additional character inputs. - Full support for both regular and special characters, including combinations.
- Built-in debounce mechanism to prevent multiple detections of the same key press.
- Clean GPIO setup and teardown.
- Raspberry Pi (any model with GPIO support)
- ZX Spectrum keyboard
- GPIO wires and connectors
- Python 3
- Libraries:
RPi.GPIO
: To manage GPIO pins.uinput
: To emulate keyboard events.
Install the required Python libraries:
sudo apt-get install python3-dev python3-setuptools python3-pip
sudo pip3 install python-uinput
-
Data Lines (Keyboard Output to Raspberry Pi Input):
- Connect ZX Spectrum keyboard data lines to the following GPIO pins:
- Data Line 1 → GPIO 26
- Data Line 2 → GPIO 19
- Data Line 3 → GPIO 13
- Data Line 4 → GPIO 6
- Data Line 5 → GPIO 5
- Connect ZX Spectrum keyboard data lines to the following GPIO pins:
-
Address Lines (Keyboard Input from Raspberry Pi Output):
- Connect ZX Spectrum keyboard address lines to the following GPIO pins:
- Address Line 1 → GPIO 25
- Address Line 2 → GPIO 24
- Address Line 3 → GPIO 23
- Address Line 4 → GPIO 22
- Address Line 5 → GPIO 27
- Address Line 6 → GPIO 18
- Address Line 7 → GPIO 17
- Address Line 8 → GPIO 4
- Connect ZX Spectrum keyboard address lines to the following GPIO pins:
- The Raspberry Pi scans the ZX Spectrum keyboard matrix by toggling address lines and reading data lines.
- When a key is pressed, the corresponding matrix position is detected, and a key event is generated using the
uinput
module. - Pressing the
RIGHTCTRL
key toggles betweennormal
andsymbol
modes for additional character input. - Pressing the
LEFTSHIFT
key modifies input for uppercase letters and some special characters.
- Matrix Scanning: Each row and column is checked to identify pressed keys.
- Key Mapping: Separate mappings for
normal
andsymbol
modes. - Debounce Mechanism: Prevents multiple detections of the same key press.
- Keyboard Simulation: Uses
uinput
to simulate key events.
read_keyboard()
: Reads the keyboard matrix to detect key presses.get_key_name(i, j)
: Determines the key's name based on the current mode and matrix position.send_key_event(key_name)
: Emits the corresponding key event usinguinput
.toggle_mode()
: Switches betweennormal
andsymbol
modes.
-
Connect the Keyboard:
- Wire the ZX Spectrum keyboard to the Raspberry Pi as described above.
-
Run the Script:
python3 zx_spectrum_keyboard.py
-
Interact with the Keyboard:
- Press keys on the ZX Spectrum keyboard, and the corresponding key events will be generated on the Raspberry Pi.
-
Toggle Modes:
- Use the
RIGHTCTRL
key to switch betweennormal
andsymbol
modes.
- Use the
-
Stop the Program:
- Press
Ctrl+C
to terminate the script safely.
- Press
-
No Key Response:
- Check your wiring.
- Verify the GPIO pin numbering matches your setup.
-
Multiple Key Presses Detected:
- Adjust the debounce delay in the
read_keyboard
loop.
- Adjust the debounce delay in the
-
Error:
ModuleNotFoundError: No module named 'uinput'
:- Ensure the
python-uinput
library is installed.
- Ensure the
- Add support for more keyboard layouts.
- Optimize scanning speed and debounce logic.
- Implement additional special key combinations.
This project is licensed under the MIT License. Feel free to use, modify, and distribute it as per the terms of the license.
Contributions are welcome! Please fork the repository and submit a pull request with your improvements or ideas.
Developed by Mike. If you have any questions or feedback, feel free to contact me at.