led_matrix_pinmatrix.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_pinmatrix.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. * FIXME: Map this from the wiring matrix to a physical location matrix at some point
  39. */
  40. uint8_t g_pwm_buffer[LED_MATRIX_ROWS * LED_MATRIX_COLS];
  41. const pin_t led_row_pins[LED_MATRIX_ROWS] = LED_MATRIX_ROW_PINS;
  42. const pin_t led_col_pins[LED_MATRIX_COLS] = LED_MATRIX_COL_PINS;
  43. void led_matrix_pinmatrix_init_pins(void) {
  44. /* Set all pins to output, we are not interested in reading any information.
  45. */
  46. for (uint8_t x = 0; x < LED_MATRIX_ROWS; x++) {
  47. setPinOutput(led_row_pins[x]);
  48. writePinLow(led_row_pins[x]);
  49. }
  50. for (uint8_t x = 0; x < LED_MATRIX_COLS; x++) {
  51. setPinOutput(led_col_pins[x]);
  52. writePinLow(led_col_pins[x]);
  53. }
  54. }
  55. void led_matrix_pinmatrix_set_value(int index, uint8_t value) {
  56. /* Set the brighness for a single LED.
  57. */
  58. if (index >= 0 && index < LED_DRIVER_LED_COUNT) {
  59. g_pwm_buffer[index] = value;
  60. }
  61. }
  62. void led_matrix_pinmatrix_set_value_all(uint8_t value) {
  63. /* Set the brighness for all LEDs.
  64. */
  65. for (int i = 0; i < LED_DRIVER_LED_COUNT; i++) {
  66. led_matrix_pinmatrix_set_value(i, value);
  67. }
  68. }
  69. void led_matrix_pinmatrix_flush(void) {
  70. /* This is a basic bit-banged pwm implementation.
  71. */
  72. uint8_t led_count = 0;
  73. for (uint8_t row = 0; row < LED_MATRIX_ROWS; row++) {
  74. writePinLow(led_row_pins[row]);
  75. for (uint8_t col = 0; col < LED_MATRIX_COLS; col++) {
  76. /* We spend ~128us on each LED, dividing that time between lit and unlit.
  77. */
  78. const uint8_t brightness = pgm_read_byte(&CIE1931_CURVE[g_pwm_buffer[led_count]]) / 2;
  79. if (brightness > 0) {
  80. writePinHigh(led_col_pins[col]);
  81. for (int i = 0; i < 128; i++) {
  82. if (i == brightness) {
  83. writePinLow(led_col_pins[col]);
  84. }
  85. led_wait_us(1);
  86. }
  87. } else {
  88. led_wait_us(128);
  89. }
  90. led_count++;
  91. }
  92. }
  93. }