def initPyglet(fullscreen=False): '''intialize the pyglet window, keyhandler''' global window # set up the window if fullscreen: # N.B. accurate video timing only guaranteed with fullscreen config = pyglet.gl.Config(double_buffer=True) window = pyglet.window.Window(vsync=True, config=config) else: config = pyglet.gl.Config(double_buffer=True) window = pyglet.window.Window(width=1024, height=768, vsync=True, config=config) # setup a key press handler, just store key-press in global variable window.push_handlers(on_key_press, on_text) global nframe nframe = 0 # override window's flip method to record the exact *time* the # flip happended window.flip = types.MethodType(timedflip, window) window.lastfliptime = getTimeStamp() global fliplogtime fliplogtime = window.lastfliptime return window
def draw(self, t): '''Show a block of text to the user for a given duration on a blank screen''' if not self.isRunning: self.isRunning = True # mark that we're running self.t0 = getTimeStamp() if self.clearScreen: self.window.clear() self.instructLabel.draw()
def timedflip(self): '''pseudo method type which records the timestamp for window flips''' type(self).flip(self) olft = self.lastfliptime self.lastfliptime = getTimeStamp() flipstats.addpoint(self.lastfliptime - olft) global fliplogtime if self.lastfliptime > fliplogtime: fliplogtime = fliplogtime + 5000 print("\nFlipTimes:" + str(flipstats)) print("Hist:\n" + flipstats.hist())
def draw(self, t): '''check for results from decoder. show if found..''' global last_text, last_key_press if not self.isRunning: super().draw(t) return if not self.noisetag.isConnected(): if self.stage == 0: # try-connection print('Not connected yet!!') self.noisetag.connect(self.host, self.port, queryifhostnotfound=False, timeout_ms=self.drawconnect_timeout_ms) if self.noisetag.isConnected(): self.set_text(self.connected_text % (self.noisetag.gethostport())) self.t0 = getTimeStamp() self.duration = 1000 self.noisetag.subscribe("MSPQ") elif self.elapsed_ms() > self.autoconnect_timeout_ms: # waited too long, giveup and ask user self.stage = 1 # ensure old key-presses are gone last_text = None last_key_press = None elif self.stage == 1: # query hostname # query the user for host/port # accumulate user inputs if last_key_press: if last_key_press == pyglet.window.key.BACKSPACE: # remove last character self.usertext = self.usertext[:-1] last_key_press = None if last_text: print(last_text + ":" + str(ord(last_text))) if last_text == '\n' or last_text == '\r': # set as new host to try self.host = self.usertext self.usertext = '' self.set_text(self.trying_text % (self.host)) self.stage = 0 # back to try-connection stage elif last_text: # add to the host string self.usertext += last_text last_text = None if self.stage == 1: # in same stage # update display with user input self.set_text(self.query_text % (self.usertext)) super().draw(t)
def is_done(self): # check termination conditions isDone = False if not self.isRunning: return False if self.waitKey: global last_key_press if last_key_press: self.key_press = last_key_press isDone = True last_key_press = None if getTimeStamp(self.t0) > self.duration: isDone = True if isDone: self.noisetag.removeSubscription("D") self.noisetag.modeChange("idle") return isDone
def draw(self, t): '''Show a set of colored circles based on the lastSigQuality''' if not self.isRunning: self.isRunning = True # mark that we're running self.t0 = getTimeStamp() self.noisetag.addSubscription( "D") # subscribe to "DataPacket" messages self.noisetag.modeChange("ElectrodeQuality") if self.clearScreen: self.window.clear() # get the sig qualities electrodeQualities = self.noisetag.getLastSignalQuality() if not electrodeQualities: # default to 4 off qualities electrodeQualities = [.5] * len(self.sprite) if len(electrodeQualities) != len(self.sprite): self.update_nch(len(electrodeQualities)) issig2noise = True #any([s>1.5 for s in electrodeQualities]) # update the colors print("Qual:", end='') for i, qual in enumerate(electrodeQualities): self.label[i].text = "%d: %3.1f" % (i, qual) print(self.label[i].text + " ", end='') if issig2noise: qual = log10(qual) / 2 # n2s=50->1 n2s=10->.5 n2s=1->0 qual = max(0, min(1, qual)) qualcolor = (int(255 * qual), int(255 * (1 - qual)), 0 ) #red=bad, green=good self.sprite[i].color = qualcolor print("") # draw the updated batch self.batch.draw() # get the raw signals msgs = self.noisetag.getNewMessages() for m in msgs: if m.msgID == DataPacket.msgID: print('D', end='', flush=True) if getTimeStamp( ) > self.t0 + self.datawindow_ms: # slide buffer self.dataringbuffer.popleft() self.dataringbuffer.append(m.samples) # draw the lines if self.dataringbuffer: nch = len(self.linebbox) for ci in range(nch): # flatten into list samples for each channel d = [t[ci] for m in self.dataringbuffer for t in m] # map to screen coordinates bbox = self.linebbox[ci] yscale = bbox[3] / self.datascale_uv # 10 uV between lines mu = sum(d[int(len(d) * .8):]) / (len(d) * .8 ) # center last samples y = [bbox[1] + (s - mu) * yscale for s in d] x = [bbox[0] + bbox[2] * i / len(y) for i in range(len(d))] # interleave x, y to make gl happy coords = tuple(c for xy in zip(x, y) for c in xy) # draw this line pyglet.graphics.draw(len(d), pyglet.gl.GL_LINES, ('v2f', coords))
def elapsed_ms(self): return getTimeStamp(self.t0)