# Assistive tech

import board, neopixel, time, digitalio, adafruit_mpr121, time
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from audiopwmio import PWMAudioOut as AudioOut
from audiocore import WaveFile

# set up the speaker
audio = AudioOut(board.GP18)

#set up neopixel strip
strip_num_of_lights = 30
strip = neopixel.NeoPixel(board.GP16, strip_num_of_lights, brightness = 0.5, auto_write=True)

#set up 2 buttons
# Use Pull.UP for external buttons wired to ground
button_A = digitalio.DigitalInOut(board.GP6) # Wired to pin GP6
button_A.switch_to_input(pull=digitalio.Pull.UP)

button_B = digitalio.DigitalInOut(board.GP7) # Wired to pin GP7
button_B.switch_to_input(pull=digitalio.Pull.UP)

#set up keyboard HID
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayoutUS(kbd) # use US keyboard

#set up touchpad
i2c = board.STEMMA_I2C()
touch_pad = adafruit_mpr121.MPR121(i2c)

path = "sounds/"

#play sound function
def play_sound(filename):
    with open(path + filename, "rb") as wave_file:
        wave = WaveFile(wave_file)
        audio.play(wave)
        while audio.playing:
            pass


while True:
    #if button A is pressed, press left arrow
    if button_A.value == False or touch_pad[0].value: # Button is pressed when False
        print("touchpad 0 or button A hit")
        kbd.press(Keycode.LEFT_ARROW)
        strip.fill((255,255,255))
        #play_sound("hit.wav")
        strip.fill((0,0,0))



    #if button b is pressed, press right arrow
    elif button_B.value == False or touch_pad[1].value: # Button is pressed when False
        print("touchpad 1 or button B hit")
        kbd.press(Keycode.RIGHT_ARROW)
        strip.fill((255,255,255))
        #play_sound("hit.wav")

    else:
        strip.fill((0,0,0))

    kbd.release_all()






