Provide the source code of a microcontroller or tiny computer in JavaScript. Node.js-based computers are supported. Require f3js
package and use its API to design the device enclosure.
// LCD
var LCD = require('jsupm_i2clcd');
var myLcd = new LCD.Jhd1313m1(0, 0x3E, 0x62);
// Buzzer
var upmBuzzer = require("jsupm_buzzer");
var myBuzzer = new upmBuzzer.Buzzer(6);
myBuzzer.setVolume(0.01);
myBuzzer.stopSound();
// Slider
var upm_grove = require('jsupm_grove');
var mySlide = new upm_grove.GroveSlide(0);
// Ultrasonic
var ultrasonic = require("jsupm_groveultrasonic");
var mySonic = new ultrasonic.GroveUltraSonic(8);
// Motion
var grove_motion = require('jsupm_biss0001');
var myMotion = new grove_motion.BISS0001(2);
// Servo
var servoModule = require("jsupm_servo");
var myServo = new servoModule.ES08A(5);
myServo.haltPwm();
/////////////////////////////////////////
var __ = require("underscore");
var Twit = require('twit');
function TweetBuzzer(keys) {
var client = new Twit(keys);
var r=100, g=100, b=100;
// var words = ['love', 'happy', 'hate', 'like', 'angry', 'sad'];
// var words = ['apple', 'google', 'microsoft', 'twitter'];
var words = [
'love' // Tweet Search Keyword (High)
, 'hate' // Tweet Search Keyword (Low)
];
var myLcd, myBuzzer, mySlide;
var status, stream;
var beep = false;
this.run = function(lcd, buzzer, slide, stt)
{
myLcd = lcd;
myBuzzer = buzzer;
mySlide = slide;
status = stt;
var stream = client.stream("statuses/filter", {track: words});
stream.on("tweet", function(tw){
if (status.iru && !beep) onTweet(tw);
});
};
this.destroy = function()
{
stream.destroy();
stream = null;
};
////////////////////////
var onTweet = function(tw)
{
myLcd.setCursor(0,0);
myLcd.write(" ");
myLcd.setCursor(0,0);
myLcd.write("@"+tw.user.screen_name);
var note = 3000;
var hit = false;
myLcd.setCursor(1, 0);
myLcd.write(Array(words.length+1).join('_'));
__.each(words, function(w, i){
var re = new RegExp(w, 'i');
if (tw.text.match(re)) {
myLcd.setCursor(1, words.length-(i+1));
myLcd.write("+");
myLcd.setCursor(1, words.length+1);
myLcd.write(Array(15-words.length).join(' '));
myLcd.setCursor(1, words.length+1);
myLcd.write(w);
note /= words.length - i;
hit = true;
}
});
if (hit) {
var vol = mySlide.voltage_value();
if (status.iru && !status.kita && !beep ) {
beep = true;
if (vol >= 0.1) {
myBuzzer.setVolume(0.2*(vol-0.1));
myBuzzer.playSound(note, 50000);
myBuzzer.stopSound();
}
setTimeout(function(){beep = false}, 10);
}
}
};
}
var tb = new TweetBuzzer({
"consumer_key" : "" // Twitter consumer key
, "consumer_secret" : "" // Twitter consumer secret
, "access_token" : "" // Twitter access token
, "access_token_secret" : "" // Twitter access token secret
});
/////////////////////////////////////////
function Dareka() {
var oshirase_shita = false;
var before_dist = 0;
var myLcd, upmBuzzer, myBuzzer, myMotion;
var status;
var kitaInterval;
var timeout_iru = 60000 * 3;
this.check = function(lcd, upm_buzzer, buzzer, sonic, motion, servo, stt)
{
status = stt;
myLcd = lcd, upmBuzzer = upm_buzzer, myBuzzer = buzzer;
mySonic = sonic, myMotion = motion, myServo = servo;
kitaInterval = setInterval(function() {
if (myMotion.value() === true) {
myLcd.setCursor(1, 15);
myLcd.write("*");
if (!status.iru) {
setTimeout(function(){
status.iru = false;
myLcd.setColor(100,100,100);
}, timeout_iru);
}
status.iru = true;
myLcd.setColor(255,255,0);
} else {
myLcd.setCursor(1, 15);
myLcd.write(" ");
}
if (status.iru) {
check_dist();
}
}, 300);
};
this.destroy = function()
{
clearInterval(kitaInterval);
myServo.haltPwm();
};
var check_dist = function()
{
var travelTime = mySonic.getDistance();
if (travelTime > 0) {
var distance = (travelTime / 29 / 2).toFixed(3);
// myLcd.setCursor(1,0);
// myLcd.write(Math.round(distance)+"[cm]");
// console.log(Math.round(distance)+"[cm]");
if (before_dist - 50 > distance) {
if (!status.kita) oshirase();
}
before_dist = distance;
}
};
var oshirase = function()
{
console.log("Darekairu!");
status.kita = true;
myLcd.setColor(255, 0, 0);
myBuzzer.setVolume(0.3);
myBuzzer.playSound(upmBuzzer.SI/4, 100000);
myBuzzer.playSound(upmBuzzer.SI/5, 100000);
myBuzzer.playSound(upmBuzzer.SI/6, 100000);
/*
myBuzzer.playSound(1000000/(440* Math.pow(2, (3/12))/2), 100000);
myBuzzer.playSound(1000000/(440* Math.pow(2, (7/12))/2), 100000);
myBuzzer.playSound(1000000/(440* Math.pow(2, (10/12))/2), 100000);
*/
myBuzzer.stopSound();
setTimeout(function(){
status.kita = false;
if (status.iru) {
myLcd.setColor(255, 255, 0);
} else {
myLcd.setColor(100, 100, 100);
}
}, 1000);
};
}
var dareka = new Dareka();
/////////////////////////////////////////
var stt = { iru: false, kita: false };
tb.run(myLcd, myBuzzer, mySlide, stt);
dareka.check(myLcd, upmBuzzer, myBuzzer, mySonic, myMotion, myServo, stt);
var f3js = require('f3js');
var dep = 2, w = 2;
// 本体ケース
var c = f3js.createContainer();
c.x = 8, c.y = 130;
var p = c.createPath();
p.moveTo(0, 0);
p.jointTo(75, 0, w, dep);
p.jointTo(75, 74, w, dep);
p.lineTo(65, 74);
p.lineTo(65, 45);
p.jointTo(28, 45, w, dep);
p.lineTo(28, 130);
// p.jointTo(0, 130, w, dep);
p.lineTo(0, 130);
p.jointTo(0, 0, w, dep);
p.close();
var plates = p.extrude(114);
plates[0].x = 10; plates[0].y = 10;
f3js.add(plates[0]);
f3js.drawRectangle(66,150,7,15);
plates[1].x = 88; plates[1].y = 10;
var front = plates[1];
f3js.drawText(
"isyda" // Engraved mark
, 20
, 14
);
myLcd.rotation = 10;
front.add(myLcd, 27, 66, -90);
front.add(myBuzzer, 57, 36, 90);
front.add(mySlide, 37, 16);
front.add(myMotion, 57, 86, 90);
f3js.add(plates[1]);
plates[2].x = 165; plates[2].y = 10;
f3js.add(plates[2]);
plates[3].x = 210; plates[3].y = 10;
f3js.add(plates[3]);
plates[4].x = 90; plates[4].y = 130;
f3js.add(plates[4]);
plates[5].x = 100; plates[5].y = 130;
f3js.add(plates[5]);
// Sonicケース
var sc = f3js.createContainer();
sc.x = 180, sc.y = 130;
var sp = sc.createPath();
sp.moveTo(0, 0);
sp.jointTo(60, 0, w, dep);
sp.lineTo(60, 50);
sp.jointTo(0, 50, w, dep);
sp.lineTo(0, 0);
sp.close();
f3js.drawRectangle(202.5, 148, 15, 7);
var splates = sp.extrude(60);
splates[0].x = 180, splates[0].y = 185;
f3js.add(splates[0]);
splates[1].x = 245, splates[1].y = 130;
f3js.add(splates[1]);
splates[2].x = 245, splates[2].y = 195;
splates[2].add(mySonic, 30, 17)
f3js.add(splates[2]);
f3js.drawCircle(320, 35, 1);
f3js.drawCircle(320, 35+52, 1);
// When exiting: clear interval and print message
process.on('SIGINT', function() {
console.log("Exiting...");
dareka.destroy();
tb.destroy();
myLcd.setColor(100, 100, 100);
myBuzzer.stopSound();
process.exit(0);
});