Skip to content

Commit

Permalink
apa102: make spimode part of the Opts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgth committed Aug 10, 2021
1 parent c768f7d commit 20c235b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
24 changes: 14 additions & 10 deletions apa102/apa102.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ const NeutralTemp uint16 = 6500

// DefaultOpts is the recommended default options.
var DefaultOpts = Opts{
NumPixels: 150, // 150 LEDs is a common strip length.
Intensity: 255, // Full blinding power.
Temperature: 5000, // More pleasing white balance than NeutralTemp.
DisableGlobalPWM: false, // Use full 13 bits range.
NumPixels: 150, // 150 LEDs is a common strip length.
Intensity: 255, // Full blinding power.
Temperature: 5000, // More pleasing white balance than NeutralTemp.
DisableGlobalPWM: false, // Use full 13 bits range.
SpiMode: spi.Mode3, // SPI Mode3 works on most devices.
}

// PassThruOpts makes the driver draw RGB pixels exactly as specified.
Expand All @@ -51,6 +52,7 @@ var PassThruOpts = Opts{
Intensity: 255,
Temperature: NeutralTemp,
DisableGlobalPWM: true,
SpiMode: spi.Mode3,
}

// Opts defines the options for the device.
Expand Down Expand Up @@ -81,6 +83,12 @@ type Opts struct {
// to 8 bits, this also disables the dynamic perceptual mapping of intensity
// since there is not enough bits of resolution to do it effectively.
DisableGlobalPWM bool
// SpiMode sets the clock polarity and phase as one of the 4 possible SPI Modes.
//
// Most devices can use spi.Mode3, but the Raspberry Pi 3 secondary SPI port
// for example does not support this Mode. You may need to use spi.Mode0
// in this and similar cases.
SpiMode spi.Mode
}

