~xdavidwu/motion-control

f28dcabf45128e2ac3c3724b8a1a6c59b8ecdd08 — xdavidwu 2 years ago d1a365f
sensors: wrap errno into errors::error
M cmd/motion-control/main+gy801.ha => cmd/motion-control/main+gy801.ha +5 -5
@@ 1,4 1,5 @@
use ahrs::complementary;
use errors;
use fmt;
use fs;
use getopt;


@@ 10,7 11,6 @@ use net::ip;
use net::udp;
use os;
use proto::control;
use rt;
use sensors::ADXL345;
use sensors::L3G4200D;
use strconv;


@@ 93,8 93,8 @@ export fn main() int = {
		yield ADXL345;
	case let err: fs::error =>
		fmt::fatalf("Cannot open i2c device: {}", fs::strerror(err));
	case let err: rt::errno =>
		fmt::fatalf("Cannot ioctl i2c device: {}", rt::strerror(err));
	case let err: errors::error =>
		fmt::fatalf("Cannot ioctl i2c device: {}", errors::strerror(err));
	};
	defer ADXL345::destroy(ADXL345);



@@ 103,8 103,8 @@ export fn main() int = {
		yield L3G4200D;
	case let err: fs::error =>
		fmt::fatalf("Cannot open i2c device: {}", fs::strerror(err));
	case let err: rt::errno =>
		fmt::fatalf("Cannot ioctl i2c device: {}", rt::strerror(err));
	case let err: errors::error =>
		fmt::fatalf("Cannot ioctl i2c device: {}", errors::strerror(err));
	};
	defer L3G4200D::destroy(L3G4200D);


M sensors/ADXL345/ADXL345.ha => sensors/ADXL345/ADXL345.ha +11 -5
@@ 1,3 1,4 @@
use errors;
use fmt;
use fs;
use i2c;


@@ 6,28 7,33 @@ use io;
use os;
use rt;

export fn new(bus: int) (ADXL345 | fs::error | rt::errno) = {
export fn new(bus: int) (ADXL345 | fs::error | errors::error) = {
	const path = fmt::asprintf("/dev/i2c-{}", bus);
	defer free(path);
	const file = os::open(path, fs::flags::RDONLY)?;
	rt::ioctl(file, i2c::I2C_SLAVE, 0x53)?;
	match (rt::ioctl(file, i2c::I2C_SLAVE, 0x53)) {
	case let e: rt::errno =>
		return errors::errno(e);
	case =>
		yield;
	};
	return alloc(ADXL345_impl {
		file = file,
	});
};

export fn init(sensor: ADXL345) (void | rt::errno) = {
export fn init(sensor: ADXL345) (void | errors::error) = {
	i2c::smbus::write_byte_data(sensor.file, REG_POWER_CTL, POWER_CTL_MEASURE)?;
	i2c::smbus::write_byte_data(sensor.file, REG_DATA_FORMAT, DATA_FORMAT_FULL_RES)?;
};

fn read_data(sensor: ADXL345, reg: u8) (f64 | rt::errno) = {
fn read_data(sensor: ADXL345, reg: u8) (f64 | errors::error) = {
	const raw = i2c::smbus::read_word_data(sensor.file, reg)?;
	const raw = raw: i16;
	return raw: f64 * SCALE_FACTOR_FULL_RES;
};

export fn read(sensor: ADXL345) ((f64, f64, f64) | rt::errno) = {
export fn read(sensor: ADXL345) ((f64, f64, f64) | errors::error) = {
	return (
		read_data(sensor, REG_DATAX0)?,
		read_data(sensor, REG_DATAY0)?,

M sensors/L3G4200D/L3G4200D.ha => sensors/L3G4200D/L3G4200D.ha +11 -5
@@ 1,3 1,4 @@
use errors;
use fmt;
use fs;
use i2c;


@@ 6,17 7,22 @@ use io;
use os;
use rt;

export fn new(bus: int) (L3G4200D | fs::error | rt::errno) = {
export fn new(bus: int) (L3G4200D | fs::error | errors::error) = {
	const path = fmt::asprintf("/dev/i2c-{}", bus);
	defer free(path);
	const file = os::open(path, fs::flags::RDONLY)?;
	rt::ioctl(file, i2c::I2C_SLAVE, 0x69)?;
	match (rt::ioctl(file, i2c::I2C_SLAVE, 0x69)) {
	case let e: rt::errno =>
		return errors::errno(e);
	case =>
		yield;
	};
	return alloc(L3G4200D_impl {
		file = file,
	});
};

export fn init(sensor: L3G4200D) (void | rt::errno) = {
export fn init(sensor: L3G4200D) (void | errors::error) = {
	smbus::write_byte_data(sensor.file, REG_CTRL_REG1,
		CTRL_REG1_PD | CTRL_REG1_Zen | CTRL_REG1_Yen | CTRL_REG1_Xen | CTRL_REG1_DR_100HZ)?;
	smbus::write_byte_data(sensor.file, REG_CTRL_REG2, CTRL_REG2_HPCF_0_05HZ_AT_100HZ)?;


@@ 24,14 30,14 @@ export fn init(sensor: L3G4200D) (void | rt::errno) = {
	smbus::write_byte_data(sensor.file, REG_CTRL_REG5, CTRL_REG5_OUT_HP_ONLY)?;
};

fn read_data(sensor: L3G4200D, reg: u8) (f64 | rt::errno) = {
fn read_data(sensor: L3G4200D, reg: u8) (f64 | errors::error) = {
	const raw = smbus::read_byte_data(sensor.file, reg)?;
	const raw2 = smbus::read_byte_data(sensor.file, reg + 1)?;
	const raw = (raw: u16 + (raw2: u16 << 8)): i16;
	return raw: f64 * FS_250DPS_So;
};

export fn read(sensor: L3G4200D) ((f64, f64, f64) | rt::errno) = {
export fn read(sensor: L3G4200D) ((f64, f64, f64) | errors::error) = {
	return (
		read_data(sensor, REG_OUT_X_L)?,
		read_data(sensor, REG_OUT_Y_L)?,

M tools/sensors-dump/main+gy801.ha => tools/sensors-dump/main+gy801.ha +5 -5
@@ 1,11 1,11 @@
use ahrs::complementary;
use ahrs::tilt;
use ahrs::integration;
use errors;
use fmt;
use fs;
use io;
use os;
use rt;
use linux::timerfd;
use math;
use sensors::ADXL345;


@@ 36,8 36,8 @@ export fn main() void = {
		yield ADXL345;
	case let err: fs::error =>
		fmt::fatalf("Cannot open i2c device: {}", fs::strerror(err));
	case let err: rt::errno =>
		fmt::fatalf("Cannot ioctl i2c device: {}", rt::strerror(err));
	case let err: errors::error =>
		fmt::fatalf("Cannot ioctl i2c device: {}", errors::strerror(err));
	};
	defer ADXL345::destroy(ADXL345);



@@ 46,8 46,8 @@ export fn main() void = {
		yield L3G4200D;
	case let err: fs::error =>
		fmt::fatalf("Cannot open i2c device: {}", fs::strerror(err));
	case let err: rt::errno =>
		fmt::fatalf("Cannot ioctl i2c device: {}", rt::strerror(err));
	case let err: errors::error =>
		fmt::fatalf("Cannot ioctl i2c device: {}", errors::strerror(err));
	};
	defer L3G4200D::destroy(L3G4200D);