You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
4.2 KiB

Backlighting for JJ40 and underglow initialisation code (#2260) * Cleanup Mechmini keymap. Once the custom RGB function is defined, there is no need to manually handle RGB code. * Change default to KEYMAP_MIT, not KEYMAP_OFFSET * Add custom RGB code for JJ40 * Reset Mechmini advertised power draw to 500. Will have to test actual maximum power draw later. * RGB working on JJ40. * Fix: saturation increase/decrease flipped * Add new directory for my custom keymap with RGB keycodes * Swap LAlt and LGUI * Update JJ40 max power draw with measured value * Update: fun40 rules.mk to enable underglow; earlier failed Travis CI * Fix: init RGB LEDs on boot. Also added HHKB-like keymap for XD60. * Super rudimentary backlight test, init RGB LEDs on boot * Backlighting works - stays on for now * Toggling working * Now can override backlight.c functions. Problem was functions in backlight.c weren't called before due to a lack of matrix_scan_quantum() in matrix.c * Timers not working * Delete global.h * Cleanup * Compiles * Good sign: LEDs stop working again * Handle timer1 overflow * Progress: fix: forgot to init * Backlighting fully working now except breathing. * Revert keymap to original keycodes * Update XD60 keymap README * Update JJ40 keymap with backlight toggles * Breathing working just fine. * Update references * Add backlight_set() call * Cleanup code to disable backlight * Fix: does not compile * Fix: missing call to rgblight_task. * Testing with BACKLIGHT_BREATHING * Cleanup * Cleanup comments * More commenting cleanup. * Do not enable BACKLIGHT_BREATHING by default
6 years ago
  1. /**
  2. * Breathing effect code for PS2AVRGB boards (ATMEGA32A)
  3. * Works in conjunction with `backlight.c`.
  4. *
  5. * Code adapted from `quantum.c` to register with the existing TIMER1 overflow
  6. * handler in `backlight.c` instead of setting up its own timer.
  7. * Kenneth A. (github.com/krusli | krusli.me)
  8. */
  9. #ifdef BACKLIGHT_ENABLE
  10. #ifdef BACKLIGHT_BREATHING
  11. #include "backlight_custom.h"
  12. #ifndef BREATHING_PERIOD
  13. #define BREATHING_PERIOD 6
  14. #endif
  15. #define breathing_min() do {breathing_counter = 0;} while (0)
  16. #define breathing_max() do {breathing_counter = breathing_period * 244 / 2;} while (0)
  17. // TODO make this share code with quantum.c
  18. #define BREATHING_NO_HALT 0
  19. #define BREATHING_HALT_OFF 1
  20. #define BREATHING_HALT_ON 2
  21. #define BREATHING_STEPS 128
  22. static uint8_t breathing_period = BREATHING_PERIOD;
  23. static uint8_t breathing_halt = BREATHING_NO_HALT;
  24. static uint16_t breathing_counter = 0;
  25. static bool breathing = false;
  26. bool is_breathing(void) {
  27. return breathing;
  28. }
  29. // See http://jared.geek.nz/2013/feb/linear-led-pwm
  30. static uint16_t cie_lightness(uint16_t v) {
  31. if (v <= 5243) // if below 8% of max
  32. return v / 9; // same as dividing by 900%
  33. else {
  34. uint32_t y = (((uint32_t) v + 10486) << 8) / (10486 + 0xFFFFUL); // add 16% of max and compare
  35. // to get a useful result with integer division, we shift left in the expression above
  36. // and revert what we've done again after squaring.
  37. y = y * y * y >> 8;
  38. if (y > 0xFFFFUL) // prevent overflow
  39. return 0xFFFFU;
  40. else
  41. return (uint16_t) y;
  42. }
  43. }
  44. void breathing_enable(void) {
  45. breathing = true;
  46. breathing_counter = 0;
  47. breathing_halt = BREATHING_NO_HALT;
  48. // interrupt already registered
  49. }
  50. void breathing_pulse(void) {
  51. if (get_backlight_level() == 0)
  52. breathing_min();
  53. else
  54. breathing_max();
  55. breathing_halt = BREATHING_HALT_ON;
  56. // breathing_interrupt_enable();
  57. breathing = true;
  58. }
  59. void breathing_disable(void) {
  60. breathing = false;
  61. // backlight_set(get_backlight_level());
  62. b_led_set(get_backlight_level()); // custom implementation of backlight_set()
  63. }
  64. void breathing_self_disable(void)
  65. {
  66. if (get_backlight_level() == 0)
  67. breathing_halt = BREATHING_HALT_OFF;
  68. else
  69. breathing_halt = BREATHING_HALT_ON;
  70. }
  71. void breathing_toggle(void) {
  72. if (is_breathing())
  73. breathing_disable();
  74. else
  75. breathing_enable();
  76. }
  77. void breathing_period_set(uint8_t value)
  78. {
  79. if (!value)
  80. value = 1;
  81. breathing_period = value;
  82. }
  83. void breathing_period_default(void) {
  84. breathing_period_set(BREATHING_PERIOD);
  85. }
  86. void breathing_period_inc(void)
  87. {
  88. breathing_period_set(breathing_period+1);
  89. }
  90. void breathing_period_dec(void)
  91. {
  92. breathing_period_set(breathing_period-1);
  93. }
  94. /* To generate breathing curve in python:
  95. * from math import sin, pi; [int(sin(x/128.0*pi)**4*255) for x in range(128)]
  96. */
  97. static const uint8_t breathing_table[BREATHING_STEPS] PROGMEM = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 17, 20, 24, 28, 32, 36, 41, 46, 51, 57, 63, 70, 76, 83, 91, 98, 106, 113, 121, 129, 138, 146, 154, 162, 170, 178, 185, 193, 200, 207, 213, 220, 225, 231, 235, 240, 244, 247, 250, 252, 253, 254, 255, 254, 253, 252, 250, 247, 244, 240, 235, 231, 225, 220, 213, 207, 200, 193, 185, 178, 170, 162, 154, 146, 138, 129, 121, 113, 106, 98, 91, 83, 76, 70, 63, 57, 51, 46, 41, 36, 32, 28, 24, 20, 17, 15, 12, 10, 8, 6, 5, 4, 3, 2, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  98. // Use this before the cie_lightness function.
  99. static inline uint16_t scale_backlight(uint16_t v) {
  100. return v / BACKLIGHT_LEVELS * get_backlight_level();
  101. }
  102. void custom_breathing_handler(void) {
  103. uint16_t interval = (uint16_t) breathing_period * 244 / BREATHING_STEPS;
  104. // resetting after one period to prevent ugly reset at overflow.
  105. breathing_counter = (breathing_counter + 1) % (breathing_period * 244);
  106. uint8_t index = breathing_counter / interval % BREATHING_STEPS;
  107. if (((breathing_halt == BREATHING_HALT_ON) && (index == BREATHING_STEPS / 2)) ||
  108. ((breathing_halt == BREATHING_HALT_OFF) && (index == BREATHING_STEPS - 1)))
  109. {
  110. // breathing_interrupt_disable();
  111. }
  112. setPWM(cie_lightness(scale_backlight((uint16_t) pgm_read_byte(&breathing_table[index]) * 0x0101U)));
  113. }
  114. #endif // BACKLIGHT_BREATHING
  115. #endif // BACKLIGHT_ENABLE