started to push the screensaver over

This commit is contained in:
Florian Streibelt 2011-10-18 22:30:25 +02:00
parent 0aad574f60
commit f575423506
2 changed files with 145 additions and 61 deletions

View File

@ -1,36 +1,70 @@
import serial import serial
from thread import start_new_thread, allocate_lock
import time
class Display: class Display:
def __init__(self,portname="/dev/ttyUSB0"): def __init__(self,portname="/dev/ttyUSB0"):
self.serialport=None self.serialport=None
self.open_port(portname) self.portlock = allocate_lock()
self.portname=portname
self.open_port()
self.brightness=0
self.screensaver = Screensaver(self)
self.screensaver.start()
def __del__(self): def __del__(self):
if (not self.screensaver == None):
self.screensaver.terminate()
self.screensaver = None
if (not self.serialport == None): if (not self.serialport == None):
self.serialport.close() self.serialport.close()
self.serialport=None self.serialport=None
def open_port(self,portname): def open_port(self):
self.serialport = serial.Serial(portname,9600,timeout=2,rtscts=True, dsrdtr=True)
print ("Initializing display:\n %s\n" % self.serialport ) self.mutex_get()
self.cmd_reset() try:
self.write("Hello world") self.serialport = serial.Serial(self.portname,9600,timeout=2,rtscts=True, dsrdtr=True)
print ("Initializing display on port:\n %s\n" % self.serialport )
self.cmd_reset()
self.mutex_release()
except Exception as e:
print ("Exception opening serial port '%s' for display\n" % self.portname)
print ("Ignoring and trying to open it again later\n")
print (e)
self.serialport = None
self.mutex_release()
pass
def cmd_reset(self): def cmd_reset(self):
#reset the display #reset the display
self.write("\x1b\x40") self.cmd("\x1b\x40")
def cmd_cursor_show(self,on=True): def cmd_cursor_show(self,on=True):
#show or hide the cursor #show or hide the cursor
if (on): if (on):
self.write("\x1f\x43\x01") self.cmd("\x1f\x43\x01")
else: else:
self.write("\x1f\x43\x00") self.cmd("\x1f\x43\x00")
def cmd_blink(self,rate=127): def cmd_blink(self,rate=127):
self.write("\x1f\x45%c" % rate) self.cmd("\x1f\x45%c" % chr(rate))
def cmd_brightness(self,level):
if (level==0): #turn off:
self.cmd_display_on(False)
self.brightness = level
return
else:
if (self.brightness==0): #turn on, then set wanted brightness:
self.cmd_display_on(True)
self.brightness = level
self.cmd("\x1F\x58%c" % chr(level-1) )
def cmd_display_on(self,on=True): def cmd_display_on(self,on=True):
# blink-command: # blink-command:
@ -51,24 +85,105 @@ class Display:
cmd_curmode(3) cmd_curmode(3)
def cmd_curmode(self,val=2): def cmd_curmode(self,val=2):
self.write("\x1f%c" % val) self.cmd("\x1f%c" % chr(val))
def cmd_clear(self): def cmd_clear(self):
self.write("\x0c") self.cmd("\x0c")
def cmd_time(self): def cmd_time(self):
# activate timer display in lower right corner. # activate timer display in lower right corner.
# actual time has to be set via display_set_timer. # actual time has to be set via display_set_timer.
self.write("\x1f\x55") self.cmd("\x1f\x55")
def cmd_time_set(self,h,m): def cmd_time_set(self,h=None,m=None):
if (h==None or m==None):
now = time.localtime()
h=now.tm_hour
m=now.tm_min
# configures hour and minutes of the internal clock and # configures hour and minutes of the internal clock and
# displays the time in the lower right corner. # displays the time in the lower right corner.
# turns off when something gets written into the line. # turns off when something gets written into the line.
# two bytes have to follow with hour and minute # two bytes have to follow with hour and minute
self.write("\x1F\x54%c%c" % (h,m) ) self.cmd("\x1F\x54%c%c" % (chr(h),chr(m)) )
self.cmd_curmode_hscroll() # makes the time more robust...
def cmd(self,text): #for commands: do not reset screensaver timeout
self.write(text,False)
def write(self,text,idle_reset=True): #for data: reset screensaver timeout
if (not self.serialport == None):
if (idle_reset):
self.screensaver.idle_reset();
self.serialport.write(text)
else:
self.open_port()
def write(self,text): def mutex_get(self):
self.serialport.write(text) self.portlock.acquire()
def mutex_release(self):
self.portlock.release()
def terminate(self):
if (not self.screensaver == None):
self.screensaver.terminate()
self.screensaver = None
import threading
import time
class Screensaver (threading.Thread):
def __init__(self,display):
self.display=display
self.runme=True
self.idlecounter=0
self.timeout_dim=30
self.timeout_message=60
self.timeout_off=120
self.mutex = allocate_lock()
threading.Thread.__init__(self)
def run(self):
print "Starting Screensaver thread"
while(self.runme):
self.mutex.acquire()
self._main_loop()
self.mutex.release()
time.sleep(.5)
print "Exiting Screensaver thread"
def terminate(self):
self.runme = False
def idle_reset(self):
self.mutex.acquire()
self.idlecounter=0
self.mutex.release()
def _main_loop(self):
if (self.idlecounter < self.timeout_off):
self.idlecounter+=1
if (self.idlecounter>=self.timeout_dim and self.idlecounter <= (self.timeout_dim+7)):
x = (8-( self.idlecounter - self.timeout_dim))/2
self.display.mutax_get()
self.display.cmd_brightness(1+x)
self.display.mutax_release()
return
if (self.idlecounter==self.timeout_message):
return
if (self.idlecounter==self.timeout_off):
self.display.mutax_get()
display.cmd_display_on(False)
self.display.mutax_release()
return
def __del__(self):
print("bye")

