78 lines
2.1 KiB
Python
78 lines
2.1 KiB
Python
|
import time
|
||
|
import os
|
||
|
current_path = os.path.dirname(os.path.realpath(__file__))
|
||
|
import threading
|
||
|
import json
|
||
|
try:
|
||
|
import pystray
|
||
|
from pystray import Icon as icon, Menu as menu, MenuItem as item
|
||
|
from PIL import Image, ImageDraw
|
||
|
import serial
|
||
|
import win32api
|
||
|
from win32con import *
|
||
|
except ImportError:
|
||
|
print("Please execute 'pip install -r requirements.txt'")
|
||
|
exit()
|
||
|
|
||
|
def exit_everything():
|
||
|
os._exit(1)
|
||
|
|
||
|
def debug_print(msg: str):
|
||
|
current_time = time.strftime("%H:%M:%S")
|
||
|
print("[DEBUG]["+current_time+"] " + msg)
|
||
|
|
||
|
def debug_print_error(msg: str):
|
||
|
current_time = time.strftime("%H:%M:%S")
|
||
|
print("[ERROR]["+current_time+"] " + msg)
|
||
|
|
||
|
if not os.path.exists(current_path+"/modes.json"):
|
||
|
with open(current_path+"/modes.json", "w") as f:
|
||
|
json.dump({"mode": 0}, f)
|
||
|
else:
|
||
|
with open(current_path+"/modes.json", "r") as f:
|
||
|
global mode
|
||
|
mode = json.load(f)["mode"]
|
||
|
|
||
|
#mode switches
|
||
|
def volumen_mode():
|
||
|
debug_print("volument mode")
|
||
|
global mode
|
||
|
mode = 0
|
||
|
with open(current_path+"/modes.json", "w") as f:
|
||
|
json.dump({"mode": 0}, f)
|
||
|
|
||
|
#mode actions
|
||
|
|
||
|
#mode 0: volument mode
|
||
|
def mute_unmute():
|
||
|
win32api.keybd_event(VK_VOLUME_MUTE, 0)
|
||
|
|
||
|
def volument_up():
|
||
|
win32api.keybd_event(VK_VOLUME_UP, 0)
|
||
|
|
||
|
def volument_down():
|
||
|
win32api.keybd_event(VK_VOLUME_DOWN, 0)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
host = serial.Serial('COM3', 9600)
|
||
|
|
||
|
tray_icon = Image.open(current_path+r'/icon.ico')
|
||
|
tray_menu = (item('Volumen', volumen_mode), item('Exit', exit_everything))
|
||
|
icon = pystray.Icon('USB Knob', tray_icon, 'USB Knob', tray_menu)
|
||
|
icon.run_detached()
|
||
|
|
||
|
while True:
|
||
|
try:
|
||
|
output = host.readline()
|
||
|
output = output.decode("ASCII")
|
||
|
output = output.strip()
|
||
|
if mode == 0:
|
||
|
if output == str("0x0"):
|
||
|
mute_unmute()
|
||
|
elif output == str("1x0"):
|
||
|
volument_up()
|
||
|
elif output == str("1x1"):
|
||
|
volument_down()
|
||
|
|
||
|
except KeyboardInterrupt:
|
||
|
break
|