/**
 * Well known colors for a NeoPixel strip
 */ enum NeoPixelColors { Red = 0xFF0000 , Orange = 0xFFA500 , Yellow = 0xFFFF00 , Green = 0x00FF00 , Blue = 0x0000FF , Indigo = 0x4b0082 , Violet = 0x8a2be2 , Purple = 0xFF00FF , White = 0xFFFFFF , Black = 0x000000 } /**
 * Different modes for RGB or RGB+W NeoPixel strips
 */ enum NeoPixelMode { RGB = 1 , RGBW = 2 , RGB_RGB = 3 } /**
 * Functions to operate NeoPixel strips.
 */ namespace neopixel { /**
     * A NeoPixel strip
     */ export class Strip { buf : Buffer ; pin : DigitalPin ; brightness : number ; start : number ; _length : number ; _mode : NeoPixelMode ; _matrixWidth : number ; param rgb showColor ( rgb ) { rgb = rgb >> 0 ; this . setAllRGB ( rgb ) ; this . show ( ) ; } param startHue param endHue showRainbow ( startHue = 1 , endHue = 360 ) { if ( this . _length <= 0 ) return ; startHue = startHue >> 0 ; endHue = endHue >> 0 ; const saturation = 100 ; const luminance = 50 ; const steps = this . _length ; const direction = HueInterpolationDirection . Clockwise ; const h1 = startHue ; const h2 = endHue ; const hDistCW = ( ( h2 + 360 ) - h1 ) % 360 ; const hStepCW = Math . idiv ( ( hDistCW * 100 ) , steps ) ; const hDistCCW = ( ( h1 + 360 ) - h2 ) % 360 ; const hStepCCW = Math . idiv ( - ( hDistCCW * 100 ) , steps ) ; let hStep ; if ( direction === HueInterpolationDirection . Clockwise ) { hStep = hStepCW ; } else if ( direction === HueInterpolationDirection . CounterClockwise ) { hStep = hStepCCW ; } else { hStep = hDistCW < hDistCCW ? hStepCW : hStepCCW ; } const h1_100 = h1 * 100 ; const s1 = saturation ; const s2 = saturation ; const sDist = s2 - s1 ; const sStep = Math . idiv ( sDist , steps ) ; const s1_100 = s1 * 100 ; const l1 = luminance ; const l2 = luminance ; const lDist = l2 - l1 ; const lStep = Math . idiv ( lDist , steps ) ; const l1_100 = l1 * 100 ; if ( steps === 1 ) { this . setPixelColor ( 0 , hsl ( h1 + hStep , s1 + sStep , l1 + lStep ) ) ; } else { this . setPixelColor ( 0 , hsl ( startHue , saturation , luminance ) ) ; for ( let i = 1 ; i < steps - 1 ; i ++ ) { const h = Math . idiv ( ( h1_100 + i * hStep ) , 100 ) + 360 ; const s = Math . idiv ( ( s1_100 + i * sStep ) , 100 ) ; const l = Math . idiv ( ( l1_100 + i * lStep ) , 100 ) ; this . setPixelColor ( i , hsl ( h , s , l ) ) ; } this . setPixelColor ( steps - 1 , hsl ( endHue , saturation , luminance ) ) ; } this . show ( ) ; } param value param high showBarGraph ( value , high ) { if ( high <= 0 ) { this . clear ( ) ; this . setPixelColor ( 0 , NeoPixelColors . Yellow ) ; this . show ( ) ; return ; } value = Math . abs ( value ) ; const n = this . _length ; const n1 = n - 1 ; let v = Math . idiv ( ( value * n ) , high ) ; if ( v == 0 ) { this . setPixelColor ( 0 , 0x666600 ) ; for ( let i = 1 ; i < n ; ++ i ) this . setPixelColor ( i , 0 ) ; } else { for ( let i = 0 ; i < n ; ++ i ) { if ( i <= v ) { const b = Math . idiv ( i * 255 , n1 ) ; this . setPixelColor ( i , neopixel . rgb ( b , 0 , 255 - b ) ) ; } else this . setPixelColor ( i , 0 ) ; } } this . show ( ) ; } param pixeloffset param rgb setPixelColor ( pixeloffset , rgb ) { this . setPixelRGB ( pixeloffset >> 0 , rgb >> 0 ) ; } param width setMatrixWidth ( width ) { this . _matrixWidth = Math . min ( this . _length , width >> 0 ) ; } param x param y param rgb setMatrixColor ( x , y , rgb ) { if ( this . _matrixWidth <= 0 ) return ; x = x >> 0 ; y = y >> 0 ; rgb = rgb >> 0 ; const cols = Math . idiv ( this . _length , this . _matrixWidth ) ; if ( x < 0 || x >= this . _matrixWidth || y < 0 || y >= cols ) return ; let i = x + y * this . _matrixWidth ; this . setPixelColor ( i , rgb ) ; } param pixeloffset param white setPixelWhiteLED ( pixeloffset , white ) { if ( this . _mode === NeoPixelMode . RGBW ) { this . setPixelW ( pixeloffset >> 0 , white >> 0 ) ; } } /**
         * Send all the changes to the strip.
         */ show ( ) { ws2812b . sendBuffer ( this . buf , this . pin ) ; } /**
         * Turn off all LEDs.
         * You need to call ``show`` to make the changes visible.
         */ clear ( ) { const stride = this . _mode === NeoPixelMode . RGBW ? 4 : 3 ; this . buf . fill ( 0 , this . start * stride , this . _length * stride ) ; } /**
         * Gets the number of pixels declared on the strip
         */ length ( ) { return this . _length ; } param brightness setBrightness ( brightness ) { this . brightness = brightness & 0xff ; } /**
         * Apply brightness to current colors using a quadratic easing function.
         **/ easeBrightness ( ) { const stride = this . _mode === NeoPixelMode . RGBW ? 4 : 3 ; const br = this . brightness ; const buf = this . buf ; const end = this . start + this . _length ; const mid = Math . idiv ( this . _length , 2 ) ; for ( let i = this . start ; i < end ; ++ i ) { const k = i - this . start ; const ledoffset = i * stride ; const br = k > mid ? Math . idiv ( 255 * ( this . _length - 1 - k ) * ( this . _length - 1 - k ) , ( mid * mid ) ) : Math . idiv ( 255 * k * k , ( mid * mid ) ) ; const r = ( buf [ ledoffset + 0 ] * br ) >> 8 ; buf [ ledoffset + 0 ] = r ; const g = ( buf [ ledoffset + 1 ] * br ) >> 8 ; buf [ ledoffset + 1 ] = g ; const b = ( buf [ ledoffset + 2 ] * br ) >> 8 ; buf [ ledoffset + 2 ] = b ; if ( stride == 4 ) { const w = ( buf [ ledoffset + 3 ] * br ) >> 8 ; buf [ ledoffset + 3 ] = w ; } } } param start param length range ( start , length ) { start = start >> 0 ; length = length >> 0 ; let strip = new Strip ( ) ; strip . buf = this . buf ; strip . pin = this . pin ; strip . brightness = this . brightness ; strip . start = this . start + Math . clamp ( 0 , this . _length - 1 , start ) ; strip . _length = Math . clamp ( 0 , this . _length - ( strip . start - this . start ) , length ) ; strip . _matrixWidth = 0 ; strip . _mode = this . _mode ; return strip ; } param offset shift ( offset = 1 ) { offset = offset >> 0 ; const stride = this . _mode === NeoPixelMode . RGBW ? 4 : 3 ; this . buf . shift ( - offset * stride , this . start * stride , this . _length * stride ) ; } param offset rotate ( offset = 1 ) { offset = offset >> 0 ; const stride = this . _mode === NeoPixelMode . RGBW ? 4 : 3 ; this . buf . rotate ( - offset * stride , this . start * stride , this . _length * stride ) ; } /**
         * Set the pin where the neopixel is connected, defaults to P0.
         */ setPin ( pin ) { this . pin = pin ; pins . digitalWritePin ( this . pin , 0 ) ; } /**
         * Estimates the electrical current (mA) consumed by the current light configuration.
         */ power ( ) { const stride = this . _mode === NeoPixelMode . RGBW ? 4 : 3 ; const end = this . start + this . _length ; let p = 0 ; for ( let i = this . start ; i < end ; ++ i ) { const ledoffset = i * stride ; for ( let j = 0 ; j < stride ; ++ j ) { p += this . buf [ i + j ] ; } } return Math . idiv ( this . length ( ) * 7 , 10 ) + Math . idiv ( p * 480 , 10000 ) ; } private setBufferRGB ( offset , red , green , blue ) { if ( this . _mode === NeoPixelMode . RGB_RGB ) { this . buf [ offset + 0 ] = red ; this . buf [ offset + 1 ] = green ; } else { this . buf [ offset + 0 ] = green ; this . buf [ offset + 1 ] = red ; } this . buf [ offset + 2 ] = blue ; } private setAllRGB ( rgb ) { let red = unpackR ( rgb ) ; let green = unpackG ( rgb ) ; let blue = unpackB ( rgb ) ; const br = this . brightness ; if ( br < 255 ) { red = ( red * br ) >> 8 ; green = ( green * br ) >> 8 ; blue = ( blue * br ) >> 8 ; } const end = this . start + this . _length ; const stride = this . _mode === NeoPixelMode . RGBW ? 4 : 3 ; for ( let i = this . start ; i < end ; ++ i ) { this . setBufferRGB ( i * stride , red , green , blue ) ; } } private setAllW ( white ) { if ( this . _mode !== NeoPixelMode . RGBW ) return ; let br = this . brightness ; if ( br < 255 ) { white = ( white * br ) >> 8 ; } let buf = this . buf ; let end = this . start + this . _length ; for ( let i = this . start ; i < end ; ++ i ) { let ledoffset = i * 4 ; buf [ ledoffset + 3 ] = white ; } } private setPixelRGB ( pixeloffset , rgb ) { if ( pixeloffset < 0 || pixeloffset >= this . _length ) return ; let stride = this . _mode === NeoPixelMode . RGBW ? 4 : 3 ; pixeloffset = ( pixeloffset + this . start ) * stride ; let red = unpackR ( rgb ) ; let green = unpackG ( rgb ) ; let blue = unpackB ( rgb ) ; let br = this . brightness ; if ( br < 255 ) { red = ( red * br ) >> 8 ; green = ( green * br ) >> 8 ; blue = ( blue * br ) >> 8 ; } this . setBufferRGB ( pixeloffset , red , green , blue ) ; } private setPixelW ( pixeloffset , white ) { if ( this . _mode !== NeoPixelMode . RGBW ) return ; if ( pixeloffset < 0 || pixeloffset >= this . _length ) return ; pixeloffset = ( pixeloffset + this . start ) * 4 ; let br = this . brightness ; if ( br < 255 ) { white = ( white * br ) >> 8 ; } let buf = this . buf ; buf [ pixeloffset + 3 ] = white ; } } param pin param numleds export function create ( pin , numleds , mode ) { let strip = new Strip ( ) ; let stride = mode === NeoPixelMode . RGBW ? 4 : 3 ; strip . buf = pins . createBuffer ( numleds * stride ) ; strip . start = 0 ; strip . _length = numleds ; strip . _mode = mode || NeoPixelMode . RGB ; strip . _matrixWidth = 0 ; strip . setBrightness ( 128 ) ; strip . setPin ( pin ) ; return strip ; } param red param green param blue export function rgb ( red , green , blue ) { return packRGB ( red , green , blue ) ; } /**
     * Gets the RGB value of a known color
    */ export function colors ( color ) { return color ; } function packRGB ( a , b , c ) { return ( ( a & 0xFF ) << 16 ) | ( ( b & 0xFF ) << 8 ) | ( c & 0xFF ) ; } function unpackR ( rgb ) { let r = ( rgb >> 16 ) & 0xFF ; return r ; } function unpackG ( rgb ) { let g = ( rgb >> 8 ) & 0xFF ; return g ; } function unpackB ( rgb ) { let b = ( rgb ) & 0xFF ; return b ; } param h param s param l export function hsl ( h , s , l ) { h = Math . round ( h ) ; s = Math . round ( s ) ; l = Math . round ( l ) ; h = h % 360 ; s = Math . clamp ( 0 , 99 , s ) ; l = Math . clamp ( 0 , 99 , l ) ; let c = Math . idiv ( ( ( ( 100 - Math . abs ( 2 * l - 100 ) ) * s ) << 8 ) , 10000 ) ; let h1 = Math . idiv ( h , 60 ) ; let h2 = Math . idiv ( ( h - h1 * 60 ) * 256 , 60 ) ; let temp = Math . abs ( ( ( ( h1 % 2 ) << 8 ) + h2 ) - 256 ) ; let x = ( c * ( 256 - ( temp ) ) ) >> 8 ; let r$ ; let g$ ; let b$ ; if ( h1 == 0 ) { r$ = c ; g$ = x ; b$ = 0 ; } else if ( h1 == 1 ) { r$ = x ; g$ = c ; b$ = 0 ; } else if ( h1 == 2 ) { r$ = 0 ; g$ = c ; b$ = x ; } else if ( h1 == 3 ) { r$ = 0 ; g$ = x ; b$ = c ; } else if ( h1 == 4 ) { r$ = x ; g$ = 0 ; b$ = c ; } else if ( h1 == 5 ) { r$ = c ; g$ = 0 ; b$ = x ; } let m = Math . idiv ( ( Math . idiv ( ( l * 2 << 8 ) , 100 ) - c ) , 2 ) ; let r = r$ + m ; let g = g$ + m ; let b = b$ + m ; return packRGB ( r , g , b ) ; } export enum HueInterpolationDirection { Clockwise , CounterClockwise , Shortest } }