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>
This commit is contained in:
Ryan 2020-12-16 14:24:42 +11:00 committed by GitHub
parent 3925ff5342
commit 37fb14f1b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 203 additions and 506 deletions

View File

@ -296,6 +296,16 @@ This command allows you to generate QMK documentation locally. It can be uses fo
qmk generate-docs
```
## `qmk generate-rgb-breathe-table`
This command generates a lookup table (LUT) header file for the [RGB Lighting](feature_rgblight.md) feature's breathing animation. Place this file in your keyboard or keymap directory as `rgblight_breathe_table.h` to override the default LUT in `quantum/`.
**Usage**:
```
qmk generate-rgb-breathe-table [-q] [-o OUTPUT] [-m MAX] [-c CENTER]
```
## `qmk kle2json`
This command allows you to convert from raw KLE data to QMK Configurator JSON. It accepts either an absolute file path, or a file name in the current directory. By default it will not overwrite `info.json` if it is already present. Use the `-f` or `--force` flag to overwrite.

View File

@ -1,2 +1,3 @@
from . import api
from . import docs
from . import rgb_breathe_table

View File

@ -0,0 +1,79 @@
"""Generate rgblight_breathe_table.h
"""
import math
from argparse import ArgumentTypeError
from milc import cli
import qmk.path
def breathing_center(value):
value = float(value)
if value >= 1 and value <= 2.7:
return value
else:
raise ArgumentTypeError('Breathing center must be between 1 and 2.7')
def breathing_max(value):
value = int(value)
if value in range(0, 256):
return value
else:
raise ArgumentTypeError('Breathing max must be between 0 and 255')
@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')
@cli.argument('-m', '--max', arg_only=True, type=breathing_max, default=255, help='The breathing maximum value, from 0 to 255. Default: 255')
@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to')
@cli.argument('-q', '--quiet', arg_only=True, action='store_true', help='Quiet mode, only output error messages')
@cli.subcommand('Generates an RGB Light breathing table header.')
def generate_rgb_breathe_table(cli):
"""Generate a rgblight_breathe_table.h file containing a breathing LUT for RGB Lighting (Underglow) feature.
"""
breathe_values = [0] * 256
for pos in range(0, 256):
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 /
values_template = ''
for s in range(0, 3):
step = 1 << s
values_template += '#if RGBLIGHT_BREATHE_TABLE_SIZE == {}\n'.format(256 >> s)
for pos in range(0, 256, step):
values_template += ' ' if pos % 8 == 0 else ''
values_template += '0x{:02X}'.format(breathe_values[pos])
values_template += ',' if (pos + step) < 256 else ''
values_template += '\n' if (pos+step) % 8 == 0 else ' ' # noqa: yapf insists there be no whitespace around +
values_template += '#endif'
values_template += '\n\n' if s < 2 else ''
table_template = '''#pragma once
#define RGBLIGHT_EFFECT_BREATHE_TABLE
// clang-format off
// Breathing center: {0:.2f}
// Breathing max: {1:d}
const uint8_t PROGMEM rgblight_effect_breathe_table[] = {{
{2}
}};
static const int table_scale = 256 / sizeof(rgblight_effect_breathe_table);
'''.format(cli.args.center, cli.args.max, values_template)
if cli.args.output:
cli.args.output.parent.mkdir(parents=True, exist_ok=True)
if cli.args.output.exists():
cli.args.output.replace(cli.args.output.name + '.bak')
cli.args.output.write_text(table_template)
if not cli.args.quiet:
cli.log.info('Wrote header to %s.', cli.args.output)
else:
print(table_template)

View File

@ -190,3 +190,10 @@ def test_clean():
result = check_subcommand('clean', '-a')
check_returncode(result)
assert result.stdout.count('done') == 2
def test_generate_rgb_breathe_table():
result = check_subcommand("generate-rgb-breathe-table", "-c", "1.2", "-m", "127")
check_returncode(result)
assert 'Breathing center: 1.2' in result.stdout
assert 'Breathing max: 127' in result.stdout

File diff suppressed because it is too large Load Diff

View File

@ -1,49 +0,0 @@
//
// calculate rgblight_effect_breathe_table[] values
//
// this is host program for quantum/rgblight.c:void rgblight_effect_breathing();
//
// example:
// $ edit util/rgblight_breathing_table_calc.c
// $ cc -o util/rgblight_breathing_table_calc util/rgblight_breathing_table_calc.c
// $ ./util/rgblight_breathing_table_calc > keyboards/KEYBOARD_NAME/keymaps/KEYMAP_NAME/rgblight_breathe_table.h
//
#include <stdio.h>
#include <math.h>
#include <stdint.h>
/// customize breeathing effect part ///////////////////////////
#define RGBLIGHT_EFFECT_BREATHE_CENTER 1.85 // 1 to 2.7
#define RGBLIGHT_EFFECT_BREATHE_MAX 255 // 0 to 255
////////////////////////////////////////////////////////////////
int main(void) {
int pos, step;
int table[256];
for (pos = 0; pos < 256; pos ++ ) {
table[pos] = (uint8_t)(
(exp(sin((pos/255.0)*M_PI))- RGBLIGHT_EFFECT_BREATHE_CENTER/M_E)
* (RGBLIGHT_EFFECT_BREATHE_MAX/(M_E-1/M_E))
);
}
printf("#ifndef RGBLIGHT_EFFECT_BREATHE_TABLE\n");
printf("#define RGBLIGHT_EFFECT_BREATHE_TABLE\n\n");
printf("const uint8_t rgblight_effect_breathe_table[] PROGMEM = {\n");
printf(" /* #define RGBLIGHT_EFFECT_BREATHE_CENTER %.2f */\n", RGBLIGHT_EFFECT_BREATHE_CENTER);
printf(" /* #define RGBLIGHT_EFFECT_BREATHE_MAX %d */\n", RGBLIGHT_EFFECT_BREATHE_MAX);
for (int s = 0, step = (1<<s); s < 3 ; s += 1, step = (1<<s) ) {
printf("\n #if RGBLIGHT_BREATHE_TABLE_SIZE == %d\n",
s == 0 ? 256:(s== 1 ? 128: 64));
for (pos = 0; pos < 256; pos += step ) {
printf(" 0x%x%s", table[pos], (pos+step)>=256?"":"," );
if ((pos+step) % 8 == 0)
printf("\n");
}
printf(" #endif /* %d bytes table */\n", s == 0 ? 256:(s== 1 ? 128: 64));
}
printf("};\n");
printf("\nstatic const int table_scale = 256/sizeof(rgblight_effect_breathe_table);\n");
printf("\n#endif /* RGBLIGHT_EFFECT_BREATHE_TABLE */\n");
return 0;
}