Liking cljdoc? Tell your friends :D

helins.linux.gpio

This namespace provides utilities for handling a GPIO device. Compatible with Linux 4.8 and higher.

Access to a GPIO device can be obtained by using the device function. Three things can then be accomplished:

  • Request some information about the GPIO device or a specific GPIO line.
  • Request a handle for driving one or several lines at once.
  • Request a watcher for efficiently monitoring one or several lines.

A handle controls all the lines it is responsible for at once. Whether those lines are actually driven exactly at the same time, atomically, depends on the underlying driver and is opaque to this library. Once a handle is created with the handle function on the appropriate GPIO device, one or several dedicated buffers can be created with the buffer function. The purpose of a buffer is to represent and/or manipulate the state of the lines controlled by a handle (up to 64). The state of a line can either be HIGH (true) or LOW (false).

A watcher is used essentially for interrupts. Requested events, such as a line transitioning from LOW to HIGH, are queued by the kernel until read. However, Linux not being a real-time OS, such "interrupts" are imperfect and should not be used for critical tasks where a simple microcontroller will be of a better fit.

Lines are abstracted by associating a line number with a tag (ie. any value meant to be more representative of what the line does, often a keyword).

As usual, all IO functions might throw if anything goes wrong and do not forget to use close or the "with-open idiom" on all acquired IO resources. Closing a GPIO device do not close related handles and watchers.

Here is a description of the keywords typically used throughout this library :

:gpio/active-low? Lines are typically active-high, meaning they become active when HIGH (true) and inactive when LOW (false). Active-low lines reverse this logical flow. They become active when LOW and inactive when HIGH.

:gpio/consumer An arbitrary string can be supplied when creating a handle. If a line is in use and associated with a consumer, it will show up when querying its state using describe-line. It allows for tracking who is using what.

:gpio/direction A line can either be used as :input or :output, never both at the same time. All lines associated with a handle must be of the same direction.

:gpio/label The operating system might associate a GPIO device and individual lines with string labels.

:gpio/line-number A single GPIO device can represent at most 64 lines. Hence, a line number is between 0 and 63 inclusive.

:gpio/name The operating system might associate a GPIO device and individual lines with string names.

:gpio/open-drain? Lines can act as open drains (ie. can only be driven to LOW).

:gpio/open-source? Lines can be open sources (ie. can only be driven to HIGH).

:gpio/state The state of a line can either be logical HIGH (true) or logical LOW (false).

:gpio/tag Instead of using raw line numbers which don't really mean anything, they can be associated with any value such as a string or a keyword. Besides being more descriptive, the program remains valid when the user dedices to use different lines while creating a handle (as long as tags remain the same).

This namespace provides utilities for handling a GPIO device. Compatible with Linux 4.8 and higher.

Access to a GPIO device can be obtained by using the `device` function. Three things can then be accomplished:

- Request some information about the GPIO device or a specific GPIO line.
- Request a handle for driving one or several lines at once.
- Request a watcher for efficiently monitoring one or several lines.

A handle controls all the lines it is responsible for at once. Whether those lines are actually driven exactly at
the same time, atomically, depends on the underlying driver and is opaque to this library. Once a handle is created
with the `handle` function on the appropriate GPIO device, one or several dedicated buffers can be created with the
`buffer` function. The purpose of a buffer is to represent and/or manipulate the state of the lines controlled by a
handle (up to 64). The state of a line can either be HIGH (true) or LOW (false).

A watcher is used essentially for interrupts. Requested events, such as a line transitioning from LOW to HIGH, are
queued by the kernel until read. However, Linux not being a real-time OS, such "interrupts" are imperfect and should
not be used for critical tasks where a simple microcontroller will be of a better fit.

Lines are abstracted by associating a line number with a tag (ie. any value meant to be more representative of what the
line does, often a keyword).

As usual, all IO functions might throw if anything goes wrong and do not forget to use `close` or the "with-open idiom"
on all acquired IO resources. Closing a GPIO device do not close related handles and watchers.


Here is a description of the keywords typically used throughout this library :

`:gpio/active-low?`
Lines are typically active-high, meaning they become active when HIGH (true) and inactive when LOW (false). Active-low
lines reverse this logical flow. They become active when LOW and inactive when HIGH.

`:gpio/consumer`
An arbitrary string can be supplied when creating a handle. If a line is in use and associated with a consumer, it will
show up when querying its state using `describe-line`. It allows for tracking who is using what.

`:gpio/direction`
A line can either be used as :input or :output, never both at the same time. All lines associated
with a handle must be of the same direction.

`:gpio/label`
The operating system might associate a GPIO device and individual lines with string labels.

`:gpio/line-number`
A single GPIO device can represent at most 64 lines. Hence, a line number is between 0 and 63 inclusive.

