logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

rgb_breathe_table.py (2787B)


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