-
Notifications
You must be signed in to change notification settings - Fork 1
/
cberry.py
157 lines (108 loc) · 3.63 KB
/
cberry.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#****************************************************
# Reimar Barnstorf www.7soft.de *
#****************************************************
# Python Imports
import os
import pygame
import sys
import subprocess
import time
from signal import alarm, signal, SIGALRM, SIGKILL
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
lib_root="/home/pi/devel/mumble-bot/cberry/"
line_offset=16
font_size=16
font_size_bottom=14
bmp_file="/home/pi/devel/mumble-bot/img/temp.bmp"
image_file=bmp_file[:-4]
telegram_user_group="telegram:telegram"
class Cberry:
window=None
font1=None
font2=None
font3=None
current_line_pos=0
def __init__(self, filename=None):
os.environ["SDL_VIDEODRIVER"] = "dummy"
pygame.font.init()
# this section is an unbelievable nasty hack - for some reason Pygame
# needs a keyboardinterrupt to initialise in some limited circs (second time running)
class Alarm(Exception):
pass
def alarm_handler(signum, frame):
raise Alarm
signal(SIGALRM, alarm_handler)
alarm(3)
try:
#pygame.init()
self.window = pygame.display.set_mode((320,240),0,24)#c-berry need 24 bit bmp
alarm(0)
except Alarm:
raise KeyboardInterrupt
self.window = pygame.display.set_mode((320,240),0,24)#c-berry need 24 bit bmp
#print("pygame initalized")
self.font1 = pygame.font.SysFont("dejavusans", font_size)
#self.font1.set_bold(1)
self.font2 = pygame.font.SysFont("dejavusansmono",font_size_bottom)
#self.font2.set_bold(1)
def turn_screen_on(self):
os.system(lib_root+"tft_init")
self.clear_screen()
os.system(lib_root+"tft_pwm 10")
def turn_screen_off(self):
os.system(lib_root+"tft_close")
return
def print_on_screen(self,online_users,ip,cert_exp):
lt = time.localtime()
self.window.fill(BLACK)
self.print_line("PbtH Mumble-Bot")
self.next_line()
self.next_line()
self.print_line("Date/Time: "+time.strftime("%Y-%m-%d", lt)+" "+time.strftime("%H:%M:%S", lt))
self.next_line()
#self.print_line(ip)
#self.next_line()
self.next_line()
for user in online_users:
user_obj=online_users[user]
self.print_line(user_obj.name)
if user_obj.event_counter%2!=0:
color="online"
pygame.draw.circle(self.window, GREEN, (100,int(self.current_line_pos+line_offset/2)), 6)
self.print_line(user_obj.last_active,self.font2,0,110,GREEN)
else:
color="offline"
pygame.draw.circle(self.window, WHITE, (100,int(self.current_line_pos+line_offset/2)), 6, 1)
self.print_line(user_obj.last_active,self.font2,0,110)
self.next_line()
self.print_line("Certificate: "+cert_exp,self.font1,200)
pygame.image.save(self.window, bmp_file)#generate image
subprocess.check_output('chown ' +telegram_user_group+' '+bmp_file, shell=True)#change permission
os.system(lib_root+"tft_bmp "+bmp_file) #show image
self.current_line_pos=0
def next_line(self):
self.current_line_pos=self.current_line_pos+line_offset
def print_line(self,line,font=None,y_pos=0,x_pos=0,color=WHITE):
global current_line_pos
if font==None:
font=self.font1
if y_pos==0:
y_pos=self.current_line_pos
line=self.replace_special_chars(line)
label2 = font.render(line.encode('utf-8'), True, color,BLACK)
self.window.blit(label2, (x_pos,y_pos))
def replace_special_chars(self,value):
value=value.replace(u'ü', 'ue')
value=value.replace(u'ß', 'ss')
value=value.replace(u'ö', 'oe')
value=value.replace(u'ä', 'ae')
return value
def clear_screen(self):
os.system(lib_root+"tft_clear")
return