Skip to content

Commit

Permalink
more colors, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
danjacques committed Apr 19, 2018
1 parent 7f72890 commit 9ed6c30
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions demo/colorphase/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
// Use of this source code is governed under the MIT License
// that can be found in the LICENSE file.

// Package colorphase defines the logic for the "colorphase" demo app.
//
// This app listens for any PixelPusher devices and broadcasts a color cycling
// animation to each, fading in and out of intensity of various colors.
//
// This demonstrates how to listen for connections, register them with a
// discovery Registry, generate pixel state mutations, and send those mutations
// to the target device.
package colorphase

import (
Expand Down Expand Up @@ -89,32 +97,41 @@ func cycleColors(d device.D, sleepInterval time.Duration) {
}
}

// 101 110 011 100 010 001
const cyclerMask = uint(0x2E711)

type cycler struct {
r, g, b int
offset int
v int
mask uint
}

func (c *cycler) Next() (p pixel.P) {
var v *int
var pos *byte
switch c.offset {
case 0:
v, pos = &c.r, &p.Red
case 1:
v, pos = &c.g, &p.Green
case 2:
v, pos = &c.b, &p.Blue
if c.mask == 0 {
c.mask = cyclerMask
}

// Select our intensity. >0xFF fades downwards towards 0.
v := c.v
if v > 0xFF {
v = 0xFF - v
}

// Set masked colors.
if c.mask&0x01 != 0 {
p.Red = byte(v)
}
if c.mask&0x02 != 0 {
p.Green = byte(v)
}
if c.mask&0x04 != 0 {
p.Blue = byte(v)
}

*v++
switch {
case *v > 0x1FF:
*v, *pos = 0, 0
c.offset = ((c.offset + 1) % 3)
case *v > 0xFF:
*pos = 0xFF - byte(*v)
default:
*pos = byte(*v)
// Cycle.
c.v++
if c.v > 0x1FF {
c.v = 0
c.mask >>= 3
}
return
}

0 comments on commit 9ed6c30

Please sign in to comment.