# SPDX-FileCopyrightText: 2021 Liz Clark for Adafruit Industries
#
# SPDX-License-Identifier: MIT
# Modified August 7, 2024 by Loreto Dumitrescu for Huffman input

import time
import board
import busio
from adafruit_neokey.neokey1x4 import NeoKey1x4
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keycode import Keycode

# use STEMMA I2C bus on RP2040 QT Py
i2c_bus = busio.I2C(board.SCL1, board.SDA1)

# Create a NeoKey object
neokey = NeoKey1x4(i2c_bus, addr=0x30)

#  create a keyboard object
keyboard = Keyboard(usb_hid.devices)

print("Huffman Input Device")

#  states for key presses
key_0_state = False
key_1_state = False
key_2_state = False
key_3_state = False

while True:
    #  switch debouncing
    #  also turns off NeoPixel on release
    if not neokey[0] and key_0_state:
        key_0_state = False
        neokey.pixels[0] = 0x0
    if not neokey[1] and key_1_state:
        key_1_state = False
        neokey.pixels[1] = 0x0
    if not neokey[2] and key_2_state:
        key_2_state = False
        neokey.pixels[2] = 0x0
    if not neokey[3] and key_3_state:
        key_3_state = False
        neokey.pixels[3] = 0x0

    #  if 1st neokey is pressed...
    if neokey[0] and not key_0_state:
        print("Button 1 pressed")
        #  turn on NeoPixel
        neokey.pixels[0] = 0xFF0000
        #  open macOS emoji menu
        keyboard.send(Keycode.ONE)
        #  delay for opening menu
        time.sleep(.2)
        key_0_state = True

    #  if 2nd neokey is pressed...
    if neokey[1] and not key_1_state:
        print("Button 2 pressed")
        #  turn on NeoPixel
        neokey.pixels[1] = 0xFFFF00
        #  open macOS emoji menu
        keyboard.send(Keycode.TWO)
        #  delay for opening menu
        time.sleep(.2)
        key_1_state = True

    #  if 3rd neokey is pressed...
    if neokey[2] and not key_2_state:
        print("Button 3 pressed")
        #  turn on NeoPixel
        neokey.pixels[2] = 0x00FF00
        #  open macOS emoji menu
        keyboard.send(Keycode.THREE)
        #  delay for opening menu
        time.sleep(.2)
        key_2_state = True

    #  if 4th neokey is pressed...
    if neokey[3] and not key_3_state:
        print("Button 4 pressed")
        #  turn on NeoPixel
        neokey.pixels[3] = 0x00FFFF
        #  open macOS emoji menu
        keyboard.send(Keycode.FOUR)
        #  delay for opening menu
        time.sleep(.2)
        key_3_state = True