// New returns a strip that communicates over SPI to APA102 LEDs.
Expand All @@ -92,12 +100,8 @@ type Opts struct {
// As per APA102-C spec, the chip's max refresh rate is 400hz.
// https://en.wikipedia.org/wiki/Flicker_fusion_threshold is a recommended
// reading.
//
// Most devices can use spi.Mode3, but the raspberry pi 3 secondary SPI
// for example does not support this Mode. You may need to use spi.Mode0
// in this and similar cases.
func New(p spi.Port, o *Opts, spiMode spi.Mode) (*Dev, error) {
c, err := p.Connect(20*physic.MegaHertz, spiMode, 8)
func New(p spi.Port, o *Opts) (*Dev, error) {
c, err := p.Connect(20*physic.MegaHertz, o.SpiMode, 8)
if err != nil {
return nil, err
}
Expand Down
28 changes: 14 additions & 14 deletions apa102/apa102_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ func TestDevEmpty(t *testing.T) {
buf := bytes.Buffer{}
o := DefaultOpts
o.NumPixels = 0
d, _ := New(spitest.NewRecordRaw(&buf), &o, spi.Mode3)
d, _ := New(spitest.NewRecordRaw(&buf), &o)
if n, err := d.Write([]byte{}); n != 0 || err != nil {
t.Fatalf("%d %v", n, err)
}
Expand All @@ -346,7 +346,7 @@ func TestDevEmpty(t *testing.T) {
}

func TestConnectFail(t *testing.T) {
if d, err := New(&configFail{}, &DefaultOpts, spi.Mode3); d != nil || err == nil {
if d, err := New(&configFail{}, &DefaultOpts); d != nil || err == nil {
t.Fatal("Connect() call have failed")
}
}
Expand All @@ -355,7 +355,7 @@ func TestDevLen(t *testing.T) {
buf := bytes.Buffer{}
o := DefaultOpts
o.NumPixels = 1
d, _ := New(spitest.NewRecordRaw(&buf), &o, spi.Mode3)
d, _ := New(spitest.NewRecordRaw(&buf), &o)
if n, err := d.Write([]byte{0}); n != 0 || err == nil {
t.Fatalf("%d %v", n, err)
}
Expand Down Expand Up @@ -555,7 +555,7 @@ func TestWrites(t *testing.T) {
for _, tt := range writeTests {
buf := bytes.Buffer{}
tt.opts.NumPixels = len(tt.pixels) / 3
d, _ := New(spitest.NewRecordRaw(&buf), &tt.opts, spi.Mode3)
d, _ := New(spitest.NewRecordRaw(&buf), &tt.opts)
n, err := d.Write(tt.pixels)
if err != nil {
t.Fatal(err)
Expand All @@ -580,7 +580,7 @@ func TestDevLong(t *testing.T) {
colors := make([]color.NRGBA, 256)
o := DefaultOpts
o.NumPixels = len(colors)
d, _ := New(spitest.NewRecordRaw(&buf), &o, spi.Mode3)
d, _ := New(spitest.NewRecordRaw(&buf), &o)
if n, err := d.Write(ToRGB(colors)); n != len(colors)*3 || err != nil {
t.Fatalf("%d %v", n, err)
}
Expand All @@ -601,7 +601,7 @@ func TestDevWrite_Long(t *testing.T) {
buf := bytes.Buffer{}
o := DefaultOpts
o.NumPixels = 1
d, _ := New(spitest.NewRecordRaw(&buf), &o, spi.Mode3)
d, _ := New(spitest.NewRecordRaw(&buf), &o)
if n, err := d.Write([]byte{0, 0, 0, 1, 1, 1}); n != 0 || err == nil {
t.Fatal(n, err)
}
Expand Down Expand Up @@ -769,7 +769,7 @@ var drawTests = []struct {
func TestDraws(t *testing.T) {
for _, tt := range drawTests {
buf := bytes.Buffer{}
d, _ := New(spitest.NewRecordRaw(&buf), &tt.opts, spi.Mode3)
d, _ := New(spitest.NewRecordRaw(&buf), &tt.opts)
if err := d.Draw(d.Bounds(), tt.img, image.Point{}); err != nil {
t.Fatalf("%s: %v", tt.name, err)
}
Expand Down Expand Up @@ -874,7 +874,7 @@ var offsetDrawTests = []struct {
func TestOffsetDraws(t *testing.T) {
for _, tt := range offsetDrawTests {
buf := bytes.Buffer{}
d, _ := New(spitest.NewRecordRaw(&buf), &tt.opts, spi.Mode3)
d, _ := New(spitest.NewRecordRaw(&buf), &tt.opts)
if err := d.Draw(tt.offset, tt.img, tt.point); err != nil {
t.Fatalf("%s: %v", tt.name, err)
}
Expand All @@ -895,7 +895,7 @@ func TestHalt(t *testing.T) {
o := DefaultOpts
o.NumPixels = 4
o.Temperature = 5000
d, _ := New(&s, &o, spi.Mode3)
d, _ := New(&s, &o)
if err := d.Halt(); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -925,7 +925,7 @@ func benchmarkWrite(b *testing.B, o Opts, length int, f genColor) {
}
o.NumPixels = length
b.ReportAllocs()
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o, spi.Mode3)
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o)
_, _ = d.Write(pixels[:])
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand Down Expand Up @@ -983,7 +983,7 @@ func BenchmarkWriteColorfulVariation(b *testing.B) {
o.NumPixels = len(pixels) / 3
o.Intensity = 250
o.Temperature = 5000
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o, spi.Mode3)
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o)
_, _ = d.Write(pixels[:])
b.ResetTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -1003,7 +1003,7 @@ func benchmarkDraw(b *testing.B, o Opts, img draw.Image, f genColor) {
}
o.NumPixels = img.Bounds().Max.X
b.ReportAllocs()
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o, spi.Mode3)
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o)
r := d.Bounds()
p := image.Point{}
if err := d.Draw(r, img, p); err != nil {
Expand Down Expand Up @@ -1062,7 +1062,7 @@ func BenchmarkDrawSlowpath(b *testing.B) {
o := DefaultOpts
o.NumPixels = img.Bounds().Max.X
b.ReportAllocs()
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o, spi.Mode3)
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &o)
r := d.Bounds()
p := image.Point{}
if err := d.Draw(r, img, p); err != nil {
Expand All @@ -1078,7 +1078,7 @@ func BenchmarkDrawSlowpath(b *testing.B) {

func BenchmarkHalt(b *testing.B) {
b.ReportAllocs()
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &DefaultOpts, spi.Mode3)
d, _ := New(spitest.NewRecordRaw(ioutil.Discard), &DefaultOpts)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := d.Halt(); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions apa102/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"image/color"
"log"

"periph.io/x/conn/v3/spi"
"periph.io/x/conn/v3/spi/spireg"
"periph.io/x/devices/v3/apa102"
"periph.io/x/host/v3"
Expand All @@ -34,7 +33,7 @@ func Example() {
o.NumPixels = 300
o.Intensity = 127
o.Temperature = 3500
dev, err := apa102.New(p, &o, spi.Mode0)
dev, err := apa102.New(p, &o)
if err != nil {
log.Fatalf("failed to open: %v", err)
}
Expand Down Expand Up @@ -62,7 +61,7 @@ func ExampleToRGB() {

o := apa102.PassThruOpts
o.NumPixels = 2
dev, err := apa102.New(p, &o, spi.Mode3)
dev, err := apa102.New(p, &o)
if err != nil {
log.Fatalf("failed to open: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.

module github.com/GermanBionicSystems/devices
module periph.io/x/devices/v3

go 1.13

Expand Down
3 changes: 1 addition & 2 deletions rainbowhat/rainbowhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package rainbowhat
import (
"periph.io/x/conn/v3/gpio"
"periph.io/x/conn/v3/i2c/i2creg"
"periph.io/x/conn/v3/spi"
"periph.io/x/conn/v3/spi/spireg"
"periph.io/x/devices/v3/apa102"
"periph.io/x/devices/v3/bmxx80"
Expand Down Expand Up @@ -54,7 +53,7 @@ func NewRainbowHat(ao *apa102.Opts) (*Dev, error) {

opts := *ao
opts.NumPixels = 7
ledstrip, err := apa102.New(spiPort, &opts, spi.Mode3)
ledstrip, err := apa102.New(spiPort, &opts)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 20c235b

Please sign in to comment.