logo

qmk_firmware

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

indicator_leds.c (3483B)


  1. /*
  2. Copyright 2019 MechMerlin <mechmerlin@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/interrupt.h>
  15. #include <avr/io.h>
  16. #include <stdbool.h>
  17. #include <util/delay.h>
  18. #include "indicator_leds.h"
  19. #define LED_T1H 900
  20. #define LED_T1L 600
  21. #define LED_T0H 400
  22. #define LED_T0L 900
  23. void send_bit_d4(bool bitVal) {
  24. if(bitVal) {
  25. asm volatile (
  26. "sbi %[port], %[bit] \n\t"
  27. ".rept %[onCycles] \n\t"
  28. "nop \n\t"
  29. ".endr \n\t"
  30. "cbi %[port], %[bit] \n\t"
  31. ".rept %[offCycles] \n\t"
  32. "nop \n\t"
  33. ".endr \n\t"
  34. ::
  35. [port] "I" (_SFR_IO_ADDR(PORTD)),
  36. [bit] "I" (4),
  37. [onCycles] "I" (NS_TO_CYCLES(LED_T1H) - 2),
  38. [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2));
  39. } else {
  40. asm volatile (
  41. "sbi %[port], %[bit] \n\t"
  42. ".rept %[onCycles] \n\t"
  43. "nop \n\t"
  44. ".endr \n\t"
  45. "cbi %[port], %[bit] \n\t"
  46. ".rept %[offCycles] \n\t"
  47. "nop \n\t"
  48. ".endr \n\t"
  49. ::
  50. [port] "I" (_SFR_IO_ADDR(PORTD)),
  51. [bit] "I" (4),
  52. [onCycles] "I" (NS_TO_CYCLES(LED_T0H) - 2),
  53. [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2));
  54. }
  55. }
  56. void send_bit_d6(bool bitVal)
  57. {
  58. if(bitVal) {
  59. asm volatile (
  60. "sbi %[port], %[bit] \n\t"
  61. ".rept %[onCycles] \n\t"
  62. "nop \n\t"
  63. ".endr \n\t"
  64. "cbi %[port], %[bit] \n\t"
  65. ".rept %[offCycles] \n\t"
  66. "nop \n\t"
  67. ".endr \n\t"
  68. ::
  69. [port] "I" (_SFR_IO_ADDR(PORTD)),
  70. [bit] "I" (6),
  71. [onCycles] "I" (NS_TO_CYCLES(LED_T1H) - 2),
  72. [offCycles] "I" (NS_TO_CYCLES(LED_T1L) - 2));
  73. } else {
  74. asm volatile (
  75. "sbi %[port], %[bit] \n\t"
  76. ".rept %[onCycles] \n\t"
  77. "nop \n\t"
  78. ".endr \n\t"
  79. "cbi %[port], %[bit] \n\t"
  80. ".rept %[offCycles] \n\t"
  81. "nop \n\t"
  82. ".endr \n\t"
  83. ::
  84. [port] "I" (_SFR_IO_ADDR(PORTD)),
  85. [bit] "I" (6),
  86. [onCycles] "I" (NS_TO_CYCLES(LED_T0H) - 2),
  87. [offCycles] "I" (NS_TO_CYCLES(LED_T0L) - 2));
  88. }
  89. }
  90. void send_value(uint8_t byte, enum Device device) {
  91. for(uint8_t b = 0; b < 8; b++) {
  92. if(device == Device_STATUSLED) {
  93. send_bit_d4(byte & 0b10000000);
  94. }
  95. if(device == Device_PCBRGB) {
  96. send_bit_d6(byte & 0b10000000);
  97. }
  98. byte <<= 1;
  99. }
  100. }
  101. void send_color(uint8_t r, uint8_t g, uint8_t b, enum Device device) {
  102. send_value(r, device);
  103. send_value(g, device);
  104. send_value(b, device);
  105. }
  106. // Port from backlight_set_state
  107. void indicator_leds_set(bool leds[8]) {
  108. cli();
  109. send_color(leds[1] ? 255 : 0, leds[2] ? 255 : 0, leds[0] ? 255 : 0, Device_STATUSLED);
  110. send_color(leds[4] ? 255 : 0, leds[3] ? 255 : 0, leds[5] ? 255 : 0, Device_STATUSLED);
  111. leds[6] ? (PORTD &= ~0b10000000) : (PORTD |= 0b10000000);
  112. sei();
  113. show();
  114. }