Skip to content

Instantly share code, notes, and snippets.

@codepope
Created February 13, 2021 17:44
Show Gist options
  • Save codepope/f673be482da34b455cb8e86625e30165 to your computer and use it in GitHub Desktop.
Save codepope/f673be482da34b455cb8e86625e30165 to your computer and use it in GitHub Desktop.
pimoroni rgb keypad for circuitpython - wip
mport board
import digitalio
import busio
import random
import time
class RGBKeyPad:
WIDTH=4
HEIGHT=4
NUMPADS=WIDTH*HEIGHT
CS_PIN=board.GP17
SCK_PIN=board.GP18
MOSI_PIN=board.GP19
BUFFERSIZE=4+NUMPADS*4
def __init__(self):
self.cs=digitalio.DigitalInOut(self.CS_PIN)
self.cs.direction=digitalio.Direction.OUTPUT
self.cs.value=True
self.spi=busio.SPI(self.SCK_PIN,MOSI=self.MOSI_PIN)
self.buffer=bytearray(self.BUFFERSIZE)
self.set_brightness(0.1)
self.update()
def update(self):
self.cs.value=False
self.spi.try_lock()
self.spi.write(self.buffer)
print(self.buffer)
self.spi.unlock()
def set_brightness(self,b):
if b<0.0 or b>1.0:
return
binval=0b1110000 | int(b*0b11111)
for i in range(self.NUMPADS):
self.buffer[4+i*4] = binval
def illuminate(self,x,y,r,g,b):
if x < 0 or x >= self.WIDTH or y < 0 or y >= self.HEIGHT:
return
offset = 4 + ((x + (y * self.WIDTH)) * 4)
self.buffer[offset + 1] = b
self.buffer[offset + 2] = g
self.buffer[offset + 3] = r
def illuminate_pad(self,i,r,g,b):
offset = 4 + (i * 4)
self.buffer[offset + 1] = b
self.buffer[offset + 2] = g
self.buffer[offset + 3] = r
def clear(self):
for i in range(self.NUMPADS):
self.illuminate_pad(i,0,0,0)
rgb=RGBKeyPad()
t=0.1
ti=0.1
while True:
rgb.clear()
rgb.update()
time.sleep(0.1)
for m in range(0,4):
for n in range(0,4):
rgb.illuminate(n,m,random.randint(128,255),random.randint(128,255),random.randint(128,255))
rgb.set_brightness(t)
t=t+ti
if t<0.1 or t>0.9:
ti=-ti
rgb.update()
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment