# Keyboard Emulator Using a Pi Pico and CircuitPython
# Additional Libraries: adafruit_hid
# https://github.com/adafruit/Adafruit_CircuitPython_HID

# Original code: 2018 Kattni Rembor for Adafruit Industries (MIT License)
# Updated version: 2025 Steve McL for Iffy Books (wtfpl)

import time
import board
import digitalio
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode

time.sleep(1)  # Sleep to avoid race conditions

# The keyboard object
keyboard = Keyboard(usb_hid.devices)
keyboard_layout = KeyboardLayoutUS(keyboard)  # US keyboard layout

# The GPIO pins we'll use for our keys
gpio_pins = [board.GP3, board.GP12]

key_pins = []

for pin in gpio_pins:
    key_pin = digitalio.DigitalInOut(pin)
    key_pin.direction = digitalio.Direction.INPUT
    key_pin.pull = digitalio.Pull.UP
    key_pins.append(key_pin)

led = digitalio.DigitalInOut(board.GP25)
led.direction = digitalio.Direction.OUTPUT
led.value = False

# Infinite loop checking whether a key is pressed
while True:
    for key_pin in key_pins:
        if not key_pin.value:  # Is the key pressed?
            
            # Action for first key
            if key_pin == key_pins[0]:
                
                # Type email signature
                keyboard_layout.write("--\nName Nameson\nIffy Books\n404 S. 20th St., PHL\nhttps://iffybooks.net")
            
            # Action for second key
            elif key_pin == key_pins[1]:
                
                # Toggle LED
                if led.value == False:
                    led.value = True
                else:
                    led.value = False
                    
                # Press keyboard shortcut keys
                keyboard.press(Keycode.SHIFT, Keycode.COMMAND, Keycode.SEMICOLON)
                time.sleep(0.2)
                keyboard.release_all()