kico-r4/code.py

431 lines
10 KiB
Python

import busio
import board
from board import *
from rainbowio import colorwheel
import waveshare_LCD1602
from digitalio import DigitalInOut, Direction, Pull
import time
import board
import usb_hid
from adafruit_hid.keyboard import Keyboard
from keyboard_layout_win_uk import KeyboardLayout
from keycode_win_uk import Keycode
"""Default I2C ports on boards that have one"""
i2c = busio.I2C(GP19, GP18)
buttonOne = DigitalInOut(board.GP0)
buttonOne.pull = Pull.UP
buttonTwo = DigitalInOut(board.GP1)
buttonTwo.pull = Pull.UP
buttonThree = DigitalInOut(board.GP2)
buttonThree.pull = Pull.UP
kbd = Keyboard(usb_hid.devices)
layout = KeyboardLayout(kbd)
lcd = waveshare_LCD1602.LCD1602(i2c, 16, 2)
lcd.setCursor(0,0)
def macro_parser(macroFileName=(str)):
file = open("macros/" + macroFileName, 'r')
clearScr = 1
lineToShow = ""
holdingKeys = 0
sendCombo = 0
keysToPress = []
for line in file:
print(line)
line = line.replace("\n", "")
workingLine = line
sleepingTime = 0
if line == "HOLD":
holdingKeys = 1
if line == "RELEASE":
if holdingKeys == 1:
sendCombo = 1
if sendCombo == 1:
if len(keysToPress) == 1:
kbd.send(keysToPress[0])
kbd.release_all()
elif len(keysToPress) == 2:
kbd.send(keysToPress[0], keysToPress[1])
kbd.release_all()
elif len(keysToPress) == 3:
kbd.send(keysToPress[0], keysToPress[1], keysToPress[2])
kbd.release_all()
elif len(keysToPress) == 4:
kbd.send(keysToPress[0], keysToPress[1], keysToPress[2], keysToPress[3])
kbd.release_all()
elif len(keysToPress) == 5:
kbd.send(keysToPress[0], keysToPress[1], keysToPress[2], keysToPress[3], keysToPress[4])
kbd.release_all()
elif len(keysToPress) == 6:
kbd.send(keysToPress[0], keysToPress[1], keysToPress[2], keysToPress[3], keysToPress[4], keysToPress[5])
kbd.release_all()
sendCombo = 0
holdingKeys = 0
keysToPress = []
if line == "super":
if holdingKeys == 1:
keysToPress.append(Keycode.WINDOWS)
else:
time.sleep(0.1)
kbd.send(Keycode.WINDOWS)
if line == "escape":
if holdingKeys == 1:
keysToPress.append(Keycode.ESCAPE)
else:
time.sleep(0.1)
kbd.send(Keycode.ESCAPE)
if line == "tab":
if holdingKeys == 1:
keysToPress.append(Keycode.TAB)
else:
time.sleep(0.1)
kbd.send(Keycode.TAB)
if line == "windows":
if holdingKeys == 1:
keysToPress.append(Keycode.WINDOWS)
else:
time.sleep(0.1)
kbd.send(Keycode.WINDOWS)
if line == "ctrl":
if holdingKeys == 1:
keysToPress.append(Keycode.LEFT_CONTROL)
else:
time.sleep(0.1)
kbd.send(Keycode.LEFT_CONTROL)
if line == "shift":
if holdingKeys == 1:
keysToPress.append(Keycode.LEFT_SHIFT)
else:
time.sleep(0.1)
kbd.send(Keycode.LEFT_SHIFT)
if line.startswith("LCD_SHOW \""):
line = line.replace("LCD_SHOW \"", "\"")
line = line.replace('"', "")
lineToShow = line
clearScr = 0
if line.startswith("#"):
pass
if line == "":
pass
if line.startswith("WAIT"):
line = line.replace("WAIT", "")
line = line.replace(" ", "")
sleepingTime = line
clearScr = 1
if line.startswith('"') and line.endswith('"'):
lineLength = len(line)
tempLine = ""
currentLine = line
for i in range(0, lineLength):
if i == 0:
pass
elif i != (lineLength - 1):
tempLine = tempLine + currentLine[i]
line = tempLine
if holdingKeys == 0:
layout.write(line)
line = currentLine
if len(tempLine) == 1:
if holdingKeys == 1:
keysToPress.append(layout.keycodes(tempLine)[0])
if line == "downarrow":
if holdingKeys == 1:
keysToPress.append(Keycode.DOWN_ARROW)
else:
time.sleep(0.1)
kbd.send(Keycode.DOWN_ARROW)
elif line == "uparrow":
if holdingKeys == 1:
keysToPress.append(Keycode.UP_ARROW)
else:
time.sleep(0.1)
kbd.send(Keycode.UP_ARROW)
elif line == "leftarrow":
if holdingKeys == 1:
keysToPress.append(Keycode.LEFT_ARROW)
else:
time.sleep(0.1)
kbd.send(Keycode.LEFT_ARROW)
elif line == "space":
if holdingKeys == 1:
keysToPress.append(Keycode.SPACE)
else:
time.sleep(0.1)
kbd.send(Keycode.SPACE)
elif line == "capslock":
if holdingKeys == 1:
keysToPress.append(Keycode.CAPS_LOCK)
else:
time.sleep(0.1)
kbd.send(Keycode.CAPS_LOCK)
elif line == "rightarrow":
if holdingKeys == 1:
keysToPress.append(Keycode.RIGHT_ARROW)
else:
time.sleep(0.1)
kbd.send(Keycode.RIGHT_ARROW)
elif line == "forwardslash":
if holdingKeys == 1:
keysToPress.append(Keycode.FORWARD_SLASH)
else:
time.sleep(0.1)
kbd.send(Keycode.FORWARD_SLASH)
elif line == "backslash":
if holdingKeys == 1:
keysToPress.append(Keycode.BACKSLASH)
else:
time.sleep(0.1)
kbd.send(Keycode.BACKSLASH)
elif line == "backspace":
if holdingKeys == 1:
keysToPress.append(Keycode.BACKSPACE)
else:
time.sleep(0.1)
kbd.send(Keycode.BACKSPACE)
elif line == "enter":
if holdingKeys == 1:
keysToPress.append(Keycode.ENTER)
else:
time.sleep(0.1)
kbd.send(Keycode.ENTER)
print("KEYS_PRESSED: " + str(len(keysToPress)))
if clearScr == 1 and workingLine.startswith("WAIT "):
lcd.clear()
lcd.setCursor(0,1)
lcd.printout("> "+workingLine)
lcd.setCursor(0,0)
lcd.printout(lineToShow)
print(lineToShow)
if lineToShow != "":
lineToShow = ""
else:
lcd.clear()
lcd.setCursor(0,1)
lcd.printout("> "+workingLine)
time.sleep(float(sleepingTime))
time.sleep(0.5)
while True:
if not buttonOne.value:
lcd.clear()
lcd.setCursor(0,0)
lcd.printout("Install Arch")
time.sleep(1)
lcd.clear()
macro_parser("archinstall.mcr")
lcd.clear()
elif not buttonTwo.value:
lcd.clear()
lcd.setCursor(0,0)
lcd.printout("Macro Syntax")
time.sleep(1)
lcd.clear()
macro_parser("syntax.mcr")
lcd.clear()
elif not buttonThree.value:
lcd.clear()
lcd.setCursor(0,0)
lcd.printout("Test Macro")
time.sleep(1)
lcd.clear()
macro_parser("test.mcr")
lcd.clear()
lcd.setCursor(0,0)
lcd.printout("Kico v4")
lcd.setCursor(0,1)
lcd.printout("Hit a key!")