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.
var WebSocket = require('ws');
var ws = new WebSocket('ws://192.168.10.100:8080/entry');
var upmBuzzer = require('jsupm_buzzer');
var numBuzzers = 4;
var inputs = [3, 5, 6, 9];
var buzzers = [];
var volume = 0.1;
var f3js = require('f3js');
var c = f3js.createContainer();
c.x = 50;
c.y = 50;
var c1 = 47, c2 = 33, width = 70;
var a = c.createPath();
a.moveTo(70, 140);
a.curveTo(70, 140 + c1, 140, 140 + c1, 70 + width, 140);
a.curveTo(70 + width, 140, 70 + width, 140 - c2, 70 + width * 1.5, 140 - width * 0.5);
a.curveTo(222, 105, 222, 30, 175, 35);
a.curveTo(175, 35, 140, 35, 140, 0);
a.curveTo(140, -47, 70, -47, 70, 0);
a.curveTo(70, 0, 70, 35, 35, 35);
a.curveTo(-12, 35, -12, 105, 35, 105);
a.curveTo(35, 105, 70, 105, 70, 140);
a.close();
a.matrix.translate(-40, -5);
var ps = a.extrude(60);
ps[0].y = 50;
ps[0].x = 270;
f3js.add(ps[0]);
for (var i = 0; i < numBuzzers; i ++) {
var buzzer = new upmBuzzer.Buzzer(inputs[i]);
buzzer.setVolume(volume);
buzzer.stopSound();
buzzer.stopSound();
var p = c.add(buzzer, 0, 0);
buzzers.push(p);
}
upmBuzzer.Buzzer.prototype.translate = function() {};
upmBuzzer.Buzzer.prototype.rotate = function() {};
buzzers[0].translate(65, -15);
buzzers[1].translate(145, 65);
buzzers[1].rotate(90);
buzzers[2].translate(65, 145);
buzzers[2].rotate(180);
buzzers[3].translate(-15, 65);
buzzers[3].rotate(270);
var pathl = c.createPath();
pathl.moveTo(0, 40).dovetailTo(0, 85, 3, 1, false, false, true);
ps = pathl.extrude(60);
f3js.add(ps[0]);
ps[0].x = 10; ps[0].y = 230;
var pathr = c.createPath();
pathr.moveTo(130, 40).dovetailTo(130, 90, 3, 1, false, false, true);
ps = pathr.extrude(60);
f3js.add(ps[0]);
ps[0].x = 70; ps[0].y = 230;
var patht = c.createPath();
patht.moveTo(30, 25).dovetailTo(100, 25, 3, 1, false, false, true);
ps = patht.extrude(60);
f3js.add(ps[0]);
ps[0].x = 130; ps[0].y = 230;
var pathb = c.createPath();
pathb.moveTo(30, 100).dovetailTo(100, 100, 3, 1, false, false, true);
ps = patht.extrude(60);
f3js.add(ps[0]);
ps[0].x = 210; ps[0].y = 230;
var pathlc = c.createPath();
pathlc.moveTo(220, 40).dovetailTo(220, 85, 3, 1, false, false, true);
var pathrc = c.createPath();
pathrc.moveTo(350, 40).dovetailTo(350, 90, 3, 1, false, false, true);
var pathtc = c.createPath();
pathtc.moveTo(250, 25).dovetailTo(320, 25, 3, 1, false, false, true);
var pathbc = c.createPath();
pathbc.moveTo(250, 100).dovetailTo(320, 100, 3, 1, false, false, true);
f3js.drawRectangle(30, 95, 10, 5);
f3js.drawRectangle(130, 30, 5, 10);
f3js.drawRectangle(190, 130, 10, 5);
f3js.drawRectangle(95, 190, 5, 10);
ws.on('message', function(message) {
var data = JSON.parse(message);
switch (data.message) {
case 'note_on':
if(buzzers[data.channel]) {
buzzers[data.channel].setVolume(volume);
buzzers[data.channel].playSound(1000000 / data.frequency, 0);
}
break;
case 'note_off':
if(buzzers[data.channel]) {
buzzers[data.channel].stopSound();
buzzers[data.channel].stopSound();
}
break;
}
});