rgb_breathe_table.py 2.8 KB

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