From aa02a850998a3dd537bd74b172757e13ba8a46d8 Mon Sep 17 00:00:00 2001 From: Jaana Burcu Dogan Date: Wed, 22 Jun 2016 22:52:29 -0700 Subject: [PATCH] piglow: various readability improvements --- piglow/{examples_test.go => example_test.go} | 9 +- piglow/examples/strobe.go | 51 +++---- piglow/piglow.go | 65 +-------- piglow/piglow_test.go | 134 +++++++------------ 4 files changed, 74 insertions(+), 185 deletions(-) rename piglow/{examples_test.go => example_test.go} (61%) diff --git a/piglow/examples_test.go b/piglow/example_test.go similarity index 61% rename from piglow/examples_test.go rename to piglow/example_test.go index 6f80a7f..5788765 100644 --- a/piglow/examples_test.go +++ b/piglow/example_test.go @@ -8,9 +8,14 @@ import ( ) func Example() { - p, _ := piglow.Open(&i2c.Devfs{Dev: "/dev/i2c-1", Addr: piglow.Address}) + p, err := piglow.Open(&i2c.Devfs{Dev: "/dev/i2c-1", Addr: piglow.Address}) + if err != nil { + panic(err) + } - p.Setup() + if err := p.Setup(); err != nil { + panic(err) + } brightness := 0 for i := 0; i < 10; i++ { diff --git a/piglow/examples/strobe.go b/piglow/examples/strobe.go index c6a261c..4212512 100644 --- a/piglow/examples/strobe.go +++ b/piglow/examples/strobe.go @@ -1,9 +1,6 @@ package main import ( - "os" - "os/signal" - "syscall" "time" "github.com/goiot/devices/piglow" @@ -16,42 +13,24 @@ func main() { panic(err) } - // catch signals and terminate the app - sigc := make(chan os.Signal, 1) - signal.Notify(sigc, - syscall.SIGHUP, - syscall.SIGINT, - syscall.SIGTERM, - syscall.SIGQUIT) + defer func() { + p.Shutdown() + p.Close() + }() - if err := p.Setup(); err != nil { - panic(err) + time.Sleep(50 * time.Millisecond) + for i := 1; i <= 18; i++ { + if err := p.SetLEDBrightness(i, 1); err != nil { + panic(err) + } + time.Sleep(10 * time.Millisecond) } - for { - select { - case <-sigc: - p.Shutdown() - p.Close() - return - default: - time.Sleep(50 * time.Millisecond) - - for i := 1; i <= 18; i++ { - if err := p.SetLEDBrightness(i, 1); err != nil { - panic(err) - } - time.Sleep(10 * time.Millisecond) - } - - time.Sleep(50 * time.Millisecond) - - for i := 18; i > 0; i-- { - if err := p.SetLEDBrightness(i, 0); err != nil { - panic(err) - } - time.Sleep(10 * time.Millisecond) - } + time.Sleep(50 * time.Millisecond) + for i := 18; i > 0; i-- { + if err := p.SetLEDBrightness(i, 0); err != nil { + panic(err) } + time.Sleep(10 * time.Millisecond) } } diff --git a/piglow/piglow.go b/piglow/piglow.go index 8110ffd..b34cd5d 100644 --- a/piglow/piglow.go +++ b/piglow/piglow.go @@ -17,11 +17,7 @@ type PiGlow struct { // Reset resets the internal registers func (p *PiGlow) Reset() error { - if err := p.conn.Write([]byte{0x17, 0xFF}); err != nil { - return err - } - - return nil + return p.conn.Write([]byte{0x17, 0xFF}) } // Shutdown sets the software shutdown mode of the PiGlow @@ -40,23 +36,18 @@ func (p *PiGlow) Setup() error { if err := p.Reset(); err != nil { return err } - if err := p.Enable(); err != nil { return err } - if err := p.SetLEDControlRegister(1, 0xFF); err != nil { return err } - if err := p.SetLEDControlRegister(2, 0xFF); err != nil { return err } - if err := p.SetLEDControlRegister(3, 0xFF); err != nil { return err } - return nil } @@ -68,8 +59,8 @@ func Open(o driver.Opener) (*PiGlow, error) { if err != nil { return nil, err } - - return &PiGlow{conn: conn}, nil + g := &PiGlow{conn: conn} + return g, nil } // Close frees the underlying resources. It must be called once @@ -83,19 +74,15 @@ func (p *PiGlow) Green(level int) error { if err := p.conn.Write([]byte{0x04, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x06, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x0E, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x16, 0xFF}); err != nil { return err } - return nil } @@ -104,19 +91,15 @@ func (p *PiGlow) Blue(level int) error { if err := p.conn.Write([]byte{0x05, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x0C, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x0F, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x16, 0xFF}); err != nil { return err } - return nil } @@ -125,19 +108,15 @@ func (p *PiGlow) Yellow(level int) error { if err := p.conn.Write([]byte{0x03, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x09, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x10, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x16, 0xFF}); err != nil { return err } - return nil } @@ -146,19 +125,15 @@ func (p *PiGlow) Orange(level int) error { if err := p.conn.Write([]byte{0x02, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x08, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x11, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x16, 0xFF}); err != nil { return err } - return nil } @@ -167,19 +142,15 @@ func (p *PiGlow) White(level int) error { if err := p.conn.Write([]byte{0x0A, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x0B, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x0D, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x16, 0xFF}); err != nil { return err } - return nil } @@ -188,20 +159,13 @@ func (p *PiGlow) Red(level int) error { if err := p.conn.Write([]byte{0x01, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x07, byte(level)}); err != nil { return err } - if err := p.conn.Write([]byte{0x12, byte(level)}); err != nil { return err } - - if err := p.conn.Write([]byte{0x16, 0xFF}); err != nil { - return err - } - - return nil + return p.conn.Write([]byte{0x16, 0xFF}) } // SetLEDControlRegister sets the control register 1-3 to the bitmask enables. @@ -228,12 +192,7 @@ func (p *PiGlow) SetLEDControlRegister(register, enables int) error { if err := p.conn.Write([]byte{address, byte(enables)}); err != nil { return err } - - if err := p.conn.Write([]byte{0x16, 0xFF}); err != nil { - return err - } - - return nil + return p.conn.Write([]byte{0x16, 0xFF}) } // SetLEDBrightness sets the led 1-18 to the level 0-255. @@ -241,12 +200,7 @@ func (p *PiGlow) SetLEDBrightness(led, level int) error { if err := p.conn.Write([]byte{byte(led), byte(level)}); err != nil { return err } - - if err := p.conn.Write([]byte{0x16, 0xFF}); err != nil { - return err - } - - return nil + return p.conn.Write([]byte{0x16, 0xFF}) } // SetBrightness sets all the LEDs to the level 0-255. @@ -256,10 +210,5 @@ func (p *PiGlow) SetBrightness(level int) error { return err } } - - if err := p.conn.Write([]byte{0x16, 0xFF}); err != nil { - return err - } - - return nil + return p.conn.Write([]byte{0x16, 0xFF}) } diff --git a/piglow/piglow_test.go b/piglow/piglow_test.go index 4921b9c..0d160f4 100644 --- a/piglow/piglow_test.go +++ b/piglow/piglow_test.go @@ -27,13 +27,11 @@ func (c conn) Tx(w, r []byte) error { return err } } - if r != nil { if _, err := c.buf.Read(r); err != nil { return err } } - return nil } @@ -49,7 +47,6 @@ func openPiGlow(t *testing.T) (*PiGlow, *bytes.Buffer) { if err != nil { t.Fatal(err) } - return device, o.buf } @@ -61,114 +58,94 @@ func assert(t *testing.T, want, got interface{}) { func TestGreen(t *testing.T) { device, buf := openPiGlow(t) - if err := device.Green(1); err != nil { t.Fatal(err) } - - expected := []byte{ + want := []byte{ 0x04, 0x01, 0x06, 0x01, 0x0E, 0x01, 0x16, 0xFF, } - - assert(t, expected, buf.Bytes()) + assert(t, want, buf.Bytes()) } func TestBlue(t *testing.T) { device, buf := openPiGlow(t) - if err := device.Blue(1); err != nil { t.Fatal(err) } - - expected := []byte{ + want := []byte{ 0x05, 0x01, 0x0C, 0x01, 0x0F, 0x01, 0x16, 0xFF, } - - assert(t, expected, buf.Bytes()) + assert(t, want, buf.Bytes()) } func TestYellow(t *testing.T) { device, buf := openPiGlow(t) - if err := device.Yellow(1); err != nil { t.Fatal(err) } - - expected := []byte{ + want := []byte{ 0x03, 0x01, 0x09, 0x01, 0x010, 0x01, 0x16, 0xFF, } - - assert(t, expected, buf.Bytes()) + assert(t, want, buf.Bytes()) } func TestOrange(t *testing.T) { device, buf := openPiGlow(t) - if err := device.Orange(1); err != nil { t.Fatal(err) } - - expected := []byte{ + want := []byte{ 0x02, 0x01, 0x08, 0x01, 0x011, 0x01, 0x16, 0xFF, } - - assert(t, expected, buf.Bytes()) + assert(t, want, buf.Bytes()) } func TestWhite(t *testing.T) { device, buf := openPiGlow(t) - if err := device.White(1); err != nil { t.Fatal(err) } - - expected := []byte{ + want := []byte{ 0x0A, 0x01, 0x0B, 0x01, 0x0D, 0x01, 0x16, 0xFF, } - - assert(t, expected, buf.Bytes()) + assert(t, want, buf.Bytes()) } func TestRed(t *testing.T) { device, buf := openPiGlow(t) - if err := device.Red(1); err != nil { t.Fatal(err) } - - expected := []byte{ + want := []byte{ 0x01, 0x01, 0x07, 0x01, 0x012, 0x01, 0x16, 0xFF, } - - assert(t, expected, buf.Bytes()) + assert(t, want, buf.Bytes()) } func TestSetBrightness(t *testing.T) { device, buf := openPiGlow(t) - if err := device.SetBrightness(10); err != nil { t.Fatal(err) } - - expected := []byte{ + want := []byte{ 0x01, 0x0A, 0x02, 0x0A, 0x03, 0x0A, @@ -189,17 +166,16 @@ func TestSetBrightness(t *testing.T) { 0x012, 0x0A, 0x16, 0xFF, } - - assert(t, expected, buf.Bytes()) + assert(t, want, buf.Bytes()) } func TestSetLEDBrightness(t *testing.T) { device, buf := openPiGlow(t) var states = []struct { - led int - level int - expected []byte + led int + level int + want []byte }{ {1, 5, []byte{0x1, 5, 0x16, 0xFF}}, {2, 10, []byte{0x2, 10, 0x16, 0xFF}}, @@ -223,76 +199,38 @@ func TestSetLEDBrightness(t *testing.T) { for _, state := range states { buf.Reset() - if err := device.SetLEDBrightness(state.led, state.level); err != nil { - t.Fatal(err) + t.Log(err) } - - assert(t, state.expected, buf.Bytes()) + assert(t, state.want, buf.Bytes()) } } func TestReset(t *testing.T) { device, buf := openPiGlow(t) - if err := device.Reset(); err != nil { t.Fatal(err) } - - expected := []byte{ - 0x17, 0xFF, - } - - assert(t, expected, buf.Bytes()) + want := []byte{0x17, 0xff} + assert(t, want, buf.Bytes()) } func TestShutdown(t *testing.T) { device, buf := openPiGlow(t) - if err := device.Shutdown(); err != nil { t.Fatal(err) } - - expected := []byte{ - 0x00, 0x00, - } - - assert(t, expected, buf.Bytes()) + want := []byte{0x00, 0x00} + assert(t, want, buf.Bytes()) } func TestEnable(t *testing.T) { device, buf := openPiGlow(t) - if err := device.Enable(); err != nil { t.Fatal(err) } - - expected := []byte{ - 0x00, 0x01, - } - - assert(t, expected, buf.Bytes()) -} - -func TestSetup(t *testing.T) { - device, buf := openPiGlow(t) - - if err := device.Setup(); err != nil { - t.Fatal(err) - } - - expected := []byte{ - 0x17, 0xFF, - 0x00, 0x01, - 0x13, 0xFF, - 0x16, 0xFF, - 0x14, 0xFF, - 0x16, 0xFF, - 0x15, 0xFF, - 0x16, 0xFF, - } - - assert(t, expected, buf.Bytes()) + want := []byte{0x00, 0x01} + assert(t, want, buf.Bytes()) } func TestSetLEDControlRegister(t *testing.T) { @@ -301,7 +239,7 @@ func TestSetLEDControlRegister(t *testing.T) { var states = []struct { register int enables int - expected []byte + want []byte err error }{ {1, 0xFF, []byte{0x13, 0xFF, 0x16, 0xFF}, nil}, @@ -314,7 +252,25 @@ func TestSetLEDControlRegister(t *testing.T) { buf.Reset() err := device.SetLEDControlRegister(state.register, state.enables) - assert(t, state.expected, buf.Bytes()) + assert(t, state.want, buf.Bytes()) assert(t, state.err, err) } } + +func TestSetup(t *testing.T) { + device, buf := openPiGlow(t) + if err := device.Setup(); err != nil { + t.Fatal(err) + } + got := []byte{ + 0x17, 0xFF, + 0x00, 0x01, + 0x13, 0xFF, + 0x16, 0xFF, + 0x14, 0xFF, + 0x16, 0xFF, + 0x15, 0xFF, + 0x16, 0xFF, + } + assert(t, got, buf.Bytes()) +}