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@29:a362df191524, 2016-09-07 (annotated)
- Committer:
- mutech
- Date:
- Wed Sep 07 22:21:27 2016 +0000
- Revision:
- 29:a362df191524
- Parent:
- 28:b452e097da53
- Child:
- 32:64c391617f6c
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 | 28:b452e097da53 | 21 | * Rev 0.98 2016-09-08 |
| mutech | 0:dff187a80020 | 22 | */ |
| mutech | 0:dff187a80020 | 23 | |
| mutech | 0:dff187a80020 | 24 | #include "WS281X.h" |
| mutech | 6:5aff0da4b663 | 25 | #if defined(TARGET_STM) |
| mutech | 0:dff187a80020 | 26 | #include "pinmap.h" |
| mutech | 6:5aff0da4b663 | 27 | #endif |
| mutech | 0:dff187a80020 | 28 | |
| mutech | 4:4af895bb2979 | 29 | // TARGET_STM32F7 |
| mutech | 0:dff187a80020 | 30 | // TARGET_DISCO_F746NG |
| mutech | 0:dff187a80020 | 31 | // TARGET_NUCLEO_F746ZG |
| mutech | 4:4af895bb2979 | 32 | // TARGET_STM32F0 |
| mutech | 0:dff187a80020 | 33 | // TARGET_NUCLEO_F030R8 |
| mutech | 0:dff187a80020 | 34 | // TARGET_NUCLEO_F070RB |
| mutech | 17:55c563d45bfa | 35 | // TARGET_NUCLEO_F072RB |
| mutech | 4:4af895bb2979 | 36 | // TARGET_LPC82X |
| mutech | 0:dff187a80020 | 37 | // TARGET_LPC824 |
| mutech | 0:dff187a80020 | 38 | |
| mutech | 0:dff187a80020 | 39 | //---------------------------------------------------------------------------- |
| mutech | 27:bc79f444883b | 40 | // 指定されたバッファの先頭からblock_size分をbuf_sizeが満杯になるまで繰り返しコピーする |
| mutech | 27:bc79f444883b | 41 | template <class T> |
| mutech | 27:bc79f444883b | 42 | static void repeat_buffer(T *buffer, int buf_size, int block_size = 1) |
| mutech | 27:bc79f444883b | 43 | { |
| mutech | 27:bc79f444883b | 44 | if (buffer && block_size > 0 && (uint16_t)block_size < buf_size) |
| mutech | 27:bc79f444883b | 45 | { |
| mutech | 27:bc79f444883b | 46 | T *dest = buffer + block_size; |
| mutech | 27:bc79f444883b | 47 | int left = buf_size - block_size; |
| mutech | 27:bc79f444883b | 48 | while (left > block_size) |
| mutech | 27:bc79f444883b | 49 | { |
| mutech | 27:bc79f444883b | 50 | memcpy(dest, buffer, block_size * sizeof(T)); |
| mutech | 27:bc79f444883b | 51 | dest += block_size; |
| mutech | 27:bc79f444883b | 52 | left -= block_size; |
| mutech | 27:bc79f444883b | 53 | block_size <<= 1; // 次回は2倍のサイズの転送 |
| mutech | 27:bc79f444883b | 54 | } |
| mutech | 27:bc79f444883b | 55 | memcpy(dest, buffer, left * sizeof(T)); |
| mutech | 27:bc79f444883b | 56 | } |
| mutech | 27:bc79f444883b | 57 | } |
| mutech | 27:bc79f444883b | 58 | |
| mutech | 27:bc79f444883b | 59 | //---------------------------------------------------------------------------- |
| mutech | 28:b452e097da53 | 60 | WS281X::WS281X(PinName wirePin, PinMode pinMode, int maxPixels, RGBOrder order) |
| mutech | 9:087006b19049 | 61 | : _wirePin(wirePin), _gpio(), _buf_owner(false) |
| mutech | 9:087006b19049 | 62 | { |
| mutech | 9:087006b19049 | 63 | gpio_init_inout(&_gpio, wirePin, PIN_OUTPUT, pinMode, 0); |
| mutech | 9:087006b19049 | 64 | #if defined(TARGET_STM) |
| mutech | 9:087006b19049 | 65 | pin_mode_ex(wirePin, pinMode); |
| mutech | 9:087006b19049 | 66 | #endif |
| mutech | 9:087006b19049 | 67 | |
| mutech | 28:b452e097da53 | 68 | rgbOrder(order); |
| mutech | 9:087006b19049 | 69 | _dummyPixel = 0; |
| mutech | 20:28fe0d0d081b | 70 | setPixelBuffer(0, maxPixels); |
| mutech | 9:087006b19049 | 71 | } |
| mutech | 9:087006b19049 | 72 | |
| mutech | 9:087006b19049 | 73 | WS281X::WS281X(PinName wirePin, PinMode pinMode, |
| mutech | 28:b452e097da53 | 74 | RGBColor *buffer, int maxPixels, RGBOrder order) |
| mutech | 9:087006b19049 | 75 | : _wirePin(wirePin), _gpio(), _buf_owner(false) |
| mutech | 0:dff187a80020 | 76 | { |
| mutech | 0:dff187a80020 | 77 | gpio_init_inout(&_gpio, wirePin, PIN_OUTPUT, pinMode, 0); |
| mutech | 0:dff187a80020 | 78 | #if defined(TARGET_STM) |
| mutech | 0:dff187a80020 | 79 | pin_mode_ex(wirePin, pinMode); |
| mutech | 0:dff187a80020 | 80 | #endif |
| mutech | 0:dff187a80020 | 81 | |
| mutech | 28:b452e097da53 | 82 | rgbOrder(order); |
| mutech | 0:dff187a80020 | 83 | _dummyPixel = 0; |
| mutech | 27:bc79f444883b | 84 | setPixelBuffer(buffer, maxPixels); |
| mutech | 0:dff187a80020 | 85 | } |
| mutech | 0:dff187a80020 | 86 | |
| mutech | 0:dff187a80020 | 87 | WS281X::~WS281X() |
| mutech | 0:dff187a80020 | 88 | { |
| mutech | 9:087006b19049 | 89 | if (_buf_owner && _pixels) |
| mutech | 9:087006b19049 | 90 | delete[] _pixels; |
| mutech | 9:087006b19049 | 91 | } |
| mutech | 9:087006b19049 | 92 | |
| mutech | 27:bc79f444883b | 93 | void WS281X::setPixelBuffer(RGBColor *buffer, int maxPixels) |
| mutech | 9:087006b19049 | 94 | { |
| mutech | 9:087006b19049 | 95 | if (_buf_owner && _pixels) |
| mutech | 0:dff187a80020 | 96 | delete[] _pixels; |
| mutech | 9:087006b19049 | 97 | |
| mutech | 9:087006b19049 | 98 | _buf_owner = false; |
| mutech | 27:bc79f444883b | 99 | _pixels = buffer; |
| mutech | 9:087006b19049 | 100 | |
| mutech | 20:28fe0d0d081b | 101 | _maxPixels = (maxPixels < 0) ? 0 : (maxPixels > MAX_PIXELS) ? MAX_PIXELS : maxPixels; |
| mutech | 20:28fe0d0d081b | 102 | if (!_pixels && _maxPixels > 0) |
| mutech | 9:087006b19049 | 103 | { |
| mutech | 20:28fe0d0d081b | 104 | _pixels = new RGBColor[_maxPixels]; |
| mutech | 9:087006b19049 | 105 | _buf_owner = true; |
| mutech | 9:087006b19049 | 106 | } |
| mutech | 20:28fe0d0d081b | 107 | _numPixels = _maxPixels; |
| mutech | 20:28fe0d0d081b | 108 | clear(); |
| mutech | 20:28fe0d0d081b | 109 | } |
| mutech | 9:087006b19049 | 110 | |
| mutech | 27:bc79f444883b | 111 | int WS281X::numPixels(int value) |
| mutech | 20:28fe0d0d081b | 112 | { |
| mutech | 27:bc79f444883b | 113 | if (value >= 0) |
| mutech | 27:bc79f444883b | 114 | _numPixels = (value > _maxPixels) ? _maxPixels : value; |
| mutech | 27:bc79f444883b | 115 | return _numPixels; |
| mutech | 0:dff187a80020 | 116 | } |
| mutech | 0:dff187a80020 | 117 | |
| mutech | 0:dff187a80020 | 118 | #if defined(TARGET_STM) |
| mutech | 6:5aff0da4b663 | 119 | /** |
| mutech | 6:5aff0da4b663 | 120 | * Configure pin pull-up/pull-down/OpenDrain |
| mutech | 6:5aff0da4b663 | 121 | * typedef enum { |
| mutech | 6:5aff0da4b663 | 122 | * PullNone = 0, |
| mutech | 6:5aff0da4b663 | 123 | * PullUp = 1, |
| mutech | 6:5aff0da4b663 | 124 | * PullDown = 2, |
| mutech | 6:5aff0da4b663 | 125 | * OpenDrain = 3, |
| mutech | 6:5aff0da4b663 | 126 | * PullDefault = PullNone |
| mutech | 6:5aff0da4b663 | 127 | * } PinMode; |
| mutech | 6:5aff0da4b663 | 128 | */ |
| mutech | 0:dff187a80020 | 129 | void WS281X::pin_mode_ex(PinName pin, PinMode mode) |
| mutech | 0:dff187a80020 | 130 | { |
| mutech | 6:5aff0da4b663 | 131 | int port_index = STM_PORT(pin); |
| mutech | 6:5aff0da4b663 | 132 | int pin_index = STM_PIN(pin); |
| mutech | 6:5aff0da4b663 | 133 | int offset = pin_index << 1; |
| mutech | 6:5aff0da4b663 | 134 | GPIO_TypeDef * port_reg = ((GPIO_TypeDef *) (GPIOA_BASE + (port_index << 10))); |
| mutech | 6:5aff0da4b663 | 135 | |
| mutech | 6:5aff0da4b663 | 136 | // Configure pull-up/pull-down resistors |
| mutech | 6:5aff0da4b663 | 137 | uint32_t pupd = (uint32_t)mode & 3; |
| mutech | 6:5aff0da4b663 | 138 | if (pupd > 2) |
| mutech | 6:5aff0da4b663 | 139 | pupd = 0; // Open-drain = No pull-up/No pull-down |
| mutech | 6:5aff0da4b663 | 140 | |
| mutech | 0:dff187a80020 | 141 | if (mode == OpenDrain) |
| mutech | 0:dff187a80020 | 142 | { |
| mutech | 6:5aff0da4b663 | 143 | port_reg->PUPDR &= ~(0x3 << offset); // Open-drain = No pull-up/No pull-down |
| mutech | 6:5aff0da4b663 | 144 | port_reg->OTYPER |= 1 << pin_index; |
| mutech | 0:dff187a80020 | 145 | } |
| mutech | 0:dff187a80020 | 146 | else |
| mutech | 6:5aff0da4b663 | 147 | { |
| mutech | 6:5aff0da4b663 | 148 | port_reg->OTYPER &= ~(1 << pin_index); |
| mutech | 6:5aff0da4b663 | 149 | // pin_mode(pin, mode); |
| mutech | 6:5aff0da4b663 | 150 | port_reg->PUPDR &= ~(0x3 << offset); |
| mutech | 6:5aff0da4b663 | 151 | port_reg->PUPDR |= (mode & 0x03) << offset; |
| mutech | 6:5aff0da4b663 | 152 | } |
| mutech | 0:dff187a80020 | 153 | } |
| mutech | 0:dff187a80020 | 154 | #endif |
| mutech | 0:dff187a80020 | 155 | |
| mutech | 28:b452e097da53 | 156 | WS281X::RGBOrder WS281X::rgbOrder(WS281X::RGBOrder order) |
| mutech | 0:dff187a80020 | 157 | { |
| mutech | 29:a362df191524 | 158 | if (order != READ_ORDER) |
| mutech | 0:dff187a80020 | 159 | { |
| mutech | 29:a362df191524 | 160 | _rgbOrder = order; |
| mutech | 29:a362df191524 | 161 | switch(order) |
| mutech | 29:a362df191524 | 162 | { |
| mutech | 29:a362df191524 | 163 | case RGB: _1st = 0; _2nd = 1; _3rd = 2; break; // WS2811 |
| mutech | 29:a362df191524 | 164 | case RBG: _1st = 0; _2nd = 2; _3rd = 1; break; |
| mutech | 29:a362df191524 | 165 | case GRB: _1st = 1; _2nd = 0; _3rd = 2; break; // WS2812 |
| mutech | 29:a362df191524 | 166 | case GBR: _1st = 2; _2nd = 0; _3rd = 1; break; |
| mutech | 29:a362df191524 | 167 | case BRG: _1st = 1; _2nd = 2; _3rd = 0; break; |
| mutech | 29:a362df191524 | 168 | case BGR: _1st = 2; _2nd = 1; _3rd = 0; break; |
| mutech | 29:a362df191524 | 169 | default: |
| mutech | 29:a362df191524 | 170 | _1st = 0; _2nd = 1; _3rd = 2; |
| mutech | 29:a362df191524 | 171 | _rgbOrder = GRB; // WS2812 |
| mutech | 29:a362df191524 | 172 | break; |
| mutech | 29:a362df191524 | 173 | } |
| mutech | 0:dff187a80020 | 174 | } |
| mutech | 28:b452e097da53 | 175 | return _rgbOrder; |
| mutech | 0:dff187a80020 | 176 | } |
| mutech | 0:dff187a80020 | 177 | |
| mutech | 17:55c563d45bfa | 178 | //#define _nop1() __nop() |
| mutech | 19:48ac403f172f | 179 | #define _nop1() do {asm volatile ("nop"); } while(0) |
| mutech | 17:55c563d45bfa | 180 | #define _nop2() _nop1(); _nop1() |
| mutech | 0:dff187a80020 | 181 | #define _nop3() _nop1(); _nop2() |
| mutech | 0:dff187a80020 | 182 | #define _nop4() _nop2(); _nop2() |
| mutech | 0:dff187a80020 | 183 | #define _nop5() _nop1(); _nop4() |
| mutech | 0:dff187a80020 | 184 | #define _nop6() _nop2(); _nop4() |
| mutech | 0:dff187a80020 | 185 | #define _nop7() _nop3(); _nop4() |
| mutech | 0:dff187a80020 | 186 | #define _nop8() _nop4(); _nop4() |
| mutech | 0:dff187a80020 | 187 | #define _nop9() _nop1(); _nop8() |
| mutech | 0:dff187a80020 | 188 | #define _nop10() _nop2(); _nop8() |
| mutech | 0:dff187a80020 | 189 | #define _nop11() _nop3(); _nop8() |
| mutech | 0:dff187a80020 | 190 | #define _nop12() _nop4(); _nop8() |
| mutech | 0:dff187a80020 | 191 | #define _nop13() _nop5(); _nop8() |
| mutech | 0:dff187a80020 | 192 | #define _nop14() _nop6(); _nop8() |
| mutech | 0:dff187a80020 | 193 | #define _nop15() _nop7(); _nop8() |
| mutech | 0:dff187a80020 | 194 | #define _nop16() _nop8(); _nop8() |
| mutech | 0:dff187a80020 | 195 | |
| mutech | 4:4af895bb2979 | 196 | #if defined(TARGET_LPC82X) |
| mutech | 5:8e6835a94e10 | 197 | // LPCXpresso824-MAX (30MHz) |
| mutech | 0:dff187a80020 | 198 | #define DELAY_T0H() do{ _nop2(); }while(0) |
| mutech | 0:dff187a80020 | 199 | #define DELAY_T1H() do{ _nop6(); }while(0) |
| mutech | 0:dff187a80020 | 200 | #define DELAY_TLOW() do{ _nop6(); }while(0) |
| mutech | 0:dff187a80020 | 201 | #define DELAY_TLOW2() //do{ _nop2(); }while(0) |
| mutech | 4:4af895bb2979 | 202 | #define DELAY_SPACE() do{ _nop4(); }while(0) |
| mutech | 4:4af895bb2979 | 203 | #define DELAY_NEXT() //do{ _nop1(); }while(0) |
| mutech | 0:dff187a80020 | 204 | #endif |
| mutech | 0:dff187a80020 | 205 | |
| mutech | 0:dff187a80020 | 206 | #if defined(TARGET_STM32F0) |
| mutech | 5:8e6835a94e10 | 207 | // NUCLEO-F030R8 (48MHz) |
| mutech | 5:8e6835a94e10 | 208 | // NUCLEO-F070RB (48MHz) |
| mutech | 23:d4061c4b6238 | 209 | // NUCLEO-F072RB (48MHz) |
| mutech | 0:dff187a80020 | 210 | #define DELAY_T0H() do{ _nop8(); _nop4(); }while(0) |
| mutech | 0:dff187a80020 | 211 | #define DELAY_T1H() do{ _nop8(); _nop8(); }while(0) |
| mutech | 0:dff187a80020 | 212 | #define DELAY_TLOW() do{ _nop16(); }while(0) |
| mutech | 0:dff187a80020 | 213 | #define DELAY_TLOW2() //do{ _nop8(); _nop4(); }while(0) |
| mutech | 4:4af895bb2979 | 214 | #define DELAY_SPACE() do{ _nop8(); _nop6(); }while(0) |
| mutech | 4:4af895bb2979 | 215 | #define DELAY_NEXT() do{ _nop8(); }while(0) |
| mutech | 0:dff187a80020 | 216 | #endif |
| mutech | 0:dff187a80020 | 217 | |
| mutech | 6:5aff0da4b663 | 218 | #if defined(TARGET_NUCLEO_F446RE) |
| mutech | 6:5aff0da4b663 | 219 | // NUCLEO-F446RE (180MHz) |
| mutech | 6:5aff0da4b663 | 220 | #define USE_DELAYFUNC 1 |
| mutech | 6:5aff0da4b663 | 221 | #define T0H (18) |
| mutech | 6:5aff0da4b663 | 222 | #define T0L (58-T0H) |
| mutech | 6:5aff0da4b663 | 223 | #define T1H (40) |
| mutech | 6:5aff0da4b663 | 224 | #define T1L (58-T1H) |
| mutech | 6:5aff0da4b663 | 225 | |
| mutech | 6:5aff0da4b663 | 226 | #define DELAY_T0H() _delay(T0H) |
| mutech | 6:5aff0da4b663 | 227 | #define DELAY_T1H() _delay(T1H-T0H) |
| mutech | 6:5aff0da4b663 | 228 | #define DELAY_TLOW() _delay(T1L) |
| mutech | 6:5aff0da4b663 | 229 | #define DELAY_TLOW2() //DELAY_TLOW() |
| mutech | 6:5aff0da4b663 | 230 | #define DELAY_SPACE() _delay(T1L-2) |
| mutech | 6:5aff0da4b663 | 231 | #define DELAY_NEXT() _delay(16) |
| mutech | 6:5aff0da4b663 | 232 | #endif |
| mutech | 6:5aff0da4b663 | 233 | |
| mutech | 4:4af895bb2979 | 234 | #if defined(TARGET_NUCLEO_F746ZG) |
| mutech | 4:4af895bb2979 | 235 | // NUCLEO-F746ZG (216MHz) |
| mutech | 6:5aff0da4b663 | 236 | #define USE_DELAYFUNC 1 |
| mutech | 0:dff187a80020 | 237 | #define T0H (35) |
| mutech | 5:8e6835a94e10 | 238 | #define T0L (130-T0H) |
| mutech | 4:4af895bb2979 | 239 | #define T1H (75) |
| mutech | 5:8e6835a94e10 | 240 | #define T1L (130-T1H) |
| mutech | 0:dff187a80020 | 241 | |
| mutech | 0:dff187a80020 | 242 | #define DELAY_T0H() _delay(T0H) |
| mutech | 0:dff187a80020 | 243 | #define DELAY_T1H() _delay(T1H-T0H) |
| mutech | 0:dff187a80020 | 244 | #define DELAY_TLOW() _delay(T1L) |
| mutech | 0:dff187a80020 | 245 | #define DELAY_TLOW2() //DELAY_TLOW() |
| mutech | 5:8e6835a94e10 | 246 | #define DELAY_SPACE() _delay(T1L+20) |
| mutech | 4:4af895bb2979 | 247 | #define DELAY_NEXT() _delay(50) |
| mutech | 4:4af895bb2979 | 248 | #endif |
| mutech | 0:dff187a80020 | 249 | |
| mutech | 4:4af895bb2979 | 250 | #if defined(TARGET_DISCO_F746NG) |
| mutech | 4:4af895bb2979 | 251 | // TARGET_DISCO_F746NG (216MHz) |
| mutech | 6:5aff0da4b663 | 252 | #define USE_DELAYFUNC 1 |
| mutech | 4:4af895bb2979 | 253 | #define T0H (35) |
| mutech | 6:5aff0da4b663 | 254 | #define T0L (125-T0H) |
| mutech | 6:5aff0da4b663 | 255 | #define T1H (90) |
| mutech | 6:5aff0da4b663 | 256 | #define T1L (125-T1H) |
| mutech | 4:4af895bb2979 | 257 | |
| mutech | 4:4af895bb2979 | 258 | #define DELAY_T0H() _delay(T0H) |
| mutech | 4:4af895bb2979 | 259 | #define DELAY_T1H() _delay(T1H-T0H) |
| mutech | 4:4af895bb2979 | 260 | #define DELAY_TLOW() _delay(T1L) |
| mutech | 4:4af895bb2979 | 261 | #define DELAY_TLOW2() //DELAY_TLOW() |
| mutech | 6:5aff0da4b663 | 262 | #define DELAY_SPACE() _delay(T1L-5) |
| mutech | 6:5aff0da4b663 | 263 | #define DELAY_NEXT() _delay(40) |
| mutech | 4:4af895bb2979 | 264 | #endif |
| mutech | 4:4af895bb2979 | 265 | |
| mutech | 6:5aff0da4b663 | 266 | #if defined(USE_DELAYFUNC) && (USE_DELAYFUNC != 0) |
| mutech | 6:5aff0da4b663 | 267 | static inline __attribute__((always_inline)) |
| mutech | 6:5aff0da4b663 | 268 | void _delay(int value) |
| mutech | 0:dff187a80020 | 269 | { |
| mutech | 22:0846aefbeeae | 270 | do { _nop1(); } while (--value); |
| mutech | 0:dff187a80020 | 271 | } |
| mutech | 0:dff187a80020 | 272 | #endif |
| mutech | 0:dff187a80020 | 273 | |
| mutech | 0:dff187a80020 | 274 | inline __attribute__((always_inline)) |
| mutech | 0:dff187a80020 | 275 | void WS281X::writeByte(__IO regsize_t *reg_set, __IO regsize_t *reg_clr, regsize_t *mask, uint8_t value) |
| mutech | 0:dff187a80020 | 276 | { |
| mutech | 0:dff187a80020 | 277 | do |
| mutech | 0:dff187a80020 | 278 | { |
| mutech | 6:5aff0da4b663 | 279 | #if 1 |
| mutech | 0:dff187a80020 | 280 | // bit7 |
| mutech | 0:dff187a80020 | 281 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 282 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 283 | *reg_clr = mask[(value >> 7) & 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 | // bit6 |
| mutech | 0:dff187a80020 | 289 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 290 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 291 | *reg_clr = mask[(value >> 6) & 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 | // bit5 |
| mutech | 0:dff187a80020 | 297 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 298 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 299 | *reg_clr = mask[(value >> 5) & 1]; |
| mutech | 0:dff187a80020 | 300 | DELAY_T1H(); |
| mutech | 0:dff187a80020 | 301 | *reg_clr = mask[0]; |
| mutech | 0:dff187a80020 | 302 | DELAY_TLOW(); |
| mutech | 0:dff187a80020 | 303 | |
| mutech | 0:dff187a80020 | 304 | // bit4 |
| mutech | 0:dff187a80020 | 305 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 306 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 307 | *reg_clr = mask[(value >> 4) & 1]; |
| mutech | 0:dff187a80020 | 308 | DELAY_T1H(); |
| mutech | 0:dff187a80020 | 309 | *reg_clr = mask[0]; |
| mutech | 0:dff187a80020 | 310 | DELAY_TLOW(); |
| mutech | 6:5aff0da4b663 | 311 | #endif |
| mutech | 0:dff187a80020 | 312 | |
| mutech | 0:dff187a80020 | 313 | // bit3 |
| mutech | 0:dff187a80020 | 314 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 315 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 316 | *reg_clr = mask[(value >> 3) & 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 | // bit2 |
| mutech | 0:dff187a80020 | 322 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 323 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 324 | *reg_clr = mask[(value >> 2) & 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 | // bit1 |
| mutech | 0:dff187a80020 | 330 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 331 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 332 | *reg_clr = mask[(value >> 1) & 1]; |
| mutech | 0:dff187a80020 | 333 | DELAY_T1H(); |
| mutech | 0:dff187a80020 | 334 | *reg_clr = mask[0]; |
| mutech | 0:dff187a80020 | 335 | DELAY_TLOW(); |
| mutech | 0:dff187a80020 | 336 | |
| mutech | 0:dff187a80020 | 337 | // bit0 |
| mutech | 0:dff187a80020 | 338 | *reg_set = mask[0]; |
| mutech | 0:dff187a80020 | 339 | DELAY_T0H(); |
| mutech | 0:dff187a80020 | 340 | *reg_clr = mask[(value >> 0) & 1]; |
| mutech | 0:dff187a80020 | 341 | DELAY_T1H(); |
| mutech | 0:dff187a80020 | 342 | *reg_clr = mask[0]; |
| mutech | 0:dff187a80020 | 343 | DELAY_TLOW2(); |
| mutech | 0:dff187a80020 | 344 | |
| mutech | 0:dff187a80020 | 345 | } while (0); |
| mutech | 0:dff187a80020 | 346 | } |
| mutech | 0:dff187a80020 | 347 | |
| mutech | 0:dff187a80020 | 348 | void WS281X::show() |
| mutech | 0:dff187a80020 | 349 | { |
| mutech | 0:dff187a80020 | 350 | // CPU_FREQ = 30MHz -> 0.0333us/cycle |
| mutech | 0:dff187a80020 | 351 | // WS2811 0: 0.25us+1.0us, 1: 1.0us+0.25us |
| mutech | 0:dff187a80020 | 352 | // WS2812 0: 0.45us+0.8us, 1: 0.8us+0.45us |
| mutech | 0:dff187a80020 | 353 | |
| mutech | 9:087006b19049 | 354 | if (!_pixels) |
| mutech | 9:087006b19049 | 355 | return; |
| mutech | 9:087006b19049 | 356 | |
| mutech | 0:dff187a80020 | 357 | #if defined(TARGET_NXP) |
| mutech | 0:dff187a80020 | 358 | __IO uint32_t *reg_set = _gpio.reg_set; |
| mutech | 0:dff187a80020 | 359 | __IO uint32_t *reg_clr = _gpio.reg_clr; |
| mutech | 0:dff187a80020 | 360 | uint32_t mask[2] = { _gpio.mask, 0 }; |
| mutech | 0:dff187a80020 | 361 | #elif defined(TARGET_STM32F0) || defined(TARGET_STM32F1) |
| mutech | 0:dff187a80020 | 362 | __IO uint32_t *reg_set = _gpio.reg_set; |
| mutech | 0:dff187a80020 | 363 | __IO uint32_t *reg_clr = _gpio.reg_clr; |
| mutech | 0:dff187a80020 | 364 | uint32_t mask[2] = { _gpio.mask, 0 }; |
| mutech | 0:dff187a80020 | 365 | #elif defined(TARGET_STM) |
| mutech | 0:dff187a80020 | 366 | __IO uint16_t *reg_set = (__IO uint16_t *)_gpio.reg_set_clr; |
| mutech | 0:dff187a80020 | 367 | __IO uint16_t *reg_clr = reg_set + 1; |
| mutech | 0:dff187a80020 | 368 | uint16_t mask[2] = { _gpio.mask, 0 }; |
| mutech | 0:dff187a80020 | 369 | #endif |
| mutech | 0:dff187a80020 | 370 | |
| mutech | 0:dff187a80020 | 371 | uint8_t *pix = (uint8_t *)_pixels; |
| mutech | 0:dff187a80020 | 372 | uint8_t *end = pix + (_numPixels * sizeof(_pixels[0])); |
| mutech | 0:dff187a80020 | 373 | |
| mutech | 0:dff187a80020 | 374 | __disable_irq(); // Disable interrupts temporarily because we don't want our pulse timing to be messed up. |
| mutech | 0:dff187a80020 | 375 | |
| mutech | 0:dff187a80020 | 376 | uint8_t value; |
| mutech | 0:dff187a80020 | 377 | do |
| mutech | 0:dff187a80020 | 378 | { |
| mutech | 0:dff187a80020 | 379 | value = pix[_1st]; |
| mutech | 0:dff187a80020 | 380 | writeByte(reg_set, reg_clr, mask, value); |
| mutech | 0:dff187a80020 | 381 | DELAY_SPACE(); |
| mutech | 0:dff187a80020 | 382 | |
| mutech | 0:dff187a80020 | 383 | value = pix[_2nd]; |
| mutech | 0:dff187a80020 | 384 | writeByte(reg_set, reg_clr, mask, value); |
| mutech | 0:dff187a80020 | 385 | DELAY_SPACE(); |
| mutech | 0:dff187a80020 | 386 | |
| mutech | 0:dff187a80020 | 387 | value = pix[_3rd]; |
| mutech | 0:dff187a80020 | 388 | writeByte(reg_set, reg_clr, mask, value); |
| mutech | 0:dff187a80020 | 389 | pix += sizeof(_pixels[0]); |
| mutech | 0:dff187a80020 | 390 | DELAY_NEXT(); |
| mutech | 0:dff187a80020 | 391 | } while (pix < end); |
| mutech | 0:dff187a80020 | 392 | |
| mutech | 0:dff187a80020 | 393 | __enable_irq(); // Re-enable interrupts now that we are done. |
| mutech | 0:dff187a80020 | 394 | |
| mutech | 0:dff187a80020 | 395 | wait_us(50); |
| mutech | 0:dff187a80020 | 396 | } |
| mutech | 0:dff187a80020 | 397 | |
| mutech | 8:0617f524d67d | 398 | // 指定位置のピクセルへ色配列を指定サイズ分をコピーする |
| mutech | 27:bc79f444883b | 399 | void WS281X::setPixels(int index, RGBColor *color, int len) |
| mutech | 8:0617f524d67d | 400 | { |
| mutech | 22:0846aefbeeae | 401 | if (_pixels && len > 0 && (uint16_t)index < _numPixels) |
| mutech | 22:0846aefbeeae | 402 | { |
| mutech | 22:0846aefbeeae | 403 | if (index + len > _numPixels) |
| mutech | 22:0846aefbeeae | 404 | len = _numPixels - index; |
| mutech | 22:0846aefbeeae | 405 | memcpy(&_pixels[index], color, len * sizeof(_pixels[0])); |
| mutech | 22:0846aefbeeae | 406 | } |
| mutech | 21:77275089d837 | 407 | } |
| mutech | 21:77275089d837 | 408 | |
| mutech | 27:bc79f444883b | 409 | void WS281X::setPixels(int index, HSVColor *color, int len) |
| mutech | 21:77275089d837 | 410 | { |
| mutech | 22:0846aefbeeae | 411 | if (_pixels && len > 0 && (uint16_t)index < _numPixels) |
| mutech | 8:0617f524d67d | 412 | { |
| mutech | 22:0846aefbeeae | 413 | if (index + len > _numPixels) |
| mutech | 22:0846aefbeeae | 414 | len = _numPixels - index; |
| mutech | 24:f93a61e727a3 | 415 | RGBColor *dest = &_pixels[index]; |
| mutech | 22:0846aefbeeae | 416 | do |
| mutech | 22:0846aefbeeae | 417 | { |
| mutech | 24:f93a61e727a3 | 418 | *dest++ = *color++; |
| mutech | 22:0846aefbeeae | 419 | } while (--len); |
| mutech | 22:0846aefbeeae | 420 | } |
| mutech | 8:0617f524d67d | 421 | } |
| mutech | 8:0617f524d67d | 422 | |
| mutech | 27:bc79f444883b | 423 | // 指定色を指定位置のピクセルから指定サイズ分書き込む |
| mutech | 28:b452e097da53 | 424 | void WS281X::fillPixels(int index, const RGBColor color, int len) |
| mutech | 8:0617f524d67d | 425 | { |
| mutech | 24:f93a61e727a3 | 426 | if (_pixels && len > 0 && (uint16_t)index < _numPixels) |
| mutech | 8:0617f524d67d | 427 | { |
| mutech | 24:f93a61e727a3 | 428 | if (index + len > _numPixels) |
| mutech | 24:f93a61e727a3 | 429 | len = _numPixels - index; |
| mutech | 27:bc79f444883b | 430 | _pixels[index] = color; |
| mutech | 27:bc79f444883b | 431 | repeat_buffer<RGBColor>(_pixels + index, len, 1); |
| mutech | 24:f93a61e727a3 | 432 | } |
| mutech | 8:0617f524d67d | 433 | } |
| mutech | 8:0617f524d67d | 434 | |
| mutech | 27:bc79f444883b | 435 | // 指定位置のピクセルから指定色を指定サイズ分書き込む |
| mutech | 28:b452e097da53 | 436 | void WS281X::fillPixels(int index, const HSVColor color, int len) |
| mutech | 27:bc79f444883b | 437 | { |
| mutech | 28:b452e097da53 | 438 | fillPixels(index, color, len); |
| mutech | 27:bc79f444883b | 439 | } |
| mutech | 27:bc79f444883b | 440 | |
| mutech | 0:dff187a80020 | 441 | // 先頭から指定サイズ分のブロックをバッファの最後までコピーする |
| mutech | 27:bc79f444883b | 442 | void WS281X::repeatPixels(int block_size) |
| mutech | 0:dff187a80020 | 443 | { |
| mutech | 22:0846aefbeeae | 444 | if (_pixels && block_size > 0 && block_size < _numPixels) |
| mutech | 0:dff187a80020 | 445 | { |
| mutech | 27:bc79f444883b | 446 | repeat_buffer<RGBColor>(_pixels, _numPixels, block_size); |
| mutech | 0:dff187a80020 | 447 | } |
| mutech | 0:dff187a80020 | 448 | } |
| mutech | 0:dff187a80020 | 449 | |
| mutech | 27:bc79f444883b | 450 | void WS281X::repeatPixels(RGBColor *source, int size) |
| mutech | 24:f93a61e727a3 | 451 | { |
| mutech | 24:f93a61e727a3 | 452 | if (_pixels && source && size > 0) |
| mutech | 24:f93a61e727a3 | 453 | { |
| mutech | 24:f93a61e727a3 | 454 | if (size > _numPixels) |
| mutech | 24:f93a61e727a3 | 455 | size = _numPixels; |
| mutech | 24:f93a61e727a3 | 456 | memcpy(_pixels, source, size * sizeof(_pixels[0])); |
| mutech | 27:bc79f444883b | 457 | repeat_buffer<RGBColor>(_pixels, _numPixels, size); |
| mutech | 24:f93a61e727a3 | 458 | } |
| mutech | 24:f93a61e727a3 | 459 | } |
| mutech | 24:f93a61e727a3 | 460 | |
| mutech | 27:bc79f444883b | 461 | void WS281X::repeatPixels(HSVColor *source, int size) |
| mutech | 24:f93a61e727a3 | 462 | { |
| mutech | 24:f93a61e727a3 | 463 | if (_pixels && source && size > 0) |
| mutech | 24:f93a61e727a3 | 464 | { |
| mutech | 24:f93a61e727a3 | 465 | if (size > _numPixels) |
| mutech | 24:f93a61e727a3 | 466 | size = _numPixels; |
| mutech | 24:f93a61e727a3 | 467 | for (int i = 0; i < size; ++i) |
| mutech | 24:f93a61e727a3 | 468 | _pixels[i] = *source++; |
| mutech | 27:bc79f444883b | 469 | repeat_buffer<RGBColor>(_pixels, _numPixels, size); |
| mutech | 9:087006b19049 | 470 | } |
| mutech | 0:dff187a80020 | 471 | } |
| mutech | 0:dff187a80020 | 472 | |
| mutech | 28:b452e097da53 | 473 | void WS281X::makeGradation(int index, RGBColor from, RGBColor to, int len) |
| mutech | 28:b452e097da53 | 474 | { |
| mutech | 28:b452e097da53 | 475 | if (!_pixels || len < 1 || index >= _numPixels || (index + len) < 0) |
| mutech | 28:b452e097da53 | 476 | return; |
| mutech | 28:b452e097da53 | 477 | |
| mutech | 28:b452e097da53 | 478 | int end = len; |
| mutech | 28:b452e097da53 | 479 | if (index + end > _numPixels) |
| mutech | 28:b452e097da53 | 480 | end = _numPixels - index; |
| mutech | 28:b452e097da53 | 481 | |
| mutech | 28:b452e097da53 | 482 | RGBColor color; |
| mutech | 28:b452e097da53 | 483 | RGBColor *dest = _pixels; |
| mutech | 28:b452e097da53 | 484 | if (index > 0) |
| mutech | 28:b452e097da53 | 485 | dest += index; |
| mutech | 28:b452e097da53 | 486 | for (int i = (index < 0) ? -index : 0; i < end; ++i) |
| mutech | 28:b452e097da53 | 487 | { |
| mutech | 28:b452e097da53 | 488 | int j = len - i; |
| mutech | 28:b452e097da53 | 489 | color.red = ((from.red * j) + (to.red * i)) / len; |
| mutech | 28:b452e097da53 | 490 | color.green = ((from.green * j) + (to.green * i)) / len; |
| mutech | 28:b452e097da53 | 491 | color.blue = ((from.blue * j) + (to.blue * i)) / len; |
| mutech | 28:b452e097da53 | 492 | *dest++ = GammaColor(color); |
| mutech | 28:b452e097da53 | 493 | } |
| mutech | 28:b452e097da53 | 494 | } |
| mutech | 28:b452e097da53 | 495 | |
| mutech | 28:b452e097da53 | 496 | void WS281X::makeRainbow(int index, HSVColor color, int len, int direction) |
| mutech | 28:b452e097da53 | 497 | { |
| mutech | 28:b452e097da53 | 498 | if (!_pixels || len < 1 || index >= _numPixels || (index + len) < 0) |
| mutech | 28:b452e097da53 | 499 | return; |
| mutech | 28:b452e097da53 | 500 | |
| mutech | 28:b452e097da53 | 501 | int end = len; |
| mutech | 28:b452e097da53 | 502 | if (index + end > _numPixels) |
| mutech | 28:b452e097da53 | 503 | end = _numPixels - index; |
| mutech | 28:b452e097da53 | 504 | |
| mutech | 28:b452e097da53 | 505 | HSVColor hsv(color); |
| mutech | 28:b452e097da53 | 506 | RGBColor *dest = _pixels; |
| mutech | 28:b452e097da53 | 507 | if (index > 0) |
| mutech | 28:b452e097da53 | 508 | dest += index; |
| mutech | 28:b452e097da53 | 509 | direction = (direction >= 0) ? -3600 : 3600; |
| mutech | 28:b452e097da53 | 510 | for (int i = (index < 0) ? -index : 0; i < end; ++i) |
| mutech | 28:b452e097da53 | 511 | { |
| mutech | 28:b452e097da53 | 512 | hsv.hue = color.hue + direction * i / len; |
| mutech | 28:b452e097da53 | 513 | *dest++ = GammaColor(hsv); |
| mutech | 28:b452e097da53 | 514 | } |
| mutech | 28:b452e097da53 | 515 | } |
| mutech | 28:b452e097da53 | 516 | |
| mutech | 0:dff187a80020 | 517 | // 指定色でバッファを埋めた後表示 |
| mutech | 8:0617f524d67d | 518 | void WS281X::show(const RGBColor color) |
| mutech | 0:dff187a80020 | 519 | { |
| mutech | 0:dff187a80020 | 520 | clear(color); |
| mutech | 0:dff187a80020 | 521 | show(); |
| mutech | 0:dff187a80020 | 522 | } |
| mutech | 0:dff187a80020 | 523 | |
| mutech | 0:dff187a80020 | 524 | //---------------------------------------------------------------------------- |