Skip to content

Instantly share code, notes, and snippets.

@salami738
Created February 16, 2018 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save salami738/17994c7c600a6a8fd0320cfc1c03797e to your computer and use it in GitHub Desktop.
Save salami738/17994c7c600a6a8fd0320cfc1c03797e to your computer and use it in GitHub Desktop.
control_osd_and_specialbuttons.py
#!/usr/bin/python
#
# Skript sollte mittels der folgenden Zeile in der crontab hinterlegt werden:
# @reboot /home/pi/display_overlay/control_osd_and_specialbuttons.py >/tmp/osd.py.log 2>&1
#
# TODO Shift button einlesen und Befehle ausfuehren
#
import RPi.GPIO as GPIO
import array
import os
import signal
import subprocess
import math
import smbus
import serial
import time
from subprocess import *
from config import *
currentIcon = ""
ser = serial.Serial(
# ttyS0 -> GPIO serial (mini UART)
# ttyACM0 -> USB serial
# ttyAMA0 -> Bluetooth serial
port='/dev/ttyS0',
baudrate = 57600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=2
)
def changeicon(percent):
global currentIcon
if currentIcon != percent:
currentIcon = percent
cmdLine = PNGVIEWPATH + "/pngview -b 0x000F -l 3000" + percent + " -x 430 -y 1 " + ICONPATH + "/battery_" + percent + ".png &"
newPngViewProcessPid = int(subprocess.Popen(cmdLine.split(" ")).pid)
if DEBUGMSG == 1:
print("cmdLine: " + cmdLine + ", newPngViewProcessPid: " + str(newPngViewProcessPid))
out = check_output("ps aux | grep [p]ngview | awk '{ print $2 }'", shell=True)
for pid in out.split('\n'):
if pid.isdigit() and int(pid) != newPngViewProcessPid:
if DEBUGMSG == 1:
print("killing: " + str(pid))
os.system("kill " + pid)
if DEBUGMSG == 1:
print("Changed battery icon to " + percent + "%")
else:
if DEBUGMSG == 1:
print("Changing of icon not needed")
def endProcess(signalnum = None, handler = None):
os.system("killall pngview");
exit(0)
def readBatteryVoltageFromArduino():
# 'V' command is for reading battery voltage. Response example: 4.1234 (V)
ser.write(bytes('V'))
batteryVoltage = float(ser.readline())
print "batteryVoltage: %f V" % batteryVoltage
return batteryVoltage
# Prepare handlers for process exit
signal.signal(signal.SIGTERM, endProcess)
signal.signal(signal.SIGINT, endProcess)
if DEBUGMSG == 1:
print("Started osd and special button script")
while True:
ret = readBatteryVoltageFromArduino()
if ret <= VOLT0:
changeicon("000")
elif ret <= VOLT20:
changeicon("020")
elif ret <= VOLT40:
changeicon("040")
elif ret <= VOLT60:
changeicon("060")
elif ret <= VOLT80:
changeicon("080")
else:
changeicon("100")
# newline
print("")
time.sleep(REFRESH_RATE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment