Skip to content

Commit

Permalink
Merge f7ef211 into 0729219
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Oct 2, 2020
2 parents 0729219 + f7ef211 commit 2015cb7
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/ttblit/tool/flasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __init__(self, subparser):
self.op_list = operations.add_parser('list', help='List games/files on your 32Blit')
self.op_debug = operations.add_parser('debug', help='Enter serial debug mode')
self.op_reset = operations.add_parser('reset', help='Reset your 32Blit')
self.op_info = operations.add_parser('info', help='Get 32Blit run status')

def find_comport(self):
ret = []
Expand Down Expand Up @@ -68,22 +69,35 @@ def _decorated(self, args):
ports = self.find_comport()

for port in ports:
sp = serial.Serial(port)
sp = serial.Serial(port, timeout=5)
fn(self, sp, args)
sp.close()
return _decorated

def _send_file(self, serial, file, dest, directory=None):
def _get_status(self, serial):
serial.write(b'32BLINFO\x00')
response = serial.read(8)
if response == b'':
raise RuntimeError('Timeout waiting for 32Blit status.')
return 'game' if response == b'32BL_EXT' else 'firmware'

def _reset(self, serial):
serial.write(b'32BL_RST\x00')

def _reset_to_firmware(self, serial):
if self._get_status(serial) == 'game':
self._reset(serial)

def _send_file(self, serial, file, dest, directory='/'):
sent_byte_count = 0
chunk_size = 64
file_name = file.name
file_size = file.stat().st_size

if dest == 'sd':
if directory is None:
directory = '/'
else:
if not directory.endswith('/'):
directory = f'{directory}/'

logging.info(f'Saving {file} ({file_size} bytes) as {file_name} in {directory}')
command = f'32BLSAVE{directory}{file_name}\x00{file_size}\x00'
elif dest == 'flash':
Expand All @@ -104,10 +118,12 @@ def _send_file(self, serial, file, dest, directory=None):

@serial_command
def run_save(self, serial, args):
self._send_file(serial, args.get('file'), 'sd', directory=args.get('directory', 'games'))
self._reset_to_firmware(serial)
self._send_file(serial, args.get('file'), 'sd', directory=args.get('directory'))

@serial_command
def run_flash(self, serial, args):
self._reset_to_firmware(serial)
self._send_file(serial, args.get('file'), 'flash')

@serial_command
Expand All @@ -125,4 +141,10 @@ def run_debug(self, serial, args):
@serial_command
def run_reset(self, serial, args):
logging.info('Resetting your 32Blit...')
serial.write(b'32BL_RST\x00')
self._reset(serial)

@serial_command
def run_info(self, serial, args):
logging.info('Getting 32Blit run status...')
status = self._get_status(serial)
print(f'Running: {status}')

0 comments on commit 2015cb7

Please sign in to comment.