Skip to content

Commit 3ed04b2

Browse files
develop version with initial source doc
1 parent 98d1a23 commit 3ed04b2

9 files changed

+17627
-10
lines changed

PySimpleGUI.py

+17,475
Large diffs are not rendered by default.
777 KB
Binary file not shown.
17 Bytes
Binary file not shown.

installation.txt

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
copy UPS2 files to Projects/UPS2
2+
3+
Raspi zero for graphic desktop (if no desktop display):
4+
sudo apt-get install lxde
5+
und ev. tkinter, falls ups2_GUI nicht funzt:
6+
sudo apt install python3-tk
7+
8+
für parallel mode (--> pi zero). GPIOD library installieren:
9+
sudo apt install python3-libgpiod
10+
11+
12+
ADD IN CONFIG.TXT
13+
==================
14+
###ECOM UPS-2 for parallel mode
15+
#set GPIO20 as Input with pullup high
16+
gpio=20=ip,pu
17+
18+
19+
###ECOM UPS-2 for serial mode
20+
enable UART in config.txt
21+
22+
Edit /etc/rc.local
23+
===================
24+
--> sudo chmod +x rc.local //wird beim Start ausgeführt
25+
26+
#serial mode
27+
python3 /home/pi/Projects/UPS2/ups2_serial.py &
28+
29+
#parallel mode
30+
python3 /home/pi/Projects/UPS-2/ups2_portHandler.py &
31+
32+
Edit /etc/xdg/lxsession/LXDE-pi/autostart
33+
=========================================
34+
#serial mode
35+
python3 /home/pi/Projects/UPS2/ups2_serial.py &
36+
37+
#parallel mode
38+
python3 /home/pi/Projects/UPS-2/ups2_portHandler.py &
39+
40+
#GUI, falls Autostart gewünscht
41+
python3 /home/pi/Projects/UPS-2/ups2_GUI.py
42+
43+
44+
HAT EEPROM
45+
===========
46+
Tools:
47+
https://blog.gc2.at/post/hat-eeprom/
48+
https://www.raspberrypi.org/forums/viewtopic.php?t=108134
49+

