From 0bc708839d2295cb5638dee97b53452ed05233e5 Mon Sep 17 00:00:00 2001 From: clinton Date: Thu, 29 Sep 2016 21:44:03 +1000 Subject: [PATCH] added support for the pcal9535 --- controller/pcal9535a/pcal9535a.go | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 controller/pcal9535a/pcal9535a.go diff --git a/controller/pcal9535a/pcal9535a.go b/controller/pcal9535a/pcal9535a.go new file mode 100644 index 0000000..7dad5a9 --- /dev/null +++ b/controller/pcal9535a/pcal9535a.go @@ -0,0 +1,43 @@ +// PCAL953A, low volage GPIO expander as found in the Raspberry +// Pi Relay board by Seeed Studio. +// +// http://wiki.seeedstudio.com/wiki/Raspberry_Pi_Relay_Board_v1.0 +package pcal9535a + +import ( + "github.com/kidoman/embd" +) + +const ( + REG_MODE = 0x06 +) + +type PCAL9535A struct { + Bus embd.I2CBus + Addr byte + D byte +} + +// New creates and connects to a PCAL9535A GPIO expander. +func New(bus embd.I2CBus, addr byte) (*PCAL9535A, error) { + return &PCAL9535A{ + Bus: bus, + Addr: addr, + D: 0xff, + }, bus.WriteByteToReg(addr, REG_MODE, 0xff) +} + +// Sets the nominated GPIO pin to either high (on = true) or low (on = false) +func (c *PCAL9535A) SetPin(pin uint, on bool) error { + if on { + c.D &= ^(byte(0x1) << pin) + } else { + c.D |= (byte(0x1) << pin) + } + + return c.Bus.WriteByteToReg(c.Addr, REG_MODE, c.D) +} + +func (c *PCAL9535A) GetPin(pin uint) bool { + return (((c.D >> pin) & 1) == 0) +}