|
| 1 | +#include <Arduino.h> |
| 2 | +#include <stdint.h> |
| 3 | +#include "targets.h" |
| 4 | + |
| 5 | +#if (GPIO_PIN_LED_WS2812 != UNDEF_PIN) && (GPIO_PIN_LED_WS2812_FAST != UNDEF_PIN) |
| 6 | +#define WS2812_LED_IS_USED 1 |
| 7 | + |
| 8 | +static inline void LEDsend_1(void) { |
| 9 | + digitalWriteFast(GPIO_PIN_LED_WS2812_FAST, HIGH); |
| 10 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 11 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 12 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 13 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 14 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 15 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 16 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 17 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 18 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 19 | + digitalWriteFast(GPIO_PIN_LED_WS2812_FAST, LOW); |
| 20 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 21 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 22 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 23 | + __NOP(); __NOP(); __NOP(); |
| 24 | +} |
| 25 | + |
| 26 | +static inline void LEDsend_0(void) { |
| 27 | + digitalWriteFast(GPIO_PIN_LED_WS2812_FAST, HIGH); |
| 28 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 29 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 30 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 31 | + __NOP(); __NOP(); __NOP(); |
| 32 | + digitalWriteFast(GPIO_PIN_LED_WS2812_FAST, LOW); |
| 33 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 34 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 35 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 36 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 37 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 38 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 39 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 40 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 41 | + __NOP(); __NOP(); __NOP(); __NOP(); __NOP(); |
| 42 | +} |
| 43 | + |
| 44 | +static inline uint32_t bitReverse(uint8_t input) |
| 45 | +{ |
| 46 | + uint8_t r = input; // r will be reversed bits of v; first get LSB of v |
| 47 | + uint8_t s = 8 - 1; // extra shift needed at end |
| 48 | + |
| 49 | + for (input >>= 1; input; input >>= 1) |
| 50 | + { |
| 51 | + r <<= 1; |
| 52 | + r |= input & 1; |
| 53 | + s--; |
| 54 | + } |
| 55 | + r <<= s; // shift when input's highest bits are zero |
| 56 | + return r; |
| 57 | +} |
| 58 | + |
| 59 | +void WS281BsetLED(uint8_t const * const RGB) // takes RGB data |
| 60 | +{ |
| 61 | + pinMode(GPIO_PIN_LED_WS2812, OUTPUT); |
| 62 | + |
| 63 | + uint32_t LedColourData = |
| 64 | + bitReverse(RGB[1]) + // Green |
| 65 | + (bitReverse(RGB[0]) << 8) + // Red |
| 66 | + (bitReverse(RGB[2]) << 16); // Blue |
| 67 | + uint8_t bits = 24; |
| 68 | + while (bits--) |
| 69 | + { |
| 70 | + (LedColourData & 0x1) ? LEDsend_1() : LEDsend_0(); |
| 71 | + LedColourData >>= 1; |
| 72 | + } |
| 73 | + delayMicroseconds(50); // needed to latch in the values |
| 74 | +} |
| 75 | + |
| 76 | +void WS281BsetLED(uint8_t const r, uint8_t const g, uint8_t const b) // takes RGB data |
| 77 | +{ |
| 78 | + uint8_t data[3] = {r, g, b}; |
| 79 | + WS281BsetLED(data); |
| 80 | +} |
| 81 | + |
| 82 | +void WS281BsetLED(uint32_t color) // takes RGB data |
| 83 | +{ |
| 84 | + uint8_t data[3]; |
| 85 | + data[0] = (color & 0x00FF0000) >> 16; |
| 86 | + data[1] = (color & 0x0000FF00) >> 8; |
| 87 | + data[2] = (color & 0x000000FF) >> 0; |
| 88 | + WS281BsetLED(data); |
| 89 | +} |
| 90 | + |
| 91 | +#endif /* (GPIO_PIN_LED_WS2812 != UNDEF_PIN) && (GPIO_PIN_LED_WS2812_FAST != UNDEF_PIN) */ |
0 commit comments