|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Moddable Tech, Inc. |
| 3 | + * |
| 4 | + * This file is part of the Moddable SDK. |
| 5 | + * |
| 6 | + * This work is licensed under the |
| 7 | + * Creative Commons Attribution 4.0 International License. |
| 8 | + * To view a copy of this license, visit |
| 9 | + * <http://creativecommons.org/licenses/by/4.0>. |
| 10 | + * or send a letter to Creative Commons, PO Box 1866, |
| 11 | + * Mountain View, CA 94042, USA. |
| 12 | + * |
| 13 | + */ |
| 14 | +import Timer from "timer"; |
| 15 | +import Time from "time"; |
| 16 | +import Digital from "pins/digital"; |
| 17 | +import Monitor from "pins/digital/monitor"; |
| 18 | +import { NeoStrand, NeoStrandEffect } from "neostrand"; |
| 19 | + |
| 20 | +const CYCLE_AUTOMATICALLY = 10000; // ms to cycle to the next effect |
| 21 | + |
| 22 | +const LEN = 144; |
| 23 | + |
| 24 | +const Timing_WS2812B = { |
| 25 | + mark: { level0: 1, duration0: 18, level1: 0, duration1: 7, }, |
| 26 | + space: { level0: 1, duration0: 7, level1: 0, duration1: 18, }, |
| 27 | + reset: { level0: 0, duration0: 600, level1: 0, duration1: 600, } }; |
| 28 | + |
| 29 | +const strand = new NeoStrand({length: LEN, pin: 22, order: "RGB", timing: Timing_WS2812B}); |
| 30 | + |
| 31 | +strand.brightness = 128; |
| 32 | + |
| 33 | +let mode = 0; |
| 34 | +let schemes = []; |
| 35 | + |
| 36 | +let baseDictionary = { strand }; |
| 37 | + |
| 38 | +let marqueeDictionary = Object.assign({}, baseDictionary, { }); |
| 39 | +let marquee1 = Object.assign({}, baseDictionary, |
| 40 | + { sizeA: 1, rgbA: { r: 0x80, g: 0, b: 0 }, |
| 41 | + sizeB: 4, rgbB: { r: 0, g: 0, b: 0x80 } |
| 42 | + }); |
| 43 | +let marquee2 = Object.assign({}, marquee1, { reverse: 1 }); |
| 44 | +let hueSpanDictionary = Object.assign({}, baseDictionary, { }); |
| 45 | +let hueSpan1 = Object.assign({}, baseDictionary, { reverse: 1 }); |
| 46 | +let hueSpan2 = Object.assign({}, baseDictionary, { size: (strand.length / 4) }); |
| 47 | +let hueSpan3 = Object.assign({}, hueSpan2, { reverse: 1 }); |
| 48 | +let sineDictionary = Object.assign({}, baseDictionary, { offset: .5 }); |
| 49 | +let pulseDictionary = Object.assign({}, baseDictionary, { }); |
| 50 | +let pulse1 = Object.assign({}, baseDictionary, |
| 51 | + { direction: 0, |
| 52 | + duration: 5000, |
| 53 | + position: "random" }); |
| 54 | +let patternDictionary = Object.assign({}, baseDictionary, { }); |
| 55 | + |
| 56 | +let pattern1 = Object.assign({}, baseDictionary, |
| 57 | + { pattern: [ 0x003300, 0x003300, 0x003300, 0x131313, 0x131313 ] }); |
| 58 | + |
| 59 | +let pattern2 = Object.assign({}, baseDictionary, |
| 60 | + { pattern: [ 0x110000, 0x008800, 0x008800, 0x008800, 0x008800 ] }); |
| 61 | + |
| 62 | +schemes.push( [ new NeoStrand.Marquee( marqueeDictionary ) ]); |
| 63 | +schemes.push( [ new NeoStrand.Marquee( marquee1 ) ]); |
| 64 | +schemes.push( [ new NeoStrand.Marquee( marquee2 ) ]); |
| 65 | +schemes.push( [ new NeoStrand.HueSpan( hueSpanDictionary ) ]); |
| 66 | +schemes.push( [ new NeoStrand.HueSpan( hueSpan1 ) ]); |
| 67 | +schemes.push( [ new NeoStrand.HueSpan( hueSpan2 ) ]); |
| 68 | +schemes.push( [ new NeoStrand.HueSpan( hueSpan3 ) ]); |
| 69 | +schemes.push( [ new NeoStrand.Sine( sineDictionary ) ]); |
| 70 | +schemes.push( [ new NeoStrand.Pulse( pulseDictionary ) ]); |
| 71 | +schemes.push( [ new NeoStrand.Pulse( pulse1 ) ]); |
| 72 | +schemes.push( [ new NeoStrand.Pulse( pulse1 ) , |
| 73 | + new NeoStrand.Pulse( pulse1 ), |
| 74 | + new NeoStrand.Pulse( pulse1 ), |
| 75 | + ]); |
| 76 | +schemes.push( [ new NeoStrand.Pattern( patternDictionary ) ]); |
| 77 | +schemes.push( [ new NeoStrand.Pattern( pattern1 ) ]); |
| 78 | +schemes.push( [ new NeoStrand.Pattern( pattern2) ]); |
| 79 | + |
| 80 | +strand.setScheme(schemes[0]); |
| 81 | + |
| 82 | +strand.start(50); |
| 83 | + |
| 84 | +let monitor = new Monitor({ |
| 85 | + pin: 0, |
| 86 | + mode: Digital.InputPullUp, |
| 87 | + edge: Monitor.Falling }); |
| 88 | +monitor.lastMS = Time.ticks; |
| 89 | + |
| 90 | +monitor.onChanged = function() { |
| 91 | + let t = Time.ticks; |
| 92 | + if ((t - this.lastMS) > 125) { |
| 93 | + mode = (mode + 1) % schemes.length; |
| 94 | + strand.setScheme(schemes[mode]); |
| 95 | + } |
| 96 | + this.lastMS = t; |
| 97 | +} |
| 98 | + |
| 99 | +class RandomColor extends NeoStrandEffect { |
| 100 | + constructor(dictionary) { |
| 101 | + super(dictionary); |
| 102 | + this.name = "RandomColor" |
| 103 | + this.size = dictionary.size ? dictionary.size : 5; |
| 104 | + this.max = dictionary.max ? dictionary.max : 64; |
| 105 | + } |
| 106 | + |
| 107 | + loopPrepare(effect) { |
| 108 | + effect.colors_set = 0; |
| 109 | + } |
| 110 | + |
| 111 | + activate(effect) { |
| 112 | + effect.timeline.on(effect, { effectValue: [ 0, effect.dur ] }, effect.dur, null, 0); |
| 113 | + effect.reset(effect); |
| 114 | + } |
| 115 | + |
| 116 | + set effectValue(value) { |
| 117 | + if (0 == this.colors_set) { |
| 118 | + for (let i=this.start; i<this.end; i++) { |
| 119 | + if (0 == (i % this.size)) |
| 120 | + this.color = this.strand.makeRGB(Math.random() * this.max, Math.random() * this.max, Math.random() * this.max); |
| 121 | + this.strand.set(i, this.color, this.start, this.end); |
| 122 | + } |
| 123 | + this.colors_set = 1; |
| 124 | + } |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +schemes.push( [ new RandomColor( { strand, loop: 1, duration: 5000 } ) ]); |
| 129 | + |
| 130 | + |
0 commit comments