1
1
# -*- coding: utf-8 -*-
2
+ """
3
+ Graphical interface for UPS-2 UART serial mode.
4
+
5
+ Usage:
6
+ * For autostart: put this script in /etc/xdg/lxsession/LXDE-pi/autostart
7
+ * This script needs PySimpleGUI.py found on https://github.com/PySimpleGUI/PySimpleGUI
8
+ * Raspberry serial interface must be enabled (with raspi-config) i.e.
9
+ * ups2_serial.py worker service must be started (instructions see docstring)
10
+ """
11
+ #Todo: abs
12
+ # calendar timed functions
13
+ # email and/or other messaging functions
14
+
2
15
import PySimpleGUI as sg
3
16
import os
4
17
import ups2_Interface as ups
5
18
6
- """
7
- #for autostart: put this script in /etc/xdg/lxsession/LXDE-pi/autostart
8
- """
19
+
9
20
10
21
print ('PySimpleGUI Version' , sg .version )
11
22
14
25
ser = ups .ecInitSerial ()
15
26
fT = open ("/sys/class/thermal/thermal_zone0/temp" , "r" ) #Pi processor temperature
16
27
17
-
18
28
#globals
19
29
bgOFF = 'lightgrey'
20
30
bgOK = '#A2C477'
28
38
keyPiCPU = 'K_PI_T'
29
39
30
40
def ecPopWin (count , title = 'ECOM UPS2' ):
31
- #Open a modal popup for count seconds and return 'shutdown' or 'cancel'
41
+ '''Opens a modal popup for count seconds and returns "shutdown" or "cancel"
42
+
43
+ Args:
44
+ param count: countdown time in seconds
45
+ param title: String to display in popup banner
46
+
47
+ '''
32
48
33
49
devider = count * 10
34
50
layout2 = [[sg .Text ('Pi shuddown in' ),
@@ -59,7 +75,16 @@ def ecPopWin(count, title = 'ECOM UPS2'):
59
75
60
76
61
77
def ecFormatDisplay (sysStatus , window ):
62
- #Set display attributes according to system state
78
+ '''Set display attributes according to system state.
79
+
80
+ This function refreshes the display depending on the system state
81
+ Must be periodically called by the main control loop
82
+
83
+ Args:
84
+ sysStatus: string received from UPS-2 hardware
85
+ window: initialized main window
86
+ '''
87
+
63
88
s = int (sysStatus , 16 ) #sysStatus comes as string
64
89
65
90
#default values. In dictionary in order to easily update depending on status
@@ -81,7 +106,7 @@ def ecFormatDisplay(sysStatus, window):
81
106
if s & 0x0020 : #batt V present
82
107
battState .update (bgColor = bgOK , stat = 'OK' )
83
108
if s & 0x0040 : #usb V present
84
- usbState .update (bgColor = bgOK , text = 'active' )
109
+ usbState .update (bgColor = bgOK , text = 'active' )
85
110
if (s & 0x0110 ) == 0x0110 : #main V LOW
86
111
mainState .update (stat = 'LOW' , bgColor = bgLOW )
87
112
if s & 0x0200 : #main V HIGH
@@ -92,9 +117,8 @@ def ecFormatDisplay(sysStatus, window):
92
117
battState .update (stat = 'HIGH' , bgColor = bgHIGH )
93
118
if s & 0x1000 : #CPU temp HIGH
94
119
battState .update (stat = 'HIGH' , bgColor = bgHIGH )
95
-
96
120
97
- #adapt elements
121
+ #adapt value displays
98
122
window ['K_MAIN_V' ].update (background_color = mainState ['bgColor' ])
99
123
window ['K_MAIN_V' ].Widget .configure (borderwidth = mainState ['borderW' ]) #use underlying element
100
124
window ['K_MAIN_STATE' ].update (mainState ['stat' ])
@@ -103,9 +127,15 @@ def ecFormatDisplay(sysStatus, window):
103
127
window ['K_BATT_STATE' ].update (battState ['stat' ])
104
128
window ['K_USB' ].update (background_color = usbState ['bgColor' ])
105
129
window ['K_USB' ].update (usbState ['text' ])
130
+ #adapt power button
106
131
107
-
108
-
132
+ if s & 0x0004 :
133
+ # window['Power OFF'].set_tooltip('Disabled, if USB 5V supplied')
134
+ window ['Power OFF' ].update (disabled = True , disabled_button_color = ('lightgrey' , 'none' ))
135
+
136
+ else :
137
+ window ['Power OFF' ].update (disabled = False )
138
+ # window['Power OFF'].SetTooltip('')
109
139
110
140
# Main window column layout
111
141
col1 = [[sg .Text (text = 'Main supply' , key = "K_MAIN_LBL" , size = (10 ,0 ), justification = 'right' )],
@@ -130,7 +160,7 @@ def ecFormatDisplay(sysStatus, window):
130
160
mainFrame = [[ sg .Column (col1 ), sg .Column (col2 ), sg .Column (col3 ), sg .Column (col4 ), sg .Column (col5 )]]
131
161
132
162
#buttons to appear in frame below
133
- keypanel = [[sg .Button ('Power OFF' ), sg .Button ('Restart' ), sg .Button ('Standby' ), sg .Button ('Quit' )]]
163
+ keypanel = [[sg .Button ('Power OFF' , tooltip = 'Disabled if USB powered' ), sg .Button ('Restart' ), sg .Button ('Standby' ), sg .Button ('Quit' )]]
134
164
135
165
#frame layout around buttons
136
166
layout = [[sg .Frame ('Power Supply Overview' , mainFrame )],
0 commit comments