Skip to content

Commit

Permalink
Apa102 use default brightness (hybridgroup#671)
Browse files Browse the repository at this point in the history
* spi/apa102: use uint8 type with a max of 31 for brightness

Signed-off-by: Erwan Morvan <[email protected]>
  • Loading branch information
Erwan Morvan authored and deadprogram committed May 31, 2019
1 parent da10293 commit d73466a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
54 changes: 36 additions & 18 deletions drivers/spi/apa102.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,39 @@ import (
"gobot.io/x/gobot"
)

// APA102Driver is a driver for the APA102 programmable RGB LEDs
// APA102Driver is a driver for the APA102 programmable RGB LEDs.
type APA102Driver struct {
name string
connector Connector
connection Connection
Config
gobot.Commander

vals []color.RGBA
vals []color.RGBA
brightness uint8
}

// NewAPA102Driver creates a new Gobot Driver for APA102 RGB LEDs.
//
// Params:
// a *Adaptor - the Adaptor to use with this Driver
// count int - how many LEDs are in the array controlled by this driver
// a *Adaptor - the Adaptor to use with this Driver.
// count int - how many LEDs are in the array controlled by this driver.
// bright - the default brightness to apply for all LEDs (must be between 0 and 31).
//
// Optional params:
// spi.WithBus(int): bus to use with this driver
// spi.WithChip(int): chip to use with this driver
// spi.WithMode(int): mode to use with this driver
// spi.WithBits(int): number of bits to use with this driver
// spi.WithSpeed(int64): speed in Hz to use with this driver
// spi.WithBus(int): bus to use with this driver.
// spi.WithChip(int): chip to use with this driver.
// spi.WithMode(int): mode to use with this driver.
// spi.WithBits(int): number of bits to use with this driver.
// spi.WithSpeed(int64): speed in Hz to use with this driver.
//
func NewAPA102Driver(a Connector, count int, options ...func(Config)) *APA102Driver {
func NewAPA102Driver(a Connector, count int, bright uint8, options ...func(Config)) *APA102Driver {
d := &APA102Driver{
name: gobot.DefaultName("APA102"),
connector: a,
vals: make([]color.RGBA, count),
Config: NewConfig(),
name: gobot.DefaultName("APA102"),
connector: a,
vals: make([]color.RGBA, count),
brightness: uint8(math.Min(float64(bright), 31)),
Config: NewConfig(),
}
for _, option := range options {
option(d)
Expand Down Expand Up @@ -81,6 +84,17 @@ func (d *APA102Driver) SetRGBA(i int, v color.RGBA) {
d.vals[i] = v
}

// SetBrightness sets the ith LED's brightness to the given value.
// Must be between 0 and 31.
func (d *APA102Driver) SetBrightness(i uint8) {
d.brightness = uint8(math.Min(float64(i), 31))
}

// Brightness return driver brightness value.
func (d *APA102Driver) Brightness() uint8 {
return d.brightness
}

// Draw displays the RGBA values set on the actual LED strip.
func (d *APA102Driver) Draw() error {
// TODO(jbd): dotstar allows other RGBA alignments, support those layouts.
Expand All @@ -94,10 +108,14 @@ func (d *APA102Driver) Draw() error {

for i, c := range d.vals {
j := (i + 1) * 4
tx[j] = 0xe0 + byte(math.Min(float64(c.A), 31))
tx[j+1] = byte(c.B)
tx[j+2] = byte(c.G)
tx[j+3] = byte(c.R)
if c.A != 0 {
tx[j] = 0xe0 + byte(math.Min(float64(c.A), 31))
} else {
tx[j] = 0xe0 + byte(d.brightness)
}
tx[j+1] = c.B
tx[j+2] = c.G
tx[j+3] = c.R
}

// end frame with at least n/2 0xff vals
Expand Down
10 changes: 5 additions & 5 deletions drivers/spi/apa102_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var _ gobot.Driver = (*APA102Driver)(nil)

func initTestDriver() *APA102Driver {
d := NewAPA102Driver(&TestConnector{}, 10)
d := NewAPA102Driver(&TestConnector{}, 10, 31)
return d
}

Expand All @@ -30,10 +30,10 @@ func TestDriverLEDs(t *testing.T) {
d := initTestDriver()
d.Start()

d.SetRGBA(0, color.RGBA{255, 255, 255, 0})
d.SetRGBA(1, color.RGBA{255, 255, 255, 0})
d.SetRGBA(2, color.RGBA{255, 255, 255, 0})
d.SetRGBA(3, color.RGBA{255, 255, 255, 0})
d.SetRGBA(0, color.RGBA{255, 255, 255, 15})
d.SetRGBA(1, color.RGBA{255, 255, 255, 15})
d.SetRGBA(2, color.RGBA{255, 255, 255, 15})
d.SetRGBA(3, color.RGBA{255, 255, 255, 15})

gobottest.Assert(t, d.Draw(), nil)
}

0 comments on commit d73466a

Please sign in to comment.