Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit118bdab

Browse files
committed
make imports less ugly
If code outside our main entry point, e.g. our examples, wantedto use the src_to_binary function, it needed an ugly import ofesp32_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 andresulted in a ULP binary file as output, is now also more cleanlyavailable as assemble_file() inside the esp32_ulp module.Fixes#39.
1 parent10a0374 commit118bdab

File tree

3 files changed

+35
-30
lines changed

3 files changed

+35
-30
lines changed

‎esp32_ulp/__init__.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from .utilimportgarbage_collect
2+
3+
from .preprocessimportpreprocess
4+
from .assembleimportAssembler
5+
from .linkimportmake_binary
6+
garbage_collect('after import')
7+
8+
9+
defsrc_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+
foraddr,syminaddrs_syms:
16+
print('%04d %s'% (addr,sym))
17+
18+
text,data,bss_len=assembler.fetch()
19+
returnmake_binary(text,data,bss_len)
20+
21+
22+
defassemble_file(filename):
23+
withopen(filename)asf:
24+
src=f.read()
25+
26+
binary=src_to_binary(src)
27+
28+
iffilename.endswith('.s')orfilename.endswith('.S'):
29+
filename=filename[:-2]
30+
withopen(filename+'.ulp','wb')asf:
31+
f.write(binary)
32+

‎esp32_ulp/__main__.py‎

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,9 @@
11
importsys
2-
3-
from .utilimportgarbage_collect
4-
5-
from .preprocessimportpreprocess
6-
from .assembleimportAssembler
7-
from .linkimportmake_binary
8-
garbage_collect('after import')
9-
10-
11-
defsrc_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-
foraddr,syminaddrs_syms:
18-
print('%04d %s'% (addr,sym))
19-
20-
text,data,bss_len=assembler.fetch()
21-
returnmake_binary(text,data,bss_len)
2+
from .importassemble_file
223

234

245
defmain(fn):
25-
withopen(fn)asf:
26-
src=f.read()
27-
28-
binary=src_to_binary(src)
29-
30-
iffn.endswith('.s')orfn.endswith('.S'):
31-
fn=fn[:-2]
32-
withopen(fn+'.ulp','wb')asf:
33-
f.write(binary)
6+
assemble_file(fn)
347

358

369
if__name__=='__main__':

‎examples/counter.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
fromesp32importULP
1313
frommachineimportmem32
1414

15-
fromesp32_ulp.__main__importsrc_to_binary
15+
fromesp32_ulpimportsrc_to_binary
1616

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp