module main author unknown version 1 0 description '' script 65 61 { comment '## NeoPixel-Ring mit Grove-Anschluss ## Der Ring hat 16 LEDs. Der NeoPixel-Rin muss an den Grove-Anschluss A1 rot 3.3 Volt, schwarz GND, gelb Datenleitung an Pin 16 ' neoPixelAttach 16 16 clearNeoPixels setNeoPixelColor 1 (colorSwatch 35 190 30 255) forever { waitMillis 100 rotateNeoPixelsBy 1 } } script 937 512 { for i 64 {} } 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 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' } module TFT Output author MicroBlocks version 1 12 description 'Draw graphics and write text on boards with a TFT display, such as the M5Stack, M5Stick, Citilab ED1 or (discontinued) IoT-Bus.' spec ' ' '[tft:clear]' 'clear TFT display' space spec ' ' '[tft:rect]' 'draw rectangle on TFT at x _ y _ width _ height _ color _ : filled _' 'num num num num color bool' 10 10 40 30 nil true spec ' ' '[tft:roundedRect]' 'draw rounded rectangle on TFT at x _ y _ width _ height _ radius _ color _ : filled _' 'num num num num num color bool' 10 10 40 30 8 nil true spec ' ' '[tft:circle]' 'draw circle on TFT at x _ y _ radius _ color _ : filled _' 'num num num color bool' 40 40 30 nil true spec ' ' '[tft:triangle]' 'draw triangle on TFT at x _ y _ , x _ y _ , x _ y _ color _ : filled _' 'num num num num num num color bool' 20 20 30 80 60 5 nil true spec ' ' '[tft:line]' 'draw line on TFT from x _ y _ to x _ y _ color _' 'num num num num color' 12 8 25 15 spec ' ' 'tft_drawVector' 'draw vector x _ y _ angle _ length _ color _' 'num num num num color' 40 40 45 40 space spec ' ' '[tft:text]' 'write _ on TFT at x _ y _ color _ : scale _ wrap _ : bg color _' 'str num num color num bool color' 'Hello World!' 5 5 nil 2 true spec ' ' 'tft_drawText' 'draw text _ on TFT at x _ y _ color _ : scale _ : bg color _' 'str num num color num color' 'Line 1 Line 2' 50 20 nil 2 space spec ' ' '[tft:setPixel]' 'set TFT pixel x _ y _ to _' 'num num color' 10 10 spec ' ' '[tft:pixelRow]' 'draw pixel row _ x _ y _ : bytesPerPixel _ : palette _' 'auto num num num str' 'aList' 0 0 4 spec ' ' '[tft:drawBitmap]' 'draw bitmap _ palette _ on TFT at x _ y _' 'str str num num' 'aBitmap' 'a list of colors' 10 10 space spec 'r' 'tft_colorSwatch' '_' 'color' spec 'r' 'makeColor' 'color r _ g _ b _ (0-255)' 'num num num' 0 100 100 spec 'r' 'makeGray' 'gray _ %' 'num' 50 spec 'r' 'randomColor' 'random color' space spec 'r' '[tft:getWidth]' 'TFT width' spec 'r' '[tft:getHeight]' 'TFT height' space spec ' ' '[tft:setBacklight]' 'set TFT backlight _ (0-10)' 'num' 10 space spec ' ' '_deferMonochromeDisplayUpdates' '_defer monochrome display updates' spec ' ' '_resumeMonochromeDisplayUpdates' '_resume monochrome display updates' to '_deferMonochromeDisplayUpdates' { '[tft:deferUpdates]' } to '_resumeMonochromeDisplayUpdates' { '[tft:resumeUpdates]' } to makeColor 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 makeGray percent { gray = ((percent * 255) / 100) gray = (maximum 0 (minimum gray 255)) return ((gray << 16) | ((gray << 8) | gray)) } 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 tft_colorSwatch color { return color } to tft_drawText s x y color optionalScale optionalBGColor { s = ('[data:convertType]' s 'string') local 'scale' (argOrDefault 5 2) local 'bgColor' (argOrDefault 6 '') local 'lines' ('[data:split]' s ('[data:unicodeString]' 10)) for line ('[data:split]' s ('[data:unicodeString]' 10)) { if (isType bgColor 'number') { '[tft:text]' line x y color scale false bgColor } else { '[tft:text]' line x y color scale false } y += (8 * scale) } } to tft_drawVector x y angle length color { local 'endX' (x + ((length * ('[misc:sin]' (100 * (angle + 90)))) >> 14)) local 'endY' (y + ((length * ('[misc:sin]' (100 * angle))) >> 14)) '[tft:line]' x y endX endY color }