-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathglobal_functions.py
executable file
·157 lines (125 loc) · 4.84 KB
/
global_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
'''!
Contains functions invoked by several different parts of the code.
'''
import sys, os
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/')
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/assets')
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/assets/saved_fonts')
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/sensor_pages')
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/general_pages')
sys.path.append('/home/pi/Sensor_Scripts/pygame_code/tricorder/resources')
import pygame
import datetime
from subprocess import PIPE, Popen, check_output
import psutil
from fonts import FONT_DIN,FONT_OKUDA,FONT_OKUDA_BOLD,WIFI_FONT
from colors import WHITE, ORANGE, LIGHT_BLUE
# ================ Global functions ================================= #
def my_map(x,in_min,in_max,out_min,out_max):
'''
Standard mapping formula. Used in many places
:note: Standard formula
:custom: Custom
:param x: Input value to be mapped
:param in_min: Least possible value for input
:param in_max: Max possible value for input
:param out_min: Least possible value for output
:param out_max: Max possible value for output
:return: Scaled value
'''
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
def blitRotate2(surf, image, topleft, angle):
''' Rotate an image around its center
:return: surf,w,h
'''
rotated_image = pygame.transform.rotate(image, angle)
new_rect = rotated_image.get_rect(center = image.get_rect(topleft = topleft).center)
surf.blit(rotated_image, new_rect.topleft)
def get_text_dimensions(text='',font_style=FONT_DIN,font_color=WHITE,style=0,font_size=28):
''' Return size of to-be-rendered text'''
val_txt=font_style.render(text,fgcolor=font_color,size=font_size)
x=list(val_txt)
w=x[1][2]
h=x[1][3]
surf=x[0]
return surf,w,h
def flip_buttons(pressed_button,button_list):
''' Unselect all other buttons except the one just pressed'''
if pressed_button.selected:
return
else:
pressed_button.selected=True
for button in button_list:
if button!=pressed_button:
button.selected=False
def update_cpu_stats(dt=None):
'''! Get rapsberry pi usage stats'''
process = Popen(['vcgencmd', 'measure_temp'], stdout=PIPE)
output, _error = process.communicate()
output = output.decode()
pos_start = output.index('=') + 1
pos_end = output.rindex("'")
temp = float(output[pos_start:pos_end])
cpu_percent=int(round(psutil.cpu_percent()))
bars=''
for iter in range(cpu_percent//10):
bars+='||'
cpu_pct=bars+f' {cpu_percent}%'
cpu_temp=f'{temp}°C'
# self.cpu_label.text=cpu_txt
return cpu_pct,cpu_temp
def get_wifi_name():
'''! Check for connected wifi network. Return network name'''
try:
wifi_name=check_output(['iwgetid','-r']).decode('utf-8').strip('\n')
if wifi_name=='Wu Tang LAN':
wifi_name='Terok Nor'
except Exception:
wifi_name='No Wifi'
wifi_name=wifi_name
return wifi_name
def get_date_time():
'''! Get formatted time for top toolbar display'''
now = datetime.datetime.now()
day=now.strftime('%a')
date=now.strftime('%b %-d')
hour_sec=now.strftime('%-I:%M:%S %p')
# hour_sec=now.strftime('%-I:%M %p')
return (day,date, hour_sec)
def blit_some_stats(screen,width,day,date,hour_sec,fps,cpu_pct,cpu_temp,wifi_name,wifi_symbol,bluetooth_img):
'''! These pieces of info are always displayed in the top bar or in the Okudagrams'''
# Day/Date
day,date,hour_sec=get_date_time()
FONT_OKUDA_BOLD.render_to(screen, (10, 205), day+', '+date, fgcolor=ORANGE,style=0,size=38)
# Time
txt_surf,w,h=get_text_dimensions(text=hour_sec,font_style=FONT_DIN,font_color=WHITE,style=0,font_size=28)
screen.blit(txt_surf,((width//2-w//2+30),10))
# FPS
rr=255
txt_surf,w,h=get_text_dimensions(text=fps+' FPS',font_style=FONT_OKUDA,font_color=LIGHT_BLUE,style=0,font_size=44)
screen.blit(txt_surf,(105-w,rr))
# CPU percentage
rr+=60
txt_surf,w,h=get_text_dimensions(text=cpu_pct,font_style=FONT_OKUDA,font_color=LIGHT_BLUE,style=0,font_size=32)
screen.blit(txt_surf,(105-w,rr))
# CPU temp
rr+=40
txt_surf,w,h=get_text_dimensions(text=cpu_temp,font_style=FONT_OKUDA,font_color=LIGHT_BLUE,style=0,font_size=32)
screen.blit(txt_surf,(105-w,rr))
# Wifi name
txt_surf,w,h=get_text_dimensions(text=wifi_name,font_style=FONT_OKUDA,font_color=WHITE,style=0,font_size=34)
screen.blit(txt_surf,((width-w-10),10))
# Wifi symbol
txt_surf,w2,h=get_text_dimensions(text=wifi_symbol,font_style=WIFI_FONT,font_color=WHITE,style=0,font_size=26)
screen.blit(txt_surf,(width-w-10-w2-10,8))
# Bluetooth
x_pos=width-w-10-w2-10
screen.blit(bluetooth_img,(x_pos-23,4))
return w,w2
def adjust_gauge_lims(curr_val,gauge):
'''! For circular gauges. Adjusts upper/lower limits to nearest round numbers'''
lower_lims=[0,1,10,100,1000,10000,100000]
upper_lims=[1,10,100,1000,10000,100000,1000000]
for low,up in zip(lower_lims,upper_lims):
if low<=curr_val<up:
gauge.in_max=up