module main author unknown version 1 0 description '' script 176 51 { whenStarted '[display:mbDisplay]' 15237440 } script 179 226 { whenButtonPressed 'A' neoPixelAttach 20 16 neoPixelSetAllToColor (colorSwatch 35 190 30 255) setNeoPixelColor 1 (colorSwatch 190 50 13 255) forever { waitMillis 50 rotateNeoPixelsBy 1 NeoPixel_shift_all_colors 5 } } script 180 499 { whenButtonPressed 'B' stopAll neoPixelAttach 20 16 clearNeoPixels } module 'LED Display' Output author MicroBlocks version 1 20 choices led_imageMenu heart 'small heart' yes no happy sad confused angry asleep surprised silly fabulous meh 't-shirt' 'roller skate' duck house tortoise butterfly 'stick figure' ghost sword giraffe skull umbrella snake rabbit cow 'quarter note' 'eight note' pitchfork target triangle 'left triangle' 'chess board' diamond 'small diamond' square 'small square' scissors description 'Display primitives for the 5x5 LED display on the BBC micro:bit, Calliope mini and M5Atom Matrix. Boards with TFT displays (such as the Citilab ED1 or the M5Stack family) support these primitives with a simulated "fat pixel" display. ' variables _stop_scrolling_text spec ' ' '[display:mbDisplay]' 'display _' 'microbitDisplay' 15237440 spec ' ' 'led_displayImage' 'display image _ : x _ y _' 'menu.led_imageMenu num num' 'happy' 1 1 spec ' ' '[display:mbDisplayOff]' 'clear display' space spec ' ' '[display:mbPlot]' 'plot x _ y _' 'num num' 3 3 spec ' ' '[display:mbUnplot]' 'unplot x _ y _' 'num num' 3 3 space spec ' ' 'displayCharacter' 'display character _' 'str' 'A' spec ' ' 'scroll_text' 'scroll text _ : pausing _ ms' 'str num' 'HELLO ROSA!' 100 spec ' ' 'stopScrollingText' 'stop scrolling' advanced spec ' ' 'set display color' 'set display color _' 'color' spec 'r' 'led_image' 'LED image #BR# _' 'microbitDisplay' 15237440 spec ' ' 'display2x5Number' 'display number (0-99) _' 'num' 10 space spec 'r' '_led_namedImage' '_led_namedImage _' 'menu.led_imageMenu' 'happy' spec 'r' '_led_imageData' '_led_imageData' to _led_imageData { return 'heart:4685802,small heart:145728,yes:2269696,no:18157905,happy:15237440,sad:18284864,confused:22348096,angry:23036241,asleep:459616,surprised:4526090,silly:25984017,fabulous:15008639,meh:2236443,t-shirt:15154043,roller skate:11534104,duck:489702,house:10976708,tortoise:359872,butterfly:29332475,stick figure:18158564,ghost:23068334,sword:4657284,giraffe:10946627,skull:15171246,umbrella:6460398,snake:469859,rabbit:16104613,cow:4685361,quarter note:7573636,eight note:7590276,pitchfork:4357813,target:4681156,triangle:1026176,left triangle:32805985,chess board:11184810,diamond:4539716,small diamond:141440,square:33080895,small square:469440,scissors:20287859,' } to _led_namedImage name { local 'data' (_led_imageData) local 'i' ('[data:find]' name data) if (i == -1) { comment 'Name not found' return 0 } local 'start' (('[data:find]' ':' data i) + 1) local 'end' (('[data:find]' ',' data i) - 1) return ('[data:convertType]' ('[data:copyFromTo]' data start end) 'number') } to display2x5Number number { local 'font' ('[data:makeList]' 3247203 2164802 3181635 3214403 2165793 3214371 3247139 2164803 3244131 3214435) local 'temp' ('[data:convertType]' number 'string') if ((size temp) == 1) { temp = ('[data:join]' '0' temp) } led_displayImage (at ((at 1 temp) + 1) font) led_displayImage (at ((at 2 temp) + 1) font) 4 1 } to displayCharacter s { s = ('[data:join]' '' s) if ((size s) == 0) { '[display:mbDisplayOff]' return 0 } '[display:mbDrawShape]' ('[display:mbShapeForLetter]' (at 1 s)) 1 1 } to led_displayImage imageName optionalX optionalY { local 'image' imageName if (isType image 'string') { image = (_led_namedImage imageName) } '[display:mbDrawShape]' image (argOrDefault 2 1) (argOrDefault 3 1) } to led_image twentyFiveBitInt { comment 'An LED image is a 25-bit integer' return twentyFiveBitInt } to scroll_text text optionalDelay { text = ('[data:join]' '' text) local 'delay' 100 if ((pushArgCount) > 1) { delay = optionalDelay } _stop_scrolling_text = (booleanConstant false) if ('Pico:ed' == (boardType)) { for position (((size text) * 6) + 18) { if _stop_scrolling_text {return 0} '[display:mbDisplayOff]' '[tft:text]' text (17 - position) 0 (colorSwatch 125 125 125 255) 1 true waitMillis (delay / 2) } } (or ('KidsIOT' == (boardType)) ('CodingBox' == (boardType))) { for position (((size text) * 6) + 21) { if _stop_scrolling_text {return 0} '[tft:deferUpdates]' '[tft:clear]' '[tft:text]' text (128 - (6 * position)) 6 (colorSwatch 255 255 255 255) 6 false '[tft:resumeUpdates]' waitMillis (delay / 8) } } else { for position (((size text) * 6) + 6) { if _stop_scrolling_text {return 0} for i (size text) { '[display:mbDrawShape]' ('[display:mbShapeForLetter]' ('[data:unicodeAt]' i text)) (((i * 6) + 2) - position) 1 } waitMillis delay } } } to 'set display color' color { '[display:mbSetColor]' color } to stopScrollingText { _stop_scrolling_text = (booleanConstant true) waitMillis 10 '[display:mbDisplayOff]' } 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 }