~xdavidwu/motion-control

d1a365fb927b303db6786662f02dfad9a94d91c7 — xdavidwu 2 years ago 3c08855
i2c: wrap errors to errors::error
1 files changed, 12 insertions(+), 6 deletions(-)

M i2c/smbus/libi2c.ha
M i2c/smbus/libi2c.ha => i2c/smbus/libi2c.ha +12 -6
@@ 1,38 1,44 @@
use errors;
use i2c;
use io;
use rt;

export fn access(file: io::file, read_write: u8, command: u8, _size: u32, data: *data)
		(int | rt::errno) = {
		(int | errors::error) = {
	const args = ioctl_data {
		read_write = read_write,
		command = command,
		_size = _size,
		data = data,
	};
	return rt::ioctl(file: int, i2c::I2C_SMBUS, &args);
	return match (rt::ioctl(file: int, i2c::I2C_SMBUS, &args)) {
	case let i: int =>
		yield i;
	case let e: rt::errno =>
		yield errors::errno(e);
	};
};

export fn read_byte_data(file: io::file, command: u8) (u8 | rt::errno) = {
export fn read_byte_data(file: io::file, command: u8) (u8 | errors::error) = {
	const data = data {...};
	access(file, READ, command, BYTE_DATA, &data)?;
	return data.byte;
};

export fn read_word_data(file: io::file, command: u8) (u16 | rt::errno) = {
export fn read_word_data(file: io::file, command: u8) (u16 | errors::error) = {
	const data = data {...};
	access(file, READ, command, WORD_DATA, &data)?;
	return data.word;
};

export fn write_byte_data(file: io::file, command: u8, value: u8) (void | rt::errno) = {
export fn write_byte_data(file: io::file, command: u8, value: u8) (void | errors::error) = {
	const data = data {
		byte = value,
	};
	access(file, WRITE, command, BYTE_DATA, &data)?;
};

export fn write_word_data(file: io::file, command: u8, value: u16) (void | rt::errno) = {
export fn write_word_data(file: io::file, command: u8, value: u16) (void | errors::error) = {
	const data = data {
		word = value,
	};