mirror of https://github.com/periph/devices
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
// Copyright 2018 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.
|
|
|
|
package apa102_test
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"log"
|
|
|
|
"periph.io/x/conn/v3/spi/spireg"
|
|
"periph.io/x/devices/v3/apa102"
|
|
"periph.io/x/host/v3"
|
|
)
|
|
|
|
func Example() {
|
|
// Make sure periph is initialized.
|
|
if _, err := host.Init(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Use spireg SPI port registry to find the first available SPI bus.
|
|
p, err := spireg.Open("")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer p.Close()
|
|
|
|
// Opens a 300 lights strip at 50% intensity with color temperature at
|
|
// 3500°Kelvin.
|
|
o := apa102.DefaultOpts
|
|
o.NumPixels = 300
|
|
o.Intensity = 127
|
|
o.Temperature = 3500
|
|
dev, err := apa102.New(p, &o)
|
|
if err != nil {
|
|
log.Fatalf("failed to open: %v", err)
|
|
}
|
|
img := image.NewNRGBA(image.Rect(0, 0, dev.Bounds().Dy(), 1))
|
|
for x := 0; x < img.Rect.Max.X; x++ {
|
|
img.SetNRGBA(x, 0, color.NRGBA{uint8(x), uint8(255 - x), 0, 255})
|
|
}
|
|
if err := dev.Draw(dev.Bounds(), img, image.Point{}); err != nil {
|
|
log.Fatalf("failed to draw: %v", err)
|
|
}
|
|
}
|
|
|
|
func ExampleToRGB() {
|
|
// Make sure periph is initialized.
|
|
if _, err := host.Init(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
// Use spireg SPI port registry to find the first available SPI bus.
|
|
p, err := spireg.Open("")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer p.Close()
|
|
|
|
o := apa102.PassThruOpts
|
|
o.NumPixels = 2
|
|
dev, err := apa102.New(p, &o)
|
|
if err != nil {
|
|
log.Fatalf("failed to open: %v", err)
|
|
}
|
|
if _, err = dev.Write(apa102.ToRGB([]color.NRGBA{{R: 0xFF, G: 0xFF, B: 0xFF}, {R: 0x80, G: 0x80, B: 0x80}})); err != nil {
|
|
log.Fatalf("failed to draw: %v", err)
|
|
}
|
|
}
|