I2C Bus Library for Swift
- I2C tiny USB adapter (Mac, Linux)
- I2C Kernel device (/dev/i2c-*) (Linux, Raspberry Pi etc...)
Install libusb package.
$ brew install libusb-compat libusb
$ sudo apt-get install i2c-tools libi2c-dev
// create a device (see table below.)
let device = try I2CTinyUSB()
// or
let device = try I2CBusDevice(portNumber: 0)
// write data and read 5 bytes of data to slave address 0x23
let readData = try device.write(toAddress: 0x23, data: dataToWrite, readBytes: 5)
readData.count == 5 // true
Platform | Bus connection | Driver | Class |
---|---|---|---|
macOS | I2C tiny USB | libusb | I2CTinyUSB |
Linux | I2C tiny USB | libusb | not supported, use /dev/i2c-* |
Linux | I2C tiny USB | /dev/i2c-* |
I2CBusDevice |
Linux | Native I2C bus (on GPIO) | /dev/i2c-* |
I2CBusDevice |
See demo project for every platform.