aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Stendahl <jakob.stendahl@outlook.com>2018-03-20 00:13:35 +0100
committerJakob Stendahl <jakob.stendahl@outlook.com>2018-03-20 00:13:35 +0100
commit2aaafdd72dde41faf7f906eeeb69307c2f2d80d3 (patch)
tree3e8d8bc987ea98d9113a0c0e22c15d27bc2dac88
downloadmemConfigger-2aaafdd72dde41faf7f906eeeb69307c2f2d80d3.tar.gz
memConfigger-2aaafdd72dde41faf7f906eeeb69307c2f2d80d3.zip
Initial commit
-rw-r--r--.gitignore169
-rw-r--r--README.md44
-rw-r--r--memConfigger.py124
3 files changed, 337 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..40e2f9b
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,169 @@
+*.out.*
+
+# Created by https://www.gitignore.io/api/linux,macos,python,windows
+
+### Linux ###
+*~
+
+# temporary files which can be created if a process still has a handle open of a deleted file
+.fuse_hidden*
+
+# KDE directory preferences
+.directory
+
+# Linux trash folder which might appear on any partition or disk
+.Trash-*
+
+# .nfs files are created when an open file is removed but is still being accessed
+.nfs*
+
+### macOS ###
+*.DS_Store
+.AppleDouble
+.LSOverride
+
+# Icon must end with two \r
+Icon
+
+# Thumbnails
+._*
+
+# Files that might appear in the root of a volume
+.DocumentRevisions-V100
+.fseventsd
+.Spotlight-V100
+.TemporaryItems
+.Trashes
+.VolumeIcon.icns
+.com.apple.timemachine.donotpresent
+
+# Directories potentially created on remote AFP share
+.AppleDB
+.AppleDesktop
+Network Trash Folder
+Temporary Items
+.apdisk
+
+### Python ###
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+
+# PyInstaller
+# Usually these files are written by a python script from a template
+# before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.coverage
+.coverage.*
+.cache
+.pytest_cache/
+nosetests.xml
+coverage.xml
+*.cover
+.hypothesis/
+
+# Translations
+*.mo
+*.pot
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# pyenv
+.python-version
+
+# celery beat schedule file
+celerybeat-schedule.*
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.env
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+
+### Windows ###
+# Windows thumbnail cache files
+Thumbs.db
+ehthumbs.db
+ehthumbs_vista.db
+
+# Folder config file
+Desktop.ini
+
+# Recycle Bin used on file shares
+$RECYCLE.BIN/
+
+# Windows Installer files
+*.cab
+*.msi
+*.msm
+*.msp
+
+# Windows shortcuts
+*.lnk
+
+
+# End of https://www.gitignore.io/api/linux,macos,python,windows
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..25e68b2
--- /dev/null
+++ b/README.md
@@ -0,0 +1,44 @@
+= memConfigger
+This is a simple python-script that takes a simple file with variable-names and types, and turns it into a cpp-lookup-function. I wrote it to simplify allocation of space on EEPROMS, meaning i don't have to do any math. I just have to copy-paste the function. This is probably not the best way to to it, but it works just fine.
+
+I Might add the CPP-class that i put the resulting function into when i get time to turn it into a skeleton, and make som docs on it.
+
+== Use
+You need python installed, i believe 2.7 is what i have on my computer. Altough i don't see why it wouldn't work with Python 3 as well. I think the code is compiant. Start it with
+´´´
+python memConfigger.py <input-file>
+´´´
+If the input-file exists and have the proper data, it will produce two files. Both with the same name (Except last .\*), ending with ´.out.cpp´ and ´.out.adoc´.
+
+´.cpp´ is the generated function, and ´.adoc´ is a more human-friendly lookup-table.
+
+== Input-file
+This should be a flat-file consisting of commands, one for each line. Lines that are blank, or are starting with a ´#´ is ignored.
+
+If the line starts with a ´!´, the following should be one of the following:
+- ´SIZE <INT>´ This sets the max size of the EEPROM, it doesn't actually affect the execution. If you set it in the top of you file however, it will keep track of the available space on your PROM. ´Defaults to 0´
+- ´INC <INT>´ This will push the integer forward. Making some space on you PROM, this could be usefull if you plan to reserve some space at a spesific location for later updates, that shouldn't affect the current user-data.
+
+If the line does not start with an exlamation mark, we are assuming the first word is an type. At the moment there are only three possible types. The list below starts with how a line should look, for it to be interpereted correctly:
+- ´STR_ASCII <length> <variable_name>´ This is just that, each byte will be an ascii character.
+- ´LONG <variable_name>´ This is two bytes, usually you would sum them to get their value.
+- ´INT <variable_name>´ This is just one byte.
+
+== Example-file
+´´´
+# We are setting the size, so the parser can notify us if and when we exceed it
+!SIZE 124
+
+# Just setting some vars
+STR_ASCII 10 f_name
+STR_ASCII 10 l_name
+INT age
+
+# Incrementing our index by 50
+!INC 50
+
+# Setting som more vars
+LONG XP
+STR_ASCII 20 some_more_info
+INT Another_int
+´´´
diff --git a/memConfigger.py b/memConfigger.py
new file mode 100644
index 0000000..57ae627
--- /dev/null
+++ b/memConfigger.py
@@ -0,0 +1,124 @@
+import sys
+
+if (len(sys.argv) != 2):
+ print ("Please provide an input-file.")
+ print ("\"python memConfigger.py <input-file>\"")
+ print(sys.argv)
+ sys.exit();
+
+inputFile = open(sys.argv[1], "r")
+
+memSize = 0
+cIndex = 0
+memStruct = []
+for line in inputFile.readlines():
+ line = line.lstrip(' ').replace('\n', '')
+
+ if (line == ''): continue # Skip blank lines
+ if (line[0] == '#'): continue # Skip comments
+
+ # If the line is an parser instruction
+ if (line[0] == "!"):
+ command = line.replace('!', '').split(' ')
+
+ if (command[0] == "SIZE"):
+ memSize = int(float(command[1]))
+ elif (command[0] == "INC"):
+ cIndex = cIndex + int(float(command[1]))
+ else:
+ print ("Command \"" + command[0] + "\" is not valid. Exiting...")
+ sys.exit()
+
+ # If the line is not an parser instruction
+ else:
+ expression = line.split(' ')
+ elength = 0
+ ename = ""
+ etype = ""
+
+ # Determine how many bytes are needed for the selected type
+ if (expression[0] == "STR_ASCII"):
+ elength = int(float(expression[1])) - 1
+ ename = expression[2]
+ etype = expression[0]
+ elif (expression[0] == "LONG"):
+ elength = 1
+ ename = expression[1]
+ etype = expression[0]
+ elif (expression[0] == "INT"):
+ length = 0
+ ename = expression[1]
+ etype = expression[0]
+ else:
+ print ("ERR: type \"" + etype + "\" is not valid. Exiting...")
+ sys.exit()
+
+ # Find start and stop of variable
+ start = cIndex
+ stop = cIndex + elength
+ cIndex = stop + 1
+ if (stop > memSize):
+ print ("Warning: You have now used " + str(int(stop / (memSize / 100.0))) + "% of the available space, by adding \"" + ename + "\".")
+
+ # Add var to array
+ memStruct.append([
+ etype,
+ ename,
+ elength,
+ start,
+ stop
+ ])
+
+# Print to human-readable file
+LLongestName = 0
+LLongestType = 9
+LLongestStartN = len(str(memStruct[-1][2]))
+LPadding = 4
+for variable in memStruct:
+ if (len(variable[1]) > LLongestName):
+ LLongestName = len(variable[1])
+
+with open(sys.argv[1].split('.')[0] + ".out.adoc", "w") as hOut:
+
+ hOut.write(sys.argv[1].split('.')[0] + '\n')
+ hOut.write(('-' * len(sys.argv[1].split('.')[0]) + "\n\n"))
+
+ for variable in memStruct:
+ # Name + padding
+ outStr = variable[1]
+ outStr = outStr + (' ' * (LLongestName - len(variable[1]) + LPadding))
+ # Type + padding
+ outStr = outStr + variable[0]
+ outStr = outStr + (' ' * (LLongestType - len(variable[0]) + LPadding))
+ # StartPos + padding
+ outStr = outStr + str(variable[3])
+ outStr = outStr + (' ' * (LLongestStartN - len(str(variable[3])) + LPadding))
+ # EndPos
+ outStr = outStr + str(variable[4])
+
+ hOut.write(outStr + '\n')
+
+ hOut.write("\nTYPES\n-----\n")
+ hOut.write("*STR_ASCII* Hver byte er et ascii-tegn\n")
+ hOut.write("*INT* en byte.\n")
+ hOut.write("*LONG* to bytes, altsaa det dobbelte av en int. tallene legges sammen.\n")
+ print ("Human-readable file was made : \"" + sys.argv[1].split('.')[0] + ".out.adoc\".")
+
+# Print C++ code, this block WILL remove the first item of the list, so beware!!!
+with open(sys.argv[1].split('.')[0] + ".out.cpp", "w") as cppOut:
+ cppOut.write("#region GENERATED_CODE\n")
+ cppOut.write("struct memoryAdress memoryAccess::getAddress(String varName) {\n")
+ cppOut.write("\tmemoryAdress tmp;\n")
+
+ specialCase = memStruct.pop(0)
+ cppOut.write("\tif ((String \"" + specialCase[1] + "\" == varName)")
+ cppOut.write("{tmp={" + str(variable[3]) + "," + str(variable[4]) + "};}\n")
+
+ for variable in memStruct:
+ cppOut.write("\telse if ((String) \"" + variable[1] + "\" == varName)")
+ cppOut.write("{tmp={" + str(variable[3]) + "," + str(variable[4]) + "};}\n")
+
+ cppOut.write("\treturn tmp;\n")
+ cppOut.write("}\n")
+ cppOut.write("#endregion")
+ print ("CPP-function was made : \"" + sys.argv[1].split('.')[0] + ".out.cpp\".")