trigger.py
2.83 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
#--------------------------------------------------------------------------
# Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas
# Copyright: (C) 2001 Centro de Pesquisas Renato Archer
# Homepage: http://www.softwarepublico.gov.br
# Contact: invesalius@cti.gov.br
# License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt)
#--------------------------------------------------------------------------
# Este programa e software livre; voce pode redistribui-lo e/ou
# modifica-lo sob os termos da Licenca Publica Geral GNU, conforme
# publicada pela Free Software Foundation; de acordo com a versao 2
# da Licenca.
#
# Este programa eh distribuido na expectativa de ser util, mas SEM
# QUALQUER GARANTIA; sem mesmo a garantia implicita de
# COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
# PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
# detalhes.
#--------------------------------------------------------------------------
import threading
from time import sleep
import wx
from pubsub import pub as Publisher
class Trigger(threading.Thread):
"""
Thread created to use external trigger to interact with software during neuronavigation
"""
def __init__(self, nav_id):
threading.Thread.__init__(self)
self.trigger_init = None
self.stylusplh = False
self.COM = False
self.nav_id = nav_id
self.__bind_events()
try:
import serial
self.trigger_init = serial.Serial('COM1', baudrate=9600, timeout=0)
self.COM = True
except:
#wx.MessageBox(_('Connection with port COM1 failed'), _('Communication error'), wx.OK | wx.ICON_ERROR)
print("Trigger init error: Connection with port COM1 failed")
self.COM = False
self._pause_ = False
self.start()
def __bind_events(self):
Publisher.subscribe(self.OnStylusPLH, 'PLH Stylus Button On')
def OnStylusPLH(self):
self.stylusplh = True
def stop(self):
self._pause_ = True
def run(self):
while self.nav_id:
if self.COM:
self.trigger_init.write(b'0')
sleep(0.3)
lines = self.trigger_init.readlines()
# Following lines can simulate a trigger in 3 sec repetitions
# sleep(3)
# lines = True
if lines:
wx.CallAfter(Publisher.sendMessage, 'Create marker')
sleep(0.5)
if self.stylusplh:
wx.CallAfter(Publisher.sendMessage, 'Create marker')
sleep(0.5)
self.stylusplh = False
sleep(0.175)
if self._pause_:
if self.trigger_init:
self.trigger_init.close()
return