(WIP) RGB controller on Arduino w/ sync to main PC system
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.

ws2812.cpp 942B

12345678910111213141516171819202122232425262728293031
  1. #include <Arduino.h>
  2. #include "ws2812.h"
  3. void postByte(unsigned char b, unsigned char portMask) {
  4. for (int i = 7; i >= 0; i--) {
  5. if ((b >> i) & 0x01) {
  6. PORTD |= portMask;
  7. __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
  8. PORTD &= ~portMask;
  9. __asm__("nop\n\t");
  10. } else {
  11. PORTD |= portMask;
  12. __asm__("nop\n\t");
  13. PORTD &= ~portMask;
  14. __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
  15. }
  16. }
  17. }
  18. void postLedUpdate(CRGB *leds, int len, int ledPin) {
  19. unsigned char portMask = 1 << ledPin;
  20. PORTD &= ~portMask;
  21. delayMicroseconds(9); // Force an update cycle by sending LOW for >= 9us
  22. cli();
  23. for (int i = 0; i < len; i++) {
  24. postByte(leds[i].g, portMask);
  25. postByte(leds[i].r, portMask);
  26. postByte(leds[i].b, portMask);
  27. }
  28. sei();
  29. }