Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
WS281X.cpp@27:bc79f444883b, 2016-09-06 (annotated)
- Committer:
- mutech
- Date:
- Tue Sep 06 22:12:59 2016 +0000
- Revision:
- 27:bc79f444883b
- Parent:
- 24:f93a61e727a3
- Child:
- 28:b452e097da53
WS2811/WS2812 support Library;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mutech | 0:dff187a80020 | 1 | /* WS281X.cpp (for LPC82X/STM32F0x/STM32F746xx) |
| mutech | 0:dff187a80020 | 2 | * mbed Microcontroller Library |
| mutech | 0:dff187a80020 | 3 | * Copyright (c) 2016 muetch, t.kuroki, MIT License |
| mutech | 0:dff187a80020 | 4 | * |
| mutech | 0:dff187a80020 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| mutech | 0:dff187a80020 | 6 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
| mutech | 0:dff187a80020 | 7 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
| mutech | 0:dff187a80020 | 8 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
| mutech | 0:dff187a80020 | 9 | * furnished to do so, subject to the following conditions: |
| mutech | 0:dff187a80020 | 10 | * |
| mutech | 0:dff187a80020 | 11 | * The above copyright notice and this permission notice shall be included in all copies or |
| mutech | 0:dff187a80020 | 12 | * substantial portions of the Software. |
| mutech | 0:dff187a80020 | 13 | * |
| mutech | 0:dff187a80020 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| mutech | 0:dff187a80020 | 15 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| mutech | 0:dff187a80020 | 16 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| mutech | 0:dff187a80020 | 17 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| mutech | 0:dff187a80020 | 18 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| mutech | 27:bc79f444883b | 19 | * |
| mutech | 27:bc79f444883b | 20 | * Rev 0.97 2016-09-07 |
| mutech | 0:dff187a80020 | 21 | */ |
| mutech | 0:dff187a80020 | 22 | |
| mutech | 0:dff187a80020 | 23 | #include "WS281X.h" |
| mutech | 6:5aff0da4b663 | 24 | #if defined(TARGET_STM) |
| mutech | 0:dff187a80020 | 25 | #include "pinmap.h" |
| mutech | 6:5aff0da4b663 | 26 | #endif |
| mutech | 0:dff187a80020 | 27 | |
| mutech | 4:4af895bb2979 | 28 | // TARGET_STM32F7 |
| mutech | 0:dff187a80020 | 29 | // TARGET_DISCO_F746NG |
| mutech | 0:dff187a80020 | 30 | // TARGET_NUCLEO_F746ZG |
| mutech | 4:4af895bb2979 | 31 | // TARGET_STM32F0 |
| mutech | 0:dff187a80020 | 32 | // TARGET_NUCLEO_F030R8 |
| mutech | 0:dff187a80020 | 33 | // TARGET_NUCLEO_F070RB |
| mutech | 17:55c563d45bfa | 34 | // TARGET_NUCLEO_F072RB |
| mutech | 4:4af895bb2979 | 35 | // TARGET_LPC82X |
| mutech | 0:dff187a80020 | 36 | // TARGET_LPC824 |
| mutech | 0:dff187a80020 | 37 | |
| mutech | 0:dff187a80020 | 38 | //---------------------------------------------------------------------------- |
| mutech | 27:bc79f444883b | 39 | // 指定されたバッファの先頭からblock_size分をbuf_sizeが満杯になるまで繰り返しコピーする |
| mutech | 27:bc79f444883b | 40 | template <class T> |
| mutech | 27:bc79f444883b | 41 | static void repeat_buffer(T *buffer, int buf_size, int block_size = 1) |
| mutech | 27:bc79f444883b | 42 | { |
| mutech | 27:bc79f444883b | 43 | if (buffer && block_size > 0 && (uint16_t)block_size < buf_size) |
| mutech | 27:bc79f444883b | 44 | { |
| mutech | 27:bc79f444883b | 45 | T *dest = buffer + block_size; |
| mutech | 27:bc79f444883b | 46 | int left = buf_size - block_size; |
| mutech | 27:bc79f444883b | 47 | while (left > block_size) |
| mutech | 27:bc79f444883b | 48 | { |
| mutech | 27:bc79f444883b | 49 | memcpy(dest, buffer, block_size * sizeof(T)); |
| mutech | 27:bc79f444883b | 50 | dest += block_size; |
| mutech | 27:bc79f444883b | 51 | left -= block_size; |
| mutech | 27:bc79f444883b | 52 | block_size <<= 1; // 次回は2倍のサイズの転送 |
| mutech | 27:bc79f444883b | 53 | } |
| mutech | 27:bc79f444883b | 54 | memcpy(dest, buffer, left * sizeof(T)); |
| mutech | 27:bc79f444883b | 55 | } |
| mutech | 27:bc79f444883b | 56 | } |
| mutech | 27:bc79f444883b | 57 | |
| mutech | 27:bc79f444883b | 58 | //---------------------------------------------------------------------------- |
| mutech | 20:28fe0d0d081b | 59 | WS281X::WS281X(PinName wirePin, PinMode pinMode, int maxPixels, RGBOrder rgbOrder) |
| mutech | 9:087006b19049 | 60 | : _wirePin(wirePin), _gpio(), _buf_owner(false) |
| mutech | 9:087006b19049 | 61 | { |
| mutech | 9:087006b19049 | 62 | gpio_init_inout(&_gpio, wirePin, PIN_OUTPUT, pinMode, 0); |
| mutech | 9:087006b19049 | 63 | #if defined(TARGET_STM) |
| mutech | 9:087006b19049 | 64 | pin_mode_ex(wirePin, pinMode); |
| mutech | 9:087006b19049 | 65 | #endif |
| mutech | 9:087006b19049 | 66 | |
| mutech | 9:087006b19049 | 67 | setRGBOrder(rgbOrder); |
| mutech | 9:087006b19049 | 68 | _dummyPixel = 0; |
| mutech | 20:28fe0d0d081b | 69 | setPixelBuffer(0, maxPixels); |
| mutech | 9:087006b19049 | 70 | } |
| mutech | 9:087006b19049 | 71 | |
| mutech | 9:087006b19049 | 72 | WS281X::WS281X(PinName wirePin, PinMode pinMode, |
| mutech | 27:bc79f444883b | 73 | RGBColor *buffer, int maxPixels, RGBOrder rgbOrder) |
| mutech | 9:087006b19049 | 74 | : _wirePin(wirePin), _gpio(), _buf_owner(false) |
| mutech | 0:dff187a80020 | 75 | { |
| mutech | 0:dff187a80020 | 76 | gpio_init_inout(&_gpio, wirePin, PIN_OUTPUT, pinMode, 0); |
| mutech | 0:dff187a80020 | 77 | #if defined(TARGET_STM) |
| mutech | 0:dff187a80020 | 78 | pin_mode_ex(wirePin, pinMode); |
| mutech | 0:dff187a80020 | 79 | #endif |
| mutech | 0:dff187a80020 | 80 | |
| mutech | 0:dff187a80020 | 81 | setRGBOrder(rgbOrder); |
| mutech | 0:dff187a80020 | 82 | _dummyPixel = 0; |
| mutech | 27:bc79f444883b | 83 | setPixelBuffer(buffer, maxPixels); |
| mutech | 0:dff187a80020 | 84 | } |
| mutech | 0:dff187a80020 | 85 | |
| mutech | 0:dff187a80020 | 86 | WS281X::~WS281X() |
| mutech | 0:dff187a80020 | 87 | { |
| mutech | 9:087006b19049 | 88 | if (_buf_owner && _pixels) |
| mutech | 9:087006b19049 | 89 | delete[] _pixels; |
| mutech | 9:087006b19049 | 90 | } |
| mutech | 9:087006b19049 | 91 | |
| mutech | 27:bc79f444883b | 92 | void WS281X::setPixelBuffer(RGBColor *buffer, int maxPixels) |
| mutech | 9:087006b19049 | 93 | { |
| mutech | 9:087006b19049 | 94 | if (_buf_owner && _pixels) |
| mutech | 0:dff187a80020 | 95 | delete[] _pixels; |
| mutech | 9:087006b19049 | 96 | |
| mutech | 9:087006b19049 | 97 | _buf_owner = false; |
| mutech | 27:bc79f444883b | 98 | _pixels = buffer; |
| mutech | 9:087006b19049 | 99 | |
| mutech | 20:28fe0d0d081b | 100 | _maxPixels = (maxPixels < 0) ? 0 : (maxPixels > MAX_PIXELS) ? MAX_PIXELS : maxPixels; |
| mutech | 20:28fe0d0d081b | 101 | if (!_pixels && _maxPixels > 0) |
| mutech | 9:087006b19049 | 102 | { |
| mutech | 20:28fe0d0d081b | 103 | _pixels = new RGBColor[_maxPixels]; |
| mutech | 9:087006b19049 | 104 | _buf_owner = true; |
| mutech | 9:087006b19049 | 105 | } |
| mutech | 20:28fe0d0d081b | 106 | _numPixels = _maxPixels; |
| mutech | 20:28fe0d0d081b | 107 | clear(); |
| mutech | 20:28fe0d0d081b | 108 | } |
| mutech | 9:087006b19049 | 109 | |
| mutech | 27:bc79f444883b | 110 | int WS281X::numPixels(int value) |
| mutech | 20:28fe0d0d081b | 111 | { |
| mutech | 27:bc79f444883b | 112 | if (value >= 0) |
| mutech | 27:bc79f444883b | 113 | _numPixels = (value > _maxPixels) ? _maxPixels : value; |
| mutech | 27:bc79f444883b | 114 | return _numPixels; |
| mutech | 0:dff187a80020 | 115 | } |
| mutech | 0:dff187a80020 | 116 | |
| mutech | 0:dff187a80020 | 117 | #if defined(TARGET_STM) |
| mutech | 6:5aff0da4b663 | 118 | /** |
| mutech | 6:5aff0da4b663 | 119 | * Configure pin pull-up/pull-down/OpenDrain |
| mutech | 6:5aff0da4b663 | 120 | * typedef enum { |
| mutech | 6:5aff0da4b663 | 121 | * PullNone = 0, |
| mutech | 6:5aff0da4b663 | 122 | * PullUp = 1, |
| mutech | 6:5aff0da4b663 | 123 | * PullDown = 2, |
| mutech | 6:5aff0da4b663 | 124 | * OpenDrain = 3, |
| mutech | 6:5aff0da4b663 | 125 | * PullDefault = PullNone |
| mutech | 6:5aff0da4b663 | 126 | * } PinMode; |
| mutech | 6:5aff0da4b663 | 127 | */ |
| mutech | 0:dff187a80020 | 128 | void WS281X::pin_mode_ex(PinName pin, PinMode mode) |
| mutech | 0:dff187a80020 | 129 | { |
| mutech | 6:5aff0da4b663 | 130 | int port_index = STM_PORT(pin); |
| mutech | 6:5aff0da4b663 | 131 | int pin_index = STM_PIN(pin); |
| mutech | 6:5aff0da4b663 | 132 | int offset = pin_index << 1; |
| mutech | 6:5aff0da4b663 | 133 | GPIO_TypeDef * port_reg = ((GPIO_TypeDef *) (GPIOA_BASE + (port_index << 10))); |
| mutech | 6:5aff0da4b663 | 134 | |
| mutech | 6:5aff0da4b663 | 135 | // Configure pull-up/pull-down resistors |
| mutech | 6:5aff0da4b663 | 136 | uint32_t pupd = (uint32_t)mode & 3; |
| mutech | 6:5aff0da4b663 | 137 | if (pupd > 2) |
| mutech | 6:5aff0da4b663 | 138 | pupd = 0; // Open-drain = No pull-up/No pull-down |
| mutech | 6:5aff0da4b663 | 139 | |
| mutech | 0:dff187a80020 | 140 | if (mode == OpenDrain) |
| mutech | 0:dff187a80020 | 141 | { |
| mutech | 6:5aff0da4b663 | 142 | port_reg->PUPDR &= ~(0x3 << offset); // Open-drain = No pull-up/No pull-down |
| mutech | 6:5aff0da4b663 | 143 | port_reg->OTYPER |= 1 << pin_index; |
| mutech | 0:dff187a80020 | 144 | } |
| mutech | 0:dff187a80020 | 145 | else |
| mutech | 6:5aff0da4b663 | 146 | { |
| mutech | 6:5aff0da4b663 | 147 | port_reg->OTYPER &= ~(1 << pin_index); |
| mutech | 6:5aff0da4b663 | 148 | // pin_mode(pin, mode); |
| mutech | 6:5aff0da4b663 | 149 | port_reg->PUPDR &= ~(0x3 << offset); |
| mutech | 6:5aff0da4b663 | 150 | port_reg->PUPDR |= (mode & 0x03) << offset; |
| mutech | 6:5aff0da4b663 | 151 | } |
| mutech | 0:dff187a80020 | 152 | } |
| mutech | 0:dff187a80020 | 153 | #endif |
| mutech | 0:dff187a80020 | 154 | |
| mutech | 0:dff187a80020 | 155 | void WS281X::setRGBOrder(RGBOrder rgbOrder) |
| mutech | 0:dff187a80020 | 156 | { |
| mutech | 0:dff187a80020 | 157 | _rgbOrder = rgbOrder; |
| mutech | 0:dff187a80020 | 158 | switch(_rgbOrder) |
| mutech | 0:dff187a80020 | 159 | { |
| mutech | 6:5aff0da4b663 | 160 | case RGB: _1st = 0; _2nd = 1; _3rd = 2; break; |
| mutech | 6:5aff0da4b663 | 161 | case RBG: _1st = 0; _2nd = 2; _3rd = 1; break; |
| mutech | 6:5aff0da4b663 | 162 | case GRB: _1st = 1; _2nd = 0; _3rd = 2; break; |
| mutech | 6:5aff0da4b663 | 163 | case GBR: _1st = 2; _2nd = 0; _3rd = 1; break; |
| mutech | 6:5aff0da4b663 | 164 | case BRG: _1st = 1; _2nd = 2; _3rd = 0; break; |
| mutech | 6:5aff0da4b663 | 165 | case BGR: _1st = 2; _2nd = 1; _3rd = 0; break; |
| mutech | 6:5aff0da4b663 | 166 | default: _1st = 0; _2nd = 1; _3rd = 2; break; |
| mutech | 0:dff187a80020 | 167 | } |
| mutech | 0:dff187a80020 | 168 | } |
| mutech | 0:dff187a80020 | 169 | |
| mutech | 17:55c563d45bfa | 170 | //#define _nop1() __nop() |
| mutech | 19:48ac403f172f | 171 | #define _nop1() do {asm volatile ("nop"); } while(0) |
| mutech | 17:55c563d45bfa | 172 | #define _nop2() _nop1(); _nop1() |
| mutech | 0:dff187a80020 | 173 | #define _nop3() _nop1(); _nop2() |
| mutech | 0:dff187a80020 | 174 | #define _nop4() _nop2(); _nop2() |
| mutech | 0:dff187a80020 | 175 | #define _nop5() _nop1(); _nop4() |
| mutech | 0:dff187a80020 | 176 | #define _nop6() _nop2(); _nop4() |
| mutech | 0:dff187a80020 | 177 | #define _nop7() _nop3(); _nop4() |
| mutech | 0:dff187a80020 | 178 | #define _nop8() _nop4(); _nop4() |
| mutech | 0:dff187a80020 | 179 | #define _nop9() _nop1(); _nop8() |
| mutech | 0:dff187a80020 | 180 | #define _nop10() _nop2(); _nop8() |
| mutech | 0:dff187a80020 | 181 | #define _nop11() _nop3(); _nop8() |
| mutech | 0:dff187a80020 | 182 | #define _nop12() _nop4(); _nop8() |
| mutech | 0:dff187a80020 | 183 | #define _nop13() _nop5(); _nop8() |
| mutech | 0:dff187a80020 | 184 | #define _nop14() _nop6(); _nop8() |
| mutech | 0:dff187a80020 | 185 | #define _nop15() _nop7(); _nop8() |
| mutech | 0:dff187a80020 | 186 | #define _nop16() _nop8(); _nop8() |
| mutech | 0:dff187a80020 | 187 | |
| mutech | 4:4af895bb2979 | 188 | #if defined(TARGET_LPC82X) |
| mutech | 5:8e6835a94e10 | 189 | // LPCXpresso824-MAX (30MHz) |
| mutech | 0:dff187a80020 | 190 | #define DELAY_T0H() do{ _nop2(); }while(0) |
| mutech | 0:dff187a80020 | 191 | #define DELAY_T1H() do{ _nop6(); }while(0) |
| mutech | 0:dff187a80020 | 192 | #define DELAY_TLOW() do{ _nop6(); }while(0) |
| mutech | 0:dff187a80020 | 193 | #define DELAY_TLOW2() //do{ _nop2(); }while(0) |
| mutech | 4:4af895bb2979 | 194 | #define DELAY_SPACE() do{ _nop4(); }while(0) |
| mutech | 4:4af895bb2979 | 195 | #define DELAY_NEXT() //do{ _nop1(); }while(0) |
| mutech | 0:dff187a80020 | 196 | #endif |
| mutech | 0:dff187a80020 | 197 | |
| mutech | 0:dff187a80020 | 198 | #if defined(TARGET_STM32F0) |
| mutech | 5:8e6835a94e10 | 199 | // NUCLEO-F030R8 (48MHz) |
| mutech | 5:8e6835a94e10 | 200 | // NUCLEO-F070RB (48MHz) |
| mutech | 23:d4061c4b6238 | 201 | // NUCLEO-F072RB (48MHz) |
| mutech | 0:dff187a80020 | 202 | #define DELAY_T0H() do{ _nop8(); _nop4(); }while(0) |
| mutech | 0:dff187a80020 | 203 | #define DELAY_T1H() do{ _nop8(); _nop8(); }while(0) |
| mutech | 0:dff187a80020 | 204 | #define DELAY_TLOW() do{ _nop16(); }while(0) |
| mutech | 0:dff187a80020 | 205 | #define DELAY_TLOW2() //do{ _nop8(); _nop4(); }while(0) |
| mutech | 4:4af895bb2979 | 206 | #define DELAY_SPACE() do{ _nop8(); _nop6(); }while(0) |
| mutech | 4:4af895bb2979 | 207 | #define DELAY_NEXT() do{ _nop8(); }while(0) |
| mutech | 0:dff187a80020 | 208 | #endif |
| mutech | 0:dff187a80020 | 209 | |
| mutech | 6:5aff0da4b663 | 210 | #if defined(TARGET_NUCLEO_F446RE) |
| mutech | 6:5aff0da4b663 | 211 | // NUCLEO-F446RE (180MHz) |
| mutech | 6:5aff0da4b663 | 212 | #define USE_DELAYFUNC 1 |
| mutech | 6:5aff0da4b663 | 213 | #define T0H (18) |
| mutech | 6:5aff0da4b663 | 214 | #define T0L (58-T0H) |
| mutech | 6:5aff0da4b663 | 215 | #define T1H (40) |
| mutech | 6:5aff0da4b663 | 216 | #define T1L (58-T1H) |
| mutech | 6:5aff0da4b663 | 217 | |
| mutech | 6:5aff0da4b663 | 218 | #define DELAY_T0H() _delay(T0H) |
| mutech | 6:5aff0da4b663 | 219 | #define DELAY_T1H() _delay(T1H-T0H) |
| mutech | 6:5aff0da4b663 | 220 | #define DELAY_TLOW() _delay(T1L) |
| mutech | 6:5aff0da4b663 | 221 | #define DELAY_TLOW2() //DELAY_TLOW() |
| mutech | 6:5aff0da4b663 | 222 | #define DELAY_SPACE() _delay(T1L-2) |
| mutech | 6:5aff0da4b663 | 223 | #define DELAY_NEXT() _delay(16) |
| mutech | 6:5aff0da4b663 | 224 | #endif |
| mutech | 6:5aff0da4b663 | 225 | |
| mutech | 4:4af895bb2979 | 226 | #if defined(TARGET_NUCLEO_F746ZG) |
| mutech | 4:4af895bb2979 | 227 | // NUCLEO-F746ZG (216MHz) |
| mutech | 6:5aff0da4b663 | 228 | #define USE_DELAYFUNC 1 |
| mutech | 0:dff187a80020 | 229 | #define T0H (35) |
| mutech | 5:8e6835a94e10 | 230 | #define T0L (130-T0H) |
| mutech | 4:4af895bb2979 | 231 | #define T1H (75) |
| mutech | 5:8e6835a94e10 | 232 | #define T1L (130-T1H) |
| mutech | 0:dff187a80020 | 233 | |
| mutech | 0:dff187a80020 | 234 | #define DELAY_T0H() _delay(T0H) |
| mutech | 0:dff187a80020 | 235 | #define DELAY_T1H() _delay(T1H-T0H) |
| mutech | 0:dff187a80020 | 236 | #define DELAY_TLOW() _delay(T1L) |
| mutech | 0:dff187a80020 | 237 | #define DELAY_TLOW2() //DELAY_TLOW() |
| mutech | 5:8e6835a94e10 | 238 | #define DELAY_SPACE() _delay(T1L+20) |
| mutech | 4:4af895bb2979 | 239 | #define DELAY_NEXT() _delay(50) |
| mutech | 4:4af895bb2979 | 240 | #endif |
| mutech | 0:dff187a80020 | 241 | |
| mutech | 4:4af895bb2979 | 242 | #if defined(TARGET_DISCO_F746NG) |
| mutech | 4:4af895bb2979 | 243 | // TARGET_DISCO_F746NG (216MHz) |
| mutech | 6:5aff0da4b663 | 244 | #define USE_DELAYFUNC 1 |
| mutech | 4:4af895bb2979 | 245 | #define T0H (35) |
| mutech | 6:5aff0da4b663 | 246 | #define T0L (125-T0H) |
| mutech | 6:5aff0da4b663 | 247 | #define T1H (90) |
| mutech | 6:5aff0da4b663 | 248 | #define T1L (125-T1H) |
| mutech | 4:4af895bb2979 | 249 | |
| mutech | 4:4af895bb2979 | 250 | #define DELAY_T0H() _delay(T0H) |
| mutech | 4:4af895bb2979 | 251 | #define DELAY_T1H() _delay(T1H-T0H) |
| mutech | 4:4af895bb2979 | 252 | #define DELAY_TLOW() _delay(T1L) |
| mutech | 4:4af895bb2979 | 253 | #define DELAY_TLOW2() //DELAY_TLOW() |
| mutech | 6:5aff0da4b663 | 254 | #define DELAY_SPACE() _delay(T1L-5) |
| mutech | 6:5aff0da4b663 | 255 | #define DELAY_NEXT() _delay(40) |
| mutech | 4:4af895bb2979 | 256 | #endif |
| mutech | 4:4af895bb2979 | 257 | |
| mutech | 6:5aff0da4b663 | 258 | #if defined(USE_DELAYFUNC) && (USE_DELAYFUNC != 0) |
| mutech | 6:5aff0da4b663 | 259 | static inline __attribute__((always_inline)) |
| mutech | 6:5aff0da4b663 | 260 | void _delay(int value) |
| mutech | 0:dff187a80020 | 261 | { |
| mutech | 22:0846aefbeeae | 262 | do { _nop1(); } while (--value); |
| mutech | 0:dff187a80020 | 263 | } |
| mutech | 0:dff187a80020 | 264 | #endif |
| mutech | 0:dff187a80020 | 265 | |
| mutech | 0:dff187a80020 | 266 | inline __attribute__((always_inline)) |
| mutech | 0:dff187a80020 | 267 | void WS281X::writeByte(__IO regsize_t *reg_set, __IO regsize_t *reg_clr, regsize_t *mask, uint8_t value) |
| mutech | 0:dff187a80020 | 268 | { |
| mutech | 0:dff187a80020 | 269 | do |
| mutech | 0:dff187a80020 | 270 | { |
| mutech | 6:5aff0da4b663 | 271 | #if 1 |
| mutech | 0:dff187a80020 | 272 | // bit7 |
| mutech | 0:dff187a80020 | 273 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 274 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 275 | *reg_clr = mask[(value >> 7) & 1]; |
| mutech | 0:dff187a80020 | 276 | DELAY_T1H(); |
| mutech | 0:dff187a80020 | 277 | *reg_clr = mask[0]; |
| mutech | 0:dff187a80020 | 278 | DELAY_TLOW(); |
| mutech | 0:dff187a80020 | 279 | |
| mutech | 0:dff187a80020 | 280 | // bit6 |
| mutech | 0:dff187a80020 | 281 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 282 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 283 | *reg_clr = mask[(value >> 6) & 1]; |
| mutech | 0:dff187a80020 | 284 | DELAY_T1H(); |
| mutech | 0:dff187a80020 | 285 | *reg_clr = mask[0]; |
| mutech | 0:dff187a80020 | 286 | DELAY_TLOW(); |
| mutech | 0:dff187a80020 | 287 | |
| mutech | 0:dff187a80020 | 288 | // bit5 |
| mutech | 0:dff187a80020 | 289 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 290 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 291 | *reg_clr = mask[(value >> 5) & 1]; |
| mutech | 0:dff187a80020 | 292 | DELAY_T1H(); |
| mutech | 0:dff187a80020 | 293 | *reg_clr = mask[0]; |
| mutech | 0:dff187a80020 | 294 | DELAY_TLOW(); |
| mutech | 0:dff187a80020 | 295 | |
| mutech | 0:dff187a80020 | 296 | // bit4 |
| mutech | 0:dff187a80020 | 297 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 298 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 299 | *reg_clr = mask[(value >> 4) & 1]; |
| mutech | 0:dff187a80020 | 300 | DELAY_T1H(); |
| mutech | 0:dff187a80020 | 301 | *reg_clr = mask[0]; |
| mutech | 0:dff187a80020 | 302 | DELAY_TLOW(); |
| mutech | 6:5aff0da4b663 | 303 | #endif |
| mutech | 0:dff187a80020 | 304 | |
| mutech | 0:dff187a80020 | 305 | // bit3 |
| mutech | 0:dff187a80020 | 306 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 307 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 308 | *reg_clr = mask[(value >> 3) & 1]; |
| mutech | 0:dff187a80020 | 309 | DELAY_T1H(); |
| mutech | 0:dff187a80020 | 310 | *reg_clr = mask[0]; |
| mutech | 0:dff187a80020 | 311 | DELAY_TLOW(); |
| mutech | 0:dff187a80020 | 312 | |
| mutech | 0:dff187a80020 | 313 | // bit2 |
| mutech | 0:dff187a80020 | 314 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 315 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 316 | *reg_clr = mask[(value >> 2) & 1]; |
| mutech | 0:dff187a80020 | 317 | DELAY_T1H(); |
| mutech | 0:dff187a80020 | 318 | *reg_clr = mask[0]; |
| mutech | 0:dff187a80020 | 319 | DELAY_TLOW(); |
| mutech | 0:dff187a80020 | 320 | |
| mutech | 0:dff187a80020 | 321 | // bit1 |
| mutech | 0:dff187a80020 | 322 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 323 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 324 | *reg_clr = mask[(value >> 1) & 1]; |
| mutech | 0:dff187a80020 | 325 | DELAY_T1H(); |
| mutech | 0:dff187a80020 | 326 | *reg_clr = mask[0]; |
| mutech | 0:dff187a80020 | 327 | DELAY_TLOW(); |
| mutech | 0:dff187a80020 | 328 | |
| mutech | 0:dff187a80020 | 329 | // bit0 |
| mutech | 0:dff187a80020 | 330 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 331 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 332 | *reg_clr = mask[(value >> 0) & 1]; |
| mutech | 0:dff187a80020 | 333 | DELAY_T1H(); |
| mutech | 0:dff187a80020 | 334 | *reg_clr = mask[0]; |
| mutech | 0:dff187a80020 | 335 | DELAY_TLOW2(); |
| mutech | 0:dff187a80020 | 336 | |
| mutech | 0:dff187a80020 | 337 | } while (0); |
| mutech | 0:dff187a80020 | 338 | } |
| mutech | 0:dff187a80020 | 339 | |
| mutech | 0:dff187a80020 | 340 | void WS281X::show() |
| mutech | 0:dff187a80020 | 341 | { |
| mutech | 0:dff187a80020 | 342 | // CPU_FREQ = 30MHz -> 0.0333us/cycle |
| mutech | 0:dff187a80020 | 343 | // WS2811 0: 0.25us+1.0us, 1: 1.0us+0.25us |
| mutech | 0:dff187a80020 | 344 | // WS2812 0: 0.45us+0.8us, 1: 0.8us+0.45us |
| mutech | 0:dff187a80020 | 345 | |
| mutech | 9:087006b19049 | 346 | if (!_pixels) |
| mutech | 9:087006b19049 | 347 | return; |
| mutech | 9:087006b19049 | 348 | |
| mutech | 0:dff187a80020 | 349 | #if defined(TARGET_NXP) |
| mutech | 0:dff187a80020 | 350 | __IO uint32_t *reg_set = _gpio.reg_set; |
| mutech | 0:dff187a80020 | 351 | __IO uint32_t *reg_clr = _gpio.reg_clr; |
| mutech | 0:dff187a80020 | 352 | uint32_t mask[2] = { _gpio.mask, 0 }; |
| mutech | 0:dff187a80020 | 353 | #elif defined(TARGET_STM32F0) || defined(TARGET_STM32F1) |
| mutech | 0:dff187a80020 | 354 | __IO uint32_t *reg_set = _gpio.reg_set; |
| mutech | 0:dff187a80020 | 355 | __IO uint32_t *reg_clr = _gpio.reg_clr; |
| mutech | 0:dff187a80020 | 356 | uint32_t mask[2] = { _gpio.mask, 0 }; |
| mutech | 0:dff187a80020 | 357 | #elif defined(TARGET_STM) |
| mutech | 0:dff187a80020 | 358 | __IO uint16_t *reg_set = (__IO uint16_t *)_gpio.reg_set_clr; |
| mutech | 0:dff187a80020 | 359 | __IO uint16_t *reg_clr = reg_set + 1; |
| mutech | 0:dff187a80020 | 360 | uint16_t mask[2] = { _gpio.mask, 0 }; |
| mutech | 0:dff187a80020 | 361 | #endif |
| mutech | 0:dff187a80020 | 362 | |
| mutech | 0:dff187a80020 | 363 | uint8_t *pix = (uint8_t *)_pixels; |
| mutech | 0:dff187a80020 | 364 | uint8_t *end = pix + (_numPixels * sizeof(_pixels[0])); |
| mutech | 0:dff187a80020 | 365 | |
| mutech | 0:dff187a80020 | 366 | __disable_irq(); // Disable interrupts temporarily because we don't want our pulse timing to be messed up. |
| mutech | 0:dff187a80020 | 367 | |
| mutech | 0:dff187a80020 | 368 | uint8_t value; |
| mutech | 0:dff187a80020 | 369 | do |
| mutech | 0:dff187a80020 | 370 | { |
| mutech | 0:dff187a80020 | 371 | value = pix[_1st]; |
| mutech | 0:dff187a80020 | 372 | writeByte(reg_set, reg_clr, mask, value); |
| mutech | 0:dff187a80020 | 373 | DELAY_SPACE(); |
| mutech | 0:dff187a80020 | 374 | |
| mutech | 0:dff187a80020 | 375 | value = pix[_2nd]; |
| mutech | 0:dff187a80020 | 376 | writeByte(reg_set, reg_clr, mask, value); |
| mutech | 0:dff187a80020 | 377 | DELAY_SPACE(); |
| mutech | 0:dff187a80020 | 378 | |
| mutech | 0:dff187a80020 | 379 | value = pix[_3rd]; |
| mutech | 0:dff187a80020 | 380 | writeByte(reg_set, reg_clr, mask, value); |
| mutech | 0:dff187a80020 | 381 | pix += sizeof(_pixels[0]); |
| mutech | 0:dff187a80020 | 382 | DELAY_NEXT(); |
| mutech | 0:dff187a80020 | 383 | } while (pix < end); |
| mutech | 0:dff187a80020 | 384 | |
| mutech | 0:dff187a80020 | 385 | __enable_irq(); // Re-enable interrupts now that we are done. |
| mutech | 0:dff187a80020 | 386 | |
| mutech | 0:dff187a80020 | 387 | wait_us(50); |
| mutech | 0:dff187a80020 | 388 | } |
| mutech | 0:dff187a80020 | 389 | |
| mutech | 8:0617f524d67d | 390 | // 指定位置のピクセルへ色配列を指定サイズ分をコピーする |
| mutech | 27:bc79f444883b | 391 | void WS281X::setPixels(int index, RGBColor *color, int len) |
| mutech | 8:0617f524d67d | 392 | { |
| mutech | 22:0846aefbeeae | 393 | if (_pixels && len > 0 && (uint16_t)index < _numPixels) |
| mutech | 22:0846aefbeeae | 394 | { |
| mutech | 22:0846aefbeeae | 395 | if (index + len > _numPixels) |
| mutech | 22:0846aefbeeae | 396 | len = _numPixels - index; |
| mutech | 22:0846aefbeeae | 397 | memcpy(&_pixels[index], color, len * sizeof(_pixels[0])); |
| mutech | 22:0846aefbeeae | 398 | } |
| mutech | 21:77275089d837 | 399 | } |
| mutech | 21:77275089d837 | 400 | |
| mutech | 27:bc79f444883b | 401 | void WS281X::setPixels(int index, HSVColor *color, int len) |
| mutech | 21:77275089d837 | 402 | { |
| mutech | 22:0846aefbeeae | 403 | if (_pixels && len > 0 && (uint16_t)index < _numPixels) |
| mutech | 8:0617f524d67d | 404 | { |
| mutech | 22:0846aefbeeae | 405 | if (index + len > _numPixels) |
| mutech | 22:0846aefbeeae | 406 | len = _numPixels - index; |
| mutech | 24:f93a61e727a3 | 407 | RGBColor *dest = &_pixels[index]; |
| mutech | 22:0846aefbeeae | 408 | do |
| mutech | 22:0846aefbeeae | 409 | { |
| mutech | 24:f93a61e727a3 | 410 | *dest++ = *color++; |
| mutech | 22:0846aefbeeae | 411 | } while (--len); |
| mutech | 22:0846aefbeeae | 412 | } |
| mutech | 8:0617f524d67d | 413 | } |
| mutech | 8:0617f524d67d | 414 | |
| mutech | 27:bc79f444883b | 415 | // 指定色を指定位置のピクセルから指定サイズ分書き込む |
| mutech | 27:bc79f444883b | 416 | void WS281X::fillPixels(const RGBColor color, int index, int len) |
| mutech | 8:0617f524d67d | 417 | { |
| mutech | 24:f93a61e727a3 | 418 | if (_pixels && len > 0 && (uint16_t)index < _numPixels) |
| mutech | 8:0617f524d67d | 419 | { |
| mutech | 24:f93a61e727a3 | 420 | if (index + len > _numPixels) |
| mutech | 24:f93a61e727a3 | 421 | len = _numPixels - index; |
| mutech | 27:bc79f444883b | 422 | _pixels[index] = color; |
| mutech | 27:bc79f444883b | 423 | repeat_buffer<RGBColor>(_pixels + index, len, 1); |
| mutech | 24:f93a61e727a3 | 424 | } |
| mutech | 8:0617f524d67d | 425 | } |
| mutech | 8:0617f524d67d | 426 | |
| mutech | 27:bc79f444883b | 427 | // 指定位置のピクセルから指定色を指定サイズ分書き込む |
| mutech | 27:bc79f444883b | 428 | void WS281X::fillPixels(const HSVColor color, int index, int len) |
| mutech | 27:bc79f444883b | 429 | { |
| mutech | 27:bc79f444883b | 430 | fillPixels(color, index, len); |
| mutech | 27:bc79f444883b | 431 | } |
| mutech | 27:bc79f444883b | 432 | |
| mutech | 0:dff187a80020 | 433 | // 先頭から指定サイズ分のブロックをバッファの最後までコピーする |
| mutech | 27:bc79f444883b | 434 | void WS281X::repeatPixels(int block_size) |
| mutech | 0:dff187a80020 | 435 | { |
| mutech | 22:0846aefbeeae | 436 | if (_pixels && block_size > 0 && block_size < _numPixels) |
| mutech | 0:dff187a80020 | 437 | { |
| mutech | 27:bc79f444883b | 438 | repeat_buffer<RGBColor>(_pixels, _numPixels, block_size); |
| mutech | 0:dff187a80020 | 439 | } |
| mutech | 0:dff187a80020 | 440 | } |
| mutech | 0:dff187a80020 | 441 | |
| mutech | 27:bc79f444883b | 442 | void WS281X::repeatPixels(RGBColor *source, int size) |
| mutech | 24:f93a61e727a3 | 443 | { |
| mutech | 24:f93a61e727a3 | 444 | if (_pixels && source && size > 0) |
| mutech | 24:f93a61e727a3 | 445 | { |
| mutech | 24:f93a61e727a3 | 446 | if (size > _numPixels) |
| mutech | 24:f93a61e727a3 | 447 | size = _numPixels; |
| mutech | 24:f93a61e727a3 | 448 | memcpy(_pixels, source, size * sizeof(_pixels[0])); |
| mutech | 27:bc79f444883b | 449 | repeat_buffer<RGBColor>(_pixels, _numPixels, size); |
| mutech | 24:f93a61e727a3 | 450 | } |
| mutech | 24:f93a61e727a3 | 451 | } |
| mutech | 24:f93a61e727a3 | 452 | |
| mutech | 27:bc79f444883b | 453 | void WS281X::repeatPixels(HSVColor *source, int size) |
| mutech | 24:f93a61e727a3 | 454 | { |
| mutech | 24:f93a61e727a3 | 455 | if (_pixels && source && size > 0) |
| mutech | 24:f93a61e727a3 | 456 | { |
| mutech | 24:f93a61e727a3 | 457 | if (size > _numPixels) |
| mutech | 24:f93a61e727a3 | 458 | size = _numPixels; |
| mutech | 24:f93a61e727a3 | 459 | for (int i = 0; i < size; ++i) |
| mutech | 24:f93a61e727a3 | 460 | _pixels[i] = *source++; |
| mutech | 27:bc79f444883b | 461 | repeat_buffer<RGBColor>(_pixels, _numPixels, size); |
| mutech | 9:087006b19049 | 462 | } |
| mutech | 0:dff187a80020 | 463 | } |
| mutech | 0:dff187a80020 | 464 | |
| mutech | 0:dff187a80020 | 465 | // 指定色でバッファを埋めた後表示 |
| mutech | 8:0617f524d67d | 466 | void WS281X::show(const RGBColor color) |
| mutech | 0:dff187a80020 | 467 | { |
| mutech | 0:dff187a80020 | 468 | clear(color); |
| mutech | 0:dff187a80020 | 469 | show(); |
| mutech | 0:dff187a80020 | 470 | } |
| mutech | 0:dff187a80020 | 471 | |
| mutech | 0:dff187a80020 | 472 | //---------------------------------------------------------------------------- |