|
| 1 | +/// |
| 2 | +/// @file rawWS2813C.cpp |
| 3 | +/// |
| 4 | +/// @brief Driver for WS2813C |
| 5 | +/// @version 103 |
| 6 | +/// @date 16 Feb 2024 |
| 7 | +/// |
| 8 | +/// @author Rei Vilo for Pervasive Displays https://www.pervasivedisplays.com |
| 9 | +/// @copyright Copyright (c) 2010-2025 Rei Vilo for Pervasive Displays |
| 10 | +/// @copyright License CC-BY-SA Creative Commons - Attribution - Share Alike |
| 11 | +/// https://creativecommons.org/licenses/by-sa/4.0/deed.en |
| 12 | +/// |
| 13 | + |
| 14 | +#include "rawWS2813C.h" |
| 15 | + |
| 16 | +#include <stdio.h> |
| 17 | +#include <stdint.h> |
| 18 | +#include <stdbool.h> |
| 19 | +#include "em_common.h" |
| 20 | +#include "em_gpio.h" |
| 21 | +#include "sl_udelay.h" |
| 22 | + |
| 23 | +/// |
| 24 | +/// @brief Define GPIO for WS2813C |
| 25 | +/// @param port port, gpioPortA..gpioPortD |
| 26 | +/// @param pin pin, O..16 according to port |
| 27 | +/// |
| 28 | +void wsDefine(uint8_t port, uint8_t pin); |
| 29 | + |
| 30 | +/// |
| 31 | +/// @brief Set RGB values |
| 32 | +/// @param red red component, 0..255 |
| 33 | +/// @param green green component, 0..255 |
| 34 | +/// @param blue blue component, 0..255 |
| 35 | +/// |
| 36 | +void wsWrite(uint8_t red, uint8_t green, uint8_t blue); |
| 37 | + |
| 38 | +uint8_t wsPort; |
| 39 | +uint8_t wsBit; |
| 40 | + |
| 41 | +void wsDefine(uint8_t port, uint8_t pin) |
| 42 | +{ |
| 43 | + wsPort = port; |
| 44 | + wsBit = pin; |
| 45 | + |
| 46 | + GPIO_PinModeSet(wsPort, wsBit, gpioModePushPull, 0); |
| 47 | + wsWrite(0, 0, 0); |
| 48 | +} |
| 49 | + |
| 50 | +void wsWrite(uint8_t red, uint8_t green, uint8_t blue) |
| 51 | +{ |
| 52 | + // uint32_t valueG70R70B70 = 0b111100001010101011001100; // 0xf0aacc |
| 53 | + uint32_t valueG70R70B70 = (green << 16) | (red << 8) | blue; |
| 54 | + uint32_t valueB07R07G07 = 0; |
| 55 | + |
| 56 | + for (uint8_t i = 0; i < 24; i += 1) |
| 57 | + { |
| 58 | + valueB07R07G07 <<= 1; |
| 59 | + valueB07R07G07 |= (valueG70R70B70 & 1); |
| 60 | + valueG70R70B70 >>= 1; |
| 61 | + } |
| 62 | + |
| 63 | + for (uint8_t i = 0; i < 24; i += 1) |
| 64 | + { |
| 65 | + if (valueB07R07G07 & 1) |
| 66 | + { |
| 67 | + // 1 code = T1H T1L |
| 68 | + // T1H 1-code, High-level time 580ns~1.6μs |
| 69 | + // T1L 1-code, Low-level time 220ns~420ns |
| 70 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 71 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 72 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 73 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 74 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 75 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 76 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 77 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 78 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 79 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 80 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 81 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 82 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 83 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns * 14 = 784 ns |
| 84 | + |
| 85 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 86 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 87 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 88 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 89 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 90 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns * 6 = 276 ns |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + // 0 code = T0H T0L |
| 95 | + // T0H 0-code, High-level time 220ns~380ns |
| 96 | + // T0L 0-code, Low-level time 580ns~1.6μs |
| 97 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 98 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 99 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 100 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns |
| 101 | + GPIO_PinOutSet(wsPort, wsBit); // 56 ns * 5 = 280 ns |
| 102 | + |
| 103 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 104 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 105 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 106 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 107 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 108 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 109 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 110 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 111 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 112 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 113 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 114 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 115 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 116 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns |
| 117 | + GPIO_PinOutClear(wsPort, wsBit); // 46 ns * 15 = 690 ns |
| 118 | + } |
| 119 | + valueB07R07G07 >>= 1; |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +rawWS2813C::rawWS2813C(uint32_t num_leds, pin_size_t pin) |
| 125 | +{ |
| 126 | + wsPinArduino = pin; |
| 127 | +} |
| 128 | + |
| 129 | +void rawWS2813C::begin() |
| 130 | +{ |
| 131 | + PinName wsName; |
| 132 | + |
| 133 | + wsName = pinToPinName(wsPinArduino); |
| 134 | + wsPort = getSilabsPortFromArduinoPin(wsName); |
| 135 | + wsBit = getSilabsPinFromArduinoPin(wsName); |
| 136 | + |
| 137 | + wsDefine(wsPort, wsBit); |
| 138 | +} |
| 139 | + |
| 140 | +void rawWS2813C::set_all(uint8_t red, uint8_t green, uint8_t blue) |
| 141 | +{ |
| 142 | + wsWrite(red, green, blue); |
| 143 | +}; |
0 commit comments