-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.go
208 lines (193 loc) · 5.06 KB
/
main.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2016 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
// apa102 writes to a strip of APA102 LED.
package main
import (
"errors"
"flag"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"periph.io/x/conn/v3/display"
"periph.io/x/conn/v3/physic"
"periph.io/x/conn/v3/spi"
"periph.io/x/conn/v3/spi/spireg"
"periph.io/x/devices/v3/apa102"
"periph.io/x/host/v3"
)
func access(name string) bool {
_, err := os.Stat(name)
return err == nil
}
func findFile(name string) string {
if access(name) {
return name
}
for _, p := range strings.Split(os.Getenv("GOPATH"), ":") {
if len(p) != 0 {
if p2 := filepath.Join(p, "src/periph.io/x/cmd/apa102", name); access(p2) {
return p2
}
}
}
return ""
}
// loadImg loads an image from disk.
func loadImg(name string) (image.Image, error) {
p := findFile(name)
if len(p) == 0 {
return nil, fmt.Errorf("couldn't find file %s", name)
}
/* #nosec G304 */
f, err := os.Open(p)
if err != nil {
return nil, err
}
/* #nosec G307 */
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
return nil, err
}
log.Printf("Image %s", name)
return img, nil
}
// resize is a simple but fast nearest neighbor implementation.
//
// If you need something better, please use one of the various high quality
// (slower!) Go packages available on github.
func resize(src image.Image, width, height int) *image.NRGBA {
srcMax := src.Bounds().Max
dst := image.NewNRGBA(image.Rect(0, 0, width, height))
for y := 0; y < height; y++ {
sY := (y*srcMax.Y + height/2) / height
for x := 0; x < width; x++ {
dst.Set(x, y, src.At((x*srcMax.X+width/2)/width, sY))
}
}
return dst
}
func showImage(disp display.Drawer, img image.Image, sleep time.Duration, loop bool, height int) {
r := disp.Bounds()
w := r.Dx()
orig := img.Bounds().Size()
if height == 0 {
height = img.Bounds().Dy()
}
p := image.Point{}
now := time.Now()
img = resize(img, w, height)
log.Printf("Resizing %dx%d -> %dx%d took %s", orig.X, orig.Y, w, height, time.Since(now))
now = time.Now()
for {
for p.Y = 0; p.Y < height; p.Y++ {
c := time.After(sleep)
if err := disp.Draw(disp.Bounds(), img, image.Point{}); err != nil {
log.Printf("error drawing: %v", err)
return
}
if p.Y == height-1 && !loop {
log.Printf("done %s", time.Since(now))
return
}
<-c
}
}
}
func mainImpl() error {
verbose := flag.Bool("v", false, "verbose mode")
spiID := flag.String("spi", "", "SPI port to use")
numPixels := flag.Int("n", apa102.DefaultOpts.NumPixels, "number of pixels on the strip")
intensity := flag.Int("l", int(apa102.DefaultOpts.Intensity), "light intensity [1-255]; 255 is full intensity")
temperature := flag.Int("t", int(apa102.DefaultOpts.Temperature), "light temperature in Kelvin [3500-7500]; 6500 is neutral")
globalPWM := flag.Bool("g", false, "disable the global PWM and perceptual mapping")
var hz physic.Frequency
flag.Var(&hz, "hz", "SPI port speed")
color := flag.String("color", "208020", "hex encoded color to show")
imgName := flag.String("img", "", "image to load")
lineMs := flag.Int("linems", 2, "number of ms to show each line of the image")
imgLoop := flag.Bool("imgloop", false, "loop the image")
imgHeight := flag.Int("imgh", 0, "resize the Y axis of the image")
flag.Parse()
if !*verbose {
log.SetOutput(ioutil.Discard)
}
log.SetFlags(log.Lmicroseconds)
if flag.NArg() != 0 {
return errors.New("unexpected argument, try -help")
}
if *intensity > 255 {
return errors.New("max intensity is 255")
}
if *temperature > 65535 {
return errors.New("max temperature is 65535")
}
if _, err := host.Init(); err != nil {
return err
}
// Open the display device.
s, err := spireg.Open(*spiID)
if err != nil {
return err
}
defer s.Close()
if hz != 0 {
if err = s.LimitSpeed(hz); err != nil {
return err
}
}
if p, ok := s.(spi.Pins); ok {
// TODO(maruel): Print where the pins are located.
log.Printf("Using pins CLK: %s MOSI: %s MISO: %s", p.CLK(), p.MOSI(), p.MISO())
}
o := apa102.DefaultOpts
o.NumPixels = *numPixels
o.Intensity = uint8(*intensity)
o.Temperature = uint16(*temperature)
o.DisableGlobalPWM = *globalPWM
disp, err := apa102.New(s, &o)
if err != nil {
return err
}
// Load an image and make it loop through the pixels.
if len(*imgName) != 0 {
var img image.Image
if img, err = loadImg(*imgName); err != nil {
return err
}
showImage(disp, img, time.Duration(*lineMs)*time.Millisecond, *imgLoop, *imgHeight)
return nil
}
// Shows how to create a color array.
rgb, err := strconv.ParseUint(*color, 16, 32)
if err != nil {
return err
}
r := byte(rgb >> 16)
g := byte(rgb >> 8)
b := byte(rgb)
buf := make([]byte, *numPixels*3)
for i := 0; i < len(buf); i += 3 {
buf[i] = r
buf[i+1] = g
buf[i+2] = b
}
_, err = disp.Write(buf)
return err
}
func main() {
if err := mainImpl(); err != nil {
fmt.Fprintf(os.Stderr, "apa102: %s.\n", err)
os.Exit(1)
}
}