]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/signum/3_0/elitec/keymaps/default/generate_km.py
[Keyboard] Layout and configuration updates for Signum 3.0 (#5652)
[qmk_firmware.git] / keyboards / signum / 3_0 / elitec / keymaps / default / generate_km.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 from sortedcontainers import SortedDict
5 import layout
6 import re
7
8 # TODO: auto-generate mutex layers
9 # TODO: auto-generate update_tri_layer (_state)
10
11
12 def gen_uc_iter():
13     length = len(layout.uc_dict)
14     for key, value in layout.uc_dict.items():
15         length -= 1
16         if length:
17             yield (key, value, False)
18         else:
19             yield (key, value, True)
20
21
22 def translate(s):
23     if re.match("^[0-9]$", s):
24         return ("KC_{0}".format(s), "   {0}   ".format(s))
25     elif re.match("^[a-z]$", s):
26         return ("KC_{0}".format(s.upper()), "   {0}   ".format(s))
27     elif re.match("^[A-Z]$", s):
28         return ("S(KC_{0})".format(s), "   {0}   ".format(s))
29     elif re.match("^F[0-9]{1,2}$", s):  # Fn, works from F0 to F99
30         return ("KC_{0}".format(s), "{0:^7}".format(s))
31     elif re.match("^DF[0-9]{1,2}$", s):  # DFn, works from DF0 to DF99
32         return ("DF({0})".format(s[2:]), "{0:^7}".format(s))
33     elif re.match("^MO[0-9]{1,2}$", s):  # MOn, works from MO0 to MO99
34         return ("MO({0})".format(s[2:]), "{0:^7}".format(s))
35     elif re.match("^OSL[0-9]{1,2}$", s):  # OSLn, works from OSL0 to OSL99
36         return ("OSL({0})".format(s[3:]), "{0:^7}".format(s))
37     elif re.match("^TG[0-9]{1,2}$", s):  # TGn, works from TG0 to TG99
38         return ("TG({0})".format(s[2:]), "{0:^7}".format(s))
39     elif re.match("^TO[0-9]{1,2}$", s):  # Tn, works from TO0 to TO99
40         return ("TO({0})".format(s[2:]), "{0:^7}".format(s))
41     elif re.match("^TT[0-9]{1,2}$", s):  # Tn, works from TT0 to TT99
42         return ("TT({0})".format(s[2:]), "{0:^7}".format(s))
43     elif s in layout.uc_dict:
44         return ("X("+s+")", "   {0}   ".format(chr(int(layout.uc_dict[s], 0))))
45     elif s in layout.qmk_dict:
46         return (layout.qmk_dict[s], "{0:^7}".format(s))
47     elif s == s.upper() and s.startswith("KC_"):
48         return (s, "{0:^7}".format(s[2:]))
49     else:
50         return ("XXXXXXX", "  {0}   ".format(chr(128165)))
51
52
53 def toKC(s):
54     (kc, lgd) = translate(s)
55     return kc
56
57
58 def toLgd(s):
59     (kc, lgd) = translate(s)
60     return lgd
61
62
63 if __name__ == "__main__":
64
65     template = open("km_template.txt", mode="r")
66     output = open("keymap.c", mode="w", encoding='utf-8')
67
68     doCopy = False
69
70     for line in template:
71         doCopy = True
72         if line.startswith("//<enum/>"):
73             doCopy = False
74 #            output.write(str(layout.uc_dict))
75             for k, v, isLast in gen_uc_iter():
76                 if isLast:
77                     output.write(k + "\n")
78                 else:
79                     output.write(k + ",\n")
80         if line.startswith("//<uc_map/>"):
81             doCopy = False
82             for k, v, isLast in gen_uc_iter():
83                 if isLast:
84                     output.write(u"\t[{0}] = {1}  // {2}\n".format(k, v, chr(int(v, 0))))
85                 else:
86                     output.write(u"\t[{0}] = {1},  // {2}\n".format(k, v, chr(int(v, 0))))
87         if line.startswith("//<keymaps/>"):
88             doCopy = False
89             counter = len(layout.layers)
90             layer = 0
91             S = layout.static
92             for L in layout.layers:
93                 counter -= 1
94                 r_counter = 4
95                 output.write("/* Layer %d\n" % layer)
96                 output.write(" * -------------------------------------------------               -------------------------------------------------\n")
97                 output.write(" * |{0}|{1}|{2}|{3}|{4}|{5}|               |{6}|{7}|{8}|{9}|{10}|{11}|\n".format(toLgd(L[0][0]), toLgd(L[0][1]), toLgd(L[0][2]), toLgd(L[0][3]), toLgd(L[0][4]), toLgd(L[0][5]), toLgd(L[0][6]), toLgd(L[0][7]), toLgd(L[0][8]), toLgd(L[0][9]), toLgd(L[0][10]), toLgd(L[0][11])))
98                 output.write(" * -------------------------------------------------               -------------------------------------------------\n")
99                 output.write(" * |{0}|{1}|{2}|{3}|{4}|{5}|               |{6}|{7}|{8}|{9}|{10}|{11}|\n".format(toLgd(L[1][0]), toLgd(L[1][1]), toLgd(L[1][2]), toLgd(L[1][3]), toLgd(L[1][4]), toLgd(L[1][5]), toLgd(L[1][6]), toLgd(L[1][7]), toLgd(L[1][8]), toLgd(L[1][9]), toLgd(L[1][10]), toLgd(L[1][11])))
100                 output.write(" * -------------------------------------------------               -------------------------------------------------\n")
101                 output.write(" * |{0}|{1}|{2}|{3}|{4}|{5}|               |{6}|{7}|{8}|{9}|{10}|{11}|\n".format(toLgd(L[2][0]), toLgd(L[2][1]), toLgd(L[2][2]), toLgd(L[2][3]), toLgd(L[2][4]), toLgd(L[2][5]), toLgd(L[2][6]), toLgd(L[2][7]), toLgd(L[2][8]), toLgd(L[2][9]), toLgd(L[2][10]), toLgd(L[2][11])))
102                 output.write(" * -----------------------------------------------------------------------------------------------------------------\n")
103                 output.write(" *  {0} {1} {2}        |{3}|{4}|{5}|{6}|{7}|{8}|        {9} {10} {11}".format(toLgd(L[3][0]), toLgd(L[3][1]), toLgd(L[3][2]), toLgd(L[3][3]), toLgd(L[3][4]), toLgd(L[3][5]), toLgd(L[3][6]), toLgd(L[3][7]), toLgd(L[3][8]), toLgd(L[3][9]), toLgd(L[3][10]), toLgd(L[3][11])).rstrip()+"\n")
104                 output.write(" *                                 -------------------------------------------------\n")
105                 output.write(" */\n")
106
107                 l_code = '\tLAYOUT_ortho_4x12(\n'
108                 for r in range(r_counter):
109                     r_counter -= 1
110                     c_counter = 12
111                     l_code += '\t\t'
112                     for c in range(c_counter):
113                         c_counter -= 1
114                         if c != 0:
115                             l_code += " "
116                         l_code += "%s" % toKC(L[r][c])
117                         if r_counter or c_counter:
118                             l_code += ","
119                     l_code += '\n'
120                 if counter:
121                     l_code += "\t),\n\n"
122                 else:
123                     l_code += "\t)\n"
124                 output.write(l_code)
125                 layer += 1
126         if doCopy:
127             output.write(line)
128
129     template.close()
130     output.close()