Skip to content

Instantly share code, notes, and snippets.

@flaki
Created September 8, 2015 05:52
Show Gist options
  • Save flaki/57477d2263f4b7d80af2 to your computer and use it in GitHub Desktop.
Save flaki/57477d2263f4b7d80af2 to your computer and use it in GitHub Desktop.
T2 Rust Example
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();
});
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;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment