diff options
| author | Ryan <fauxpark@gmail.com> | 2020-12-16 14:24:42 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-16 14:24:42 +1100 |
| commit | 37fb14f1b5cbcb0e5ff60abc9152073635234ba4 (patch) | |
| tree | 31b12eb9ee9450ee5f5cf5a3da290e3c4bd81c41 /lib | |
| parent | 3925ff534251f44bc21848b5509415c1e41ff419 (diff) | |
| download | qmk_firmware-37fb14f1b5cbcb0e5ff60abc9152073635234ba4.tar.gz qmk_firmware-37fb14f1b5cbcb0e5ff60abc9152073635234ba4.zip | |
CLI-ify rgblight_breathing_table_calc.c (#11174)
Co-authored-by: Takeshi ISHII <2170248+mtei@users.noreply.github.com>
Co-authored-by: Zach White <skullydazed@drpepper.org>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/python/qmk/cli/generate/__init__.py | 1 | ||||
| -rw-r--r-- | lib/python/qmk/cli/generate/rgb_breathe_table.py | 79 | ||||
| -rw-r--r-- | lib/python/qmk/tests/test_cli_commands.py | 7 |
3 files changed, 87 insertions, 0 deletions
diff --git a/lib/python/qmk/cli/generate/__init__.py b/lib/python/qmk/cli/generate/__init__.py index 13bd1f091..f9585bfb5 100644 --- a/lib/python/qmk/cli/generate/__init__.py +++ b/lib/python/qmk/cli/generate/__init__.py | |||
| @@ -1,2 +1,3 @@ | |||
| 1 | from . import api | 1 | from . import api |
| 2 | from . import docs | 2 | from . import docs |
| 3 | from . import rgb_breathe_table | ||
diff --git a/lib/python/qmk/cli/generate/rgb_breathe_table.py b/lib/python/qmk/cli/generate/rgb_breathe_table.py new file mode 100644 index 000000000..e1c5423ee --- /dev/null +++ b/lib/python/qmk/cli/generate/rgb_breathe_table.py | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | """Generate rgblight_breathe_table.h | ||
| 2 | """ | ||
| 3 | import math | ||
| 4 | from argparse import ArgumentTypeError | ||
| 5 | |||
| 6 | from milc import cli | ||
| 7 | |||
| 8 | import qmk.path | ||
| 9 | |||
| 10 | |||
| 11 | def breathing_center(value): | ||
| 12 | value = float(value) | ||
| 13 | if value >= 1 and value <= 2.7: | ||
| 14 | return value | ||
| 15 | else: | ||
| 16 | raise ArgumentTypeError('Breathing center must be between 1 and 2.7') | ||
| 17 | |||
| 18 | |||
| 19 | def breathing_max(value): | ||
| 20 | value = int(value) | ||
| 21 | if value in range(0, 256): | ||
| 22 | return value | ||
| 23 | else: | ||
| 24 | raise ArgumentTypeError('Breathing max must be between 0 and 255') | ||
| 25 | |||
| 26 | |||
| 27 | @cli.argument('-c', '--center', arg_only=True, type=breathing_center, default=1.85, help='The breathing center value, from 1 to 2.7. Default: 1.85') | ||
| 28 | @cli.argument('-m', '--max', arg_only=True, type=breathing_max, default=255, help='The breathing maximum value, from 0 to 255. Default: 255') | ||
| 29 | @cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') | ||
| 30 | @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help='Quiet mode, only output error messages') | ||
| 31 | @cli.subcommand('Generates an RGB Light breathing table header.') | ||
| 32 | def generate_rgb_breathe_table(cli): | ||
| 33 | """Generate a rgblight_breathe_table.h file containing a breathing LUT for RGB Lighting (Underglow) feature. | ||
| 34 | """ | ||
| 35 | breathe_values = [0] * 256 | ||
| 36 | for pos in range(0, 256): | ||
| 37 | breathe_values[pos] = (int)((math.exp(math.sin((pos/255) * math.pi)) - cli.args.center / math.e) * (cli.args.max / (math.e - 1 / math.e))) # noqa: yapf insists there be no whitespace around / | ||
| 38 | |||
| 39 | values_template = '' | ||
| 40 | for s in range(0, 3): | ||
| 41 | step = 1 << s | ||
| 42 | |||
| 43 | values_template += '#if RGBLIGHT_BREATHE_TABLE_SIZE == {}\n'.format(256 >> s) | ||
| 44 | |||
| 45 | for pos in range(0, 256, step): | ||
| 46 | values_template += ' ' if pos % 8 == 0 else '' | ||
| 47 | values_template += '0x{:02X}'.format(breathe_values[pos]) | ||
| 48 | values_template += ',' if (pos + step) < 256 else '' | ||
| 49 | values_template += '\n' if (pos+step) % 8 == 0 else ' ' # noqa: yapf insists there be no whitespace around + | ||
| 50 | |||
| 51 | values_template += '#endif' | ||
| 52 | values_template += '\n\n' if s < 2 else '' | ||
| 53 | |||
| 54 | table_template = '''#pragma once | ||
| 55 | |||
| 56 | #define RGBLIGHT_EFFECT_BREATHE_TABLE | ||
| 57 | |||
| 58 | // clang-format off | ||
| 59 | |||
| 60 | // Breathing center: {0:.2f} | ||
| 61 | // Breathing max: {1:d} | ||
| 62 | |||
| 63 | const uint8_t PROGMEM rgblight_effect_breathe_table[] = {{ | ||
| 64 | {2} | ||
| 65 | }}; | ||
| 66 | |||
| 67 | static const int table_scale = 256 / sizeof(rgblight_effect_breathe_table); | ||
| 68 | '''.format(cli.args.center, cli.args.max, values_template) | ||
| 69 | |||
| 70 | if cli.args.output: | ||
| 71 | cli.args.output.parent.mkdir(parents=True, exist_ok=True) | ||
| 72 | if cli.args.output.exists(): | ||
| 73 | cli.args.output.replace(cli.args.output.name + '.bak') | ||
| 74 | cli.args.output.write_text(table_template) | ||
| 75 | |||
| 76 | if not cli.args.quiet: | ||
| 77 | cli.log.info('Wrote header to %s.', cli.args.output) | ||
| 78 | else: | ||
| 79 | print(table_template) | ||
diff --git a/lib/python/qmk/tests/test_cli_commands.py b/lib/python/qmk/tests/test_cli_commands.py index 99ec59608..08e80f2c9 100644 --- a/lib/python/qmk/tests/test_cli_commands.py +++ b/lib/python/qmk/tests/test_cli_commands.py | |||
| @@ -190,3 +190,10 @@ def test_clean(): | |||
| 190 | result = check_subcommand('clean', '-a') | 190 | result = check_subcommand('clean', '-a') |
| 191 | check_returncode(result) | 191 | check_returncode(result) |
| 192 | assert result.stdout.count('done') == 2 | 192 | assert result.stdout.count('done') == 2 |
| 193 | |||
| 194 | |||
| 195 | def test_generate_rgb_breathe_table(): | ||
| 196 | result = check_subcommand("generate-rgb-breathe-table", "-c", "1.2", "-m", "127") | ||
| 197 | check_returncode(result) | ||
| 198 | assert 'Breathing center: 1.2' in result.stdout | ||
| 199 | assert 'Breathing max: 127' in result.stdout | ||
