|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# Adafruit Raspberry Pi MPR121 Keyboard Example |
| 4 | +# Author: Tony DiCola, Brennen Bearnes |
| 5 | +# |
| 6 | +# Allows you to turn touches detected by the MPR121 into key presses on a |
| 7 | +# Raspberry Pi. |
| 8 | +# |
| 9 | +# Dependencies |
| 10 | +# ============ |
| 11 | +# |
| 12 | +# Make sure you have the required dependencies by executing the following commands: |
| 13 | +# |
| 14 | +# sudo apt-get update |
| 15 | +# sudo apt-get install build-essential python-dev python-pip libudev-dev |
| 16 | +# sudo pip3 install python-uinput |
| 17 | +# sudo pip3 install adafruit-circuitpython-mpr121 |
| 18 | +# |
| 19 | +# Usage |
| 20 | +# ===== |
| 21 | +# |
| 22 | +# To use this program you first need to connect the MPR121 board to the Raspberry |
| 23 | +# Pi (either connect the HAT directly to the Pi, or wire the I2C pins SCL, SDA to |
| 24 | +# the Pi SCL, SDA, VIN to Pi 3.3V, GND to Pi GND). |
| 25 | +# |
| 26 | +# Next define the mapping of capacitive touch input presses to keyboard |
| 27 | +# button presses. Scroll down to the KEY_MAPPING dictionary definition below |
| 28 | +# and adjust the configuration as described in its comments. |
| 29 | +# |
| 30 | +# Finally run the script as root: |
| 31 | +# |
| 32 | +# sudo python3 pi_keyboard.py |
| 33 | +# |
| 34 | +# Try pressing buttons and you should see key presses made on the Pi! (Note |
| 35 | +# that you need to be logged directly into the Pi to see the keypresses - |
| 36 | +# over an SSH or console cable connection, you won't see anything.) |
| 37 | +# |
| 38 | +# Press Ctrl-C to quit at any time. |
| 39 | +# |
| 40 | +# Copyright (c) 2014-2019 Adafruit Industries |
| 41 | +# |
| 42 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 43 | +# of this software and associated documentation files (the "Software"), to deal |
| 44 | +# in the Software without restriction, including without limitation the rights |
| 45 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 46 | +# copies of the Software, and to permit persons to whom the Software is |
| 47 | +# furnished to do so, subject to the following conditions: |
| 48 | +# |
| 49 | +# The above copyright notice and this permission notice shall be included in |
| 50 | +# all copies or substantial portions of the Software. |
| 51 | +# |
| 52 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 53 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 54 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 55 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 56 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 57 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 58 | +# THE SOFTWARE. |
| 59 | + |
| 60 | +import logging |
| 61 | +import subprocess |
| 62 | +import time |
| 63 | +import board |
| 64 | +import busio |
| 65 | +import uinput |
| 66 | +import adafruit_mpr121 |
| 67 | + |
| 68 | +# Define mapping of capacitive touch pin presses to keyboard button presses. |
| 69 | + |
| 70 | +# Each line here should define a dict entry that maps the capacitive touch |
| 71 | +# input number to an appropriate key press. |
| 72 | +# |
| 73 | +# For reference the list of possible uinput.KEY_* values you can specify is |
| 74 | +# defined in linux/input.h: |
| 75 | +# http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/input.h?v=2.6.11.8 |
| 76 | +# |
| 77 | +# Make sure a cap touch input is defined only once or else the program will |
| 78 | +# fail to run! |
| 79 | + |
| 80 | +KEY_MAPPING = { |
| 81 | + 0: uinput.KEY_UP, |
| 82 | + 1: uinput.KEY_DOWN, |
| 83 | + 2: uinput.KEY_LEFT, |
| 84 | + 3: uinput.KEY_RIGHT, |
| 85 | + 4: uinput.KEY_B, |
| 86 | + 5: uinput.KEY_A, |
| 87 | + 6: uinput.KEY_ENTER, |
| 88 | + 7: uinput.KEY_SPACE, |
| 89 | +} |
| 90 | + |
| 91 | +# Sleep this long between polling for events: |
| 92 | +EVENT_WAIT_SLEEP_SECONDS = 0.25 |
| 93 | + |
| 94 | +# Uncomment to enable debug message logging (might slow down key detection). |
| 95 | +logging.basicConfig(level=logging.DEBUG) |
| 96 | + |
| 97 | +# Make sure uinput kernel module is loaded. |
| 98 | +subprocess.check_call(['modprobe', 'uinput']) |
| 99 | + |
| 100 | +# Configure virtual keyboard. |
| 101 | +device = uinput.Device(KEY_MAPPING.values()) |
| 102 | + |
| 103 | +# Create I2C bus. |
| 104 | +i2c = busio.I2C(board.SCL, board.SDA) |
| 105 | + |
| 106 | +# Create MPR121 object. |
| 107 | +mpr121 = adafruit_mpr121.MPR121(i2c) |
| 108 | + |
| 109 | +# Event loop to wait for pin changes and respond to them. |
| 110 | +print('Press Ctrl-C to quit.') |
| 111 | +while True: |
| 112 | + # Loop through all defined inputs: |
| 113 | + for pin, key in KEY_MAPPING.items(): |
| 114 | + # Call is_touched and pass it then number of the input. If it's touched |
| 115 | + # it will return True, otherwise it will return False. |
| 116 | + if mpr121[pin].value: |
| 117 | + logging.debug('Input %i touched!', pin) |
| 118 | + logging.debug('Key: %s', key) |
| 119 | + device.emit_click(key) |
| 120 | + time.sleep(EVENT_WAIT_SLEEP_SECONDS) # Small delay to keep from spamming output messages. |
0 commit comments