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.

96 lines
3.7 KiB

  1. /*
  2. * APA102 lib V1.0a
  3. *
  4. * Controls APA102 RGB-LEDs
  5. * Author: Mikkel (Duckle29 on GitHub)
  6. *
  7. * Dec 22th, 2017 v1.0a Initial Version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include "apa102.h"
  23. #include <avr/interrupt.h>
  24. #include <avr/io.h>
  25. #include <util/delay.h>
  26. #include "debug.h"
  27. // Setleds for standard RGB
  28. void inline apa102_setleds(LED_TYPE *ledarray, uint16_t leds) { apa102_setleds_pin(ledarray, leds, _BV(RGB_DI_PIN & 0xF), _BV(RGB_CLK_PIN & 0xF)); }
  29. void static inline apa102_setleds_pin(LED_TYPE *ledarray, uint16_t leds, uint8_t pinmask_DI, uint8_t pinmask_CLK) {
  30. setPinOutput(RGB_DI_PIN);
  31. setPinOutput(RGB_CLK_PIN);
  32. apa102_send_array((uint8_t *)ledarray, leds)
  33. }
  34. void apa102_send_array(uint8_t *data, uint16_t leds) { // Data is struct of 3 bytes. RGB - leds is number of leds in data
  35. apa102_start_frame();
  36. while (leds--) {
  37. apa102_send_frame(0xFF000000 | (data->b << 16) | (data->g << 8) | data->r);
  38. data++;
  39. }
  40. apa102_end_frame(leds);
  41. }
  42. void apa102_send_frame(uint32_t frame) {
  43. for (uint32_t i = 0xFF; i > 0;) {
  44. apa102_send_byte(frame & i);
  45. i = i << 8;
  46. }
  47. }
  48. void apa102_start_frame() { apa102_send_frame(0); }
  49. void apa102_end_frame(uint16_t leds) {
  50. // This function has been taken from: https://github.com/pololu/apa102-arduino/blob/master/APA102.h
  51. // and adapted. The code is MIT licensed. I think thats compatible?
  52. // We need to send some more bytes to ensure that all the LEDs in the
  53. // chain see their new color and start displaying it.
  54. //
  55. // The data stream seen by the last LED in the chain will be delayed by
  56. // (count - 1) clock edges, because each LED before it inverts the clock
  57. // line and delays the data by one clock edge. Therefore, to make sure
  58. // the last LED actually receives the data we wrote, the number of extra
  59. // edges we send at the end of the frame must be at least (count - 1).
  60. // For the APA102C, that is sufficient.
  61. //
  62. // The SK9822 only updates after it sees 32 zero bits followed by one more
  63. // rising edge. To avoid having the update time depend on the color of
  64. // the last LED, we send a dummy 0xFF byte. (Unfortunately, this means
  65. // that partial updates of the beginning of an LED strip are not possible;
  66. // the LED after the last one you are trying to update will be black.)
  67. // After that, to ensure that the last LED in the chain sees 32 zero bits
  68. // and a rising edge, we need to send at least 65 + (count - 1) edges. It
  69. // is sufficent and simpler to just send (5 + count/16) bytes of zeros.
  70. //
  71. // We are ignoring the specification for the end frame in the APA102/SK9822
  72. // datasheets because it does not actually ensure that all the LEDs will
  73. // start displaying their new colors right away.
  74. apa102_send_byte(0xFF);
  75. for (uint16_t i = 0; i < 5 + leds / 16; i++) {
  76. apa102_send_byte(0);
  77. }
  78. }
  79. void apa102_send_byte(uint8_t byte) {
  80. uint8_t i;
  81. for (i = 0; i < 8; i++) {
  82. writePin(RGB_DI_PIN, !!(byte & (1 << (7 - i))));
  83. writePinHigh(RGB_CLK_PIN);
  84. }
  85. }