View File

@ -60,6 +60,7 @@ lock = allocate_lock()
def display_scroll_text(line,text): def display_scroll_text(line,text):
global scroll_line1,scroll_line2 global scroll_line1,scroll_line2
global offset_line1,offset_line2 global offset_line1,offset_line2
global lock
lock.acquire() lock.acquire()
if (line==1): #clear the line on invocation: if (line==1): #clear the line on invocation:
send_display("\x1f\x24\x01%c\x18" % chr(line) ) send_display("\x1f\x24\x01%c\x18" % chr(line) )
@ -91,18 +92,7 @@ def display_handle_scroll(line,text,offset):
def display_cmd_dim(x): def display_cmd_dim(x):
global brightness return
if (brightness!=x):
if (x==0): #turn off:
send_display("\x1F\x45%c" % chr(255) )
brightness = x
return
else:
if (brightness==0): #turn on, then set wanted brightness:
send_display("\x1F\x45%c" % chr(0) )
brightness = x
send_display("\x1F\x58%c" % chr(x-1) )
def display_thread(x): def display_thread(x):
@ -110,6 +100,7 @@ def display_thread(x):
global offset_line1,offset_line2 global offset_line1,offset_line2
global screensaver global screensaver
global idlemessage global idlemessage
global lock
offset_line1=0 offset_line1=0
offset_line2=0 offset_line2=0
@ -122,9 +113,6 @@ def display_thread(x):
if (screensaver <= SCREENSAVER_OFF): if (screensaver <= SCREENSAVER_OFF):
screensaver=screensaver+1 screensaver=screensaver+1
if ((screensaver >= SCREENSAVER_DIM) and (screensaver <= (SCREENSAVER_DIM+7))): #activate first stage of screensaver:
x = (8-( screensaver - SCREENSAVER_DIM))/2
display_cmd_dim(1+x)
if (screensaver == SCREENSAVER_TIMEOUT): if (screensaver == SCREENSAVER_TIMEOUT):
now = time.localtime() now = time.localtime()
@ -136,36 +124,14 @@ def display_thread(x):
scroll_line2=None scroll_line2=None
offset_line1=0 offset_line1=0
offset_line2=0 offset_line2=0
if (screensaver == SCREENSAVER_OFF):
display_cmd_dim(0)
lock.release() lock.release()
time.sleep(.5) time.sleep(.5)
def send_display(s): def send_display(s):
global display_fifo global myDisplay
if not display_fifo: myDisplay.write(s)
try:
fd = os.open( "/tmp/display", os.O_WRONLY)
display_fifo = os.fdopen(fd,"w")
except OSError:
display_fifo = None
pass
return
except IOError:
display_fifo = None
pass
return
try:
display_fifo.write(s)
display_fifo.flush()
except IOError:
display_fifo = None
pass
return
#Front-End Funtion to display a Screen #Front-End Funtion to display a Screen
# with heading # with heading
@ -198,6 +164,8 @@ def print_display(s):
global scroll_line1,scroll_line2 global scroll_line1,scroll_line2
global offset_line1,offset_line2 global offset_line1,offset_line2
global screensaver global screensaver
global myDisplay
global lock
lock.acquire() lock.acquire()
screensaver=0 screensaver=0
@ -206,7 +174,7 @@ def print_display(s):
scroll_line1=None scroll_line1=None
scroll_line2=None scroll_line2=None
display_cmd_dim(5) display_cmd_dim(5)
send_display(s) myDisplay.write(s)
lock.release() lock.release()
def clear(): def clear():
@ -582,8 +550,7 @@ def main():
myDisplay = Display("/dev/ttyUSB0") myDisplay = Display("/dev/ttyUSB0")
myDisplay.cmd_reset() myDisplay.cmd_reset()
myDisplay.cmd_cursor_show(False)
print_display("\x1b\x40\x1f\x43\x00")
start_new_thread(display_thread,(1,)) start_new_thread(display_thread,(1,))
#display_scroll_text(1,"Line1: Text, dumm scrollend!") #display_scroll_text(1,"Line1: Text, dumm scrollend!")
@ -605,3 +572,5 @@ if __name__ == '__main__':
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
print_display("\x1b\x40Goodbye!") print_display("\x1b\x40Goodbye!")
myDisplay.terminate()