temperature-logger-interface/main.py

72 lines
2.1 KiB
Python

import kivy
from kivy.app import App
from kivy.clock import Clock
from kivy.logger import Logger
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
import kivy.lang
import Queue
import temp_log
import threading
dataThread = None
dataThreadDataQueue = Queue.Queue()
dataThreadCommandQueue = Queue.Queue()
class MainApp(App):
def build(self):
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):
def on_lastTemperature(self, instance, value):
self.text = 'Current temperature: ' + str(value)
if __name__ == '__main__':
dataThread = threading.Thread(None, temp_log.threaded_reader, None, ['/dev/ttyACM0', dataThreadDataQueue, dataThreadCommandQueue])
MainApp().run()