11# -*- 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+
215import PySimpleGUI as sg
316import os
417import ups2_Interface as ups
518
6- """
7- #for autostart: put this script in /etc/xdg/lxsession/LXDE-pi/autostart
8- """
19+
920
1021print ('PySimpleGUI Version' , sg .version )
1122
1425ser = ups .ecInitSerial ()
1526fT = open ("/sys/class/thermal/thermal_zone0/temp" , "r" ) #Pi processor temperature
1627
17-
1828#globals
1929bgOFF = 'lightgrey'
2030bgOK = '#A2C477'
2838keyPiCPU = 'K_PI_T'
2939
3040def 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+ '''
3248
3349 devider = count * 10
3450 layout2 = [[sg .Text ('Pi shuddown in' ),
@@ -59,7 +75,16 @@ def ecPopWin(count, title = 'ECOM UPS2'):
5975
6076
6177def 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+
6388 s = int (sysStatus , 16 ) #sysStatus comes as string
6489
6590 #default values. In dictionary in order to easily update depending on status
@@ -81,7 +106,7 @@ def ecFormatDisplay(sysStatus, window):
81106 if s & 0x0020 : #batt V present
82107 battState .update (bgColor = bgOK , stat = 'OK' )
83108 if s & 0x0040 : #usb V present
84- usbState .update (bgColor = bgOK , text = 'active' )
109+ usbState .update (bgColor = bgOK , text = 'active' )
85110 if (s & 0x0110 ) == 0x0110 : #main V LOW
86111 mainState .update (stat = 'LOW' , bgColor = bgLOW )
87112 if s & 0x0200 : #main V HIGH
@@ -92,9 +117,8 @@ def ecFormatDisplay(sysStatus, window):
92117 battState .update (stat = 'HIGH' , bgColor = bgHIGH )
93118 if s & 0x1000 : #CPU temp HIGH
94119 battState .update (stat = 'HIGH' , bgColor = bgHIGH )
95-
96120
97- #adapt elements
121+ #adapt value displays
98122 window ['K_MAIN_V' ].update (background_color = mainState ['bgColor' ])
99123 window ['K_MAIN_V' ].Widget .configure (borderwidth = mainState ['borderW' ]) #use underlying element
100124 window ['K_MAIN_STATE' ].update (mainState ['stat' ])
@@ -103,9 +127,15 @@ def ecFormatDisplay(sysStatus, window):
103127 window ['K_BATT_STATE' ].update (battState ['stat' ])
104128 window ['K_USB' ].update (background_color = usbState ['bgColor' ])
105129 window ['K_USB' ].update (usbState ['text' ])
130+ #adapt power button
106131
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('')
109139
110140# Main window column layout
111141col1 = [[sg .Text (text = 'Main supply' , key = "K_MAIN_LBL" , size = (10 ,0 ), justification = 'right' )],
@@ -130,7 +160,7 @@ def ecFormatDisplay(sysStatus, window):
130160mainFrame = [[ sg .Column (col1 ), sg .Column (col2 ), sg .Column (col3 ), sg .Column (col4 ), sg .Column (col5 )]]
131161
132162#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' )]]
134164
135165#frame layout around buttons
136166layout = [[sg .Frame ('Power Supply Overview' , mainFrame )],
0 commit comments