Skip to content

Commit 118bdab

Browse files
committed
make imports less ugly
If code outside our main entry point, e.g. our examples, wanted to use the src_to_binary function, it needed an ugly import of esp32_ulp.__main__. With this change the import becomes cleaner, e.g. `from esp32_ulp import src_to_binary`. The original "main" method, which took a filename as input and resulted in a ULP binary file as output, is now also more cleanly available as assemble_file() inside the esp32_ulp module. Fixes #39.
1 parent 10a0374 commit 118bdab

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

esp32_ulp/__init__.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from .util import garbage_collect
2+
3+
from .preprocess import preprocess
4+
from .assemble import Assembler
5+
from .link import make_binary
6+
garbage_collect('after import')
7+
8+
9+
def src_to_binary(src):
10+
assembler = Assembler()
11+
src = preprocess(src)
12+
assembler.assemble(src, remove_comments=False) # comments already removed by preprocessor
13+
garbage_collect('before symbols export')
14+
addrs_syms = assembler.symbols.export()
15+
for addr, sym in addrs_syms:
16+
print('%04d %s' % (addr, sym))
17+
18+
text, data, bss_len = assembler.fetch()
19+
return make_binary(text, data, bss_len)
20+
21+
22+
def assemble_file(filename):
23+
with open(filename) as f:
24+
src = f.read()
25+
26+
binary = src_to_binary(src)
27+
28+
if filename.endswith('.s') or filename.endswith('.S'):
29+
filename = filename[:-2]
30+
with open(filename + '.ulp', 'wb') as f:
31+
f.write(binary)
32+

esp32_ulp/__main__.py

+2-29
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,9 @@
11
import sys
2-
3-
from .util import garbage_collect
4-
5-
from .preprocess import preprocess
6-
from .assemble import Assembler
7-
from .link import make_binary
8-
garbage_collect('after import')
9-
10-
11-
def src_to_binary(src):
12-
assembler = Assembler()
13-
src = preprocess(src)
14-
assembler.assemble(src, remove_comments=False) # comments already removed by preprocessor
15-
garbage_collect('before symbols export')
16-
addrs_syms = assembler.symbols.export()
17-
for addr, sym in addrs_syms:
18-
print('%04d %s' % (addr, sym))
19-
20-
text, data, bss_len = assembler.fetch()
21-
return make_binary(text, data, bss_len)
2+
from . import assemble_file
223

234

245
def main(fn):
25-
with open(fn) as f:
26-
src = f.read()
27-
28-
binary = src_to_binary(src)
29-
30-
if fn.endswith('.s') or fn.endswith('.S'):
31-
fn = fn[:-2]
32-
with open(fn + '.ulp', 'wb') as f:
33-
f.write(binary)
6+
assemble_file(fn)
347

358

369
if __name__ == '__main__':

examples/counter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from esp32 import ULP
1313
from machine import mem32
1414

15-
from esp32_ulp.__main__ import src_to_binary
15+
from esp32_ulp import src_to_binary
1616

1717
source = """\
1818
data: .long 0

0 commit comments

Comments
 (0)