Created
December 6, 2019 17:24
-
-
Save phoddie/9f87cc8f2efd9056c20a1bf5a445466e to your computer and use it in GitHub Desktop.
Lightbulb example using Secure ECMAScript from talk given at Agoric meetup December 5, 2019 after TC39 San Francisco
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2019 Moddable Tech, Inc. | |
* | |
* This file is part of the Moddable SDK. | |
* | |
* This work is licensed under the | |
* Creative Commons Attribution 4.0 International License. | |
* To view a copy of this license, visit | |
* <http://creativecommons.org/licenses/by/4.0>. | |
* or send a letter to Creative Commons, PO Box 1866, | |
* Mountain View, CA 94042, USA. | |
* | |
*/ | |
import MY92x1 from "my92x1"; | |
import Timer from "timer"; | |
// Sonoff B1 write order: W C 0 G R B. | |
let light = new MY92x1; | |
const brightness = 255; | |
let interval = 750; | |
let step = 0; | |
Timer.repeat(id => { | |
switch (step) { | |
case 0: // red | |
light.write(0, 0, 0, brightness, 0, 0); | |
break; | |
case 1: // green | |
light.write(0, 0, 0, 0, brightness, 0); | |
break; | |
case 2: // blue | |
light.write(0, 0, 0, 0, 0, brightness); | |
break; | |
case 3: // warm | |
light.write(brightness, 0, 0, 0, 0, 0); | |
break; | |
case 4: // cool | |
light.write(0, brightness, 0, 0, 0, 0); | |
break; | |
case 5: // cool + warm | |
light.write(brightness, brightness, 0, 0, 0, 0); | |
break; | |
} | |
step += 1; | |
if (step > 5) { | |
step = 0; | |
interval = Math.max(100, interval - 125); | |
Timer.schedule(id, interval, interval); | |
} | |
}, 750); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const map = Compartment.map; | |
const whiteListMap = { | |
"main": map.app, | |
"my92x1": map["my92x1-attenuated"], | |
"timer": map.timer, | |
}; | |
const compartment = new Compartment("main", {}, whiteListMap); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"include": [ | |
"$(MODDABLE)/examples/manifest_base.json", | |
"$(MODULES)/pins/digital/manifest.json", | |
], | |
"modules": { | |
"*": [ | |
"./main", | |
"./app", | |
"./my92x1-attenuated", | |
"$(MODULES)/drivers/my92x1/*", | |
], | |
}, | |
"preload": [ | |
"my92x1", | |
"my92x1-attenuated", | |
], | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MY92x1 from "my92x1"; | |
const maxBrightness = 16; // range: 0 to 255 | |
const minInterval = 500; // units: milliseconds | |
class Attenuated { | |
#driver = new MY92x1; | |
#lastWriteTime = Date.now() - minInterval; | |
write(...colors) { | |
const now = Date.now(); | |
if ((now - this.#lastWriteTime) < minInterval) { | |
trace("my92x1: ignore write\n"); | |
return; | |
} | |
trace("my92x1: write\n"); | |
this.#lastWriteTime = now; | |
colors = colors.map(color => (color / 255) * maxBrightness); | |
this.#driver.write(...colors); | |
} | |
} | |
export default Attenuated; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment