def connect(port_path=None): print "Opening serial port..." # This way of auto-finding PEGS serial port only works on POSIX systems if port_path is None: ports = glob.glob(os.path.join(SERIAL_DEV_ROOT, "tty.usbserial*")) if len(ports) == 1: port_path = ports[0] else: if len(ports) > 1: print "Found more than one serial unit that could be PEGS. Please choose the one to connect to and re-run this command with --port <port of choice>" for p in ports: print p else: print "Could not find PEGS. Make sure they are connected, turned on, and you're either running Mac OSX 10.9 or later, or installed the FTDI drivers (see readme.md)" return None term = serial.Serial(port_path, baudrate=PEGS_BAUDRATE) # , timeout=5) print "Handshaking" TestTerminal.testPrepare(term, WELCOME_REFERENCE) handshake = SerialExchange(term, None, WELCOME_REFERENCE) if handshake.wait(): print "Connected to PEGS on port %s" % port_path return Pegs(term) else: print "Device port %s is not PEGS (or handshake failed for some other reason)" % port_path return None
def _open_bank(self, bank): # can only do this once in a session, I think assert self.bank is None assert bank is not None assert bank >= 0 assert bank < NUM_BANKS print "Opening PEGS for flash write operation" TestTerminal.testPrepare(self.dev, "Bank Selected\x0d\x0a>") bank_select = SerialExchange(self.dev, "bank" + str(bank), "Bank Selected\x0d\x0a>") assert bank_select.execute() self.bank = bank TestTerminal.testPrepare(self.dev, "Writting Enabled\x0d\x0a>") enable_write = SerialExchange(self.dev, "enwrite", "Writting Enabled\x0d\x0a>") assert enable_write.execute() self.write_enabled = True print "Slot %d opened for write. DO NOT DISCONNECT PEGS." % (self.bank + 1)
def upload_sequence(self, bank, framerate, frames): """Framerate argument shall be frames/sec. TODO: Convert as necessary""" num_frames = len(frames) assert num_frames >= 1 assert num_frames <= MAX_FRAMES assert framerate >= FRAMERATE_MIN assert framerate <= FRAMERATE_MAX # Prepare frames pixel_stream = [] for frame in frames: assert len(frame) == DISPLAY_HEIGHT for line in frame: assert len(line) == FRAME_WIDTH line_bytes = Pegs._pack_line(line) assert len(line_bytes) == 3 pixel_stream.extend(line_bytes) assert len(pixel_stream) == ((num_frames * DISPLAY_HEIGHT * DISPLAY_EYE_WIDTH * 2) / 8) for b in pixel_stream: assert len(b) == 1 assert b is not None num_frames_byte = bitstring.Bits(uint=num_frames, length=8) framerate_byte = bitstring.Bits(uint=framerate, length=8) meta_bytes = num_frames_byte + framerate_byte # Open PEGS for write self._open_bank(bank) assert self.write_enabled == True assert self.bank is not None print "Erasing" TestTerminal.testPrepare(self.dev, "Erased 4K\x0d\x0a>") erase = SerialExchange(self.dev, "Erase4K", "Erased 4K\x0d\x0a>") assert erase.execute() TestTerminal.testPrepare(self.dev, "Begin writting\x0d\x0a") write_long = SerialExchange(self.dev, "WriteLong", "Begin writting\x0d\x0a") assert write_long.execute() print "Uploading %d frames" % num_frames # Write header self.dev.write(FRAMES_MAGIC_SENTENCE) self.dev.write(meta_bytes.bytes) # Write frame frames chunk_counter = 0 last_tx = 0 for chunk in pixel_stream: while time.clock() < last_tx + UPLOAD_THROTTLE: time.sleep(UPLOAD_THROTTLE / 5) last_tx = time.clock() self.dev.write(chunk) chunk_counter = chunk_counter + 1 if chunk_counter % ((DISPLAY_HEIGHT * DISPLAY_EYE_WIDTH * 2) / 8) == 0: stdout.write("*") stdout.flush() print print "Upload done" self._close()