led_matrix_pins.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* Copyright 2017 Jason Williams
  2. * Copyright 2018 Jack Humbert
  3. * Copyright 2019 Clueboard
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifdef __AVR__
  19. # include <avr/interrupt.h>
  20. # include <avr/io.h>
  21. # include <util/delay.h>
  22. # define led_wait_us(us) wait_us(us)
  23. #else
  24. # include "ch.h"
  25. # include "hal.h"
  26. # define led_wait_us(us) chSysPolledDelayX(US2RTC(STM32_SYSCLK, us))
  27. #endif
  28. #include <stdint.h>
  29. #include <stdbool.h>
  30. #include <string.h>
  31. #include "led_matrix_pins.h"
  32. #include "led_tables.h"
  33. #include "progmem.h"
  34. #include "quantum.h"
  35. #include "backlight.h"
  36. /*
  37. * g_pwm_buffer is an array that represents the duty cycle (0-255) for each LED.
  38. */
  39. uint8_t g_pwm_buffer[LED_DRIVER_LED_COUNT];
  40. const pin_t led_pins[LED_DRIVER_LED_COUNT] = LED_MATRIX_PINS;
  41. #ifdef LED_MATRIX_PIN_SINK
  42. # define led_pin_on(pin) writePinLow(pin)
  43. # define led_pin_off(pin) writePinHigh(pin)
  44. #else
  45. # define led_pin_on(pin) writePinHigh(pin)
  46. # define led_pin_off(pin) writePinLow(pin)
  47. #endif
  48. void led_matrix_pins_init_pins(void) {
  49. /* Set all pins to output, we are not interested in reading any information.
  50. */
  51. for (uint8_t x = 0; x < LED_DRIVER_LED_COUNT; x++) {
  52. setPinOutput(led_pins[x]);
  53. led_pin_off(led_pins[x]);
  54. }
  55. }
  56. void led_matrix_pins_set_value(int index, uint8_t value) {
  57. /* Set the brighness for a single LED.
  58. */
  59. if (index >= 0 && index < LED_DRIVER_LED_COUNT) {
  60. g_pwm_buffer[index] = value;
  61. }
  62. }
  63. void led_matrix_pins_set_value_all(uint8_t value) {
  64. /* Set the brighness for all LEDs.
  65. */
  66. for (int i = 0; i < LED_DRIVER_LED_COUNT; i++) {
  67. led_matrix_pins_set_value(i, value);
  68. }
  69. }
  70. void led_matrix_pins_flush(void) {
  71. /* This is a basic bit-banged pwm implementation.
  72. */
  73. for (uint8_t i = 0; i < LED_DRIVER_LED_COUNT; i++) {
  74. /* We spend ~1.3ms on each LED, dividing that time between lit and unlit.
  75. */
  76. if (g_pwm_buffer[i] > 0) {
  77. uint8_t brightness = pgm_read_byte(&CIE1931_CURVE[g_pwm_buffer[i]]) / 2;
  78. led_pin_on(led_pins[i]);
  79. led_wait_us(brightness);
  80. led_pin_off(led_pins[i]);
  81. led_wait_us(128 - brightness);
  82. } else {
  83. led_wait_us(128);
  84. }
  85. }
  86. }