import time
import board
from digitalio import DigitalInOut, Direction, Pull
import usb_hid
from adafruit_hid.keyboard import Keyboard
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
from adafruit_hid.keycode import Keycode
from adafruit_hid.mouse import Mouse
from adafruit_display_text import label
import adafruit_rgbled
import terminalio
import displayio
from pwmio import PWMOut
import adafruit_matrixkeypad
import keypad
from keypad import KeyMatrix
import json
import os
import adafruit_ssd1306
import busio
# ===========================================
designMode = False # Adds a template for aligning UI elements on the SSD1306
mouse = Mouse(usb_hid.devices) # for rotary encoder
keyboard = Keyboard(usb_hid.devices)
shift_key = Keycode.SHIFT
screen_group = displayio.Group()
config = {
"ledPreset" : 1,
"toggleGear" : False,
"toggleConfig" : False,
"toggleDoors" : False,
"toggleLights" : False,
"ledPresetColor" : "MISC",
}
# LED settings
ignoreFirstLightPress = 0
# Panel settings
panelMode = "standard"
# OLED settings:
#https://www.instructables.com/Graphics-on-a-SSD1306-I2C-OLED-128x64-Display-With/
i2c = busio.I2C(board.GP21, board.GP20)
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c)
oled.contrast(1)
oled.fill(0)
print ("oled.fill - initial")
#text/lines from: https://www.instructables.com/Graphics-on-a-SSD1306-I2C-OLED-128x64-Display-With/
# https://content.instructables.com/FTY/KYZM/JWQNZEHM/FTYKYZMJWQNZEHM.py
# Status text
charTop = 3
charBtm = charTop+8
displayRefreshed = False
# Notifications
statusNameLocY = 16 # Y coordinates of status message - Name
statusValLocY = 27 # Y coordinates of status message - Value
status2NameLocY = 43 # Y coordinations of the second status message - Name
status2ValLocY = 54 # Y coordinations of the second status message - Value
showNotification = False
notificationCounter = 0
notificationTimeMs = 3000 # Not quite ms
notificationText = ["", 0, "", 0]
notificationTimeMs = notificationTimeMs*2.25
powerToggle = "on"
powerTogglePrev = "on"
powerTriangleMode = ["Default",44]
'''
def save_config(config, configpath):
try:
with open(configpath, "w") as configfile:
json.dump(config,configfile)
print("config:",config)
print("file saved")
except OSError as err:
print(f"Error occurred: {err}")
configpath = "/config.json"
#save_config(config,configpath)
with open(configpath,"r") as cfgload:
fileinfo = os.stat(configpath)
filesize = fileinfo[6]
if (filesize > 0):
print("loading config")
config = json.load(cfgload)
print(f'Preset: {config["ledPreset"]}')
ledPresetColor = config["ledPresetColor"]
ledPresetId = 0
print("ledPreset val:",ledPresetColor)
ignoreFirstLightPress = 0;
else:
save_config(config,configpath)
print("wrote to config file")
'''
ledPresetId = 1
ledPresetColor = "MISC"
boardToggleState = False
boardLastToggleState = False
prevToggle = 1
toggleGear = config["toggleGear"]
toggleConfig = config["toggleConfig"]
toggleDoors = config["toggleDoors"]
toggleLights = config["toggleLights"]
toggleRows = [board.GP0, board.GP1] # toggle up and down
toggleCols = [board.GP2, board.GP3, board.GP4, board.GP5]
matrixToggles = keypad.KeyMatrix(toggleRows,toggleCols)
tglLndGrUp = Keycode.T # ex: GP0,GP2 - Sending Alt + Tl
tglLndGrDn = Keycode.Y # ex: GP1,GP2 - Sending Alt + Y
tglCfgOpen = Keycode.U # ex: GP0,GP3 - Sending Alt + U
tglCfgClose = Keycode.I # ex: GP1,GP3 - Sending Alt + I
tglDrsOpen = Keycode.O # ex: GP0,GP4 - Sending Alt + O
tglDrsClose = Keycode.P # ex: GP1,GP4 - Sending Alt + P
tglHdlghtOn = Keycode.L # ex: GP0,GP5
tglHdlghtOff = Keycode.L # ex: GP1,GP5
keyToggle = [tglLndGrUp,tglCfgOpen,tglDrsOpen,tglHdlghtOn,tglLndGrDn,tglCfgClose,tglDrsClose,tglHdlghtOff]
#################################################
buttonsRows = [board.GP6, board.GP7, board.GP8, board.GP9, board.GP10]
buttonsCols = [board.GP11, board.GP12, board.GP13, board.GP14, board.GP15]
btnReqLnd = Keycode.N # GP6,GP15
btnLeadLag = Keycode.BACKSLASH # GP6,GP14
btnLeaveSt = Keycode.Y # GP6,GP13
btnTglChat = Keycode.F12 # GP6,GP12
btnChatWnd = Keycode.F11 # GP6,GP11
btnChgView = Keycode.F4 # GP7,GP11
btnRstView = Keycode.KEYPAD_ASTERISK # GP8,GP11
btnScanMd = Keycode.V # GP7,GP12
btnMngMd = Keycode.M # GP7,GP13
btnFltRdy = Keycode.R # GP7,GP14 - Sending RAlt + R
btnVTOL = Keycode.K # GP7,GP15
btnWpnTgl = Keycode.P # GP8,GP15
btnGmblTgl = Keycode.G # GP8,GP14
btnPwrTgl = Keycode.U # GP8,GP13
btnEngTgl = Keycode.I # GP8,GP12
btnQntEng = Keycode.B # GP9,GP14
btnPrtTgl = Keycode.K # GP9,GP13 - Sending RAlt + K
btnDrLock = Keycode.L # GP9,GP12 - Sending Alt+L
btnScrSht = Keycode.PRINT_SCREEN # GP9,GP11
btnShldInc = Keycode.F7 # GP10,GP14
btnEngInc = Keycode.F6 # GP10,GP13
btnWpnInc = Keycode.F5 # GP10,GP12
btnReset = Keycode.F8 # GP10,GP11
Placeholder1 = Keycode.SEVEN # GP9,GP15 - GOING TO USE THIS AS THE EIGHTH BUTTON
Placeholder2 = Keycode.EIGHT # GP10,GP15
keyButtonStd = [btnTglChat,btnChatWnd,btnLeaveSt,btnLeadLag,btnReqLnd,btnChgView,btnScanMd,btnMngMd,btnFltRdy,btnVTOL,btnRstView,btnWpnTgl,btnGmblTgl,btnPwrTgl,btnEngTgl,btnScrSht,btnDrLock,btnPrtTgl,btnQntEng,Placeholder1,btnReset,btnWpnInc,btnEngInc,btnShldInc,Placeholder2]
# Used when Toggle 5 is up
keyButtonAlt = [btnTglChat,btnChatWnd,btnLeaveSt,btnLeadLag,btnReqLnd,btnChgView,btnScanMd,btnMngMd,btnFltRdy,btnVTOL,btnRstView,btnWpnTgl,btnGmblTgl,btnPwrTgl,btnEngTgl,btnScrSht,btnDrLock,btnPrtTgl,btnQntEng,Keycode.NINE,btnReset,btnWpnInc,btnEngInc,btnShldInc,Keycode.ZERO]
#default
global keyButton
keyButton = keyButtonStd
matrixButtons = keypad.KeyMatrix(buttonsRows,buttonsCols)
# ===========================================
PWM_FREQ = 5000
COLOUR_MAX = 65535
r = PWMOut(board.GP26, frequency=PWM_FREQ) # was 20 before I2C
g = PWMOut(board.GP27, frequency=PWM_FREQ) # was 21 before I2C
b = PWMOut(board.GP22, frequency=PWM_FREQ)
led = adafruit_rgbled.RGBLED(r, g, b)
colorPresetDict = {
"off": (0,0,0),
"Default": (205, 55, 6),
"Drake": (105, 110, 1),
"MISC": (18, 21, 60),
"Crusader": (10, 15, 50),
"Origin": (10, 15, 50),
"ARGO": (190, 20, 1),
#"green": (145, 175, 2),
#"yellow": (240, 50, 4),
#"white": (115, 60, 85),
#"red": (225, 2, 2),
#"darkblue": (2, 20, 95),
}
effectPresetDict = {
"off": (0,0,0),
"green": (45, 145, 1),
"yellow": (240, 50, 4),
"white": (115, 60, 85),
"red": (225, 2, 2),
"indigo": (25, 0, 95),
}
led.color = colorPresetDict[ledPresetColor]
led.color = colorPresetDict["off"] # Start w/lights off
# ===========================================
currentTime = int(time.monotonic() * 2000)
loopTime = currentTime
ROTARY_NO_MOTION = 0
ROTARY_CCW = 1
ROTARY_CW = 2
encoderCLK_prev = 0
encoder_pins = [board.GP17,board.GP18,board.GP19] #CLK/A, DT/B, switch
encoder_pin_array = []
for pin in encoder_pins:
pin = DigitalInOut(pin)
pin.direction = Direction.INPUT
pin.pull = Pull.UP
encoder_pin_array.append(pin)
encoderBtn = encoder_pin_array[2]
# ===========================================
toggle_switch_pins = [board.GP28] #[board.GP26,board.GP27]
toggle_switch_array = []
for pin in toggle_switch_pins:
pin = DigitalInOut(pin)
pin.direction = Direction.INPUT
pin.pull = Pull.UP
toggle_switch_array.append(pin)
toggle5Up = toggle_switch_array[0]
def getToggleState():
global boardToggleState
global keyButton
global panelMode, panelModeText
global MDActive
boardToggleState = not toggle5Up.value
if (boardToggleState):
if (keyButton == keyButtonStd):
print("key button alt")
keyButton = keyButtonAlt
#panelModeText = ["Standard",26] # labels were intentionally flipped
panelMode = "standard"
displayNotification("Panel Mode",36,"Standard",40)
MDActive = False
refreshdisplay()
else:
if (keyButton == keyButtonAlt):
print("key button standard")
keyButton = keyButtonStd
#panelModeText = ["Alternate",24] # labels were intentionally flipped
panelMode = "alternate"
displayNotification("Panel Mode",36,"Alternate",38)
MDActive = True
refreshdisplay()
#time.sleep(0.25)
return boardToggleState
def readRotaryEncoder():
global currentTime
global loopTime
global encoderCLK_prev
event = ROTARY_NO_MOTION
# get the current elapsed time
currentTime = int(time.monotonic() * 2000)
if (currentTime >= (loopTime + 5)):
# 5ms since last check of encoder = 200Hz
encCLK = encoder_pin_array[0].value
encDT = encoder_pin_array[1].value
if (not encCLK) and (encoderCLK_prev):
# encoder A has gone from high to low
# CW and CCW determined
if (encDT):
# B is low so counter-clockwise
event = ROTARY_CW
else:
# encoder B is high so clockwise
event = ROTARY_CCW
encoderCLK_prev = encCLK # Store value of A for next time
loopTime = currentTime
return event
def encoderAction():
#oled.fill(0)
if (encoderBtn.value is False):
if (boardToggleState):
#save_config(config,configpath)
setLEDColor(2) # Just change the color, for now
else:
mouse.click(Mouse.LEFT_BUTTON)
time.sleep(0.1)
if (enc == ROTARY_CW):
if (boardToggleState):
#print("change LED preset up")
setLEDColor(enc)
else:
print("mouse wheel up")
mouse.move(wheel=1)
elif (enc == ROTARY_CCW):
if (boardToggleState):
#print("change LED preset down")
setLEDColor(enc)
else:
print("mouse wheel down")
mouse.move(wheel=-1)
def setLEDColor(direction):
global ledPresetId
global ledPresetColor
if (ledPresetId is None):
ledPresetId = 0
if (direction == ROTARY_CW):
if (ledPresetId < len(colorPresetDict) - 1):
ledPresetId = ledPresetId + 1
#print("hit 1")
else:
ledPresetId = 0
#print("hit 2")
elif (direction == ROTARY_CCW):
if (ledPresetId > 0):
ledPresetId = ledPresetId - 1
#print("neg hit1")
else:
ledPresetId = len(colorPresetDict) - 1
#print("neg hit 2")
#print("config[ledPresetColor] before:",config["ledPresetColor"])
#print("config[ledPresetColor] after:",config["ledPresetColor"])
print("ledPreset color:", tuple(colorPresetDict.items())[ledPresetId][0])
ledPresetColor = tuple(colorPresetDict.items())[ledPresetId][0]
config["ledPresetColor"] = ledPresetColor
led.color = tuple(colorPresetDict.items())[ledPresetId][1]
refreshdisplay()
def FlashLED(color, interval, duration):
global ledPresetColor
global powerToggle
if (not powerToggle == "leaveseat"):
duration = duration
interval = interval
start_time = time.monotonic()
end_time = start_time + duration
last_toggle_time = start_time
elapsed_time = 0
toggled = False
do_not_exec = False
led.color = effectPresetDict[color]
while time.monotonic() < end_time:
if (time.monotonic() - last_toggle_time >= interval):
if (not toggled and led.color != effectPresetDict[color]): #tuple(colorPresetDict.items())[index][1]:
led.color = effectPresetDict[color]
else:
#if powerToggle == "on":
led.color = colorPresetDict[ledPresetColor]
#else:
#led.Color = colorPresetDict["off"]
toggled = not toggled
last_toggle_time = time.monotonic()
if (time.monotonic() >= end_time):
break
if (powerToggle == "on"):
led.color = colorPresetDict[ledPresetColor] # revert to default color after animation
else:
led.color = colorPresetDict["off"]
def printChar(xpos, ypos, pattern): #print defined character
for line in range(8): #5x8 characters
for j in range(5): #low value bits only
i = j + 3
dot = pattern[line] & bits[i] #extract bit
if dot: #only print white dots
oled.pixel(xpos+i*2,ypos+line*2,dot)
oled.pixel(xpos+i*2+1,ypos+line*2,dot)
oled.pixel(xpos+i*2,ypos+line*2+1,dot)
oled.pixel(xpos+i*2+1,ypos+line*2+1,dot)
def horiz(l,r,t,c): #left, right, top, color
n = r-l+1 #horizontal line
for i in range(n):
oled.pixel(l + i, t, c)
def vert(l,t,b,c): #left, top, bottom, color
n = b-t+1 #vertical line
for i in range(n):
oled.pixel(l,t+1,c)
def box(l,r,t,b,c): #left, right, top, bottom, color
horiz(l,r,t,c) # hollow rectangle
horiz(l,r,b,c)
vert(l,t,b,c)
vert(r,t,b,c)
def block(l,r,t,b,c):
n = b-t+1 #solid rectangle
for i in range(n):
horiz(l,r,t+i,c) #one line at a time
def line(x,y,xx,yy,c): # (x,y to xx,yy)
if x > xx:
t = x
x = xx
xx = t
t = y
y = yy
yy = t
if xx-x == 0:
vert(x,min(y,yy),max(y,yy),c)
else:
n=xx-x+1
grad = float((yy-y)/(xx-x))
for i in range(n):
y3 = y + int(grad * i)
oled.pixel(x+i,y3,c)
def loadimage():
global powerToggle
WIDTH = 128
HEIGHT = 64
imageName = ledPresetColor if ledPresetColor is not None else "sc" # Default to SC logo
imageName = imageName.lower()
if (imageName == "off" or powerToggle == "off"):
imageName = "sc"
imageName += "-logo.bmp"
try:
# Open the image file and read its contents
with open(f"images/{imageName}", "rb") as f:
f.read(145) # Skip the header (54 bytes) since it's not needed for monochrome bitmaps
image_data = bytearray(f.read())
except OSError as err:
with open(f"images/sc-logo.bmp", "rb") as f:
f.read(145)
image_data = bytearray(f.read())
# Display the image on the OLED screen
oled.fill(0) # Clear the display
print ("oled.fill - image load")
# Iterate over each byte in the image data
for y in range(HEIGHT):
for x in range(WIDTH // 8):
# Calculate the index in the image data buffer
index = x + y * (WIDTH // 8)
# Read the byte from the image data
byte = image_data[index]
# Iterate over each bit in the byte
for bit in range(8):
# Calculate the pixel value (1 or 0) based on the bit
pixel = (byte >> (7 - bit)) & 0x01
# Set the pixel on the OLED display
oled.pixel(x * 8 + bit, y, pixel == 1)
oled.show()
def getendcoords(position):
return position + CharWidth*2 + 2
def getline(position, color):
return line(position-2,charBtm+2,getendcoords(position),charBtm+2,color)
def getlineL(position,color):
return block(position-2,position-2,charBtm+1,charBtm+2,color) # left, right, top, bottom, color
def getlineR(position,color):
return block(getendcoords(position),getendcoords(position),charBtm+1,charBtm+2,color) # left, right, top, bottom, color
def showactivelines(LGPos,LGActive,CFPos,CFActive,DRPos,DRActive,LTPos,LTActive,MDPos,MDActive):
global charTop, charBtm
LGbottom = getline(LGPos[0],LGActive)
LGbottomleft = getlineL(LGPos[0],LGActive)
LGbottomright = getlineR(LGPos[0],LGActive)
CFbottom = getline(CFPos[0],CFActive)
CFbottomleft = getlineL(CFPos[0],CFActive)
CFbottomright = getlineR(CFPos[0],CFActive)
DRbottom = getline(DRPos[0],DRActive)
DRbottomleft = getlineL(DRPos[0],DRActive)
DRbottomright = getlineR(DRPos[0],DRActive)
LTbottom = getline(LTPos[0],LTActive)
LTbottomleft = getlineL(LTPos[0],LTActive)
LTbottomright = getlineR(LTPos[0],LTActive)
MDbottom = getline(MDPos[0],MDActive)
MDbottomleft = getlineL(MDPos[0],MDActive)
MDbottomright = getlineR(MDPos[0],MDActive)
LGPos = [10,charTop]
LGActive = int(toggleGear == True)
CFPos = [35,charTop]
CFActive = int(toggleConfig == True)
DRPos = [60,charTop]
DRActive = int(toggleDoors == True)
LTPos = [85,charTop]
LTActive = int(toggleLights == True)
MDPos = [110,charTop]
MDActive = int(toggle5Up == True)
CharWidth = 5
panelModeText = ["",0]
presetTextPosition = 20
def displayNotification(firstmsg,locationx,secondmsg,locationx2):
global notificationCounter
global showNotification
notificationCounter = 0
showNotification = True
notificationText[0] = firstmsg
notificationText[1] = locationx
notificationText[2] = secondmsg
notificationText[3] = locationx2
refreshdisplay()
def refreshdisplay():
global ledPresetColor, powerToggle, powerTogglePrev
global presetTextPosition, panelMode, panelModeText, charBtm
global LGActive, CFActive, DRActive, LTActive, MDActive
global showNotification, notificationCounter, displayRefreshed
global notificationText, powerTriangleTextPos, statusNameLocY, statusValLocY, status2NameLocY, status2ValLocY
presetText = ledPresetColor
# Refresh the status
block(0,128,charBtm+1,charBtm+3,0) # refresh the status indicators
if (not powerToggle == "standby" and not powerToggle == "leaveseat"):
#block(0,128,statusNameLocY-2,statusValLocY+10,0) # refresh the message sections - too slow
#block(0,128,status2NameLocY-1,status2NameLocY+20,0)
oled.fill(0)
if (powerTogglePrev == "standby" or powerTogglePrev == "leaveseat"):
#block(0,128,statusNameLocY-2,statusValLocY+10,0) # refresh the image section
oled.fill(0) # just overwrite everything
if (not powerToggle == "off" and powerTogglePrev == "off"):
oled.fill(0)
# Print the manufacturer
if (presetText == "Default"):
presetTextPosition = 14
elif (presetText == "Drake"):
presetTextPosition = 20
elif (presetText == "MISC" or ledPresetColor == "ARGO"):
presetTextPosition = 24
elif (presetText == "Crusader"):
presetTextPosition = 11
elif (presetText == "Origin"):
presetTextPosition = 16
else:
presetText = ""
gearString = ["Retracted",38] if not LGActive else ["Deployed",41]
if (powerToggle == "off"):
return
elif (powerToggle == "initial"):
#oled.text("System Initializing",16,36,1)
#printText(16,36,"System Initializing")
label.Label(terminalio.FONT, text="System Initializing", color=0xFFFFFF, x=16, y=36)
True
elif (powerToggle == "standby" or powerToggle == "leaveseat"):
if (not displayRefreshed):
loadimage()
displayRefreshed = True
elif (notificationCounter < notificationTimeMs and showNotification):
#oled.text(notificationText[0],notificationText[1],statusNameLocY,1) # Display Switch Name
#oled.text(notificationText[2],notificationText[3],statusValLocY,1) # Display Switch Value
label1 = label.Label(terminalio.FONT, text=notificationText[0], color=0xFFFFFF, x=notificationText[1], y=statusNameLocY)
label2 = label.Label(terminalio.FONT, text=notificationText[2], color=0xFFFFFF, x=notificationText[3], y=statusValLocY)
screen_group.append(label1)
screen_group.append(label2)
elif (notificationCounter >= notificationTimeMs and showNotification):
showNotification = False
notificationCounter = 0
refreshdisplay() # call it one more time to show the updated message
else:
#oled.text("Landing Gear",31,16,1)
#oled.text(gearString[0],gearString[1],27,1)
label1 = label.Label(terminalio.FONT, text="Landing Gear", color=0xFFFFFF, x=31, y=16)
screen_group.append(label1)
label2 = label.Label(terminalio.FONT, text=gearString[0], color=0xFFFFFF, x=gearString[1], y=27)
screen_group.append(label2)
True
if (not powerToggle == "standby" and not powerToggle == "leaveseat"):
line(36, 38, 92, 38, 1) # Separator
displayRefreshed = False # I think I'll need to do this to refresh it the next time it's in standby
print("panel mode:",panelMode)
if (panelMode == "alternate"):
#oled.text('Manufacturer', 29, 43, 1) # "Light Preset" text
#oled.text(f'{presetText}', presetTextPosition+30, 54, 1) # Preset Color text
print('alternate hit')
else:
#oled.text('Power Allocation', 18, 43, 1) #
#oled.text(f'{powerTriangleMode[0]}', powerTriangleMode[1], 54, 1)
True
if (not powerToggle == "leaveseat"):
#block(0,128,0,charBtm+3,0) # remove the status indicators
print(f'LGPos0 - {LGPos[0]}')
print(f'LGPos1 - {LGPos[1]}')
#printText(LGPos[0], LGPos[1],"LG")
#printText(CFPos[0], CFPos[1],"CF")
#printText(DRPos[0], DRPos[1],"DR")
#oled.text('LG', LGPos[0], LGPos[1], 1) # Landing Gear
#oled.text('CF', CFPos[0], CFPos[1], 1) # Configuration
#oled.text('DR', DRPos[0], DRPos[1], 1) # Doors
#oled.text('LT', LTPos[0], LTPos[1], 1) # Lights
#oled.text('MD', MDPos[0], MDPos[1], 1) # Mode
showactivelines(LGPos,LGActive,CFPos,CFActive,DRPos,DRActive,LTPos,LTActive,MDPos,MDActive)
if (designMode): # Display a template for centering UI elements
horiz(0,128,30,1) # template x
for i in range(0,60): # template y
vert(64,i,50,1)
#oled.show(screen_group)
oled.show()
print('show here')
while True:
notificationCounter = notificationCounter + 1
if (notificationCounter > notificationTimeMs and showNotification and not displayRefreshed):
refreshdisplay()
keyPressed = matrixButtons.events.get()
togglePressed = matrixToggles.events.get()
if (keyPressed):
btnval = int(keyPressed.key_number)
print("btnval:",btnval)
key = keyButton[btnval]
if (keyPressed.pressed):
print("key value:",key)
if (btnval == 8 or btnval == 17): # 8:RAlt+R (Flight Ready), 17:RAlt+K (Port Toggle)
keyboard.send(Keycode.RIGHT_ALT, key)
led.color = colorPresetDict[ledPresetColor]
powerTogglePrev = powerToggle
powerToggle = "on"
if (btnval == 8):
#displayNotification("Flight",48,"Ready",52)
refreshdisplay() # notification text not working
if (btnval == 17):
FlashLED("red",.05,0.2)
#displayNotification("Ports",51,"Toggled",45)
refreshdisplay() # notification text not working
elif (btnval == 13 or btnval == 14): # Turn off lights when engine off (or back on.. just use flight ready to turn back on)
keyboard.send(key)
led.color = colorPresetDict["off"]
if (btnval == 13):
#displayNotification("Powering",42,"Down..",49)
refreshdisplay() # notification text not working
powerTogglePrev = powerToggle
powerToggle = "off"
loadimage()
else:
#displayNotification("Standing",42,"By..",56)
refreshdisplay() # notification text not working
powerTogglePrev = powerToggle
powerToggle = "standby"
elif (btnval == 16 or btnval == 4): # 16:Alt+L (Door Lock Toggle), 4:Alt+N (Request Landing)
keyboard.send(Keycode.ALT, key)
if (btnval == 16):
#displayNotification("Door Locks",36,"Toggled",45)
refreshdisplay() # notification text not working
if (btnval == 4):
#displayNotification("Hailing",44,"ATC..",50)
refreshdisplay() # notification text not working
# Maybe later - this locks up the program while running that while loop
# I tried to make it without a while loop, but then it only evaluates once and stops
# Flash LED when doors locking
if (btnval == 17):
FlashLED("yellow",.05,0.3)
elif (btnval == 2 or btnval == 21 or btnval == 22 or btnval == 23 or btnval == 18): # 2:Y (Leave seat), 21,22,23:Power Triangle Adjustments, 18:QT
if (btnval ==2):
#displayNotification("Leaving",45,"Seat",54)
refreshdisplay() # notification text not working
powerTogglePrev = powerToggle
powerToggle = "leaveseat"
elif (btnval == 18):
FlashLED("off",0.05,0.5) # QT
#displayNotification("Quantum Travel",24,"Engaged",44)
refreshdisplay() # notification text not working
elif (btnval == 21):
FlashLED("red",0.5,0.2) # Pwr to Wpn
#displayNotification("== Full Power ==",16,"Weapons",44)
refreshdisplay() # notification text not working
powerTriangleMode = ["Weapons",44]
refreshdisplay()
elif (btnval == 22):
FlashLED("yellow",0.5,0.2) # Pwr to Eng
#displayNotification("== Full Power ==",15,"Engines",44)
refreshdisplay() # notification text not working
powerTriangleMode = ["Engines",44]
refreshdisplay()
elif (btnval == 23):
FlashLED("green",0.5,0.2) # Pwr to Shld
#displayNotification("== Full Power ==",16,"Shields",44)
refreshdisplay() # notification text not working
powerTriangleMode = ["Shields",44]
refreshdisplay()
keyboard.press(key)
time.sleep(1)
keyboard.release(key)
if (btnval == 18): # gonna lock it up for a few seconds when entering QT, but I don't particularly care
FlashLED("white",5,1)
elif (btnval == 2):
led.color = colorPresetDict["off"] # disable the LED's and load up the SC logo
loadimage()
elif (btnval == 20): # Pwr Reset in Standard config
if (boardToggleState):
FlashLED("off",0.5,0.2)
powerTriangleMode = ["Default",44]
refreshdisplay()
#displayNotification("Power",50,"Reset",50)
refreshdisplay() # notification text not working
else: # Change Color in Alternate
setLEDColor(2)
elif (btnval == 5): # 5:F4 (Change view), 18:Quantum Toggle/Engage
keyboard.press(key)
elif (btnval == 10): # 10:* (Reset view, requires F4 to be held)
keyboard.press(61) #F4
keyboard.send(key)
keyboard.release(61)
keyboard.send(61) # because it keeps getting out of third person, for some reason
else:
keyboard.send(key)
else:
keyboard.release(key)
# IF Port Toggle, flash lights off and on yellow until it's off
# IF Doors unlocked, flash lights yellow on and off for five seconds
# IF Quantum Travel, flash lights red on and off for 3 seconds
# IF Doors Open, flash lights red on and off for five seconds
if (togglePressed):
if (prevToggle) is None:
prevToggle = 1
if (togglePressed.pressed):
btnvalT = int(togglePressed.key_number)
key = keyToggle[btnvalT]
keyToggle = [tglLndGrUp,tglCfgOpen,tglDrsOpen,tglHdlghtOn,tglLndGrDn,tglCfgClose,tglDrsClose,tglHdlghtOff]
if (key == 23): # Alt+T - GP0,2
keyboard.send(Keycode.ALT, key)
config["toggleGear"] = True
FlashLED("off",0.05,0.5)
LGActive = False
#displayNotification("Landing Gear",31,"Retracted",38)
refreshdisplay() # notification text not working
elif (key == 28): # Alt+Y - GP1,2
keyboard.send(Keycode.ALT, key)
config["toggleGear"] = False
FlashLED("off",0.2,0.2) # LG down
LGActive = True
#displayNotification("Landing Gear",31,"Deployed",41)
refreshdisplay() # notification text not working
elif (key == 24): # Alt+U - GP0,3
keyboard.send(Keycode.ALT, key)
config["toggleConfig"] = True
FlashLED("yellow",0.2,0.2)
CFActive = True
#displayNotification("Configuration",27,"Expanded",42)
refreshdisplay() # notification text not working
elif (key == 12): # Alt+I - GP1,3
keyboard.send(Keycode.ALT, key)
config["toggleConfig"] = False
FlashLED("off",0.2,0.2)
CFActive = False
#displayNotification("Configuration",27,"Retracted",38)
refreshdisplay() # notification text not working
elif (key == 18): # Alt+O (Open Doors?) - GP0,4
keyboard.send(Keycode.ALT, key)
config["toggleDoors"] = True
FlashLED("red",.05,0.5)
DRActive = True
#displayNotification("Doors",50,"Open",53)
refreshdisplay() # notification text not working
elif (key == 19): # Alt+P (Close doors?) - GP1,4
keyboard.send(Keycode.ALT, key)
config["toggleDoors"] = False
FlashLED("off",0.2,0.2)
DRActive = False
#displayNotification("Doors",50,"Closed",47)
refreshdisplay() # notification text not working
elif (key == 15): # L (Lights) - GP0,5
if (ignoreFirstLightPress > 0):
keyboard.send(key)
config["toggleLights"] = not config["toggleLights"]
if (not config["toggleLights"]):
FlashLED("white",0.2,0.2)
LTActive = True
#displayNotification("Lights",48,"On",59)
refreshdisplay() # notification text not working
else:
FlashLED("off",0.2,0.2)
LTActive = False
#displayNotification("Lights",48,"Off",56)
refreshdisplay() # notification text not working
if (ignoreFirstLightPress < 1):
ignoreFirstLightPress = ignoreFirstLightPress + 1 #ignore the press on startup
LTActive = False
else:
keyboard.send(key)
else:
print("toggle state changed")
boardToggleState = getToggleState() # checks the toggle5up value (remaps keys, adjust led preset, save config)
enc = readRotaryEncoder()
encoderAction()