From 9ed6c308d96eb36db64abfc3a07e7a4ffae382bb Mon Sep 17 00:00:00 2001 From: Dan Jacques Date: Thu, 19 Apr 2018 00:04:36 -0400 Subject: [PATCH] more colors, docs --- demo/colorphase/app.go | 57 +++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/demo/colorphase/app.go b/demo/colorphase/app.go index d6c65ab..3d634e9 100644 --- a/demo/colorphase/app.go +++ b/demo/colorphase/app.go @@ -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 ( @@ -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 }