Missing files added

This commit is contained in:
Maciej Sokołowski 2021-07-14 16:34:22 +02:00
parent 0f73e29146
commit 00d1ed0f8e
9 changed files with 167 additions and 0 deletions

View file

@ -0,0 +1,18 @@
class Keybow:
"""
Abstract class providing common interface to RGB-backlit keyboard
Subclasses should fill _switches and _display properties.
Filling _i2c is optional, unless you want to use i2c() accessor.
"""
def set_pixel(self, idx, r, g, b):
self._display.set_pixel(idx, r, g, b)
def num_keys(self):
return self._switches.num_switches()
def switch_state(self, idx):
return self._switches.switch_state(idx)
def i2c(self):
return self._i2c

View file

@ -0,0 +1,6 @@
class Display:
"""
Abstract class providing common interface to the set of pixels
"""
def set_pixel(self, idx, r, g, b):
raise NotImplementedError

View file

@ -0,0 +1,13 @@
import adafruit_dotstar
from . import Display
class Dotstar(Display):
"""
Display consisting of dotstars
"""
def __init__(self, clock, data, count):
self._pixels = adafruit_dotstar.DotStar(clock, data, count)
def set_pixel(self, idx, r, g, b):
self._pixels[idx] = (r, g, b)

View file

@ -0,0 +1,13 @@
from adafruit_is31fl3731.keybow2040 import Keybow2040 as Pixels
from . import Display
class Keybow2040(Display):
"""
Keybow 2040 4x4 display
"""
def __init__(self, i2c):
self._pixels = Pixels(i2c)
def set_pixel(self, idx, r, g, b):
self._pixels.pixelrgb(idx % 4, idx // 4, r, g, b)

View file

@ -0,0 +1,37 @@
import board
import busio
from digitalio import DigitalInOut, Direction
from .switches.tca9555 import TCA9555 as Switches
from .display.dotstar import Dotstar as Display
from . import Keybow
NUM_KEYS = 16
# Let's match PIM56X orientation
_ROTATED = {
0: 12, 1: 8, 2: 4, 3: 0,
4: 13, 5: 9, 6: 5, 7: 1,
8: 14, 9: 10, 10: 6, 11: 2,
12: 15, 13: 11, 14: 7, 15: 3,
}
class PIM551(Keybow):
def __init__(self):
self._i2c = busio.I2C(board.GP5, board.GP4)
self._switches = Switches(self._i2c, NUM_KEYS)
self._display = Display(board.GP18, board.GP19, NUM_KEYS)
self._cs = DigitalInOut(board.GP17)
self._cs.direction = Direction.OUTPUT
self._cs.value = 1
def set_pixel(self, idx, r, g, b):
# https://github.com/pimoroni/pimoroni-pico/blob/main/libraries/pico_rgb_keypad/pico_rgb_keypad.cpp#L20-L45
# code above sets CS only for the time of updating LEDs, so let's do the same
self._cs.value = 0
super().set_pixel(_ROTATED[idx], r, g, b)
self._cs.value = 1
def switch_state(self, idx):
return super().switch_state(_ROTATED[idx])

View file

@ -0,0 +1,30 @@
import board
from .switches.gpio import GPIO as Switches
from .display.keybow2040 import Keybow2040 as Display
from . import Keybow
# These are the 16 switches on Keybow, with their board-defined names.
_PINS = [board.SW0,
board.SW1,
board.SW2,
board.SW3,
board.SW4,
board.SW5,
board.SW6,
board.SW7,
board.SW8,
board.SW9,
board.SW10,
board.SW11,
board.SW12,
board.SW13,
board.SW14,
board.SW15]
class PIM56X(Keybow):
def __init__(self):
self._i2c = board.I2C()
self._switches = Switches(_PINS)
self._display = Display(self._i2c)

View file

@ -0,0 +1,9 @@
class Switches:
"""
Abstract class providing common interface to the set of switches
"""
def num_switches(self):
raise NotImplementedError
def switch_state(self, idx):
raise NotImplementedError

View file

@ -0,0 +1,19 @@
from digitalio import DigitalInOut, Direction, Pull
from . import Switches
class GPIO(Switches):
"""
Switches connected directly to GPIO
"""
def __init__(self, pins):
self._switches = [DigitalInOut(pin) for pin in pins]
for switch in self._switches:
switch.direction = Direction.INPUT
switch.pull = Pull.UP
def num_switches(self):
return len(self._switches)
def switch_state(self, idx):
return not self._switches[idx].value

View file

@ -0,0 +1,22 @@
from . import Switches
class TCA9555(Switches):
"""
Switches connected via TCA9555 IO expander on i2c
"""
def __init__(self, i2c, count):
self._count = count
self._i2c = i2c
def num_switches(self):
return self._count
def switch_state(self, idx):
buffer = bytearray(self._count // 8)
buffer[0] = 0x0
while not self._i2c.try_lock():
pass
self._i2c.writeto_then_readfrom(0x20, buffer, buffer, out_end=1)
self._i2c.unlock()
b = buffer[0] | buffer[1] << 8 # up to 16 buttons supported now
return not (1 << idx) & b