2014-12-09 03:12:21 +00:00
import kivy
from kivy . app import App
2014-12-10 22:22:52 +00:00
from kivy . clock import Clock
from kivy . logger import Logger
from kivy . properties import ObjectProperty , StringProperty
2014-12-09 03:12:21 +00:00
from kivy . uix . boxlayout import BoxLayout
2014-12-10 22:22:52 +00:00
from kivy . uix . floatlayout import FloatLayout
from kivy . uix . label import Label
import kivy . lang
import Queue
import temp_log
import threading
2014-12-09 03:12:21 +00:00
2014-12-10 22:22:52 +00:00
dataThread = None
dataThreadDataQueue = Queue . Queue ( )
dataThreadCommandQueue = Queue . Queue ( )
class MainApp ( App ) :
2014-12-09 03:12:21 +00:00
def build ( self ) :
2014-12-10 22:22:52 +00:00
mainWindow = MainWindow ( )
Clock . schedule_interval ( mainWindow . update_last_temperature , 0.25 )
return mainWindow
def on_stop ( self ) :
global dataThreadCommandQueue
dataThreadCommandQueue . put_nowait ( ' stop ' )
def on_start ( self ) :
global dataThread
dataThread . start ( )
class MainWindow ( FloatLayout ) :
""" Main Window class """
dataSource = StringProperty ( ' /dev/ttyACM0 ' )
lastTemperature = StringProperty ( ' n/a ' )
def update_last_temperature ( self , dt ) :
global dataThreadDataQueue
# This breaks if there are more updates in the queue than the frequency of update_last_temperature - it pulls old data out of the queue, and not the most recent.
try :
data = dataThreadDataQueue . get_nowait ( )
if data is not None :
self . lastTemperature = str ( data [ ' data ' ] [ ' temperature ' ] )
except Queue . Empty :
pass
def on_lastTemperature ( self , instance , value ) :
Logger . debug ( ' lastTemperature has changed to: ' + str ( value ) )
children = self . children [ : ]
while children :
child = children . pop ( )
try :
child . on_lastTemperature ( instance , value )
except Exception , e :
pass
children . extend ( child . children )
class MyLabel ( Label ) :
2014-12-09 03:12:21 +00:00
2014-12-10 22:22:52 +00:00
def on_lastTemperature ( self , instance , value ) :
self . text = ' Current temperature: ' + str ( value )
2014-12-09 03:12:21 +00:00
if __name__ == ' __main__ ' :
2014-12-10 22:22:52 +00:00
dataThread = threading . Thread ( None , temp_log . threaded_reader , None , [ ' /dev/ttyACM0 ' , dataThreadDataQueue , dataThreadCommandQueue ] )
MainApp ( ) . run ( )