def __init__(self, port="COM7"): self.lead_count = 0 target_port = port # 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 # want the LEAD_TIMEOUT to hold on to values between baseline and test, but reset between users self.LEAD_TIMEOUT = 30 # reset algorithm if leadoff for more than this many seconds self.cur_lead_on = False self.cur_hrv = 0
class ecg_real(object): def __init__(self, port="COM7"): self.lead_count = 0 target_port = port # 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 # want the LEAD_TIMEOUT to hold on to values between baseline and test, but reset between users self.LEAD_TIMEOUT = 30 # reset algorithm if leadoff for more than this many seconds 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 self.cur_rri = None # R to R interval as an int representing # samples 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 D['leadoff'] == 200: self.cur_lead_on = True # lead is on else: self.cur_lead_on = False # no connection between leads # if we are more than LEAD_TIMEOUT seconds in and leadoff is still zero if D['leadoff'] == 0: leadoff_count += 1 if leadoff_count > self.nskECG.Fs * self.LEAD_TIMEOUT: if self.nskECG.getTotalNumRRI() != 0: # reset the library self.nskECG.ecgResetAlgLib() 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) if 'hrv' in D: self.cur_hrv = D['hrv'] self.cur_hrv_t = D['timestamp'] if 'rri' in D: self.cur_rri = D['rri'] # we keep looping until something tells us to stop pass # def is_lead_on(self): return self.cur_lead_on def get_hrv(self): if self.cur_hrv: return self.cur_hrv else: return -1 def get_hrv_t(self): if self.cur_hrv_t: return self.cur_hrv_t else: return -1 def get_rri(self): if self.cur_rri: return self.cur_rri else: return -1