Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various updates alongside Rust mainline #64

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 9 additions & 24 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
language: rust
rust:
- stable
- beta
- nightly

script:
- cd tessel
- cargo build
- cargo test
- cargo doc

- cd ../accel-mma84
- cargo build
- cargo test
- cargo doc
os:
- linux
- osx

- cd ../climate-si7020
- cargo build
- cargo test
- cargo doc

- cd ../relay-mono
- cargo build
- cargo test
- cargo doc

- cd ../servo-pca9685
script:
- cargo build
- cargo test
- cargo doc

- cd ../test
- cargo build
- cargo test
matrix:
allow_failures:
- rust: nightly
11 changes: 11 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[workspace]
members = [
"tessel",
"accel-mma84",
"climate-si7020",
"relay-mono",
"servo-pca9685"
]

[patch.crates-io]
tessel = { path = "tessel" }
35 changes: 19 additions & 16 deletions accel-mma84/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ impl<'a> Accelerometer<'a> {

fn read_register(&mut self, cmd: Command) -> io::Result<u8> {
let mut xr: [u8; 1] = [0; 1];
try!(self.read_registers(cmd, &mut xr));
self.read_registers(cmd, &mut xr)?;
Ok(xr[0])
}

/// Reads sequential buffers.
fn read_registers(&mut self, cmd: Command, buf: &mut [u8]) -> io::Result<()> {
try!(self.i2c.transfer(I2C_ID, &[cmd as u8], buf));
self.i2c.transfer(I2C_ID, &[cmd as u8], buf)?;
Ok(())
}

Expand All @@ -73,52 +73,55 @@ impl<'a> Accelerometer<'a> {
}

pub fn connect(&mut self) -> io::Result<()> {
if try!(self.read_register(Command::WhoAmI)) != 0x2A {
return Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid connection code."))
if self.read_register(Command::WhoAmI)? != 0x2A {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid connection code.",
));
}

try!(self.set_scale_range(ScaleRange::Scale2G));
try!(self.set_sample_rate(SampleRate::Rate100));
self.set_scale_range(ScaleRange::Scale2G)?;
self.set_sample_rate(SampleRate::Rate100)?;

Ok(())
}

fn standby_enable(&mut self) -> io::Result<()> {
// Sets the MMA8452 to standby mode.
let value = try!(self.read_register(Command::CtrlReg1));
let value = self.read_register(Command::CtrlReg1)?;
self.write_register(Command::CtrlReg1, value & !(0x01u8))
}

fn standby_disable(&mut self) -> io::Result<()> {
// Sets the MMA8452 to active mode.
let value = try!(self.read_register(Command::CtrlReg1));
let value = self.read_register(Command::CtrlReg1)?;
self.write_register(Command::CtrlReg1, value | (0x01u8))
}

pub fn set_scale_range(&mut self, range: ScaleRange) -> io::Result<()> {
try!(self.standby_enable());
try!(self.write_register(Command::XyzDataCfg, range as u8));
try!(self.standby_disable());
self.standby_enable()?;
self.write_register(Command::XyzDataCfg, range as u8)?;
self.standby_disable()?;

Ok(())
}

pub fn set_sample_rate(&mut self, rate: SampleRate) -> io::Result<()> {
try!(self.standby_enable());
self.standby_enable()?;

// Clear the three bits of output rate control (0b11000111 = 199)
let mut value = try!(self.read_register(Command::CtrlReg1));
let mut value = self.read_register(Command::CtrlReg1)?;
value &= 0b11000111;
try!(self.write_register(Command::CtrlReg1, value | ((rate as u8) << 3)));
self.write_register(Command::CtrlReg1, value | ((rate as u8) << 3))?;

try!(self.standby_disable());
self.standby_disable()?;

Ok(())
}

