Skip to content

Commit 614f517

Browse files
committed
Added ability to read input straight from stdin, without interactive repl.
1 parent 210192e commit 614f517

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

eeprom.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import os
2020
import readline
2121
import sys
22-
import typing
2322
from io import BytesIO
2423
from itertools import count
2524
from struct import pack
@@ -59,6 +58,9 @@ class AT28C256(object):
5958
To load a local file into the EEPROM:
6059
> [l|load] [filename]
6160
61+
Send a reset command:
62+
> reset
63+
6264
Address supports hex (0xFF) and octal (0o7) notation.
6365
"""
6466

@@ -108,7 +110,7 @@ def dump(self, filename: str) -> None:
108110
with open(filename, 'wb') as f:
109111
self.fdump(f)
110112

111-
def fdump(self, f: typing.IO, console=sys.stdout) -> None:
113+
def fdump(self, f: IO, console=sys.stdout) -> None:
112114
self._send(b'd') # send dump command
113115

114116
cnt = 0
@@ -127,12 +129,12 @@ def load(self, filename: str) -> None:
127129
try:
128130
with open(filename, 'rb') as f:
129131
size = min(0x8000, os.fstat(f.fileno()).st_size)
130-
print('Loading %d bytes of %s into EEPROM...' % (size, filename))
132+
print('Loading %d bytes into EEPROM...' % size)
131133
self.fload(f, size)
132134
except FileNotFoundError:
133135
print('File not found: ' + filename)
134136

135-
def fload(self, f: IO, size: int) -> None:
137+
def fload(self, f: IO, size: int, console=sys.stdout) -> None:
136138
self._send(pack('>cH', b'l', size), ack=True)
137139

138140
with open('fload.bin', 'wb') as fout:
@@ -144,9 +146,11 @@ def fload(self, f: IO, size: int) -> None:
144146
cnt += len(data)
145147
self._send(data, ack=True)
146148
fout.write(data)
147-
print('\r%d%%' % ((cnt * 100) / size), end='')
149+
if console:
150+
print('\r%d%%' % ((cnt * 100) / size), end='', file=console)
148151

149-
print('\nComplete.')
152+
if console:
153+
print('\nComplete.', file=console)
150154

151155
def reset(self) -> None:
152156
"""Sends a reset command."""
@@ -210,7 +214,7 @@ def repl(self) -> None:
210214
'/dev/tty.usbmodemXXXX)')
211215
parser.add_argument('cmd', choices=('dump', 'load'), nargs='?',
212216
help='dumps the entire contents of the EEPOM to stdout '
213-
'or loads stdin onto the EEPROM')
217+
'or loads up to 32kb of stdin onto the EEPROM')
214218
args = parser.parse_args()
215219

216220
dev = args.port
@@ -232,7 +236,13 @@ def repl(self) -> None:
232236
if args.cmd == 'dump':
233237
eeprom.fdump(sys.stdout.buffer, console=None)
234238
elif args.cmd == 'load':
235-
pass
239+
# eagerly read all of stdin to determine the ROM size
240+
with BytesIO() as f:
241+
f.write(sys.stdin.buffer.read(0x8000))
242+
size = f.tell()
243+
f.seek(0)
244+
print('Loading %d bytes into EEPROM...' % size)
245+
eeprom.fload(f, size=size)
236246
else:
237247
histfile = os.path.join(os.path.expanduser("~"), ".eeprom_history")
238248
try:

0 commit comments

Comments
 (0)