Skip to content

helins/linux.i2c.clj

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Linux.I2C

Clojars Project

Cljdoc

Easily use I2C and its subset SMBus from Clojure JVM.

Based on linux-i2c.java which provides an API around the standard Linux interface for talking to slave devices.

Usage

This is a small overview, the full API is available on Cljdoc.

In short, without error checking :

(require '[helins.linux.i2c       :as i2c]
         '[helins.linux.i2c.smbus :as smbus])

;; Selects the relevant "/dev/i2c-X" bus from the filesystem.
;;
(with-open [bus (i2c/bus "/dev/i2c-1")]

    ;; Selects a slave device.
    ;;
    (i2c/select-slave bus
                      0x24)

    ;; Reads 8 bytes.
    ;;
    (i2c/read bus
              8)
    ;; => [...]

    ;; Writes a few bytes
    ;;
    (i2c/write bus
               [42 1 2 3])

    ;; Does a transactions, several messages without interruption.
    ;;
    (i2c/transaction bus
                     [{::i2c/slave-address 0x24
                       ::i2c/write         [42 1 2 3]}
                      {::i2c/slave-address 0x24
                       ::i2c/read          4
                       ::i2c/tag           :some-read}])

    ;; => {:some-read [...]}



    ;; A few SMBus operations.

    (smbus/quick-write bus)

    (smbus/read-byte bus
                     42)

    (smbus/write-block bus
                       43
                       [1 2 3])
    )

Libraries targeting specific devices

Here are examples of libraries leveraging theses utilities for targetting specific I2C slave devices:

  • BME280, a triple temperature-humidity-pressure sensor
  • MCP342x, a family of A/D converters
  • Horter-I2HAE, a simple A/D converter

License

Copyright © 2017 Adam Helinski

Licensed under the term of the Mozilla Public License 2.0, see LICENSE.