Close

Odyssey continues

A project log for Portable Raspberry PI Zero

a 3D printed portable computer with a QWERTY keyboard

davedarkodavedarko 11/09/2016 at 20:551 Comment

Python script is about the same where I was before. Python is weird when you're coming from JS and PHP, let me tell you.

AND the uinput part works!! woot! Thanks to adafruit, mainly. And my copy and paste skills.

I still need to map the keys and find the driver for the display (MacGyver was drawn by an adafruit python library).

import subprocess
import sys
import time

import Adafruit_GPIO as GPIO
import Adafruit_GPIO.I2C as I2C
import Adafruit_GPIO.PCF8574 as PCF

import uinput

row = 1
rowI = 1
col = 1024

addressH = 0x20
addressL = 0x21

gpioH = PCF.PCF8574(addressH, busnum=1)
gpioL = PCF.PCF8574(addressL, busnum=1)

KEY_MAPPING = {
                                0: uinput.KEY_UP,    # Each line here should define a $
                                1: uinput.KEY_DOWN,  # that maps the capacitive touch $
                                2: uinput.KEY_LEFT,  # to an appropriate key press.
                                3: uinput.KEY_RIGHT, #
                                4: uinput.KEY_B,     # For reference the list of possi$
                                5: uinput.KEY_A,     # values you can specify is defin$
                                6: uinput.KEY_ENTER, # http://www.cs.fsu.edu/~baker/de$
                                7: uinput.KEY_SPACE, # http/source/linux/include/linux$
                          }


# Make sure uinput kernel module is loaded.
subprocess.check_call(['modprobe', 'uinput'])

# Configure virtual keyboard.
device = uinput.Device(KEY_MAPPING.values())

def shiftRange (start, end):
        while start > end:
                yield start
                start = start>>1

while True:
        rowS = 0x03FF ^ row
        rowL = (rowS<<5) & 0x00FF
        rowH = (rowS>>3) & 0x00FF

        addL = rowL | 0x1F
        addH = rowH

        gpioH.iodir = addH
        gpioL.iodir = addL

        gpioH._write_pins()
        gpioL._write_pins()

        keys = gpioL._read_pins()

        keys = keys & 0x1F;
        keysI = 1
        for k in shiftRange(16,0):
                if k & keys == 0:
                        device.emit_click(KEY_MAPPING[rowI-1])
                        time.sleep(0.5)
                keysI += 1

        rowI += 1
        row = row << 1
        if row == col:
                row = 1
                rowI = 1

Discussions

davedarko wrote 01/03/2017 at 23:44 point

I think I have to throw some pullups around the I2C lines, it doesn't work anymore. Well it does with different code but I haven't changed it. meh.

  Are you sure? yes | no