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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

ledmap.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "ledmap.h"
  2. #include "ledmap_in_eeprom.h"
  3. #include "led.h"
  4. #include "softpwm_led.h"
  5. #include "action_layer.h"
  6. #include "debug.h"
  7. static led_state_t led_state_last = 0;
  8. static led_binding_t usb_led_binding = 0;
  9. static led_binding_t default_layer_binding = 0;
  10. static led_binding_t layer_binding = 0;
  11. static led_binding_t backlight_binding = 0;
  12. static led_binding_t reverse_binding = 0;
  13. static void update_led_state(led_state_t state, uint8_t force);
  14. void ledmap_init(void)
  15. {
  16. for (uint8_t i = 0; i < LED_COUNT; i++) {
  17. ledmap_t ledmap = ledmap_get_code(i);
  18. if (ledmap.reverse) {
  19. LED_BIT_SET(reverse_binding, i);
  20. }
  21. if (ledmap.backlight) {
  22. LED_BIT_SET(backlight_binding, i);
  23. }
  24. switch (ledmap.binding) {
  25. case LEDMAP_BINDING_DEFAULT_LAYER:
  26. LED_BIT_SET(default_layer_binding, i);
  27. break;
  28. case LEDMAP_BINDING_LAYER:
  29. LED_BIT_SET(layer_binding, i);
  30. break;
  31. case LEDMAP_BINDING_USB_LED:
  32. LED_BIT_SET(usb_led_binding, i);
  33. break;
  34. }
  35. }
  36. ledmap_led_init();
  37. update_led_state(0, 1);
  38. }
  39. void led_set(uint8_t usb_led)
  40. {
  41. if (usb_led_binding) {
  42. led_state_t led_state = led_state_last;
  43. for (uint8_t i = 0; i < LED_COUNT; i++) {
  44. if (usb_led_binding & LED_BIT(i)) {
  45. uint8_t param = ledmap_get_code(i).param;
  46. (usb_led & (1 << param)) ? LED_BIT_SET(led_state, i) : LED_BIT_CLEAR(led_state, i);
  47. }
  48. }
  49. update_led_state(led_state, 0);
  50. }
  51. }
  52. #ifndef NO_ACTION_LAYER
  53. void default_layer_state_change(uint32_t state)
  54. {
  55. if (default_layer_binding) {
  56. led_state_t led_state = led_state_last;
  57. for (uint8_t i = 0; i < LED_COUNT; i++) {
  58. if (default_layer_binding & LED_BIT(i)) {
  59. uint8_t param = ledmap_get_code(i).param;
  60. (state & (1UL << param)) ? LED_BIT_SET(led_state, i) : LED_BIT_CLEAR(led_state, i);
  61. }
  62. }
  63. update_led_state(led_state, 0);
  64. }
  65. }
  66. void layer_state_change(uint32_t state)
  67. {
  68. if (layer_binding) {
  69. led_state_t led_state = led_state_last;
  70. for (uint8_t i = 0; i < LED_COUNT; i++) {
  71. if (layer_binding & LED_BIT(i)) {
  72. uint8_t param = ledmap_get_code(i).param;
  73. (state & (1UL << param)) ? LED_BIT_SET(led_state, i) : LED_BIT_CLEAR(led_state, i);
  74. }
  75. }
  76. update_led_state(led_state, 0);
  77. }
  78. }
  79. #endif
  80. #ifdef SOFTPWM_LED_ENABLE
  81. void softpwm_led_init(void)
  82. {
  83. }
  84. void softpwm_led_on(uint8_t index)
  85. {
  86. if (backlight_binding & LED_BIT(index)) {
  87. ledmap_led_on(index);
  88. }
  89. }
  90. void softpwm_led_off(uint8_t index)
  91. {
  92. if (backlight_binding & LED_BIT(index)) {
  93. ledmap_led_off(index);
  94. }
  95. }
  96. void softpwm_led_state_change(uint8_t state)
  97. {
  98. if (state) {
  99. }
  100. else {
  101. update_led_state(led_state_last, 1);
  102. }
  103. }
  104. #endif
  105. void update_led_state(led_state_t state, uint8_t force)
  106. {
  107. led_state_t diff = led_state_last ^ state;
  108. if (force || diff) {
  109. for (uint8_t i = 0; i < LED_COUNT; i++) {
  110. if (softpwm_led_get_state() && (backlight_binding & LED_BIT(i))) {
  111. continue;
  112. }
  113. if (force || diff & LED_BIT(i)) {
  114. if ((state ^ reverse_binding) & LED_BIT(i)) {
  115. ledmap_led_on(i);
  116. }
  117. else {
  118. ledmap_led_off(i);
  119. }
  120. }
  121. }
  122. led_state_last = state;
  123. }
  124. }