add userguide for GPIO

related to https://github.com/google/periph/issues/432
fixes https://github.com/gokrazy/gokrazy/issues/33
This commit is contained in:
Michael Stapelberg 2020-06-15 09:56:33 +02:00
parent 59d57578b9
commit 1ec8cbf7d9
17 changed files with 221 additions and 3 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 618 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 283 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 562 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

View File

@ -2,6 +2,10 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>https://gokrazy.org/userguide/gpio/</loc>
</url>
<url>
<loc>https://gokrazy.org/userguide/remotesyslog/</loc>
</url>

View File

@ -0,0 +1,122 @@
<!DOCTYPE html>
<html> <head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Controlling a GPIO input/output pin</title>
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
<link href="/jumbotron-narrow.css" rel="stylesheet">
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400">
<style type="text/css">
body {
font-family: "Open Sans";
}
.table-striped>tr:nth-child(odd){
background-color:red;
}
</style>
</head>
<body>
<div class="container"><div class="header"><nav>
<ul class="nav nav-pills pull-right">
<li role="presentation" class=""><a href="/">Home </a></li>
<li role="presentation" class=""><a href="/platforms/">Platforms </a></li>
<li role="presentation" class=""><a href="/quickstart/">Quickstart </a></li>
<li role="presentation" class=""><a href="/showcase/">Showcase </a></li>
<li role="presentation" class=""><a href="/userguide/">Userguide </a></li>
<li role="presentation" class=""><a href="https://github.com/gokrazy/gokrazy">Source </a></li>
</ul>
</nav>
<h3 class="text-muted">gokrazy</h3>
</div>
<h1 id="controlling-a-gpio-inputoutput-pin">Controlling a GPIO input/output pin</h1>
<p>In this guide, we are using <a href="https://periph.io/">periph.io</a>, a library for
peripheral I/O in Go, to set one of the Raspberry Pis General Purpose I/O
(GPIO) pins to a logical high (3.3V) or low (0V) signal.</p>
<p>periph.io supports the Raspberry Pi 3 and Raspberry Pi 4, starting with version
<code>v3.6.4</code>.</p>
<h2 id="connect-gpio-pins-based-on-pinout">Connect GPIO pins based on pinout</h2>
<p><a href="https://pinout.xyz"><img src="/img/raspberry-pi-pinout.png" width="700" align="right" style="border: 1px solid grey; margin-bottom: 2em; margin-top: 1em"></a></p>
<p>To verify the code is doing what we expect, lets connect a multimeter as per
<a href="https://pinout.xyz">pinout.xyz</a>s pinout:</p>
<ul>
<li>pin number 18 (signal <code>BCM24</code>, labeled <code>24</code> in the pinout above)</li>
<li>pin number 20 (signal <code>GND</code>)</li>
</ul>
<p>We need to set the multimeter to “Voltage measurement, DC (direct current)”.</p>
<h2 id="setting-an-output-pin-signal">Setting an output pin signal</h2>
<p>To set the pin high and low, alternatingly, with a 5 second frequency, we will
be using the <code>hello-gpio</code> program, which is a slightly modified version of the
example at <a href="https://periph.io/device/led/">periph.io/device/led</a>:</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#f92672">package</span> <span style="color:#a6e22e">main</span>
<span style="color:#f92672">import</span> (
<span style="color:#e6db74">&#34;log&#34;</span>
<span style="color:#e6db74">&#34;time&#34;</span>
<span style="color:#e6db74">&#34;periph.io/x/periph/conn/gpio&#34;</span>
<span style="color:#e6db74">&#34;periph.io/x/periph/host&#34;</span>
<span style="color:#e6db74">&#34;periph.io/x/periph/host/rpi&#34;</span>
)
<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">doGPIO</span>() <span style="color:#66d9ef">error</span> {
<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;Loading periph.io drivers&#34;</span>)
<span style="color:#75715e">// Load periph.io drivers:
</span><span style="color:#75715e"></span> <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">_</span>, <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">host</span>.<span style="color:#a6e22e">Init</span>(); <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">err</span>
}
<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;Toggling GPIO forever&#34;</span>)
<span style="color:#a6e22e">t</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">NewTicker</span>(<span style="color:#ae81ff">5</span> <span style="color:#f92672">*</span> <span style="color:#a6e22e">time</span>.<span style="color:#a6e22e">Second</span>)
<span style="color:#66d9ef">for</span> <span style="color:#a6e22e">l</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">gpio</span>.<span style="color:#a6e22e">Low</span>; ; <span style="color:#a6e22e">l</span> = !<span style="color:#a6e22e">l</span> {
<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Printf</span>(<span style="color:#e6db74">&#34;setting GPIO pin number 18 (signal BCM24) to %v&#34;</span>, <span style="color:#a6e22e">l</span>)
<span style="color:#75715e">// Lookup a pin by its location on the board:
</span><span style="color:#75715e"></span> <span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">rpi</span>.<span style="color:#a6e22e">P1_18</span>.<span style="color:#a6e22e">Out</span>(<span style="color:#a6e22e">l</span>); <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
<span style="color:#66d9ef">return</span> <span style="color:#a6e22e">err</span>
}
<span style="color:#f92672">&lt;-</span><span style="color:#a6e22e">t</span>.<span style="color:#a6e22e">C</span>
}
<span style="color:#66d9ef">return</span> <span style="color:#66d9ef">nil</span>
}
<span style="color:#66d9ef">func</span> <span style="color:#a6e22e">main</span>() {
<span style="color:#66d9ef">if</span> <span style="color:#a6e22e">err</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">doGPIO</span>(); <span style="color:#a6e22e">err</span> <span style="color:#f92672">!=</span> <span style="color:#66d9ef">nil</span> {
<span style="color:#a6e22e">log</span>.<span style="color:#a6e22e">Fatal</span>(<span style="color:#a6e22e">err</span>)
}
}
</code></pre></div><p>Install the program on your Raspberry Pi using gokrazy (see <a href="/quickstart/">Quickstart</a>):</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-shell" data-lang="shell">gokr-packer <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span> -update<span style="color:#f92672">=</span>yes <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span> github.com/gokrazy/hello <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span> github.com/gokrazy/breakglass <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span> github.com/gokrazy/serial-busybox <span style="color:#ae81ff">\
</span><span style="color:#ae81ff"></span> github.com/gokrazy/hello-gpio
</code></pre></div><p>…and wait a few seconds for it to reboot.</p>
<p>At this point, we should be able to see the high/low signal on the multimeter,
alternating between 3.3V (high) and 0V (low) every 5 seconds:</p>
<p><a href="/img/2020-06-15-gpio.jpg"><img src="/img/2020-06-15-gpio.thumb.jpg" srcset="/img/2020-06-15-gpio.thumb.2x.jpg 2x,/img/2020-06-15-gpio.thumb.3x.jpg 3x" width="700" style="border: 1px solid grey; margin-bottom: 2em; margin-top: 1em"></a></p>
<footer class="footer" style="text-align: center">
<p>© 2017 gokrazy authors (Michael Stapelberg and contributors)</p>
</footer>
</div>
</body>
</html>
</div>
</body>
</html>

