class ecg_real(object):

	from neurosky_ecg import NeuroskyECG
	import sys

	def __init__(self):
		self.lead_count = 0
		
		target_port = 'COM3'
		#target_port = 'devA/tty.XXXXXXX'  #change this to work on OSX

		try:
		    self.nskECG = NeuroskyECG(target_port)
		except serial.serialutil.SerialException:
		    print "Could not open target serial port: %s" % target_port
		    sys.exit(1)

		#optional call, default is already 1
		self.nskECG.setHRVUpdate(1) #update hrv every 1 detected pulses

		self.cur_lead_on = False
		self.cur_hrv = 0


	def start(self):
		# start running the serial producer thread
		self.nskECG.start()

		# this loop is the consumer thread, and will pop 
		# dict values (with 'timestamp', 'ecg_raw', and 'leadoff'
		# from the internal buffer and run the analysis on the 
		# data.

		self.cur_hrv = None #whatever the current hrv value is
		self.cur_hrv_t = None #timestamp with the current hrv

		sample_count = 0 #keep track of numbers of samples we've processed
		leadoff_count = 0 #counter for length of time been leadoff
		while True:
			if not self.nskECG.isBufferEmpty():
				sample_count+=1
				D = self.nskECG.popBuffer() #get the oldest dict

				# if we are more than 2 seconds in and leadoff is still zero
				if D['leadoff']==0:
					leadoff_count+=1
					if leadoff_count> self.nskECG.Fs*2:
						if self.nskECG.getTotalNumRRI()!=0:
							#reset the library
							self.nskECG.ecgalgResetLib()
						self.nskECG.ecg_buffer.task_done() #let queue know that we're done
						continue
				else: # leadoff==200, or lead is on
					leadoff_count=0

				D = self.nskECG.ecgalgAnalyzeRaw(D)

				self.cur_lead_on = D['leadoff']
				if 'hrv' in D:
					self.cur_hrv = D['hrv']
					self.cur_hrv_t = D['timestamp']

			# we keep looping until something tells us to stop
		pass #		


	def is_lead_on(self):
		return self.cur_lead_on

	def get_hrv(self):
		return self.cur_hrv
cur_hrv = None  # whatever the current hrv value is
cur_hrv_t = None  # timestamp with the current hrv

sample_count = 0  # keep track of numbers of samples we've processed
leadoff_count = 0  # counter for length of time been leadoff
while True:
    if not nskECG.isBufferEmpty():
        sample_count += 1
        D = nskECG.popBuffer()  # get the oldest dict

        # if we are more than 2 seconds in and leadoff is still zero
        if D['leadoff'] == 0:
            leadoff_count += 1
            if leadoff_count > nskECG.Fs * 2:
                if nskECG.getTotalNumRRI() != 0:
                    # reset the library
                    nskECG.ecgalgResetLib()
                nskECG.ecg_buffer.task_done()  # let queue know that we're done
                continue
        else:  # leadoff==200, or lead is on
            leadoff_count = 0

        D = nskECG.ecgalgAnalyzeRaw(D)

        if 'hrv' in D:
            cur_hrv = D['hrv']
            cur_hrv_t = D['timestamp']

    # we keep looping until something tells us to stop
pass  #
cur_hrv = None  # whatever the current hrv value is
cur_hrv_t = None  # timestamp with the current hrv

sample_count = 0  # keep track of numbers of samples we've processed
leadoff_count = 0  # counter for length of time been leadoff
while True:
    if not nskECG.isBufferEmpty():
        sample_count+=1
        D = nskECG.popBuffer()  # get the oldest dict

        # if we are more than 2 seconds in and leadoff is still zero
        if D['leadoff']==0:
            leadoff_count+=1
            if leadoff_count> nskECG.Fs*2:
                if nskECG.getTotalNumRRI()!=0:
                    # reset the library
                    nskECG.ecgalgResetLib()
                nskECG.ecg_buffer.task_done()  # let queue know that we're done
                continue
        else: # leadoff==200, or lead is on
            leadoff_count=0

        D = nskECG.ecgalgAnalyzeRaw(D)

        if 'hrv' in D:
            cur_hrv = D['hrv']
            cur_hrv_t = D['timestamp']

    # we keep looping until something tells us to stop
pass #
예제 #4
0
class ecg_real(object):

    from neurosky_ecg import NeuroskyECG
    import sys

    def __init__(self):
        self.lead_count = 0

        target_port = 'COM3'
        #target_port = 'devA/tty.XXXXXXX'  #change this to work on OSX

        try:
            self.nskECG = NeuroskyECG(target_port)
        except serial.serialutil.SerialException:
            print("Could not open target serial port: %s" % target_port)
            sys.exit(1)

        #optional call, default is already 1
        self.nskECG.setHRVUpdate(1)  #update hrv every 1 detected pulses

        self.cur_lead_on = False
        self.cur_hrv = 0

    def start(self):
        # start running the serial producer thread
        self.nskECG.start()

        # this loop is the consumer thread, and will pop
        # dict values (with 'timestamp', 'ecg_raw', and 'leadoff'
        # from the internal buffer and run the analysis on the
        # data.

        self.cur_hrv = None  #whatever the current hrv value is
        self.cur_hrv_t = None  #timestamp with the current hrv

        sample_count = 0  #keep track of numbers of samples we've processed
        leadoff_count = 0  #counter for length of time been leadoff
        while True:
            if not self.nskECG.isBufferEmpty():
                sample_count += 1
                D = self.nskECG.popBuffer()  #get the oldest dict

                # if we are more than 2 seconds in and leadoff is still zero
                if D['leadoff'] == 0:
                    leadoff_count += 1
                    if leadoff_count > self.nskECG.Fs * 2:
                        if self.nskECG.getTotalNumRRI() != 0:
                            #reset the library
                            self.nskECG.ecgalgResetLib()
                        self.nskECG.ecg_buffer.task_done(
                        )  #let queue know that we're done
                        continue
                else:  # leadoff==200, or lead is on
                    leadoff_count = 0

                D = self.nskECG.ecgalgAnalyzeRaw(D)

                self.cur_lead_on = D['leadoff']
                if 'hrv' in D:
                    self.cur_hrv = D['hrv']
                    self.cur_hrv_t = D['timestamp']

            # we keep looping until something tells us to stop
        pass  #

    def is_lead_on(self):
        return self.cur_lead_on

    def get_hrv(self):
        return self.cur_hrv