-
Notifications
You must be signed in to change notification settings - Fork 10
/
sensor_toolbar.py
476 lines (413 loc) · 18.6 KB
/
sensor_toolbar.py
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
# -*- coding: utf-8 -*-
#
# Author: Arjun Sarwal arjun@laptop.org
# Copyright (C) 2007, Arjun Sarwal
# Copyright (C) 2009-13 Walter Bender
# Copyright (C) 2009, Benjamin Berg, Sebastian Berg
# Copyright (C) 2016, James Cameron [Gtk+ 3.0]
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# You should have received a copy of the GNU General Public License
# along with this library; if not, write to the Free Software
# Foundation, 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
from gi.repository import GLib
from gi.repository import Gtk
from gettext import gettext as _
from config import CAPTURE_GAIN, MIC_BOOST, XO1, XO15, XO175, XO4
from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.menuitem import MenuItem
from sugar3.graphics.radiotoolbutton import RadioToolButton
import logging
log = logging.getLogger('measure-activity')
log.setLevel(logging.DEBUG)
LOG_TIMER_VALUES = [1, 10, 300, 3000, 18000] # In 10th second intervals
LOG_TIMER_LABELS = {1: _('1/10 second'), 10: _('1 second'),
300: _('30 seconds'), 3000: _('5 minutes'),
30000: _('30 minutes')}
def _can_use_dc(hw):
''' Return True if this is DC sensor capable hardware '''
return hw in [XO1, XO15, XO175, XO4]
class SensorToolbar(Gtk.Toolbar):
''' The toolbar for specifiying the sensor: sound, resitance, or
voltage '''
LOWER = 0.0
UPPER = 1.0
STR_DC_R = \
_("Resistive sensor (connect sensor to pink 'Mic In' on left side \
of XO)") + ' '
STR_DC_V = \
_("Voltage sensor (connect sensor to pink 'Mic In' on left side \
of XO)") + ' '
STR_AC = _('Sound') + ' '
STR_RESISTANCE = _('Resistance') + ' (' + _('Ohms') + ') '
STR_VOLTAGE = _('Voltage') + ' (' + _('Volts') + ') '
STR_TIME = _('Time Base') + ' '
STR_FREQUENCY = _('Frequency Base') + ' '
STR_INVERT = ' ' + _('Invert') + ' '
STR_XAXIS_TEXT = _('X Axis Scale: 1 division = %(division)s %(unit)s')
# TRANSLATORS: This is milli seconds.
MS = _('ms')
# TRANSLATORS: This is Hertz, so 1/second.
HZ = _('Hz')
def __init__(self, activity, channels):
''' By default, start with resistance mode '''
super(type(self), self).__init__()
self.activity = activity
self._channels = channels
self._lock_radio_buttons = False
self._radio_button_pushed = False
self.values = []
for i in range(self._channels):
self.values.append('')
self.string_for_textbox = ''
self.gain = 1.0
self.y_mag = 3.0
self.capture_gain = CAPTURE_GAIN
self.mic_boost = MIC_BOOST
self.mode = 'sound'
# Set up Time-domain Button
self.time = RadioToolButton(icon_name='media-audio', group=None)
self.insert(self.time, -1)
self.time.set_tooltip(_('Sound'))
self.time.connect(
'clicked', self.analog_resistance_voltage_mode_cb, 'sound')
# Set up Resistance Button
self.resistance = RadioToolButton(icon_name='resistance',
group=self.time)
if _can_use_dc(self.activity.hw):
self.insert(self.resistance, -1)
self.resistance.show()
self.resistance.set_tooltip(_('Resistance Sensor'))
self.resistance.connect('clicked',
self.analog_resistance_voltage_mode_cb,
'resistance')
# Set up Voltage Button
self.voltage = RadioToolButton(icon_name='voltage', group=self.time)
if _can_use_dc(self.activity.hw):
self.insert(self.voltage, -1)
self.voltage.set_tooltip(_('Voltage Sensor'))
self.voltage.connect('clicked',
self.analog_resistance_voltage_mode_cb,
'voltage')
separator = Gtk.SeparatorToolItem()
separator.props.draw = True
self.insert(separator, -1)
self._log_value = LOG_TIMER_VALUES[1]
self.log_label = Gtk.Label(self._log_to_string(self._log_value))
toolitem = Gtk.ToolItem()
toolitem.add(self.log_label)
self.insert(toolitem, -1)
self._log_button = ToolButton('timer-10')
self._log_button.set_tooltip(_('Select logging interval'))
self._log_button.connect('clicked', self._log_selection_cb)
self.insert(self._log_button, -1)
self._setup_log_palette()
# Set up Logging/Stop Logging Button
self._record = ToolButton('media-record')
self.insert(self._record, -1)
self._record.set_tooltip(_('Start logging'))
self._record.connect('clicked', self.record_control_cb)
separator = Gtk.SeparatorToolItem()
separator.props.draw = True
self.insert(separator, -1)
toolitem = Gtk.ToolItem()
self.trigger_label = Gtk.Label(_('Trigger'))
toolitem.add(self.trigger_label)
self.insert(toolitem, -1)
# Set up Trigger Combo box
self.trigger_none = RadioToolButton()
self.trigger_none.set_icon_name('trigger-none')
self.insert(self.trigger_none, -1)
self.trigger_none.set_tooltip(_('None'))
self.trigger_none.connect('clicked',
self.update_trigger_control_cb,
self.activity.wave.TRIGGER_NONE)
self.trigger_rise = RadioToolButton(group=self.trigger_none)
self.trigger_rise.set_icon_name('trigger-rise')
self.insert(self.trigger_rise, -1)
self.trigger_rise.set_tooltip(_('Rising Edge'))
self.trigger_rise.connect('clicked',
self.update_trigger_control_cb,
self.activity.wave.TRIGGER_POS)
self.trigger_fall = RadioToolButton(group=self.trigger_none)
self.trigger_fall.set_icon_name('trigger-fall')
self.insert(self.trigger_fall, -1)
self.trigger_fall.set_tooltip(_('Falling Edge'))
self.trigger_fall.connect('clicked',
self.update_trigger_control_cb,
self.activity.wave.TRIGGER_NEG)
self.show_all()
def get_log(self):
return self._log_value
def get_log_idx(self):
if self._log_value in LOG_TIMER_VALUES:
return LOG_TIMER_VALUES.index(self._log_value)
else:
return LOG_TIMER_VALUES[0]
def set_log_idx(self, idx):
self._log_value = LOG_TIMER_VALUES[idx]
self.log_label.set_text(self._log_to_string(self._log_value))
if hasattr(self, '_log_button'):
self._log_button.set_icon_name('timer-%d' % (self._log_value))
def _log_selection_cb(self, widget):
if self._log_palette:
if not self._log_palette.is_up():
self._log_palette.popup(immediate=True)
else:
self._log_palette.popdown(immediate=True)
return
def _log_to_seconds(self, tenth_seconds):
return tenth_seconds / 10.
def _log_to_string(self, tenth_seconds):
if tenth_seconds in LOG_TIMER_LABELS:
return LOG_TIMER_LABELS[tenth_seconds]
else:
return _('1 second')
def _setup_log_palette(self):
self._log_palette = self._log_button.get_palette()
for tenth_seconds in LOG_TIMER_VALUES:
text = self._log_to_string(tenth_seconds)
menu_item = MenuItem(icon_name='timer-%d' % (tenth_seconds),
text_label=text)
menu_item.connect('activate', self._log_selected_cb, tenth_seconds)
self._log_palette.menu.append(menu_item)
menu_item.show()
def _log_selected_cb(self, button, seconds):
self.set_log_idx(LOG_TIMER_VALUES.index(seconds))
def add_frequency_slider(self, toolbox):
''' Either on the Sound toolbar or the Main toolbar '''
self._freq_stepper_up = ToolButton('freq-high')
self._freq_stepper_up.set_tooltip(_('Zoom out'))
self._freq_stepper_up.connect('clicked', self._freq_stepper_up_cb)
self._freq_stepper_up.show()
self.activity.adjustmentf = Gtk.Adjustment(
0.5, self.LOWER, self.UPPER, 0.01, 0.1, 0)
self.activity.adjustmentf.connect('value_changed', self.cb_page_sizef)
self._freq_range = Gtk.Scale(orientation=Gtk.Orientation.HORIZONTAL,
adjustment=self.activity.adjustmentf)
self._freq_range.set_inverted(True)
self._freq_range.set_draw_value(False)
self._freq_range.set_size_request(120, 15)
self._freq_range.show()
self._freq_stepper_down = ToolButton('freq-low')
self._freq_stepper_down.set_tooltip(_('Zoom in'))
self._freq_stepper_down.connect('clicked', self._freq_stepper_down_cb)
self._freq_stepper_down.show()
self._freq_range_tool = Gtk.ToolItem()
self._freq_range_tool.add(self._freq_range)
self._freq_range_tool.show()
toolbox.add(self._freq_stepper_up)
toolbox.add(self._freq_range_tool)
toolbox.add(self._freq_stepper_down)
return
def update_trigger_control_cb(self, button, value):
if button is None:
value = self.activity.wave.TRIGGER_NONE
try:
if self.activity.wave.get_fft_mode():
self.trigger_none.set_active(True)
else:
self.activity.wave.set_trigger(value)
except RuntimeError as e:
logging.debug('Warning: Maximum Recursions on the same method has taken place', e)
def analog_resistance_voltage_mode_cb(self, button=None,
mode_to_set='sound'):
''' Callback for Analog/Resistance/Voltage Buttons '''
if self._lock_radio_buttons:
logging.debug('mode selector locked')
self._radio_button_pushed = True
return
if self.mode == mode_to_set:
logging.debug('mode already set to %s' % mode_to_set)
return
self._lock_radio_buttons = True
if self.activity.CONTEXT == 'sound':
self.sound_context_off()
else:
self.sensor_context_off()
# Force time domain when switching modes
if self.activity.wave.get_fft_mode():
self.activity.timefreq_control()
# Turn off logging when switching modes
if self.activity.audiograb.we_are_logging:
self.record_control_cb()
self.set_mode(mode_to_set)
if mode_to_set == 'sound':
self.set_sound_context()
elif mode_to_set == 'resistance':
self.set_sensor_context()
elif mode_to_set == 'voltage':
self.set_sensor_context()
self.update_string_for_textbox()
return False
def unlock_radio_buttons(self):
''' Enable radio button selection '''
logging.debug('unlocking radio buttons')
if self._radio_button_pushed:
if self.mode == 'sound':
self.time.set_active(True)
elif self.mode == 'resistance':
self.resistance.set_active(True)
elif self.mode == 'voltage':
self.voltage.set_active(True)
self._lock_radio_buttons = False
self._radio_button_pushed = False
def set_mode(self, mode='sound'):
''' Set the mixer settings to match the current mode. '''
self.mode = mode
self.activity.audiograb.set_sensor_type(self.mode)
for i in range(self._channels):
self.values[i] = 0.0
return
def get_mode(self):
''' Get the mixer settings. '''
return self.mode
def _freq_stepper_up_cb(self, button=None):
''' Moves the horizontal zoom slider to the left one notch,
where one notch is 1/100 of the total range. This correspond
to zooming out as a larger number of Hertz or milliseconds
will be represented by the same space on the screen. '''
new_value = self._freq_range.get_value() + \
(self.UPPER - self.LOWER) / 100.0
if new_value <= self.UPPER:
self._freq_range.set_value(new_value)
else:
self._freq_range.set_value(self.UPPER)
def _freq_stepper_down_cb(self, button=None):
''' Moves the horizontal zoom slider to the right one notch,
where one notch is 1/100 of the total range. This corresponds
to zooming in. '''
new_value = self._freq_range.get_value() - \
(self.UPPER - self.LOWER) / 100.0
if new_value >= self.LOWER:
self._freq_range.set_value(new_value)
else:
self._freq_range.set_value(self.LOWER)
def cb_page_sizef(self, button=None):
''' Callback to scale the frequency range (zoom in and out) '''
if self._update_page_size_id:
GLib.source_remove(self._update_page_size_id)
self._update_page_size_id =\
GLib.timeout_add(250, self.update_page_size)
return True
def update_page_size(self):
''' Set up the scaling of the display. '''
self._update_page_size_id = None
value = self.activity.adjustmentf.get_value()
new_value = round(value * 100.0) / 100.0
if value != new_value:
self.activity.adjustmentf.set_value(new_value)
return False
time_div = 0.001 * max(value, 0.05)
freq_div = 1000 * max(value, 0.01)
self.activity.wave.set_div(time_div, freq_div)
self.update_string_for_textbox()
return False
def set_sound_context(self):
''' Called when analog sensing is selected '''
self.set_show_hide_windows(mode='sound')
GLib.timeout_add(500, self.sound_context_on)
self.activity.CONTEXT = 'sound'
def set_sensor_context(self):
''' Called when digital sensing is selected '''
self.set_show_hide_windows(mode='sensor')
GLib.timeout_add(500, self.sensor_context_on)
self.activity.CONTEXT = 'sensor'
def set_show_hide_windows(self, mode='sound'):
''' Shows the appropriate window identified by the mode '''
self.activity.wave.set_context_on()
for i in range(self._channels):
self.activity.side_toolbars[i].set_show_hide(True, mode)
def sensor_context_off(self):
''' Called when a DC sensor is no longer selected '''
# self.activity.audiograb.pause_grabbing()
self.activity.audiograb.stop_grabbing()
def sensor_context_on(self):
''' Called when a DC sensor is selected '''
self.update_string_for_textbox()
self.activity.wave.set_trigger(self.activity.wave.TRIGGER_NONE)
# self.activity.audiograb.resume_grabbing()
self.activity.audiograb.start_grabbing()
return False
def sound_context_off(self):
''' Called when an analog sensor is no longer selected '''
self.gain, self.y_mag = self.activity.wave.get_mag_params()
self.capture_gain = self.activity.audiograb.get_capture_gain()
self.mic_boost = self.activity.audiograb.get_mic_boost()
self.activity.audiograb.stop_grabbing()
def sound_context_on(self):
''' Called when an analog sensor is selected '''
self.activity.wave.set_mag_params(self.gain, self.y_mag)
self.update_string_for_textbox()
self.update_trigger_control_cb(None, self.activity.wave.TRIGGER_NONE)
self.activity.audiograb.start_grabbing()
return False
def set_sample_value(self, value='', channel=0):
''' Write a sample value to the textbox. '''
self.values[channel] = value
self.update_string_for_textbox()
return
def record_control_cb(self, button=None):
''' Depending upon the selected interval, does either a logging
session, or just logs the current buffer. '''
if self.activity.audiograb.we_are_logging:
self.activity.audiograb.set_logging_params(start_stop=False)
self._record.set_icon_name('media-record')
self._record.show()
self._record.set_tooltip(_('Start Recording'))
else:
Xscale = (1.00 / self.activity.audiograb.get_sampling_rate())
Yscale = 0.0
interval = self._log_value / 10. # self.interval_convert()
username = self.activity.nick
if self.activity.wave.get_fft_mode():
self.activity.data_logger.start_new_session(
username, Xscale, Yscale,
self._log_to_string(self._log_value),
channels=self._channels, mode='frequency')
else:
self.activity.data_logger.start_new_session(
username, Xscale, Yscale,
self._log_to_string(self._log_value),
channels=self._channels, mode=self.mode)
self.activity.audiograb.set_logging_params(
start_stop=True, interval=interval)
self._record.set_icon_name('record-stop')
self._record.show()
self._record.set_tooltip(_('Stop Recording'))
self.activity.new_recording = True
def update_string_for_textbox(self):
''' Update the status field at the bottom of the canvas. '''
if self.mode == 'resistance':
string_for_textbox = (self.STR_DC_R + '\n')
string_for_textbox += self.STR_RESISTANCE
elif self.mode == 'voltage':
string_for_textbox = (self.STR_DC_V + '\n')
string_for_textbox += self.STR_VOLTAGE
else:
string_for_textbox = (self.STR_AC + '\t')
if self.activity.wave.get_fft_mode():
scalex = self.STR_XAXIS_TEXT % {
'unit': self.HZ, 'division': self.activity.wave.freq_div}
string_for_textbox += self.STR_FREQUENCY
string_for_textbox += ('\n' + scalex)
elif self.mode == 'sound':
scalex = self.STR_XAXIS_TEXT % {
'unit': self.MS,
'division': self.activity.wave.time_div * 1000}
string_for_textbox += self.STR_TIME
string_for_textbox += ('\n' + scalex)
else:
for i in range(self._channels):
string_for_textbox += '\t(%s)' % (self.values[i])
invert = False
for i in range(self._channels):
if self.activity.wave.get_invert_state(channel=i):
invert = True
if invert:
string_for_textbox += self.STR_INVERT
self.activity.text_box.set_label(string_for_textbox)