View File

@ -57,6 +57,10 @@ GitHub</a>.</p>
<ul>
<li>
<a href="https://gokrazy.org/userguide/gpio/">Controlling a GPIO input/output pin</a>
</li>
<li>
<a href="https://gokrazy.org/userguide/remotesyslog/">Using Remote Syslog to send gokrazy logs over the network</a>
</li>

View File

@ -0,0 +1,88 @@
---
title: "Controlling a GPIO input/output pin"
weight: 10
---
# Controlling a GPIO input/output pin
In this guide, we are using [periph.io](https://periph.io/), a library for
peripheral I/O in Go, to set one of the Raspberry Pis General Purpose I/O
(GPIO) pins to a logical high (3.3V) or low (0V) signal.
periph.io supports the Raspberry Pi 3 and Raspberry Pi 4, starting with version
`v3.6.4`.
## Connect GPIO pins based on pinout
<a href="https://pinout.xyz"><img src="/img/raspberry-pi-pinout.png" width="700" align="right" style="border: 1px solid grey; margin-bottom: 2em; margin-top: 1em"></a>
To verify the code is doing what we expect, lets connect a multimeter as per
[pinout.xyz](https://pinout.xyz)s pinout:
* pin number 18 (signal `BCM24`, labeled `24` in the pinout above)
* pin number 20 (signal `GND`)
We need to set the multimeter to “Voltage measurement, DC (direct current)”.
## Setting an output pin signal
To set the pin high and low, alternatingly, with a 5 second frequency, we will
be using the `hello-gpio` program, which is a slightly modified version of the
example at [periph.io/device/led](https://periph.io/device/led/):
```go
package main
import (
"log"
"time"
"periph.io/x/periph/conn/gpio"
"periph.io/x/periph/host"
"periph.io/x/periph/host/rpi"
)
func doGPIO() error {
log.Printf("Loading periph.io drivers")
// Load periph.io drivers:
if _, err := host.Init(); err != nil {
return err
}
log.Printf("Toggling GPIO forever")
t := time.NewTicker(5 * time.Second)
for l := gpio.Low; ; l = !l {
log.Printf("setting GPIO pin number 18 (signal BCM24) to %v", l)
// Lookup a pin by its location on the board:
if err := rpi.P1_18.Out(l); err != nil {
return err
}
<-t.C
}
return nil
}
func main() {
if err := doGPIO(); err != nil {
log.Fatal(err)
}
}
```
Install the program on your Raspberry Pi using gokrazy (see [Quickstart](/quickstart/)):
```shell
gokr-packer \
-update=yes \
github.com/gokrazy/hello \
github.com/gokrazy/breakglass \
github.com/gokrazy/serial-busybox \
github.com/gokrazy/hello-gpio
```
…and wait a few seconds for it to reboot.
At this point, we should be able to see the high/low signal on the multimeter,
alternating between 3.3V (high) and 0V (low) every 5 seconds:
<a href="/img/2020-06-15-gpio.jpg"><img src="/img/2020-06-15-gpio.thumb.jpg" srcset="/img/2020-06-15-gpio.thumb.2x.jpg 2x,/img/2020-06-15-gpio.thumb.3x.jpg 3x" width="700" style="border: 1px solid grey; margin-bottom: 2em; margin-top: 1em"></a>

View File

@ -1,6 +1,6 @@
---
title: "Using Remote Syslog to send gokrazy logs over the network"
weight: 10
weight: 20
---
# Using Remote Syslog to send gokrazy logs over the network

View File

@ -1,6 +1,6 @@
---
title: "Using TLS in untrusted networks"
weight: 20
weight: 30
---
# Using TLS in untrusted networks

View File

@ -1,6 +1,6 @@
---
title: "Connecting to unencrypted WiFi networks"
weight: 30
weight: 40
---
# Connecting to unencrypted WiFi networks

Binary file not shown.

After

Width:  |  Height:  |  Size: 618 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 283 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 562 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB