9 print("Expected argument: string list. Strings to be put in PROGMEM, along with an index table.")
12 if len(sys.argv) <= 2:
13 print("Expected argument: destination. The path to be appended with '.h' for the destination file.")
16 stringFile = sys.argv[1]
17 destPath = sys.argv[2]
19 if destPath.endswith(".h"):
20 destPath = destPath.replace(".h", "")
21 if destPath.endswith(".cpp"):
22 destPath = destPath.replace(".cpp", "")
24 destFname = os.path.basename(destPath)
26 print("str2pgmspace: processing '{}' into '{}.[h,c]'".format(stringFile, destPath))
28 if not os.path.exists( stringFile ):
29 print("File does not exist...")
32 fileDir = os.path.dirname(stringFile) + "/"
38 with open(stringFile, "r") as flist:
48 if tokens[0] == "__IDENTIFIER":
54 if(not l.startswith("#")):
55 indexes.append(len(blob))
58 if(l[idx] == "¬"[0] and idx+2 < len(l)):
59 # char byte in hex incoming
63 blob += bytes.fromhex(hex)
66 blob += bytes([ord(l[idx])])
69 blob.append(0); # add null terminator to every string
76 idtf = "_progmem_str" if not idtf else idtf
78 outh = open(destPath+".h", "w")
79 outc = open(destPath+".c", "w")
82 outh.write("#pragma once\n\n#include <avr/pgmspace.h>\n\n")
83 outh.write("// '{}.h': generated by str2pgmspace from '{}' at {}\n".format(destPath, stringFile, datetime.datetime.now()))
84 outc.write('#include "./{}.h"\n\n'.format(destFname))
87 # add constants and interface
88 outh.write("static const int {}_COUNT = {};\n".format(idtf.upper(), len(indexes)))
89 outh.write("static const int {}_BLOB_SZ = {};\n".format(idtf.upper(), len(blob)))
90 outh.write("extern const unsigned short {}_offsets[];\n".format(idtf))
91 outh.write("extern const char {}_blob[];\n".format(idtf))
92 outh.write("\n#define {}_GET_OFFSET(I) pgm_read_word(&({}_offsets[(I)]]))\n".format(idtf.upper(), idtf))
93 outh.write("\n#define {}_GET(I) ( ((const char*) &{}_blob) + {}_GET_OFFSET(I) )\n".format(idtf.upper(), idtf, idtf.upper()))
96 # write blob to c file now
97 outc.write("const char {}_blob[] PROGMEM = \n".format(idtf))
105 for i in range(chunks):
107 end = end if end <= len(allbytes) else len(allbytes)
108 chunk = allbytes[i*16:end]
111 for j in range(len(chunk)):
112 outc.write('\\x{:02x}'.format(chunk[j]));
118 # write indices to c file now
119 outc.write("const unsigned short {}_offsets[] PROGMEM = {}\n ".format(idtf, "{"))
121 for i in range(len(indexes)):
123 outc.write(str(indexes[i]))
124 if(i < len(indexes) - 1):
133 print("str2pgmspace done!")