`:gpio/name`
The operating system might associate a GPIO device and individual lines with string names.

`:gpio/open-drain?`
Lines can act as open drains (ie. can only be driven to LOW).

`:gpio/open-source?`
Lines can be open sources (ie. can only be driven to HIGH).

`:gpio/state`
The state of a line can either be logical HIGH (true) or logical LOW (false).

`:gpio/tag`
Instead of using raw line numbers which don't really mean anything, they can be associated with any value such
as a string or a keyword. Besides being more descriptive, the program remains valid when the user dedices to use
different lines while creating a handle (as long as tags remain the same).
raw docstring

closeclj

(close resource)

Closes a GPIO resource such as a device or a handle.

It may be easier to use the standard with-open macro.

Closes a GPIO resource such as a device or a handle.

It may be easier to use the standard `with-open` macro.
sourceraw docstring

default+clj

Default values for argument options.

Default values for argument options.
sourceraw docstring

describe-chipclj

(describe-chip device)

Requests a description of the given GPIO device.

Returns a map such as:

KeyOptional?Description
:gpio/labelTrueSee namespace description
:gpio/n-lineFalseNumber of available lines for that chip
:gpio/nameTrueSee namespace description
Requests a description of the given GPIO device.

Returns a map such as:

| Key | Optional? | Description |
|---|---|---|
| :gpio/label | True | See namespace description |
| :gpio/n-line | False | Number of available lines for that chip |
| :gpio/name | True | See namespace description |
sourceraw docstring

describe-lineclj

(describe-line device line-number)

Requests a description of the given line.

Returns a map such as:

KeyOptional?Description
:gpio/active-low?FalseSee namespace description
:gpio/consumerTrueSee namespace description
:gpio/directionFalseSee namespace description
:gpio/line-numberFalseNumber of the described line
:gpio/nameTrueSee namespace description
:gpio/open-drain?FalseSee namespace description
:gpio/open-source?FalseSee namespace description
:gpio/used?FalseIs this line currently in use?
Requests a description of the given line.

Returns a map such as:

| Key | Optional? | Description |
|---|---|---|
| :gpio/active-low? | False | See namespace description |
| :gpio/consumer | True | See namespace description |
| :gpio/direction | False | See namespace description |
| :gpio/line-number | False | Number of the described line |
| :gpio/name | True | See namespace description |
| :gpio/open-drain? | False | See namespace description |
| :gpio/open-source? | False | See namespace description |
| :gpio/used? | False | Is this line currently in use? |
sourceraw docstring

deviceclj

(device device-path)

Opens a GPIO device either by specifying a full path or by giving the number of the device.

Those devices are located at '/dev/gpiochip$X' where $X is a number.

Read permission must be granted, which is enough even for writing to outputs.

Attention, closing a GPIO device does not close related handles and watchers.

Opens a GPIO device either by specifying a full path or by giving the number of the device.

Those devices are located at '/dev/gpiochip$X' where $X is a number.

Read permission must be granted, which is enough even for writing to outputs.

Attention, closing a GPIO device does not close related handles and watchers.
sourceraw docstring

handleclj

(handle device lines-number->line-option+)
(handle device line-number->line-option+ handle-option+)

Given a GPIO device, requests a handle for one or several lines which can then be used to read and/or write the state of lines.

Implements IHandle.

line-number->line-option+ is a map of line number -> description map containing:

  • :gpio/state
  • :gpio/tag

handle-option+ is an optional map which may contain :

  • :gpio/active-low?
  • :gpio/consumer
  • :gpio/direction
  • :gpio/open-drain?
  • :gpio/open-source?

See namespace docstring for description.

(handle some-device
        {17 {:gpio/tag :red-led}
         27 {:gpio/tag   :green-led
             :gpio/state true}}
        {:gpio/direction :output})
Given a GPIO device, requests a handle for one or several lines which can then be used to read and/or write
the state of lines.

Implements `IHandle`.


`line-number->line-option+` is a map of `line number` -> `description map` containing: 

- :gpio/state
- :gpio/tag


`handle-option+` is an optional map which may contain :

-  :gpio/active-low?
-  :gpio/consumer
-  :gpio/direction
-  :gpio/open-drain?
-  :gpio/open-source?

See namespace docstring for description.


```clojure
(handle some-device
        {17 {:gpio/tag :red-led}
         27 {:gpio/tag   :green-led
             :gpio/state true}}
        {:gpio/direction :output})
```
sourceraw docstring

IBuffercljprotocol

Reading or writing the state of lines in a buffer before or after doing some IO.

(write some-handle
       (-> some-buffer
           (clear-line+)
           (set-line+ {:green-led true
                       :red-led   false})))
