|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# Author: |
| 4 | +# Marian Dovgialo <marian.dowgialo@gmail.com> |
| 5 | + |
| 6 | +from ugm_blinking_engine import UgmBlinkingEngine |
| 7 | +from PyQt4.QtGui import QSound |
| 8 | +from obci.devices.haptics.HapticsControl import HapticStimulator |
| 9 | +from obci.utils import context as ctx |
| 10 | +from obci.gui.ugm import ugm_engine |
| 11 | +from PyQt4 import QtCore |
| 12 | +import time |
| 13 | + |
| 14 | +class UgmModalBlinkingEngine(UgmBlinkingEngine): |
| 15 | + """A class representing ugm application. It is supposed to fire ugm, |
| 16 | + receive messages from outside (UGM_UPDATE_MESSAGES) and send`em to |
| 17 | + ugm pyqt structure so that it can refresh. |
| 18 | + Can be used to evoke P300 of different modalities |
| 19 | + (visual, haptic, auditory, combinations). |
| 20 | + """ |
| 21 | + def __init__(self, p_config_manager, p_connection, |
| 22 | + context=ctx.get_dummy_context('UgmBlinkingEngine'), |
| 23 | + modalities=['visual',]): |
| 24 | + """Store config manager. |
| 25 | + Args: |
| 26 | + Modalities: modalities used for stimulation, list of strings. |
| 27 | + available options: 'visual', 'auditory', 'haptic' |
| 28 | + """ |
| 29 | + |
| 30 | + super(UgmModalBlinkingEngine, self).__init__(p_config_manager, |
| 31 | + p_connection, |
| 32 | + context) |
| 33 | + self.visual = 'visual' in modalities |
| 34 | + self.haptic = 'haptic' in modalities |
| 35 | + self.auditory = 'auditory' in modalities |
| 36 | + |
| 37 | + #must be here or else they connect to upper class methods |
| 38 | + self._blink_timer = QtCore.QTimer(self) |
| 39 | + self._blink_timer.setSingleShot(True) |
| 40 | + self._blink_timer.connect(self._blink_timer, QtCore.SIGNAL("timeout()"), self._blink) |
| 41 | + |
| 42 | + self._unblink_timer = QtCore.QTimer(self) |
| 43 | + self._unblink_timer.setSingleShot(True) |
| 44 | + self._unblink_timer.connect(self._unblink_timer, QtCore.SIGNAL("timeout()"), self._unblink) |
| 45 | + |
| 46 | + self._stop_timer = QtCore.QTimer(self) |
| 47 | + self._stop_timer.setSingleShot(True) |
| 48 | + self._stop_timer.connect(self._stop_timer, QtCore.SIGNAL("timeout()"), self._stop) |
| 49 | + |
| 50 | + |
| 51 | + if self._run_on_start: |
| 52 | + self.start_blinking() |
| 53 | + |
| 54 | + def _init_auditory(self, configs): |
| 55 | + soundfiles = configs.get_param('soundfiles').split(';') |
| 56 | + sounds = [QSound(f) for f in soundfiles] |
| 57 | + assert len(soundfiles) == len(self._active_ids) |
| 58 | + assert len(sounds) == len(self._active_ids) |
| 59 | + self._sounds = dict(zip(self._active_ids, sounds)) |
| 60 | + |
| 61 | + def _init_haptic(self, configs): |
| 62 | + ids = configs.get_param("haptic_device").split(":") |
| 63 | + vid, pid = [int(i, base=16) for i in ids] |
| 64 | + self.stimulator = HapticStimulator(vid, pid) |
| 65 | + channel_map = (int(i) for i in configs.get_param('haptic_channels_map').split(';')) |
| 66 | + self._haptic_map = dict(zip(self._active_ids, channel_map)) |
| 67 | + self._haptic_duration = float(configs.get_param('haptic_duration')) |
| 68 | + |
| 69 | + |
| 70 | + def set_configs(self, configs): |
| 71 | + for m in self.mgrs: |
| 72 | + m.set_configs(configs) |
| 73 | + self._active_ids = [int(i) for i in configs.get_param('active_field_ids').split(';')] |
| 74 | + self._blink_duration = float(configs.get_param('blink_duration')) |
| 75 | + if self.auditory: |
| 76 | + self._init_auditory(configs) |
| 77 | + if self.haptic: |
| 78 | + self._init_haptic(configs) |
| 79 | + self._run_on_start = int(configs.get_param('running_on_start')) |
| 80 | + |
| 81 | + |
| 82 | + def _blink(self): |
| 83 | + '''Do blinking of configured modality''' |
| 84 | + curr_blink_global_id = self._active_ids[self._curr_blink_id] |
| 85 | + start_time = time.time() |
| 86 | + if self.visual: |
| 87 | + self.update_from(self._curr_blink_ugm) |
| 88 | + if self.auditory: |
| 89 | + self._sounds[curr_blink_global_id].play() |
| 90 | + if self.haptic: |
| 91 | + self.stimulator.stimulate( |
| 92 | + self._haptic_map[curr_blink_global_id], |
| 93 | + self._haptic_duration) |
| 94 | + update_time = time.time() |
| 95 | + if self._blinks_count >= 0: |
| 96 | + self._blinks_count -= 1 |
| 97 | + self.connection.send_blink(self._curr_blink_id, update_time) |
| 98 | + t = 1000*(self._blink_duration - (time.time() - start_time)) |
| 99 | + if t < 0: |
| 100 | + t = 0.0 |
| 101 | + self.context['logger'].warning("BLINKER WARNING: blink duration to short for that computer ...") |
| 102 | + self._unblink_timer.start(t) |
| 103 | + |
| 104 | + |
| 105 | + def _unblink(self): |
| 106 | + start_time = time.time() |
| 107 | + if self.visual: |
| 108 | + self.update_from(self._curr_unblink_ugm) |
| 109 | + update_time = time.time() |
| 110 | + if self._blinks_count == 0 or self.STOP: |
| 111 | + self.STOP = False |
| 112 | + curr_time = self.time_mgr.get_time() |
| 113 | + for m in self.mgrs: |
| 114 | + m.reset() |
| 115 | + self._stop_timer.start(1000*(curr_time - (time.time()-start_time))) |
| 116 | + else: |
| 117 | + self._schedule_blink(start_time) |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == '__main__': |
| 121 | + ugm_engine.run() |
| 122 | + |
0 commit comments