https://github.com/novi/i2c-swift
I2C Bus Library for Swift
https://github.com/novi/i2c-swift
i2c linux raspberrypi swift
Last synced: about 1 year ago
JSON representation
I2C Bus Library for Swift
- Host: GitHub
- URL: https://github.com/novi/i2c-swift
- Owner: novi
- License: mit
- Created: 2017-01-17T05:50:33.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-02-06T10:50:08.000Z (over 9 years ago)
- Last Synced: 2025-04-15T15:13:06.676Z (about 1 year ago)
- Topics: i2c, linux, raspberrypi, swift
- Language: Swift
- Size: 13.7 KB
- Stars: 10
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# i2c-swift
I2C Bus Library for Swift
## Supported Platform
* [I2C tiny USB](https://github.com/novi/i2c_tiny_usb) adapter (Mac, Linux)
* I2C Kernel device (/dev/i2c-*) (Linux, Raspberry Pi etc...)
## Building
Install libusb package.
### macOS
```
$ brew install libusb-compat libusb
```
### Linux(Ubuntu 16.04)
```
$ sudo apt-get install i2c-tools libi2c-dev
```
## Usage
```swift
// 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](https://github.com/novi/i2c-swift-example) for every platform.