qmk_firmware/lib/python/qmk/cli/generate/lighting_map.py

127 lines
3.6 KiB
Python
Raw Normal View History

2022-10-16 22:55:40 +00:00
from milc import cli
from qmk.path import normpath
from qmk.commands import dump_lines
2023-03-21 15:05:40 +00:00
from qmk.lighting import load_lighting_spec
2022-10-16 22:55:40 +00:00
from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
PREFIX_MAP = {
'rgblight': {
'ifdef': 'RGBLIGHT_EFFECT',
'def': 'RGBLIGHT_MODE',
},
'rgb_matrix': {
'ifdef': 'ENABLE_RGB_MATRIX',
'def': 'RGB_MATRIX',
},
'led_matrix': {
'ifdef': 'ENABLE_LED_MATRIX',
'def': 'LED_MATRIX',
},
}
2023-03-21 15:05:40 +00:00
def _always_enabled(id):
"""Assumption that first effect is always enabled
"""
return id == '0x00'
def _wrap_ifdef(line, define):
return f'''
#ifdef {define}
{line}
#endif'''
2022-10-16 22:55:40 +00:00
def _append_lighting_map(lines, feature, spec):
2023-03-21 15:05:40 +00:00
"""Translate effect to 'constant id'->'firmware id' lookup table
2022-10-16 22:55:40 +00:00
"""
groups = spec.get('groups', {})
ifdef_prefix = PREFIX_MAP[feature]['ifdef']
def_prefix = PREFIX_MAP[feature]['def']
2023-02-19 00:51:29 +00:00
lines.append(f'static const uint8_t {feature}_effect_map[][2] PROGMEM = {{')
2022-10-16 22:55:40 +00:00
for id, obj in spec.get('effects', {}).items():
define = obj['define']
offset = f' + {obj["offset"]}' if obj['offset'] else ''
2023-03-21 15:05:40 +00:00
line = f'{{ {id}, {def_prefix}_{define}{offset}}},'
if not _always_enabled(id):
line = _wrap_ifdef(line, f'{ifdef_prefix}_{define}')
2022-10-16 22:55:40 +00:00
group = groups.get(obj.get('group', None), {}).get('define', None)
if group:
2023-03-21 15:05:40 +00:00
line = _wrap_ifdef(line, group)
2022-10-16 22:55:40 +00:00
lines.append(line)
lines.append('};')
# add helper funcs
lines.append(
f'''
2023-03-21 15:05:40 +00:00
uint8_t {feature}_effect_to_id(uint8_t val) {{
2022-10-16 22:55:40 +00:00
for(uint8_t i = 0; i < ARRAY_SIZE({feature}_effect_map); i++) {{
2023-02-19 00:51:29 +00:00
if (pgm_read_byte(&{feature}_effect_map[i][1]) == val)
return pgm_read_byte(&{feature}_effect_map[i][0]);
2022-10-16 22:55:40 +00:00
}}
return 0xFF;
}}
2023-03-21 15:05:40 +00:00
uint8_t {feature}_id_to_effect(uint8_t val) {{
2022-10-16 22:55:40 +00:00
for(uint8_t i = 0; i < ARRAY_SIZE({feature}_effect_map); i++) {{
2023-02-19 00:51:29 +00:00
if (pgm_read_byte(&{feature}_effect_map[i][0]) == val)
return pgm_read_byte(&{feature}_effect_map[i][1]);
2022-10-16 22:55:40 +00:00
}}
return 0xFF;
}}'''
)
2023-03-21 15:05:40 +00:00
def _append_lighting_bit_field(lines, feature, spec):
"""Translate effect to bit of bit-field
2022-10-16 22:55:40 +00:00
"""
groups = spec.get('groups', {})
ifdef_prefix = PREFIX_MAP[feature]['ifdef']
lines.append(f'enum {{ ENABLED_{feature.upper()}_EFFECTS = 0')
for id, obj in spec.get('effects', {}).items():
define = obj['define']
2023-03-21 15:05:40 +00:00
line = f' | (1ULL << {id})'
if not _always_enabled(id):
line = _wrap_ifdef(line, f'{ifdef_prefix}_{define}')
2022-10-16 22:55:40 +00:00
group = groups.get(obj.get('group', None), {}).get('define', None)
if group:
2023-03-21 15:05:40 +00:00
line = _wrap_ifdef(line, group)
2022-10-16 22:55:40 +00:00
lines.append(line)
lines.append('};')
2023-03-21 15:05:40 +00:00
def _append_lighting_mapping(lines, feature):
2023-03-26 02:15:41 +00:00
"""Generate lookup table and bit-field of effect
2022-10-16 22:55:40 +00:00
"""
2023-03-21 15:05:40 +00:00
spec = load_lighting_spec(feature)
2022-10-16 22:55:40 +00:00
2023-03-21 15:05:40 +00:00
_append_lighting_bit_field(lines, feature, spec)
_append_lighting_map(lines, feature, spec)
2022-10-16 22:55:40 +00:00
@cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
2023-03-21 15:05:40 +00:00
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help='Quiet mode, only output error messages')
@cli.argument('-f', '--feature', required=True, help='Feature to generate map', choices=PREFIX_MAP.keys())
2022-10-16 22:55:40 +00:00
@cli.subcommand('Generates effect header.')
2023-03-21 15:05:40 +00:00
def generate_lighting_map(cli):
2022-10-16 22:55:40 +00:00
# Preamble
lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '// clang-format off']
2023-03-21 15:05:40 +00:00
_append_lighting_mapping(lines, cli.args.feature)
2022-10-16 22:55:40 +00:00
dump_lines(cli.args.output, lines, cli.args.quiet)