pub fn read_acceleration(&mut self) -> io::Result<(f64, f64, f64)> {
let mut buf = [0; 6];
try!(self.read_registers(Command::OutXMsb, &mut buf));
self.read_registers(Command::OutXMsb, &mut buf)?;

let mut out = vec![0.0, 0.0, 0.0];

Expand Down
2 changes: 1 addition & 1 deletion accel-mma84/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ extern crate accel_mma84;
extern crate tessel;

use accel_mma84::Accelerometer;
use tessel::Tessel;
use std::thread::sleep;
use std::time::Duration;
use tessel::Tessel;

fn main() {
// Acquire port A.
Expand Down
17 changes: 10 additions & 7 deletions climate-si7020/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ enum Command {
}

const TEMPERATURE_OFFSET: f64 = 46.85;
const TEMPERATURE_SLOPE: f64 = 175.72/65536.0;
const TEMPERATURE_SLOPE: f64 = 175.72 / 65536.0;
const HUMIDITY_OFFSET: f64 = 6.0;
const HUMIDITY_SLOPE: f64 = 125.0/65536.0;
const HUMIDITY_SLOPE: f64 = 125.0 / 65536.0;

const I2C_ID: u8 = 0x40;

Expand All @@ -47,7 +47,7 @@ impl<'a> Climate<'a> {
/// Reads sequential buffers.
fn read(&mut self, values: &[Command], buf: &mut [u8]) -> io::Result<()> {
let a: Vec<u8> = values.iter().map(|x| *x as u8).collect();
try!(self.i2c.transfer(I2C_ID, &a, buf));
self.i2c.transfer(I2C_ID, &a, buf)?;
Ok(())
}

Expand All @@ -62,24 +62,27 @@ impl<'a> Climate<'a> {
let mut buf = [0; 6];
thread::sleep(Duration::from_millis(30)); //WAKE_UP_TIME
println!("hi");
try!(self.read(&[Command::ReadId3, Command::ReadId4], &mut buf));
self.read(&[Command::ReadId3, Command::ReadId4], &mut buf)?;
println!("hey");
println!("hi {:?}", buf);
if buf[0] != 0x14 {
return Err(io::Error::new(io::ErrorKind::InvalidData, "Invalid connection code."))
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"Invalid connection code.",
));
}
Ok(())
}

pub fn read_temperature(&mut self) -> io::Result<f64> {
let mut buf = [0; 2];
try!(self.read(&[Command::TempHold], &mut buf));
self.read(&[Command::TempHold], &mut buf)?;

let raw_temp = ((buf[0] as u16) << 8) + (buf[1] as u16);
let mut temp = ((raw_temp as f64) * TEMPERATURE_SLOPE) - TEMPERATURE_OFFSET;

// Convert to fahrenheit.
temp = (temp * (9.0/5.0)) + 32.0;
temp = (temp * (9.0 / 5.0)) + 32.0;

Ok(temp)
}
Expand Down
6 changes: 4 additions & 2 deletions climate-si7020/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ extern crate climate_si7020;
extern crate tessel;

use climate_si7020::Climate;
use tessel::Tessel;
use std::thread::sleep;
use std::time::Duration;
use tessel::Tessel;

fn main() {
// Acquire port A.
let (port_a, _) = Tessel::ports().unwrap();

// Create the accelerometer object and connect to the sensor.
let mut climate = Climate::new(port_a);
climate.connect().expect("Could not connect to climate sensor.");
climate
.connect()
.expect("Could not connect to climate sensor.");

println!("Reading climate sensor... (Press CTRL + C to stop)");
loop {
Expand Down
2 changes: 1 addition & 1 deletion relay-mono/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
extern crate tessel;

use std::io;
use std::ops::Range;
use std::thread;
use std::time::Duration;
use std::ops::Range;

pub struct RelayArray<'a> {
pin1: tessel::Pin<'a>,
Expand Down
5 changes: 1 addition & 4 deletions relay-mono/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#![feature(alloc_system)]
extern crate alloc_system;

extern crate relay_mono;
extern crate tessel;

use relay_mono::RelayArray;
use tessel::Tessel;
use std::thread::sleep;
use std::time::Duration;
use tessel::Tessel;

fn main() {
// Acquire port A.
Expand Down
36 changes: 27 additions & 9 deletions servo-pca9685/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
extern crate tessel;

use std::io;
use std::ops::Range;
use std::thread;
use std::time::Duration;
use std::ops::Range;

#[repr(u8)]
#[allow(dead_code)]
Expand Down Expand Up @@ -90,11 +90,14 @@ impl<'a> ServoArray<'a> {
let prescale: u8 = (((25000000 / (MAX as u64)) / frequency) - 1) as u8;

let mut buf = [0; 1];
self.i2c.transfer(self.i2c_id, &[Command::MODE1 as u8], &mut buf);
self.i2c
.transfer(self.i2c_id, &[Command::MODE1 as u8], &mut buf);
let mode = buf[0];

self.i2c.send(self.i2c_id, &[Command::MODE1 as u8, mode | 0x10]);
self.i2c.send(self.i2c_id, &[Command::PRESCALE as u8, prescale]);
self.i2c
.send(self.i2c_id, &[Command::MODE1 as u8, mode | 0x10]);
self.i2c
.send(self.i2c_id, &[Command::PRESCALE as u8, prescale]);
self.i2c.send(self.i2c_id, &[Command::MODE1 as u8, mode]);
self.i2c.send(self.i2c_id, &[Command::MODE1 as u8, 0xA1]);
}
Expand All @@ -103,10 +106,25 @@ impl<'a> ServoArray<'a> {
pub fn set_duty_cycle(&mut self, i: usize, value: f64) {
let offset = ((i - 1) * 4) as u8;
let reg = (((MAX - 1) as f64) * f64::max(f64::min(value, 1.0), 0.0)) as u16;
println!("0 0 {:?} {:?}", (reg & 0xFF) as u8, ((reg >> 8) & 0xFF) as u8);
self.i2c.send(self.i2c_id, &[Command::LED0_ON_L as u8 + offset, 0]);
self.i2c.send(self.i2c_id, &[Command::LED0_ON_H as u8 + offset, 0]);
self.i2c.send(self.i2c_id, &[Command::LED0_OFF_L as u8 + offset, (reg & 0xFF) as u8]);
self.i2c.send(self.i2c_id, &[Command::LED0_OFF_H as u8 + offset, ((reg >> 8) & 0xFF) as u8]);
println!(
"0 0 {:?} {:?}",
(reg & 0xFF) as u8,
((reg >> 8) & 0xFF) as u8
);
self.i2c
.send(self.i2c_id, &[Command::LED0_ON_L as u8 + offset, 0]);
self.i2c
.send(self.i2c_id, &[Command::LED0_ON_H as u8 + offset, 0]);
self.i2c.send(
self.i2c_id,
&[Command::LED0_OFF_L as u8 + offset, (reg & 0xFF) as u8],
);
self.i2c.send(
self.i2c_id,
&[
Command::LED0_OFF_H as u8 + offset,
((reg >> 8) & 0xFF) as u8,
],
);
}
}
2 changes: 1 addition & 1 deletion servo-pca9685/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ extern crate servo_pca9685;
extern crate tessel;

use servo_pca9685::ServoArray;
use tessel::Tessel;
use std::thread::sleep;
use std::time::Duration;
use tessel::Tessel;

fn main() {
// Acquire port A.
Expand Down
6 changes: 3 additions & 3 deletions tessel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ name = "hello-rust"
doc = false

[dependencies]
unix_socket = "0.5.0"
lazy_static = "0.1"
unix_socket = "0.5"
lazy_static = "1.2"
atomic-option = "0.1"
bit-set = "0.4.0"
bit-set = "0.5"

[dev-dependencies]
tempfile = "2.1.4"
Loading