s_shut.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import gpiod
2+
import time
3+
import sys
4+
import os
5+
import argparse
6+
''' shutdown control using simple switch and LED. '''
7+
8+
def getArgs():
9+
'''call script from file /etc/rc.local including path'''
10+
11+
ledDefault = 21
12+
switchDefault = 20
13+
powerDefault = ''
14+
15+
16+
parser = argparse.ArgumentParser()
17+
18+
parser.add_argument("-l","--ledPort", type=int, default=ledDefault, metavar='',
19+
help='Enter LED output bcm port default = ' + str(ledDefault))
20+
parser.add_argument("-s", "--switchPort", type=int, default=switchDefault, metavar='',
21+
help='Enter switch control input bcm port default = ' + str(switchDefault))
22+
parser.add_argument("-p", "--powerPort", type=int, metavar='',
23+
help='optional bcm port for ext. power timer')
24+
25+
args=parser.parse_args()
26+
worker(args)
27+
# return args
28+
29+
def worker(ports):
30+
# ports = getArgs()
31+
print('LED: GPIO'+str(ports.ledPort),
32+
'Switch: GPIO'+str(ports.switchPort),
33+
'Power: GPIO'+str(ports.powerPort))
34+
35+
chip = gpiod.Chip("0")
36+
inPort = chip.get_line(ports.switchPort)
37+
inPort.request("UPS-2", type=gpiod.LINE_REQ_EV_BOTH_EDGES)
38+
39+
outPort = chip.get_line(ports.ledPort)
40+
outPort.request("Py_Shutdown", type=gpiod.LINE_REQ_DIR_OUT)
41+
outPort.set_value(1) #signal raspi is up
42+
43+
counter = 0
44+
t_keyrelease = time.time()
45+
while True:
46+
try:
47+
keyAction = "NO_KEY"
48+
#wait for key pressed
49+
event = inPort.event_read() #falling edge: key pressed
50+
print(event)
51+
print("type", event.type)
52+
print('timestamp: {}.{}'.format(event.sec, event.nsec))
53+
if (event.type == 1):
54+
print("first event should be falling --> ignore")
55+
continue #retry wait for falling edge
56+
counter = 1
57+
t_keypress = event.sec + (event.nsec * 10e-10)
58+
pauseTime = t_keypress - t_keyrelease
59+
60+
#wait for key released
61+
event = inPort.event_read() #rising edge: key released
62+
print("type", event.type)
63+
print('timestamp: {}.{}'.format(event.sec, event.nsec))
64+
# startTime = time.process_time()
65+
t_keyrelease = event.sec + (event.nsec * 10e-10)
66+
pulseTime = t_keyrelease - t_keypress
67+
# print("calc time: ", time.process_time() - startTime)
68+
69+
counter = counter + 1
70+
print("counter =", counter,
71+
" pulse time= ", pulseTime, "s"
72+
" pause time = ", pauseTime)
73+
74+
#analyze command
75+
if pulseTime < 0.5:
76+
if pauseTime < 0.5: #this is a double click
77+
keyAction = "DOUBLE_PRESS"
78+
os.system("sudo shutdown -r now")
79+
else:
80+
keyAction = "SHORT_PRESS"
81+
elif pulseTime > 5:
82+
keyAction = "SUPER_LONG_PRESS"
83+
os.system("sudo shutdown -h now")
84+
85+
elif pulseTime > 2:
86+
keyAction = "LONG_PRESS"
87+
os.system("sudo shutdown -h now")
88+
89+
print("keyAction =", keyAction)
90+
91+
except KeyboardInterrupt:
92+
chip.close()
93+
break
94+
print("ciao")
95+
96+
if __name__ == "__main__":
97+
getArgs()

ups2_GUI.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def ecFormatDisplay(sysStatus, window):
184184
if event in (None, 'Quit'):
185185
break
186186
if event == 'Power OFF':
187-
if ecPopWin(5, 'Pwr OFF') != 'cancel':
187+
if ecPopWin(5, 'POWER OFF') != 'cancel':
188188
ups.ecReqUPSPowerDown(ser)
189189
os.system('sudo shutdown -P now\n')
190190
elif event == 'Restart':

ups2_Interface.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import fcntl
88

99
def ecInitSerial(device = '/dev/serial0'):
10-
'''Opens serial device and returns a handle to to the device'''
10+
'''Opens serial device and returns a handle'''
1111

1212

1313
ser = serial.Serial(
@@ -47,7 +47,7 @@ def ecFormatAnalog(analogStr):
4747
return l
4848

4949
def ecGetUPSValues(ser):
50-
'''Requests values from UPS-2 by serial interface service ups2_serial.py.'''
50+
'''Requests values from UPS-2'''
5151
try:
5252
request = "r?status\n"
5353
fcntl.flock(ser, fcntl.LOCK_EX)

ups2_portHandler.py

-6
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,7 @@
6161

6262
print("keyAction =", keyAction)
6363

64-
65-
6664
except KeyboardInterrupt:
6765
chip.close()
6866
break
6967
print("ciao")
70-
71-
72-
73-

ups2_serial.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
This is the interface to the UPS-2 power supply
3+
24
Place a call to this service in /etc/rc.local:
35
python3 /path/to/ups2_serial.py &
46
"""
@@ -26,7 +28,7 @@ def ecReadline(ser):
2628

2729

2830
def ecExecSerCommand(rxLine):
29-
"""Check if command comes from UPS and execute."""
31+
"""Check if commands come from UPS and execute."""
3032
command = "--"
3133
#linux system commands from UPS
3234
if rxLine[0:2] == "u!": #prefix

0 commit comments

Comments
 (0)