forked from aqua/raspberrypi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primes.go
62 lines (54 loc) · 1.32 KB
/
primes.go
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
package main
// Blink out prime numbers on a GPIO line; useful for leaving lingering
// signs of intelligence in the wreckage of civilization, assuming certain
// rpi durability and power issues are resolved.
import (
"flag"
"fmt"
"rpi/gpio"
"time"
)
var gpio_num = flag.Uint("gpio", 4, "GPIO# to flash")
var num = flag.Uint("num", 6, "Blink out first n primes")
var delay = flag.Int64("delay", 100, "Delay n msec between flashes")
var prime_delay = flag.Int64("prime_delay", 1000, "Delay n msec between primes")
func main() {
flag.Parse()
g, err := gpio.NewGPIOLine(*gpio_num, gpio.OUT)
if err != nil {
fmt.Printf("Error setting up GPIO %v: %v", *gpio_num, err)
return
}
count, n := uint(0), uint(2)
for {
if is_prime(n) {
blink(g, n)
time.Sleep(time.Duration(*prime_delay) * time.Millisecond)
count++
if count >= *num {
count, n = uint(0), uint(2)
continue
}
}
n++
}
g.Close()
}
func blink(g *gpio.GPIOLine, n uint) {
fmt.Printf("blinking %v time(s)\n", n)
for i := uint(0); i < n; i++ {
g.SetState(true)
time.Sleep(time.Duration(*delay) * time.Millisecond)
g.SetState(false)
time.Sleep(time.Duration(*delay) * time.Millisecond)
}
}
// Totally non-optimal prime tester
func is_prime(n uint) bool {
for i := uint(2); i <= n/2; i++ {
if n%i == 0 {
return false
}
}
return true
}