From 32589f3013ab3f0761539e192e7f1e72f27f2565 Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Mon, 29 Dec 2025 14:17:51 -0500 Subject: [PATCH] Initial commit of older project --- array_generator.py | 134 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100755 array_generator.py diff --git a/array_generator.py b/array_generator.py new file mode 100755 index 0000000..2d5dd3d --- /dev/null +++ b/array_generator.py @@ -0,0 +1,134 @@ +#!/usr/bin/env python3 + +import csv +import math +import numpy + + +def digit(c): + s = ord(c) + if s >= ord("0") and s <= ord("7"): + return s - ord("0") + else: + return -1 + + +def lutGenerate(): + lut = [] + while True: + bitOrder = int(input("Please enter the bit pattern (e.g. 46017235): ")) + if len(str(bitOrder)) != 8: + print("Wrong number of digits: only 8-bit supported at the moment.") + else: + break + pos = [] + for i in range(8): + b = digit(str(bitOrder)[i : i + 1]) + pos.insert(b, 7 - i) + + for i in range(256): + y = 0 + for j in range(8): + y |= ((i >> pos[j]) & 1) << j + lut.insert(i, y) + return lut + + +def bcdEncode(): + array = [] + numPoints = int(input("Please enter the number of numbers: ")) + for i in range(0, numPoints): + array.append("0x" + str(i)) + return array + + +def consecutive(): + array = [] + startPoint = int(input("Please enter the starting value: ")) + endPoint = int(input("Please enter the ending value: ")) + skip = int(input("Please enter the skip value: ")) + for i in range(startPoint, endPoint, skip): + array.append(i) + return array + + +def formula(): + array = [] + startPoint = int(input("Please enter starting value: ")) + endPoint = int(input("Please enter the ending value: ")) + formulaString = input("Please enter f(x): ") + formulaString = formulaString.replace("x", "{0}") + for i in range(startPoint, endPoint): + array.append(eval(formulaString.format(i))) + return array + + +def csvImport(filename): + array = [] + with open(filename, newline="") as csvfile: + linereader = csv.reader(csvfile) + for row in linereader: + array.append(row) + + +def main(): + print("C/C++ Array Generator by MrAureliusR") + print("Licensed under MPL v2") + completed = 0 + while completed == 0: + print("What kind of array would you like to generate?") + print("1) Consecutive") + print("2) Formula") + print("3) BCD Encode") + print("4) Import CSV") + print("5) LUT for rearranged byte") + choice = input("Please pick a value from the list above: ") + if choice == "1": + array = consecutive() + completed = 1 + elif choice == "2": + array = formula() + completed = 1 + elif choice == "3": + array = bcdEncode() + completed = 1 + elif choice == "4": + csvfilename = input("Please enter the filename: ") + array = csvImport(csvfilename) + completed = 1 + elif choice == "5": + array = lutGenerate() + completed = 1 + else: + print("Invalid selection!") + + arrayName = input("Please pick a variable name for this array: ") + arrayType = input("Please enter the array type, eg, uint8_t, float, static char*: ") + try: + arrayLength = len(array) + except UnboundLocalError: + print( + "There was a problem examining the array. Please try again or report this bug!" + ) + sys.exit(255) + + # Begin printing the generated array, starting with type name[len] { + print(arrayType + " " + arrayName + "[" + str(arrayLength) + "] { ") + arrayIter = iter(array) + j = 0 # this variable keeps track of the number of items printed per line + print(" ", end="") # indent the first line + + # iterate through all items in the array Pythonically + for value in arrayIter: + print(str(value) + ", ", end="") + j = j + 1 + if j % 10 == 0: # more than 10 items? new line and indent + print("\n ", end="") + print("\x1b[2D", end="") # erase the extraneous , at the end of the array + print(" };") + + return + + +if __name__ == "__main__": + main()