NodeConf Europe 2015
István Szmozsánszky
"Flaki"
@slsoftworks
@slsoftworks
JavaScript Developer
Mozilla Contributor
&
Community TechSpeaker
Tessel Project
Team Member
Frontend Trainer at
DPC Consulting
@slsoftworks
@slsoftworks
@slsoftworks
@slsoftworks
← mandatory cat photo
@slsoftworks
@slsoftworks
@slsoftworks
@slsoftworks
@slsoftworks
@slsoftworks
@slsoftworks
use std::thread;
use unix_socket::{UnixStream, UnixListener};
// access to the tessel and accelerometer crate
extern crate rust_tessel;
extern crate rust_accel_mma84;
fn handle_client(stream: UnixStream) {
// initialize the accelerometer
let port = rust_tessel::TesselPort::new("a").unwrap();
let mut accel = rust_accel_mma84::Accelerometer::new(port);
accel.mode_active();
// stream accelerometer data
let mut vals = [0;3];
loop {
accel.get_acceleration(&mut vals);
stream.write(vals[0]);
stream.write(vals[1]);
stream.write(vals[2]);
thread::sleep_ms(100)
}
}
let listener = UnixListener::bind("/tmp/rs2js-bridge-sock-000").unwrap();
// accept connections and process them, spawning a new thread for each one
for stream in listener.incoming() {
match stream {
Ok(stream) => {
/* connection succeeded */
thread::spawn(|| handle_client(stream));
}
Err(err) => {
/* connection failed */
break;
}
}
}
var spawn = require('child_process').spawn;
var child = spawn('accel_rs', [], {});
// Use standard out...
child.stdout.on('data', function(data) {
console.log(data.toString());
});
// ... OR use a domain socket
var net = require('net');
var client = net.connect({path: '/tmp/rs2js-bridge-sock-000'});
function() { //'connect' listener
console.log('Rust bridge opened!');
client.write('world!\r\n');
});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});