initial pass at having a ui update at regular intervales
This commit is contained in:
parent
bcf72d3bbb
commit
290fb94a9d
|
@ -0,0 +1,21 @@
|
||||||
|
<MyLabel@Label>:
|
||||||
|
text: 'Current Temperature: n/a'
|
||||||
|
# on_lastTemperature: self.update_current_temperature_label()
|
||||||
|
|
||||||
|
<MainWindow>:
|
||||||
|
label_wid: "Temperature Monitor"
|
||||||
|
BoxLayout:
|
||||||
|
orientation: 'vertical'
|
||||||
|
BoxLayout:
|
||||||
|
size_hint_y: 0.1
|
||||||
|
orientation: 'horizontal'
|
||||||
|
MyLabel:
|
||||||
|
id: 'current_temperature'
|
||||||
|
Button:
|
||||||
|
text: 'Start/Stop'
|
||||||
|
Label:
|
||||||
|
text: 'No File Chosen'
|
||||||
|
Label:
|
||||||
|
text: 'Data Source'
|
||||||
|
Label:
|
||||||
|
text: 'Graph placeholder'
|
72
main.py
72
main.py
|
@ -1,21 +1,71 @@
|
||||||
import kivy
|
import kivy
|
||||||
from kivy.app import App
|
from kivy.app import App
|
||||||
from kivy.uix.label import Label
|
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.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
|
||||||
|
|
||||||
class MyApp(App):
|
dataThread = None
|
||||||
|
dataThreadDataQueue = Queue.Queue()
|
||||||
|
dataThreadCommandQueue = Queue.Queue()
|
||||||
|
|
||||||
|
class MainApp(App):
|
||||||
|
|
||||||
def build(self):
|
def build(self):
|
||||||
return Label(text=unicode('Temperature Logger'))
|
mainWindow = MainWindow()
|
||||||
|
Clock.schedule_interval(mainWindow.update_last_temperature, 0.25)
|
||||||
|
return mainWindow
|
||||||
|
|
||||||
class MainScreen(BoxLayout):
|
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
def on_stop(self):
|
||||||
super(MainScreen, self).__init__(**kwargs)
|
global dataThreadCommandQueue
|
||||||
self.orientation = 'vertical'
|
dataThreadCommandQueue.put_nowait('stop')
|
||||||
top = new BoxLayout(orientation = 'horizontal')
|
|
||||||
self.add_widget(top)
|
|
||||||
pass
|
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__':
|
if __name__ == '__main__':
|
||||||
MyApp().run()
|
dataThread = threading.Thread(None, temp_log.threaded_reader, None, ['/dev/ttyACM0', dataThreadDataQueue, dataThreadCommandQueue])
|
||||||
|
MainApp().run()
|
||||||
|
|
44
temp_log.py
44
temp_log.py
|
@ -1,5 +1,10 @@
|
||||||
#! /usr/bin/env python
|
#! /usr/bin/env python
|
||||||
import serial, sys, time, io
|
import io
|
||||||
|
import Queue
|
||||||
|
import serial
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
defaultSerialPort = '/dev/ttyACM0'
|
defaultSerialPort = '/dev/ttyACM0'
|
||||||
defaultVersion = 0
|
defaultVersion = 0
|
||||||
|
|
||||||
|
@ -14,12 +19,42 @@ def parse_line(line, version = defaultVersion):
|
||||||
data = {}
|
data = {}
|
||||||
if (version == 0):
|
if (version == 0):
|
||||||
line = line.split(' ')
|
line = line.split(' ')
|
||||||
data['temperature'] = float(line[-2])
|
try:
|
||||||
|
data['temperature'] = float(line[-2])
|
||||||
|
except Exception, e:
|
||||||
|
pass
|
||||||
else:
|
else:
|
||||||
raise ParseException('Unimplemented line version', version)
|
raise ParseException('Unimplemented line version', version)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def threaded_reader(source, dataQueue, commandQueue):
|
||||||
|
import kivy.logger
|
||||||
|
ser = serial.Serial(source, baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
|
||||||
|
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
|
||||||
|
ser.isOpen()
|
||||||
|
buffer = ''
|
||||||
|
runTime = ''
|
||||||
|
cmd = ''
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
cmd = commandQueue.get_nowait()
|
||||||
|
except Queue.Empty:
|
||||||
|
pass
|
||||||
|
if cmd == 'stop':
|
||||||
|
break
|
||||||
|
try:
|
||||||
|
data = parse_line(ser.readline(), defaultVersion)
|
||||||
|
runTime = time.time()
|
||||||
|
except Exception, e:
|
||||||
|
kivy.logger.Logger.exception(e)
|
||||||
|
continue
|
||||||
|
if data and runTime:
|
||||||
|
item = {'time' : runTime, 'data' : data}
|
||||||
|
kivy.logger.Logger.debug(str(item))
|
||||||
|
dataQueue.put(item)
|
||||||
|
|
||||||
|
|
||||||
def main(args = sys.argv):
|
def main(args = sys.argv):
|
||||||
# @TODO switch to parse args or similar
|
# @TODO switch to parse args or similar
|
||||||
# @TODO parse of temperature logger line version
|
# @TODO parse of temperature logger line version
|
||||||
|
@ -37,8 +72,9 @@ def main(args = sys.argv):
|
||||||
try:
|
try:
|
||||||
buffer = ser.readline()
|
buffer = ser.readline()
|
||||||
data = parse_line(buffer, lineVersion)
|
data = parse_line(buffer, lineVersion)
|
||||||
print unicode(time.time()) + ',' + unicode(data['temperature'])
|
if data:
|
||||||
sys.stdout.flush()
|
print unicode(time.time()) + ',' + unicode(data['temperature'])
|
||||||
|
sys.stdout.flush()
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
sys.stderr.write(str(e) + '\n')
|
sys.stderr.write(str(e) + '\n')
|
||||||
continue
|
continue
|
||||||
|
|
Loading…
Reference in New Issue