-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
230 lines (181 loc) · 7.67 KB
/
app.py
File metadata and controls
230 lines (181 loc) · 7.67 KB
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# ===============================================================================
# Copyright 2016 Jake Ross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ===============================================================================
# ============= enthought library imports =======================
from chaco.chaco_plot_editor import ChacoPlotItem
from pyface.timer.timer import Timer
from traits.api import HasTraits, Int, Float, Instance, Array, Enum, Bool, Button, Str, List
from traitsui.api import View, Item, HGroup, Handler, spring, Group, UItem, EnumEditor
from enable.api import ComponentEditor
from chaco.api import Plot, ArrayPlotData
# ============= standard library imports ========================
import glob
import time
import os
from numpy import hstack, r_, ones, convolve, asarray
# ============= local library imports ==========================
# adapted from https://github.com/enthought/chaco/blob/master/examples/demo/advanced/data_stream.py
from base_sensor import BaseLightSensor
from paths import unique_path
# from arduino_sensor import ArduinoLightSensor
PERIOD = 250
def smooth(x, window_len=6, window='hanning'):
"""
avaliable windows=['flat', 'hanning', 'hamming', 'bartlett', 'blackman']
"""
x = asarray(x)
s = r_[2 * x[0] - x[window_len - 1::-1], x, 2 * x[-1] - x[-1:-window_len:-1]]
if window == 'flat': # moving average
w = ones(window_len, 'd')
else:
mod = __import__('numpy', fromlist=[window])
func = getattr(mod, window)
w = func(window_len)
y = convolve(w / w.sum(), s, mode='same')
return y[window_len:-window_len + 1]
class Viewer(HasTraits):
"""
"""
def __init__(self, *args, **kw):
super(Viewer, self).__init__(*args, **kw)
self.pd = ArrayPlotData()
self.pd.set_data('index', [])
self.pd.set_data('sindex', [])
self.pd.set_data('intensity', [])
self.pd.set_data('sintensity', [])
self.plot = Plot(data=self.pd)
self.plot.value_range.tight_bounds = False
self.plot.value_range.margin = 0.25
self.plot.plot(('index','intensity'), type='scatter', marker='circle', marker_size=1.5)
self.plot.plot(('sindex','sintensity'), color='green', line_width=1.5)
def traits_view(self):
v = View(UItem('plot', editor=ComponentEditor()))
return v
class BaseController(HasTraits):
# A reference to the plot viewer object
viewer = Instance(Viewer)
# The max number of data points to accumulate and show in the plot
max_num_points = Int(100)
window = Float
recording = Bool
record_button = Button
stop_button = Button
port = Str
ports = List
device = Instance(BaseLightSensor)
start_time = 0
_recording = Bool(False)
def _window_changed(self):
self.max_num_points = int(self.window*1000/float(PERIOD))
def timer_tick(self, *args):
"""
Callback function that should get called based on a timer tick. This
will generate a new random data point and set it on the `.data` array
of our viewer object.
"""
if not self.device:
return
# Generate a new number and increment the tick count
t = time.time()
ct = t - self.start_time
v = self.device.read_value()
if v is None:
return
data = self.viewer.pd
index = data.get_data('index')
value1 = data.get_data('intensity')
value1 = hstack((value1[-self.max_num_points + 1:], [v]))
index = hstack((index[-self.max_num_points + 1:], [ct]))
data.set_data('intensity', value1)
data.set_data('index', index)
if len(index)>11:
y = smooth(value1)
data.set_data('sintensity', y)
data.set_data('sindex', index)
if self._recording:
with open(self._path, 'a') as rfile:
rfile.write('{},{},{}\n'.format(t, ct, v))
def _record_button_fired(self):
root = os.path.join(os.path.expanduser('~'), 'Desktop', 'lightsensor_data')
if not os.path.isdir(root):
os.mkdir(root)
self._path = unique_path(root, 'data')
self._recording = True
def _stop_button_fired(self):
self._recording = False
class ArduionController(BaseController):
port = Str
ports = List
view = View(HGroup(UItem('record_button', enabled_when='not _recording and device'),
UItem('stop_button', enabled_when='_recording and device'),
Item('window'),
UItem('port', editor=EnumEditor(name='ports'))),
buttons=["OK", "Cancel"])
def _port_changed(self):
if self.port:
self.start_time = time.time()
if self.device:
self.device.close()
from arduino_sensor import ArduinoLightSensor
dev = ArduinoLightSensor(address=self.port)
dev.init()
self.device = dev
class KeithleyController(BaseController):
view = View(HGroup(UItem('record_button', enabled_when='not _recording and device'),
UItem('stop_button', enabled_when='_recording and device'),
Item('window')),
buttons=["OK", "Cancel"])
def _device_default(self):
self.start_time = time.time()
from keithley196_sensor import Keithley196LightSensor
dev = Keithley196LightSensor(address='GPIB0::10::INSTR')
dev.init()
return dev
class LSHandler(Handler):
def closed(self, info, is_ok):
""" Handles a dialog-based user interface being closed by the user.
Overridden here to stop the timer once the window is destroyed.
"""
info.object.timer.Stop()
return
class LightSensorApplication(HasTraits):
controller = Instance(BaseController)
viewer = Instance(Viewer, ())
timer = Instance(Timer)
view = View(Item('controller', style='custom', show_label=False),
Item('viewer', style='custom', show_label=False),
title='LightSensor App',
handler=LSHandler,
resizable=True)
controller_klass = None
def edit_traits(self, *args, **kws):
# Start up the timer! We should do this only when the demo actually
# starts and not when the demo object is created.
self.timer = Timer(PERIOD, self.controller.timer_tick)
return super(LightSensorApplication, self).edit_traits(*args, **kws)
def configure_traits(self, *args, **kws):
# Start up the timer! We should do this only when the demo actually
# starts and not when the demo object is created.
self.timer = Timer(PERIOD, self.controller.timer_tick)
return super(LightSensorApplication, self).configure_traits(*args, **kws)
def _controller_default(self):
ports = glob.glob('/dev/tty.usb*')
return self.controller_klass(viewer=self.viewer,
window=500,
ports=ports)
if __name__ == '__main__':
app = LightSensorApplication(controller_klass=KeithleyController)
app.configure_traits()
# ============= EOF =============================================