Reading or writing the state of lines in a buffer before or after doing some IO.

```clojure
(write some-handle
       (-> some-buffer
           (clear-line+)
           (set-line+ {:green-led true
                       :red-led   false})))
```

clear-line+clj

(clear-line+ buffer)

Sets all lines of the given buffer to LOW (false).

Sets all lines of the given buffer to LOW (false).

get-lineclj

(get-line buffer tag)

Retrieves the state of a single line from the given buffer.

Retrieves the state of a single line from the given buffer.

get-line+clj

(get-line+ buffer)
(get-line+ buffer tags)

Retrieves the state of several lines (or all of them if nothing is specified) from the given buffer.

Returns a map of tag -> state.

Retrieves the state of several lines (or all of them if nothing is specified) from the given buffer.

Returns a map of tag -> state.

set-lineclj

(set-line buffer tag state)

Sets the state of a single line in the given buffer.

Sets the state of a single line in the given buffer.

set-line+clj

(set-line+ buffer tag->state)

Sets the state of several lines in the given buffer.

Sets the state of several lines in the given buffer.

toggle-lineclj

(toggle-line buffer tag)

Toggles the state of a single line in the given buffer.

Toggles the state of a single line in the given buffer.

toggle-line+clj

(toggle-line+ buffer)
(toggle-line+ buffer tags)

Toggles the state of several lines in the given buffer.

Toggles the state of several lines in the given buffer.
sourceraw docstring

IBuffereablecljprotocol

For objects that can work with a GPIO buffer.

For objects that can work with a GPIO buffer.

bufferclj

(buffer this)

Produces a GPIO buffer representing the state of up to 64 lines.

It implements IBuffer and works with tags associated with this.

It does not do any IO on its own as it is meant to be used in conjunction with a handle or a watcher.

Produces a GPIO buffer representing the state of up to 64 lines.

It implements `IBuffer` and works with tags associated with `this`.

It does not do any IO on its own as it is meant to be used in conjunction with a
handle or a watcher.
sourceraw docstring

IHandlecljprotocol

IO using a GPIO handle and a associated buffer.

Reading or writing the state of lines happens virtually at once for all of them. Whether it happens exactly at the same time depends on the underlying driver and this fact is opaque. In any case, driving several lines using a single handle is more efficient than using a handle for each line.

IO using a GPIO handle and a associated buffer.

Reading or writing the state of lines happens virtually at once for all of them. Whether it happens exactly
at the same time depends on the underlying driver and this fact is opaque. In any case, driving several
lines using a single handle is more efficient than using a handle for each line.

readclj

(read handle buffer)

Using a handle, reads the current state of lines into the given buffer.

Using a handle, reads the current state of lines into the given buffer.

writeclj

(write handle buffer)

Using a handle, writes the current state of lines using the given buffer.

Using a handle, writes the current state of lines using the given buffer.
sourceraw docstring

IWatchercljprotocol

IO using a watcher.

IO using a watcher.

eventclj

(event watcher)
(event watcher timeout-ms)

Using a watcher, efficiently blocks until the state of one of the monitored inputs switches to a relevant one or the timeout elapses.

A timeout of -1 will block forever until something happens.

Using a watcher, efficiently blocks until the state of one of the monitored inputs switches to a relevant
one or the timeout elapses.

A timeout of -1 will block forever until something happens.

pollclj

(poll watcher buffer tag)

Using a watcher, reads the current state of a line.

Using a watcher, reads the current state of a line.
sourceraw docstring

watcherclj

(watcher device line-number->watch-option+)

Given a GPIO device, produces a watcher which can then be used to efficiently monitor inputs for changes or poll their current values.

Implements IWatcher.

line-number->watch-option+ is a map of line number -> description map which may contain :

  • :gpio/active-low?
  • :gpio/consumer
  • :gpio/direction
  • :gpio/open-drain?
  • :gpio/open-source?
  • :gpio/state
  • :gpio/tag

See namespace docstring for description.

(watcher some-device
         {18 {:gpio/tag       :left-button
              :gpio/direction :input}
          23 {:gpio/tag       :right-button
              :gpio/direction :output}})
Given a GPIO device, produces a watcher which can then be used to efficiently monitor inputs for changes or poll
their current values.

Implements `IWatcher`.


`line-number->watch-option+` is a map of `line number` -> `description map` which may contain :

- :gpio/active-low?
- :gpio/consumer
- :gpio/direction
- :gpio/open-drain?
- :gpio/open-source?
- :gpio/state
- :gpio/tag

See namespace docstring for description.


```clojure
(watcher some-device
         {18 {:gpio/tag       :left-button
              :gpio/direction :input}
          23 {:gpio/tag       :right-button
              :gpio/direction :output}})
```
sourceraw docstring

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close