Skip to content

Instantly share code, notes, and snippets.

@Mohamedemad4
Created January 8, 2020 11:20
Show Gist options
  • Save Mohamedemad4/9b3b63fb126a87a1f413ec17a0c2eea8 to your computer and use it in GitHub Desktop.
Save Mohamedemad4/9b3b63fb126a87a1f413ec17a0c2eea8 to your computer and use it in GitHub Desktop.
Stores all strings from println and print commands in PROGMEM. to minimaize the .data section.surprised GCC didn't already have this. put this script outside your code directory and run: python3 putStrInPROGMEM.py yourcodedir
#!/usr/bin/python3
#this but for arduino https://www.avrfreaks.net/forum/tut-c-gcc-and-progmem-attribute?page=all
import os
import re
import sys
import pprint
p=re.compile("println\(([^\)]+)\)|print\(([^\)]+)\)")
strings_vars=""
p2v_dict={}
prefix="s_"
fc=0
def rprogmemstr(string):
vn=prefix+str(len(strings_vars.split("\n")))
return 'char const {0}[] PROGMEM = {1};'.format(vn,string),vn
def read_all_serialPrint(file):
for line in file.split("\n"):
m=edgeCasesCheck(p.findall(line))
if m==None:
continue
addToStringsvar(m)
def addToStringsvar(s):
global strings_vars
r=rprogmemstr(s)
strings_vars+="\n"
strings_vars+=r[0]
p2v_dict.update({s:r[1]})
return
def edgeCasesCheck(p):
for i in p:
for s in i:
if s.startswith('"')==False:
pass
else:
return s
def replace_with_p2v(file):
f=open(file,'r').read()
for ps in p2v_dict:
f=f.replace(ps,p2v_dict[ps])
open(file,"w").write(strings_vars+"\n"+f)
return
'''
put this script outside your code directory
and run:
python3 putStrInPROGMEM.py yourcodedir
'''
for i in os.listdir(sys.argv[1]):
f=os.path.join(sys.argv[1],i)
print(f)
fc+=1
prefix=prefix+str(fc)
read_all_serialPrint(open(f,"r").read())
replace_with_p2v(f)
print("")
pprint.pprint(p2v_dict)
p2v_dict={}
strings_vars=""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment