import csv

print("Exporter")

file = open("../PM8000_Modbus_Map.csv", "r")
data = list(csv.reader(file, delimiter=","))
file.close()

header = """
#include <stdint.h>
struct PM8000RegType
{
    uint16_t address;
    uint16_t size;
    char type[20];
    char name[70];

};

//The reason for the if statement is to use the code storage instead of declaring this in a array.
PM8000RegType nextPM8000Reg(uint16_t currentRegister)
{
    PM8000RegType val = (PM8000RegType){0,0,"",""};
  
[replace]

    return val;
}

"""



lines = ""
for d in range(1, len(data)):
    if not data[d][1].isnumeric():
        continue
    #print("{} @ {}:{} ({}) Tag: {}".format(data[d][0], data[d][1] ,data[d][2] ,data[d][3],data[d][7]))
    lines += "    if (currentRegister < "+data[d][1]+") {\n"
    lines += "        val = (PM8000RegType){ " + data[d][1] + ", " + data[d][2] + ",  \"" + data[d][3] + "\",  \"" +  data[d][0].encode('ascii',"ignore").decode('ascii') + "\" };\n"
    lines += "        return val;\n"
    lines += "    }\n"
#

print(header.replace("[replace]", lines))

file = open("PM8000_Modbus_Map.h", "w")
file.write(header.replace("[replace]", lines))
file.close()