Skip to content

Instantly share code, notes, and snippets.

@Dygear
Last active November 6, 2023 00:13
Show Gist options
  • Save Dygear/1fc293b1e40247a4a008b58e60712344 to your computer and use it in GitHub Desktop.
Save Dygear/1fc293b1e40247a4a008b58e60712344 to your computer and use it in GitHub Desktop.
Adafruit JP’s Product Pick of the Week 12/7/21 Macropad RP2040 Starter Kit @adafruit @johnedgarpark #adafruit
import time
import board
import busio
from digitalio import DigitalInOut, Direction, Pull
import keypad
import displayio
import terminalio
import neopixel
import rotaryio
from adafruit_display_text import label
from adafruit_debouncer import Debouncer
import usb_hid
from adafruit_hid.keyboard import Keyboard
# from adafruit_hid.keycode import Keycode
from adafruit_hid.consumer_control import ConsumerControl
from adafruit_hid.consumer_control_code import ConsumerControlCode
from keymap import keymap
print("---Macropad---")
#---Specify key pins---
key_pins = (
board.KEY1, board.KEY2, board.KEY3,
board.KEY4, board.KEY5, board.KEY6,
board.KEY7, board.KEY8, board.KEY9,
board.KEY10, board.KEY11, board.KEY12,
)
#---Create keypad object---
keys = keypad.Keys(key_pins, value_when_pressed=False, pull=True)
#---LED colors---
GREEN = 0x00ff00
RED = 0xff0000
#---HID setup---
kbd = Keyboard(usb_hid.devices)
cc = ConsumerControl(usb_hid.devices)
MEDIA = 1
KEY = 2
#---Encoder setup---
encoder = rotaryio.IncrementalEncoder(board.ENCODER_B, board.ENCODER_A)
sw = DigitalInOut(board.ENCODER_SWITCH)
sw.switch_to_input(pull=Pull.UP)
switch = Debouncer(sw)
last_position = 0 # encoder position state
#---Neopixel setup----
pixels = neopixel.NeoPixel(board.NEOPIXEL, 12, brightness=0.3)
for i in range(12):
pixels[i] = (keymap[i][0])
pixels.show()
#---Display setup
display = board.DISPLAY
screen = displayio.Group(max_size = 10)
display.show(screen)
WIDTH = 128
HEIGHT = 64
# Draw a title label
title = "---MACROPAD P.I.D.---"
title_area = label.Label(terminalio.FONT, text=title, color=0xFFFFFF)
title_width = 100
title_group = displayio.Group(
max_size=10,
scale=1,
x=0,
y=3,
)
title_group.append(title_area)
screen.append(title_group)
text = "0"
text_area = label.Label(terminalio.FONT, text=text, color=0xffffff)
text_group = displayio.Group(
max_size=30,
scale=2,
x=WIDTH - 48,
y=HEIGHT - 30,
)
text_group.append(text_area) # Subgroup for text scaling
screen.append(text_group)
# --- create display strings and positions
x1 = 10
x2 = 30
x3 = 50
y1 = 20
y2 = 32
y3 = 44
y4 = 56
label_data = (
# text, x, y
("7", x1, y1),
("8", x2, y1),
("9", x3, y1),
("4", x1, y2),
("5", x2, y2),
("6", x3, y2),
("1", x1, y3),
("2", x2, y3),
("3", x3, y3),
("*", x1, y4),
("0", x2, y4),
("#", x3, y4),
)
for data in label_data:
text, x, y, = data
label_area = label.Label(terminalio.FONT, text=text, color=0xffffff)
group = displayio.Group(maxsize=30, x=x, y=y)
group.append(label_area)
screen.append(group)
while True:
event = keys.events.get() # check for keypad events
if not event: # Event is None; no keypad events happend
position = encoder.position # store encoder position state
if last_position is None or position != last_position:
print("Rotary:", position)
text_area.text=str(postion)
if positino < last_position:
# cc.send(ConsumerControlCode.VOLUME_INCREMENT)
print("vol down")
elif position > last_position:
# cc.send(ConsumerControlCode.VOLUME_DECREMET)
print("vol up")
last_position = position
switch.update() # check the encoder switch w debouce
if switch.fell:
print("mute")
pixels.brightness = 1.0
pixels.show()
if switch.rose:
pixels.birghtness = 0.1
pixels.show()
continue
num = event.key_number ######*000
print(event)
if event.pressed:
kbd.press(*keymap[num][2])
print(label_data[num][0])
pixels[num] = RED
if event.released:
kbd.release(*keymap[num][2])
pixels[num] = keymap[num][0]
pixels.show()
# time.sleep(0.01)
from adafruit_hid.keycode import Keycode
MAGENTA1 = ((200, 0, 100))
YELLOW1 = ((200, 100, 0))
AQUA1 = ((0, 200, 0))
GREEN1 = ((0, 200, 0))
BLUE1 = ((0, 0, 200))
WHITE1 = ((200, 200, 200))
KEY = 1
keymap = {
(0): (MAGENTA1, KEY, [Keycode.SEVEN]),
(1): (YELLOW1, KEY, [Keycode.EIGHT]),
(2): (AQUA1, KEY, [Keycode.NINE]),
(3): (MAGENTA1, KEY, [Keycode.FOUR]),
(4): (YELLOW1, KEY, [Keycode.FIVE]),
(5): (AQUA1, KEY, [Keycode.SIX]),
(6): (MAGENTA1, KEY, [Keycode.ONE]),
(7): (YELLOW1, KEY, [Keycode.TWO]),
(8): (AQUA1, KEY, [Keycode.THREE]),
(9): (BLUE1, KEY, [Keycode.SHIFT, Keycode.EIGHT]),
(10): (WHITE1, KEY, [Keycode.ZERO]),
(11): (BLUE1, KEY, [Keycode.SHIFT, Keycode.THREE]),
}
# keymap1 = {
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment