#============================================================#
#=  DESCRIPTION:                                            =#
#=   This program generates an EAGLE CAD file for           =#
#=  projects which can be used as a basis for developing a  =#
#=  PCB for projects that make use of CHARLIEPLEXING.       =#
#=  made for Python 3.7.0                                   =#
#=                                                          =#
#=  OPTIONS:                                                =#
#=  numberOfPins ------ [INTEGER] number of pins used for   =#
#=                       charlieplexing                     =#
#=  package ----------- [INTEGER] index of package to use   =#
#=                       form "packages" list               =#
#=  fileName ---------- [STRING] desired filename for       =#
#=                       output file                        =#
#=                                                          =#
#=  LICENSE:                                                =#
#=   This file was made by Luka Pogačnik and published      =#
#=  under CC BY-SA license (attribution, share alike)       =#
#=                                                          =#
#============================================================#
import sys

numberOfPins = 4    #max is 16; after that my naming scheme
                    # falls apart (names start to overlap)
                    # replaxe all %x with %02x if you need
                    # more pins (hint: find and replace all function)
                    
package = 1         #this will generate schematic for 5mm LEDs
                    #packages are listed below
packages = ["3MM","5MM","CHIPLED_0603","CHIPLED_0805","CHIPLED_1206"]

fileName = "schematic.sch"
                    #you can change the name if you want but if you keep
                    # the suffix ".sch" you will be able to open the file
                    # in Eagle CAD directly

parts = ""
instances = ""
nets = ""
prefix = ""
try:
    with open("prefix.txt","r") as f:
        prefix = f.read()
except:
    print("ERROR: Can't find [prefix.txt]. Please make sure you put it in the same folder as this file")
    sys.exit()

with open(fileName,'w') as f:
    parts += "<parts>\n"
    instances += "<instances>\n"  
    for x in range(numberOfPins):
        for y in range(numberOfPins):
            if (x != y):
                name = "%s%x%x"%("L",y,x)
                parts += '<part name="%s" library="led" deviceset="LED" device="%s"/>\n'%(name,packages[package])
                instances += '<instance part="%s" gate="G$1" x="%0.2f" y="%0.2f"/>\n'%(name,(x*2.54)*4,-1*(y*2.54)*4)

    nets = "<nets>\n"
    for i in range(numberOfPins):
        net=""
        net += '<net name="N$%x" class="0">\n<segment>\n'%(i)

        lx= -(numberOfPins+4)*2.54
        ly= i*-1*4*2.54 + 2.54
        for j in range(numberOfPins):
            x = (j)*4*2.54
            y = ly
            if (i!=j):
                net += '<wire x1="%0.2f" y1="%0.2f" x2="%0.2f" y2="%0.2f" width="0.1524" layer="91"/>\n'%(x,ly,lx,ly)
                net += ' <junction x="%0.2f" y="%0.2f"/>\n'%(x,y)
                net += '  <pinref part="L%x%x" gate="G$1" pin="A"/>\n'%(i,j)
                lx = x
  
        lx = -2*2.54 + i*4*2.54
        ly = (i+1)*2.54
        if i == 0:
            net += ' <junction x="%0.2f" y="%0.2f"/>\n'%(-2*2.54,2.54)
        else:
            net += '<wire x1="%0.2f" y1="%0.2f" x2="%0.2f" y2="%0.2f" width="0.1524" layer="91"/>\n'%(lx,ly,(-i-2)*2.54,ly)
            net += '<wire x1="%0.2f" y1="%0.2f" x2="%0.2f" y2="%0.2f" width="0.1524" layer="91"/>\n'%((-i-2)*2.54,ly,(-i-2)*2.54,(-i*4+1)*2.54)
            net += ' <junction x="%0.2f" y="%0.2f"/>\n'%((-i-2)*2.54,(-i*4+1)*2.54)
                
        for k in range(numberOfPins):
            x = lx+2*2.54
            y = -(k*4 +2)*2.54                
            if (i!=k):
                net += '<wire x1="%0.2f" y1="%0.2f" x2="%0.2f" y2="%0.2f" width="0.1524" layer="91"/>\n'%(lx,y,lx,ly)
                net += '<wire x1="%0.2f" y1="%0.2f" x2="%0.2f" y2="%0.2f" width="0.1524" layer="91"/>\n'%(lx,y,x,y)
                net += ' <junction x="%0.2f" y="%0.2f"/>\n'%(lx,y)
                net += '  <pinref part="L%x%x" gate="G$1" pin="C"/>\n'%(k,i)
                ly = y
        net += '</segment>\n</net>\n'
        
        nets += net
    nets += "</nets>\n"       
    parts += "</parts>\n"
    instances += "</instances>\n"

    f.write(prefix)
    f.write(parts)
    f.write("<sheets>\n<sheet>\n<plain>\n</plain>\n")
    f.write(instances)
    f.write("<busses>\n</busses>\n")
    f.write(nets)
    f.write("</sheet>\n</sheets>\n</schematic>\n</drawing>\n</eagle>")
    
print("done")
print("generated %i leds"%(numberOfPins*(numberOfPins-1)))
if numberOfPins > 16:
    print("WARNING: if you didn't fix the code, LED names are doubled.\n\tFind and replace all instances of '%x' and replace them with '%02x'!")
