Skip to content

Instantly share code, notes, and snippets.

@Krimsit
Last active November 16, 2017 12:12
Show Gist options
  • Save Krimsit/d7d689e2d776b56bc704c5555646b822 to your computer and use it in GitHub Desktop.
Save Krimsit/d7d689e2d776b56bc704c5555646b822 to your computer and use it in GitHub Desktop.
var ir = require("@amperka/ir-receiver").connect(P0);
var led = require("@amperka/led").connect(P2);
var opers = {
antimulti: {
str: "3fe8977",
value: "/"
},multi: {
str: "3ff8877",
value: "*"
},minus: {
str: "3ff807f",
value: "-"
},summ: {
str: "3fea15f",
value: "+"
}
};
var numbers = {
zero: {
str: "3fda25f",
value: "0"
},one: {
str: "3fcc33f",
value: "1"
},two: {
str: "3fc639f",
value: "2"
},three: {
str: "3fdea17",
value: "3"
},four: {
str: "3fc43bf",
value: "4"
},five: {
str: "3fce31f",
value: "5"
},six: {
str: "3fd6a97",
value: "6"
},seven: {
str: "3fd0af7",
value: "7"
},eight: {
str: "3fd2ad7",
value: "8"
},nine: {
str: "3fd4ab7",
value: "9"
},
};
var ToggleBlinkLED = function(t, n){
var i = 0;
var inter = setInterval(function(){
led.blink(t - 200);
setTimeout(function(){}, 100);
console.log(i);
i++;
if(i == n*2){
clearInterval(inter);
led.turnOff();
}
}, t);
};
var Out = function(a){
console.log("************ " + a + " *************");
if(Math.round(a) < 10){
ToggleBlinkLED(500, Math.round(a));
}
};
var calculate = function(a, b, op){
switch(op){
case "+":
Out(a + b);break;
case "-":
Out(a - b);break;
case "*":
Out(a * b);break;
case "/":
Out(a / b);break;
default:
console.log("Err");
}
};
var parseStr = function(str, op){
var arr = str.split(op).map(Number);
console.log(arr);
console.log(str);
calculate(arr[0], arr[1], op);
};
var quell = {
str: "3ff08f7"
};
var reset1 = {
str: "3fe41bf",
value: "reset"
};
var mathConsts = {
pi: {
str: "3fc8b77",
value: Math.PI
},
e: {
str: "3fc0bf7",
value: Math.E
}
};
var calc = "";
var op = "";
ir.on("receive", function(code, repeat){
for(key in opers){
if(code.toString(16) == opers[key].str){
calc += ""+opers[key].value;
op = ""+opers[key].value;
console.log(opers[key].value);
}
}
for(key in numbers){
if(code.toString(16) == numbers[key].str){
calc += ""+numbers[key].value;
console.log(numbers[key].value);
}
}
for(key in mathConsts){
if(code.toString(16) == mathConsts[key].str){
Out(mathConsts[key].value);
}
}
if(code.toString(16) == quell.str){
parseStr(calc, op);
}
if(code.toString(16) == reset1.str){
Out("RESET");
calc = "";
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment