qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

rgb_breathe_table.py (2787B)


      1 """Generate rgblight_breathe_table.h
      2 """
      3 import math
      4 from argparse import ArgumentTypeError
      5 
      6 from milc import cli
      7 
      8 from qmk.constants import GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE
      9 from qmk.commands import dump_lines
     10 from qmk.path import normpath
     11 
     12 
     13 def breathing_center(value):
     14     value = float(value)
     15     if value >= 1 and value <= 2.7:
     16         return value
     17     else:
     18         raise ArgumentTypeError('Breathing center must be between 1 and 2.7')
     19 
     20 
     21 def breathing_max(value):
     22     value = int(value)
     23     if value in range(0, 256):
     24         return value
     25     else:
     26         raise ArgumentTypeError('Breathing max must be between 0 and 255')
     27 
     28 
     29 def _generate_table(lines, center, maximum):
     30     breathe_values = [0] * 256
     31     for pos in range(0, 256):
     32         breathe_values[pos] = (int)((math.exp(math.sin((pos / 255) * math.pi)) - center / math.e) * (maximum / (math.e - 1 / math.e)))
     33 
     34     values_template = ''
     35     for s in range(0, 3):
     36         step = 1 << s
     37 
     38         values_template += '#if RGBLIGHT_BREATHE_TABLE_SIZE == {}\n'.format(256 >> s)
     39 
     40         for pos in range(0, 256, step):
     41             values_template += '    ' if pos % 8 == 0 else ''
     42             values_template += '0x{:02X}'.format(breathe_values[pos])
     43             values_template += ',' if (pos + step) < 256 else ''
     44             values_template += '\n' if (pos + step) % 8 == 0 else ' '
     45 
     46         values_template += '#endif'
     47         values_template += '\n\n' if s < 2 else ''
     48 
     49     table_template = '''#define RGBLIGHT_EFFECT_BREATHE_TABLE
     50 
     51 // Breathing center: {0:.2f}
     52 // Breathing max:    {1:d}
     53 
     54 const uint8_t PROGMEM rgblight_effect_breathe_table[] = {{
     55 {2}
     56 }};
     57 
     58 static const int table_scale = 256 / sizeof(rgblight_effect_breathe_table);
     59 '''.format(center, maximum, values_template)
     60     lines.append(table_template)
     61 
     62 
     63 @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')
     64 @cli.argument('-m', '--max', arg_only=True, type=breathing_max, default=255, help='The breathing maximum value, from 0 to 255. Default: 255')
     65 @cli.argument('-o', '--output', arg_only=True, type=normpath, help='File to write to')
     66 @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help='Quiet mode, only output error messages')
     67 @cli.subcommand('Generates an RGB Light breathing table header.')
     68 def generate_rgb_breathe_table(cli):
     69     """Generate a rgblight_breathe_table.h file containing a breathing LUT for RGB Lighting (Underglow) feature.
     70     """
     71 
     72     # Build the header file.
     73     header_lines = [GPL2_HEADER_C_LIKE, GENERATED_HEADER_C_LIKE, '#pragma once', '// clang-format off']
     74 
     75     _generate_table(header_lines, cli.args.center, cli.args.max)
     76 
     77     # Show the results
     78     dump_lines(cli.args.output, header_lines, cli.args.quiet)