vi2c
is a tiny I2C communication library written in V.
struct I2CDevice
Represents an I2C device. Fields:
m_fd (int)
: File descriptor (-1 if not open).m_name (string)
: Name of the device.m_device_address (u8)
: I2C device address.m_device_filename (string)
: File path of the I2C device.m_is_connected (bool)
: Indicates if the device is connected.m_is_forced (bool)
: Indicates if the connection was forced.m_is_10bit (bool)
:true
if 10 bit address, otherwise 7 bit address.
Creates a new I2CDevice instance.
filename (string)
: File path of the I2C device.address (u8)
: I2C device address.name (string)
: Name of the device.is_10bit (bool)
:true
if 10 bit address, otherwise 7 bit address.
Connects to the I2C device.
force_connection (bool)
: Indicates if the connection should be forced. Force (or not) using this slave address, even if it is already in use by a driver.
Returns true
if the connection is successful, otherwise false
.
Reads data from the I2C device.
max_length (int)
: Maximum length of data to read.
Returns the number of bytes read and the data as a byte slice.
Writes data to the I2C device.
data ([]u8)
: Data to write.
Returns the number of bytes written.
Reads data from a register of the I2C device.
reg (u8)
: Register address to read from.max_length (int)
: Maximum length of data to read.
Returns the number of bytes read and the data as a byte slice.
Reads a register from the I2C device.
reg (u8)
: Register address to read.
Returns the value read from the register as a byte slice.
Writes data to a register of the I2C device.
reg (u8)
: Register address to write to.data ([]u8)
: Data to write.
Returns the number of bytes written.
Writes a value to a register of the I2C device.
reg (u8)
: Register address to write to.value (u8)
: Value to write.
Returns the number of bytes written.
Disconnects from the I2C device.
Checks if the connection to the I2C device was forced.
Returns true
if the connection was forced, otherwise false
.
Checks if the connection to the I2C device is established.
Returns true
if connected, otherwise false
.
Gets the name of the I2C device. Returns the name of the device.
Gets the file path of the I2C device. Returns the file path of the device.
Gets the address of the I2C device. Returns the address of the device.
Gets the file descriptor of the I2C device. Returns the file descriptor.
Sets the number of retries for I2C communication.
retries (int)
: The number of retries to set.
Returns (bool)
: true if the retries were successfully set, otherwise false
.
Sets the timeout for I2C communication.
timeout_ms (int)
: Timeout value in milliseconds.
Returns (bool)
: true
if the timeout was successfully set, otherwise false
.
Checks if the I2C device address is 10-bit.
Returns (bool)
: true if the I2C device address is 10-bit, otherwise false
.
Returns a formatted string representing the I2C device.
module main
import time
import vi2c
fn main() {
mut ic2_dev := vi2c.new('/dev/i2c-9', 0x48, 'Temp sensor')
println(ic2_dev)
if !ic2_dev.connect(true) {
println('Failed to connect')
return
}
println(ic2_dev)
for _ in 1 .. 100 {
len, data := ic2_dev.read_data_from_reg(~0, 2)
if len == 2 {
val := 0.00390625 * f32(u32(data[0]) << 8 | u32(data[1]))
println('data: <${data.hex()}> temp: ${val} *C')
}
time.sleep(1000000000)
}
ic2_dev.disconnect()
}
cd ~/.vmodules
cd vi2c/playground/
To compile it for Linux host machine (x64/x86-64), make sure to specify the include
path:
v -cflags '-I /usr/include/' . -o test_i2c_comm_x64
To cross-compile it for Aarch64, make sure that aarch64-linux-gnu-gcc
and corresponding libraries are installed.
Set aarch64-linux-gnu-gcc
as -cc
compiler, disable and add the include
of Aarch64 - in this case /usr/aarch64-linux-gnu/include/
.
v -cc aarch64-linux-gnu-gcc -gc none -cflags '--static -I /usr/aarch64-linux-gnu/include/' test_i2c_comm.v -o test_i2c_comm_aa64
NOTE: Please note that the library and the example code are tested in my Ubuntu 20.4 and an Aarch64 machine.