Open2

QMK firmware RGB Matrix の led_config_t g_led_config を info.json 用に変換する Python スクリプト

3araht3araht

QMK firmware の RGB Matrix の設定を info.json に引越ししないといけなくなって、いよいよ重い腰を上げて変更しようとおもって giabalanaipico 用の設定は手作業で変更しました。
しかし、とても規則性の高い編集なのに手作業なのは勿体無い。。。

そこで、Python スクリプトで info.json 用に変換するコードを書きました。

みなさんの工数削減になれば嬉しいです♪

convLedMatrix.py
# Copyright 2023 3araht
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

NO_LED = 999

###########################################################
# settings start.
###########################################################

# Replace the below with your own data, and replace "{" and "}" with "[" and "]" respectively.
ledmatrix = [
    [ NO_LED, NO_LED, 11,  10,  9,   8,   7  ],
    [ NO_LED, NO_LED, 12,  13,  14,  15,  16 ],
    [ NO_LED, NO_LED, 35,  34,  33,  32,  31 ],
    [ NO_LED, NO_LED, 36,  37,  38,  39,  40 ],
    [ NO_LED, NO_LED, 59,  58,  57,  56,  55 ],

    [ 0,      1,      2,   3,   4,   5,   6  ],
    [ 23,     22,     21,  20,  19,  18,  17 ],
    [ 24,     25,     26,  27,  28,  29,  30 ],
    [ 47,     46,     45,  44,  43,  42,  41 ],
    [ 48,     49,     50,  51,  52,  53,  54 ],


    [ 60,     61,     62,  63,  64,  65,  66  ],
    [ 84,     83,     82,  81,  80,  79,  78  ],
    [ 85,     86,     87,  88,  89,  90,  91  ],
    [ 110,    109,    108, 107, 106, 105, 104 ],
    [ NO_LED, 111,    112, 113, 114, 115, 116 ],

    [ NO_LED, NO_LED, 71,  70,  69,  68,  67  ],
    [ NO_LED, 72,     73,  74,  75,  76,  77  ],
    [ NO_LED, 97,     96,  95,  94,  93,  92  ],
    [ NO_LED, 98,     99,  100, 101, 102, 103 ],
    [ NO_LED, 122,    121, 120, 119, 118, 117 ]
]

ledlocation = [
    [  88,  0 ], [  80,  0 ], [  72,  0 ], [  64,  0 ], [  56,  0 ], [  48,  0 ], [  40,  0 ], [  32,  0 ], [  24,  0 ], [  16,  0 ], [   8,  0 ], [   0,  0 ],
    [   4, 16 ], [  12, 16 ], [  20, 16 ], [  28, 16 ], [  36, 16 ], [  44, 16 ], [  52, 16 ], [  60, 16 ], [  68, 16 ], [  76, 16 ], [  84, 16 ], [  92, 16 ],
    [  96, 32 ], [  88, 32 ], [  80, 32 ], [  72, 32 ], [  64, 32 ], [  56, 32 ], [  48, 32 ], [  40, 32 ], [  32, 32 ], [  24, 32 ], [  16, 32 ], [   8, 32 ],
    [  12, 48 ], [  20, 48 ], [  28, 48 ], [  36, 48 ], [  44, 48 ], [  52, 48 ], [  60, 48 ], [  68, 48 ], [  76, 48 ], [  84, 48 ], [  92, 48 ], [ 100, 48 ],
    [ 104, 64 ], [  96, 64 ], [  88, 64 ], [  80, 64 ], [  72, 64 ], [  64, 64 ], [  56, 64 ], [  48, 64 ], [  40, 64 ], [  32, 64 ], [  24, 64 ], [  16, 64 ],

    [ 120,  0 ], [ 128,  0 ], [ 136,  0 ], [ 144,  0 ], [ 152,  0 ], [ 160,  0 ], [ 168,  0 ], [ 176,  0 ], [ 184,  0 ], [ 192,  0 ], [ 200,  0 ], [ 208,  0 ],
    [ 212, 16 ], [ 204, 16 ], [ 196, 16 ], [ 188, 16 ], [ 180, 16 ], [ 172, 16 ], [ 164, 16 ], [ 156, 16 ], [ 148, 16 ], [ 140, 16 ], [ 132, 16 ], [ 124, 16 ], [ 116, 16 ],
    [ 120, 32 ], [ 128, 32 ], [ 136, 32 ], [ 144, 32 ], [ 152, 32 ], [ 160, 32 ], [ 168, 32 ], [ 176, 32 ], [ 184, 32 ], [ 192, 32 ], [ 200, 32 ], [ 208, 32 ],        [ 224, 32 ],
    [ 212, 48 ], [ 204, 48 ], [ 196, 48 ], [ 188, 48 ], [ 180, 48 ], [ 172, 48 ], [ 164, 48 ], [ 156, 48 ], [ 148, 48 ], [ 140, 48 ], [ 132, 48 ], [ 124, 48 ], [ 116, 48 ],
    [ 120, 64 ], [ 128, 64 ], [ 136, 64 ], [ 144, 64 ], [ 152, 64 ], [ 160, 64 ], [ 168, 64 ], [ 176, 64 ], [ 184, 64 ], [ 192, 64 ], [ 200, 64 ], [ 208, 64 ]
]

