backlight_soft.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "quantum.h"
  2. #include "backlight.h"
  3. #if !defined(BACKLIGHT_PIN) && !defined(BACKLIGHT_PINS)
  4. # error "Backlight pin/pins not defined. Please configure."
  5. #endif
  6. #ifdef BACKLIGHT_BREATHING
  7. # error "Backlight breathing is not available for software PWM. Please disable."
  8. #endif
  9. #ifndef BACKLIGHT_ON_STATE
  10. # define BACKLIGHT_ON_STATE 1
  11. #endif
  12. #ifdef BACKLIGHT_PINS
  13. # define BACKLIGHT_PIN_INIT BACKLIGHT_PINS
  14. #else
  15. # define BACKLIGHT_PIN_INIT \
  16. { BACKLIGHT_PIN }
  17. #endif
  18. static uint16_t s_duty_pattern = 0;
  19. static const pin_t backlight_pins[] = BACKLIGHT_PIN_INIT;
  20. #define BACKLIGHT_LED_COUNT (sizeof(backlight_pins) / sizeof(pin_t))
  21. #define FOR_EACH_LED(x) \
  22. for (uint8_t i = 0; i < BACKLIGHT_LED_COUNT; i++) { \
  23. pin_t backlight_pin = backlight_pins[i]; \
  24. { x } \
  25. }
  26. void backlight_on(pin_t backlight_pin) {
  27. #if BACKLIGHT_ON_STATE == 0
  28. writePinLow(backlight_pin);
  29. #else
  30. writePinHigh(backlight_pin);
  31. #endif
  32. }
  33. void backlight_off(pin_t backlight_pin) {
  34. #if BACKLIGHT_ON_STATE == 0
  35. writePinHigh(backlight_pin);
  36. #else
  37. writePinLow(backlight_pin);
  38. #endif
  39. }
  40. void backlight_init_ports(void) {
  41. // Setup backlight pin as output and output to off state.
  42. FOR_EACH_LED(setPinOutput(backlight_pin); backlight_off(backlight_pin);)
  43. }
  44. // clang-format off
  45. /** \brief PWM duty patterns
  46. *
  47. * We scale the current backlight level to an index within this array. This allows
  48. * backlight_task to focus on just switching LEDs on/off, and we can predict the duty pattern
  49. */
  50. static uint16_t backlight_duty_table[] = {
  51. 0b0000000000000000,
  52. 0b1000000000000000,
  53. 0b1000000010000000,
  54. 0b1000001000010000,
  55. 0b1000100010001000,
  56. 0b1001001001001000,
  57. 0b1010101010101010,
  58. 0b1110111011101110,
  59. 0b1111111111111111,
  60. };
  61. #define backlight_duty_table_size (sizeof(backlight_duty_table) / sizeof(backlight_duty_table[0]))
  62. // clang-format on
  63. static uint8_t scale_backlight(uint8_t v) { return v * (backlight_duty_table_size - 1) / BACKLIGHT_LEVELS; }
  64. void backlight_task(void) {
  65. static uint8_t backlight_tick = 0;
  66. if (s_duty_pattern & ((uint16_t)1 << backlight_tick)) {
  67. FOR_EACH_LED(backlight_on(backlight_pin);)
  68. } else {
  69. FOR_EACH_LED(backlight_off(backlight_pin);)
  70. }
  71. backlight_tick = (backlight_tick + 1) % 16;
  72. }
  73. void backlight_set(uint8_t level) { s_duty_pattern = backlight_duty_table[scale_backlight(level)]; }