Last active
August 29, 2015 14:00
-
-
Save bathtimefish/3dcc80e90a802b6981c5 to your computer and use it in GitHub Desktop.
EspruinoのLED1~3を簡単に制御するためのユーティリティクラス
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
var Signals = (function () { | |
function Signals() { | |
this.leds = { | |
"LED1": { light: false, type: undefined, intervalId: undefined, pin: LED1 }, | |
"LED2": { light: false, type: undefined, intervalId: undefined, pin: LED2 }, | |
"LED3": { light: false, type: undefined, intervalId: undefined, pin: LED3 } | |
}; | |
} | |
Signals.prototype.initLEDs = function (patterns) { | |
for (var p in patterns) { | |
if (this.leds[p].intervalId) { | |
this.lightOut(p); | |
clearInterval(this.leds[p].intervalId); | |
this.leds[p].intervalId = undefined; | |
this.leds[p].type = undefined; | |
} | |
} | |
}; | |
Signals.prototype.set = function (patterns) { | |
this.initLEDs(patterns); | |
for (var p in patterns) { | |
switch (patterns[p]) { | |
case "on": | |
this.light(p); | |
break; | |
case "off": | |
this.lightOut(p); | |
break; | |
case "blink": | |
this.blink(p); | |
break; | |
} | |
} | |
}; | |
Signals.prototype.light = function (name) { | |
this.leds[name].pin.write(true); | |
this.leds[name].type = "on"; | |
}; | |
Signals.prototype.lightOut = function (name) { | |
this.leds[name].pin.write(false); | |
this.leds[name].type = "off"; | |
}; | |
Signals.prototype.blink = function (name) { | |
var that = this; | |
var flg = false; | |
this.leds[name].intervalId = setInterval(function () { | |
flg = !flg; | |
that.leds[name].pin.write(flg); | |
}, 1000); | |
this.leds[name].type = "blink"; | |
}; | |
return Signals; | |
})(); |
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
class Signals { | |
leds: any; | |
constructor(){ | |
this.leds = { | |
"LED1": {light:false, type:undefined, intervalId:undefined, pin:LED1}, | |
"LED2": {light:false, type:undefined, intervalId:undefined, pin:LED2}, | |
"LED3": {light:false, type:undefined, intervalId:undefined, pin:LED3}, | |
}; | |
} | |
initLEDs(patterns) { | |
for(var p in patterns) { | |
if(this.leds[p].intervalId){ | |
this.lightOut(p); | |
clearInterval(this.leds[p].intervalId); | |
this.leds[p].intervalId = undefined; | |
this.leds[p].type = undefined; | |
} | |
} | |
} | |
set(patterns: any) { | |
this.initLEDs(patterns); | |
for(var p in patterns) { | |
switch(patterns[p]) { | |
case "on": | |
this.light(p); | |
break; | |
case "off": | |
this.lightOut(p); | |
break; | |
case "blink": | |
this.blink(p); | |
break; | |
} | |
} | |
} | |
light(name: string){ | |
this.leds[name].pin.write(true); | |
this.leds[name].type = "on"; | |
} | |
lightOut(name: string){ | |
this.leds[name].pin.write(false); | |
this.leds[name].type = "off"; | |
} | |
blink(name: string) { | |
var that = this; | |
var flg = false; | |
this.leds[name].intervalId = setInterval(function() { | |
flg = !flg; | |
that.leds[name].pin.write(flg) | |
}, 1000); | |
this.leds[name].type = "blink"; | |
} | |
} |
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
/* TEST RUN */ | |
var patterns = { | |
"LED1": "off", | |
"LED2": "blink", | |
"LED3": "on" | |
}; | |
var signals = new Signals(); | |
signals.set(patterns); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment