Last active
April 11, 2025 03:51
-
-
Save Badbird5907/635a11dc695ad8f25497a60426980627 to your computer and use it in GitHub Desktop.
Makropad Firmware
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import board | |
import busio | |
import adafruit_pcf8574 | |
from kmk.kmk_keyboard import KMKKeyboard | |
from kmk.scanners import DiodeOrientation | |
from kmk.keys import KC, Key | |
from kmk.modules.layers import Layers | |
from kmk.modules.encoder import EncoderHandler | |
from kmk.extensions.rgb import RGB, AnimationModes | |
from kmk.extensions.media_keys import MediaKeys | |
from kmk.extensions.display import Display, TextEntry, ImageEntry | |
from kmk.extensions.display.ssd1306 import SSD1306 | |
from kmk.modules.midi import MidiKeys | |
print("INIT") | |
class MIDISliderDirection(Key): | |
def __init__(self, get_val, update, ctrl): | |
self.key = None | |
self.get_val = get_val | |
self.ctrl = ctrl | |
self.update = update | |
def on_press(self, keyboard, coord_int=None): | |
self.update() | |
val = self.get_val() | |
self.key = KC.MIDI_CC(self.ctrl,val) | |
keyboard.add_key(self.key) | |
def on_release(self, keyboard, coord_int=None): | |
keyboard.remove_key(self.key) | |
class MIDISlider(): | |
def __init__(self, ctrl, clamp, step, default, push, reset_ctrl = None): | |
self.val = default | |
def get_val(): | |
return self.val | |
def update_down(): | |
self.val = max(clamp[0], self.val - step) | |
print("down", self.val) | |
def update_up(): | |
self.val = min(clamp[1], self.val + step) | |
print("up", self.val) | |
def reset(): | |
self.val = default | |
print("reset") | |
self.down = MIDISliderDirection(get_val, update_down, ctrl) | |
self.up = MIDISliderDirection(get_val, update_up, ctrl) | |
if (push): | |
self.push = MIDISliderDirection(get_val, reset, reset_ctrl if reset_ctrl else ctrl) | |
# initialize I2C | |
i2c = busio.I2C(board.SCL, board.SDA) | |
while not i2c.try_lock(): | |
pass | |
try: | |
addresses = i2c.scan() | |
print("I2C addresses found:", [hex(addr) for addr in addresses]) | |
finally: | |
i2c.unlock() | |
pcf = adafruit_pcf8574.PCF8574(i2c, address=0x38) | |
keyboard = KMKKeyboard() | |
keyboard.col_pins = (board.D0, board.D1, board.D2, board.D3) | |
keyboard.row_pins = (board.D10, board.D9, board.D8) | |
keyboard.diode_orientation = DiodeOrientation.COL2ROW | |
keyboard.modules.append(MidiKeys()) | |
encoder_handler = EncoderHandler() | |
encoder_handler.pins = [ | |
(pcf.get_pin(0), pcf.get_pin(1), None, True, 4), | |
(pcf.get_pin(2), pcf.get_pin(3), None, True, 4) | |
] | |
vol0 = MIDISlider(7, (0,127), 8, 106, True) # Voicemeeter A1 | |
vol1 = MIDISlider(6, (0,127), 8, 106, True) # Voicemeeter V6 | |
encoder_handler.map = [ | |
((vol0.down,vol0.up), (vol1.down,vol1.up)) | |
] | |
keyboard.modules.append(encoder_handler) | |
rgb = RGB( | |
pixel_pin=board.D7, | |
num_pixels=10, | |
rgb_order=(1, 0, 2), # GRB order | |
animation_mode=AnimationModes.RAINBOW, | |
refresh_rate=60, | |
) | |
keyboard.extensions.append(rgb) | |
oled_driver = SSD1306(i2c=i2c) | |
display = Display( | |
display=oled_driver, | |
entries=[ | |
TextEntry(text="Makropad", x=0, y=0, y_anchor="T"), | |
TextEntry(text=lambda: f"A1: {vol0.val} | V6: {vol1.val}", x=0, y=16, y_anchor="T"), | |
], | |
height=32, | |
width=128, | |
dim_time=10, | |
dim_target=0.2, | |
off_time=1200, | |
brightness=1, | |
flip=True, | |
) | |
keyboard.extensions.append(display) | |
keyboard.modules.append(Layers()) | |
keyboard.extensions.append(MediaKeys()) | |
keyboard.keymap = [ | |
[ # Base layer | |
KC.A, KC.B, KC.C, KC.D, | |
KC.E, KC.F, KC.G, KC.H, | |
vol0.push, KC.J, KC.K, vol1.push, | |
] | |
] | |
if __name__ == "__main__": | |
keyboard.go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment