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