-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
137 lines (100 loc) · 3.38 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
---
output: github_document
editor_options:
markdown:
wrap: 72
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# rpigpior
<!-- <a href="https://mnr.github.io/rpigpior/"><img src="man/figures/logo.png" align="right" height="138"></a> -->
<!-- badges: start -->
[![Project Status: Active – The project has reached a stable, usable
state and is being actively
developed.](https://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/#active)
[![R-CMD-check](https://github.com/mnr/rpigpior/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/mnr/rpigpior/actions/workflows/R-CMD-check.yaml)
<!-- badges: end -->
`rpigpior` allows the R programming language access to the Raspberry Pi
GPIO using libgpiod. Because libgpiod is already included in Raspbian,
there is nothing else to install.
`rpigpior` provides these tools:
- `is.rpi` - Returns TRUE if this code is running under Raspbian
- `rpi_get` - Returns the value of a board level pin (1-40) or an
error if the pin isn't a data line (3.3v, 5v, or GROUND)
- `rpi_set` - Sets a pin to on or off
- `rpi_monitor` - watches a pin for a number of rising or falling
events
- `rpi_pwm` - provides diagnostics and support for hardware pulse
width modulation
- `rpi_i2c_get` - retrieve a value from an i2c connection
- `rpi_i2c_set` - set an i2c register to a value
## Installation
`rpigpior` is hosted on github. Until it arrives at cran, you can
install the development version of rpigpior from
[GitHub](https://github.com/mnr/rpigpior) with:
``` r
library(remotes)
remotes::install_github("mnr/rpigpior")
```
Most documentation will tell you to use devtools, but
`install.packages("devtools")` on a Raspberry Pi is an onerous task.
*However*...if you installed R on your Raspberry Pi using
[r4pi](https://r4pi.org/), you can use devtools just like normal.
``` r
# Start R with "sudo R" to install packages
# install.packages("devtools")
# You might want to switch back to non-admin R
library(devtools)
install_github("mnr/rpigpior")
```
Once you've installed, use `library` just like any other R package:
``` r
library(rpigpior)
```
## Is this a Raspberry Pi?
It's helpful to check if your code is running on a Raspberry Pi.
``` r
if ( is.rpi() ) {
print("Yes, this is a RPi")
} else {
print ("No, this is not a RPi")
}
```
## Reading a pin
If you connect a switch to board pin 40, this code will read it:
``` r
library(rpigpior)
rpi_get(40) # reads board pin 40
rpi_get(c(7,40)) # reads board pins 7 and 40
> GPIO004 GPIO21
0 1
```
That assumes the switch to board pin 40 is "pushed" or closed. By the
way, there is a diagram of this located in the *articles* section of the
website (as identified in the package description)
## Turning pins on and off
If you have a LEDs connected to board pin 19, 21, and 23, this code will
turn them on, then one of them off, then all of them off
```
library(rpigpior)
toggleThese <- c(19,21,23)
rpi_set(toggleThese,1)
Sys.sleep(1)
rpi_set(toggleThese, c(1,0,1))
Sys.sleep(1)
rpi_set(toggleThese, 0)
```
## Citation:
```{r}
citation("rpigpior")
```
## Related notes:
[Related mailing list:
linux-gpio\@vger.kernal.org](https://www.spinics.net/lists/linux-gpio/)