module main author unknown version 1 0 description '' script 118 89 { comment '## NeoPixel-Streifen mit Grove-STecker ## Anschluss am Calliope über Grove A1 Grove-Kabel schwarz, rot, weiß, gelb rot 3.3 Volt, schwarz GND, gelb Daten für NeoPixel an Pin 16 ' neoPixelAttach 30 16 neoPixelSetAllToColor (colorSwatch 35 190 30 255) setNeoPixelColor 1 (colorSwatch 190 16 45 255) forever { waitMillis 500 rotateNeoPixelsBy 1 } } module NeoPixel Output author MicroBlocks version 1 15 description 'Control NeoPixel (WS2812) RGB LED strips and rings. ' variables _np_pixels _np_pin _np_haswhite spec ' ' 'neoPixelAttach' 'attach _ LED NeoPixel strip to pin _ : has white _' 'num auto bool' 10 '' false spec ' ' 'setNeoPixelColors10' 'set NeoPixels _ _ _ _ _ _ _ _ _ _' 'color color color color color color color color color color' spec ' ' 'setNeoPixelColors25' 'set NeoPixels #BR# _ _ _ _ _ #BR# _ _ _ _ _ #BR# _ _ _ _ _ #BR# _ _ _ _ _ #BR# _ _ _ _ _' 'color color color color color color color color color color color color color color color color color color color color color color color color color' spec ' ' 'clearNeoPixels' 'clear NeoPixels' spec ' ' 'neoPixelSetAllToColor' 'set all NeoPixels color _' 'color' spec ' ' 'setNeoPixelColor' 'set NeoPixel _ color _' 'num color' 1 space spec 'r' 'neoPixel_colorSwatch' '_' 'color' spec 'r' 'colorFromRGB' 'color r _ g _ b _ (0-255)' 'num num num' 0 100 100 spec 'r' 'randomColor' 'random color' space spec ' ' 'rotateNeoPixelsBy' 'rotate NeoPixels by _' 'auto' 1 space spec ' ' 'NeoPixel_brighten' 'brighten NeoPixel _ by _' 'num num' 1 10 spec ' ' 'NeoPixel_brighten_all' 'brighten all NeoPixels by _' 'num' 10 spec ' ' 'NeoPixel_shift_color' 'shift NeoPixel _ color by _' 'num num' 1 10 spec ' ' 'NeoPixel_shift_all_colors' 'shift all NeoPixel colors by _' 'num' 10 space spec ' ' '_NeoPixel_ensureInitialized' '_NeoPixel_ensureInitialized' spec ' ' '_NeoPixel_increaseRGB' '_NeoPixel_increaseRGB of _ by _' 'num num' 1 10 spec ' ' '_NeoPixel_rotate' '_NeoPixel_rotate_left _' 'bool' true spec ' ' '_NeoPixel_update' '_NeoPixel_update' spec ' ' '_NeoPixel_shift_hue' '_NeoPixel_shift_hue of _ by _' 'auto auto' '10' '10' to NeoPixel_brighten i delta { '_NeoPixel_increaseRGB' i delta '_NeoPixel_update' } to NeoPixel_brighten_all delta { for i (size _np_pixels) { '_NeoPixel_increaseRGB' i delta } '_NeoPixel_update' } to NeoPixel_shift_all_colors delta { for i (size _np_pixels) { '_NeoPixel_shift_hue' i delta } '_NeoPixel_update' } to NeoPixel_shift_color i delta { '_NeoPixel_shift_hue' i delta '_NeoPixel_update' } to '_NeoPixel_ensureInitialized' { if (_np_pixels == 0) {if (or ((boardType) == 'M5Atom-Matrix') (or ((boardType) == 'Mbits') ((boardType) == 'micro:STEAMakers'))) { neoPixelAttach 25 '' false } ((boardType) == 'D1-Mini') { comment 'D1 mini kit' neoPixelAttach 7 15 false } ((boardType) == 'Foxbit') { neoPixelAttach 35 '' false } ((boardType) == 'CodingBox') { neoPixelAttach 35 '' false } else { neoPixelAttach 10 '' false }} } to '_NeoPixel_increaseRGB' i delta { if (or (i < 1) (i > (size _np_pixels))) {return} local 'rgb' (at i _np_pixels) if (rgb != 0) { local 'h' ('[misc:hue]' rgb) local 's' ('[misc:saturation]' rgb) local 'v' (('[misc:brightness]' rgb) + delta) v = (maximum 20 (minimum v 100)) atPut i _np_pixels ('[misc:hsvColor]' h s v) } } to '_NeoPixel_rotate' left { '_NeoPixel_ensureInitialized' local 'length' (size _np_pixels) if left { local 'first' (at 1 _np_pixels) for i (length - 1) { atPut i _np_pixels (at (i + 1) _np_pixels) } atPut length _np_pixels first } else { local 'last' (at length _np_pixels) for i (length - 1) { atPut ((length - i) + 1) _np_pixels (at (length - i) _np_pixels) } atPut 1 _np_pixels last } } to '_NeoPixel_shift_hue' i delta { if (or (i < 1) (i > (size _np_pixels))) {return} local 'rgb' (at i _np_pixels) if (rgb != 0) { local 'h' ((('[misc:hue]' rgb) + delta) % 360) local 's' ('[misc:saturation]' rgb) local 'v' ('[misc:brightness]' rgb) atPut i _np_pixels ('[misc:hsvColor]' h s v) } } to '_NeoPixel_update' { comment 'NeoPixel pin and hasWhite may have been changed by another library.' '[display:neoPixelSetPin]' _np_pin _np_hasWhite '[display:neoPixelSend]' _np_pixels waitMicros 300 } to clearNeoPixels { '_NeoPixel_ensureInitialized' atPut 'all' _np_pixels 0 '_NeoPixel_update' } to colorFromRGB r g b { r = (maximum 0 (minimum r 255)) g = (maximum 0 (minimum g 255)) b = (maximum 0 (minimum b 255)) return (((r << 16) | (g << 8)) | b) } to neoPixelAttach number pinNumber optionalHasWhite { _np_pin = pinNumber _np_hasWhite = false if ((pushArgCount) > 2) { _np_hasWhite = optionalHasWhite } if (or (_np_pixels == 0) (number != (size _np_pixels))) { _np_pixels = (newList number) } atPut 'all' _np_pixels 0 '[display:neoPixelSetPin]' _np_pin _np_hasWhite } to neoPixelSetAllToColor color { '_NeoPixel_ensureInitialized' atPut 'all' _np_pixels color '_NeoPixel_update' } to neoPixel_colorSwatch color { return color } to randomColor { local 'n1' (random 100 200) local 'n2' (random 0 100) if (1 == (random 1 3)) { return ((n1 << 16) | (n2 << 8)) } (1 == (random 1 2)) { return ((n2 << 16) | n1) } else { return ((n1 << 8) | n2) } } to rotateNeoPixelsBy n { '_NeoPixel_ensureInitialized' local 'rotateLeft' (n < 0) if (or ((boardType) == 'CircuitPlayground') ((boardType) == 'CircuitPlayground Bluefruit')) { rotateLeft = (n > 0) } repeat (absoluteValue n) { '_NeoPixel_rotate' rotateLeft } '_NeoPixel_update' } to setNeoPixelColor i color { '_NeoPixel_ensureInitialized' if (and (1 <= i) (i <= (size _np_pixels))) { atPut i _np_pixels color '_NeoPixel_update' } } to setNeoPixelColors10 c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 { '_NeoPixel_ensureInitialized' for i (minimum (size _np_pixels) (pushArgCount)) { atPut i _np_pixels (getArg i) } '_NeoPixel_update' } to setNeoPixelColors25 c1 c2 c3 c4 c5 c6 c7 c8 c9 c10 c11 c12 c13 c14 c15 c16 c17 c18 c19 c20 c21 c22 c23 c24 c25 { '_NeoPixel_ensureInitialized' for i (minimum (size _np_pixels) (pushArgCount)) { atPut i _np_pixels (getArg i) } '_NeoPixel_update' }