Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@


def str2bool(v):
return v.lower() in ("yes", "true", "True", "1")
lower = v.lower()
if lower in {"yes", "true", "1"}:
return True
elif lower in {"no", "false", "0"}:
return False

raise Exception(f"{v} does not match any true or false values, cannot cast")


bin_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
Expand Down Expand Up @@ -351,8 +357,8 @@ def handle_shutdown(pin):
gpio.add_event_detect(int(pin), gpio.BOTH, callback=handle_button, bouncetime=1)

# Send centering commands
device.emit(uinput.ABS_X, VREF / 2, syn=False);
device.emit(uinput.ABS_Y, VREF / 2);
device.emit(uinput.ABS_X, VREF / 2, syn=False)
device.emit(uinput.ABS_Y, VREF / 2)

# Set up OSD service
try:
Expand All @@ -367,8 +373,8 @@ def handle_shutdown(pin):
logging.error("ERROR: Failed to start OSD, got return code [" + str(osd_poll) + "]\n")
sys.exit(1)
except Exception as e:
logging.exception("ERROR: Failed start OSD binary");
sys.exit(1);
logging.exception(f"ERROR: Failed start OSD binary: {e}")
sys.exit(1)


# Check for shutdown state
Expand All @@ -384,16 +390,16 @@ def checkShdn(volt):

# Read voltage
def readVoltage():
global last_bat_read;
global last_bat_read
voltVal = adc.read_adc(0, gain=1)
print voltVal
print 'read'
print(voltVal)
print('read')
volt = int((float(voltVal) * (4.09 / 2047.0)) * 100)

if volt < 300 or (last_bat_read > 300 and last_bat_read - volt > 6 and not last_bat_read == 450):
volt = last_bat_read;
volt = last_bat_read

last_bat_read = volt;
last_bat_read = volt

return volt

Expand All @@ -408,18 +414,18 @@ def readVolumeLevel():
res = process.readline()
process.close()

vol = 0;
vol = 0
try:
vol = int(res.replace("%", "").replace("'C\n", ""))
except Exception as e:
logging.info("Audio Err : " + str(e))

return vol;
return vol


# Read wifi (Credits: kite's SAIO project) Modified to only read, not set wifi.
def readModeWifi(toggle=False):
ret = 0;
ret = 0
wifiVal = not os.path.exists(osd_path + 'wifi') # int(ser.readline().rstrip('\r\n'))
if toggle:
wifiVal = not wifiVal
Expand Down Expand Up @@ -475,7 +481,7 @@ def readModeWifi(toggle=False):


def readModeBluetooth(toggle=False):
ret = 0;
ret = 0
BtVal = not os.path.exists(osd_path + 'bluetooth') # int(ser.readline().rstrip('\r\n'))
if toggle:
BtVal = not BtVal
Expand Down Expand Up @@ -531,12 +537,7 @@ def doShutdown(channel=None):
def updateOSD(volt=0, bat=0, temp=0, wifi=0, audio=0, lowbattery=0, info=False, charge=False, bluetooth=False):
global showOverlay
showState = showOverlay if SHOW_OVERLAY_HOTKEY_ONLY else True
commands = "s" + str(int(showState)) + " p" + str(int((backlightSetting / 1024) * 100)) + " v" + str(
volt) + " b" + str(bat) + " t" + str(temp) + " w" + str(
wifi) + " a" + str(
audio) + " j" + ("1 " if joystick else "0 ") + " u" + ("1 " if bluetooth else "0 ") + " l" + (
"1 " if lowbattery else "0 ") + " " + ("on " if info else "off ") + (
"charge" if charge else "ncharge") + "\n"
commands = f"s{int(showState)} p{int((backlightSetting / 1024) * 100)} v{volt} b{bat} t{temp} w{wifi} a{audio} j{int(joystick)} u{int(bluetooth)} l{lowbattery} {"on" if info else "off"} {"charge" if charge else "ncharge"} \n"
# print commands
osd_proc.send_signal(signal.SIGUSR1)
osd_in.write(commands)
Expand Down Expand Up @@ -569,7 +570,7 @@ def inputReading():
global charge
global joystick
while (1):
if joystick == True:
if joystick:
checkJoystickInput()
time.sleep(.05)

Expand Down Expand Up @@ -615,8 +616,8 @@ def checkKeyInputPowerSaving():


def checkJoystickInput():
an1 = adc.read_adc(2, gain=2 / 3);
an0 = adc.read_adc(1, gain=2 / 3);
an1 = adc.read_adc(2, gain=2 / 3)
an0 = adc.read_adc(1, gain=2 / 3)

logging.debug("X: {} | Y: {}".format(an0, an1))
logging.debug("Above: {} | Below: {}".format((VREF / 2 + DZONE), (VREF / 2 - DZONE)))
Expand All @@ -643,13 +644,13 @@ def constrain(val, min_val, max_val):
def brightnessUp():
global backlightSetting
backlightSetting = constrain(backlightSetting + 128, 0, 1024)
wiringpi.pwmWrite(13, backlightSetting);
wiringpi.pwmWrite(13, backlightSetting)


def brightnessDown():
global backlightSetting
backlightSetting = constrain(backlightSetting - 128, 0, 1024)
wiringpi.pwmWrite(13, backlightSetting);
wiringpi.pwmWrite(13, backlightSetting)


def exit_gracefully(signum=None, frame=None):
Expand All @@ -667,26 +668,25 @@ def exit_gracefully(signum=None, frame=None):
wifi = readModeWifi()
bluetooth = bluetooth = readModeBluetooth()

if JOYSTICK_ENABLED == 'True':
if JOYSTICK_ENABLED == "True":
inputReadingThread = thread.start_new_thread(inputReading, ())

try:
while 1:
try:
if not adc == False:
if adc:
volt = readVoltage()
bat = getVoltagepercent(volt)
checkShdn(volt)
updateOSD(volt, bat, 20, wifi, volume, lowbattery, info, charge, bluetooth)
print 'update OSD'
print('update OSD')
overrideCounter.wait(10)
if overrideCounter.is_set():
overrideCounter.clear()
runCounter = 0;
runCounter = 0

except Exception:
logging.info("EXCEPTION")
pass
except Exception as e:
logging.info(f"EXCEPTION: {e}")

except KeyboardInterrupt:
exit_gracefully()
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# WARNING: THESE ARE LATEST VERSIONS, MAY NOT WORK WITH OLD CODE
adafruit-circuitpython-ads1x15==2.4.3
RPi.GPIO==0.7.1
python-uinput==1.0.1
thread==2.0.5 # Unsure if this is the correct package
wiringpi==2.60.1 # DEPRICATED AS OF AUGUST 2019