ledflags = [
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,

        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,   4,
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
        4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
]

###########################################################
# settings end.
###########################################################
# Do not replace the "{" and "}" used below.
###########################################################


lenrow = len(ledmatrix[0])
lencol = len(ledmatrix)

totalled = len(ledlocation)

# print("lenrow = ", lenrow, "lencol = ", lencol, "totalled = ", totalled)

ledmatrix_new = [0] * (NO_LED+1)
for j in range(lencol) :
    for i in range(lenrow) :
        # print(j, i, ledmatrix[j][i])
        ledmatrix_new[ledmatrix[j][i]] = [j, i]

# print(ledmatrix_new)


print("\t\t\"layout\": [")
# k = 0
# while type(ledmatrix_new[k]) == list:
#     print("\t\t\t{\"matrix\": [", ledmatrix_new[k][0], ",", ledmatrix_new[k][1], "], \"x\":", ledlocation[k][0], ", \"y\":", ledlocation[k][1], ", \"flags\":", ledflags[k], "},")
#     k += 1

for k in range(totalled):
    if  type(ledmatrix_new[k]) == list:
        tmptext = "\t\t\t{\"matrix\": [" + str(ledmatrix_new[k][0]) + "," + str(ledmatrix_new[k][1]) + "], \"x\": " + str(ledlocation[k][0]) + ", \"y\": " + str(ledlocation[k][1]) + ", \"flags\": " + str(ledflags[k]) + "}"
        # print("\t\t\t{\"matrix\": [", ledmatrix_new[k][0], ",", ledmatrix_new[k][1], "], \"x\":", ledlocation[k][0], ", \"y\":", ledlocation[k][1], ", \"flags\":", ledflags[k], "},")
    else:
        tmptext = "\t\t\t{\"x\": " + str(ledlocation[k][0]) + ", \"y\": " + str(ledlocation[k][1]) + ", \"flags\": " + str(ledflags[k]) + "}"
        # print("\t\t\t{ \"x\":", ledlocation[k][0], ", \"y\":", ledlocation[k][1], ", \"flags\":", ledflags[k], "},")
    if k < totalled - 1:
        tmptext = tmptext + ","
    print(tmptext)

print("\t\t]")

なお、rgb_matrix 向けのinfo.json の書き方はこちら。
https://github.com/qmk/qmk_firmware/blob/master/docs/reference_info_json.md#rgb-matrix-idrgb-matrix

3araht3araht

qmk info -f json -kb <キーボード名> で変換してくれる。
発動条件は、Keyboard 階層 で RGB Matrix を有効にすることでした。
http://rules.mk に RGB_MATRIX_ENABLE = yes を書いておくかinfo.json の features に "rgb_matrix": true と書くか。
現状keymap 階層の info.json は無視みたい。コンパイルでも無視される。

上のスクリプトは不要ですねw。