From 9d863fdf15222c8e4309b197a5b1f6c9754fe38a Mon Sep 17 00:00:00 2001 From: Ron Evans Date: Tue, 20 Nov 2018 09:14:27 +0100 Subject: [PATCH] blinkm: add initial support for BlinkM I2C controlled RGB LED Signed-off-by: Ron Evans --- blinkm/blinkm.go | 54 +++++++++++++++++++++++++++++++++++++++++++++ blinkm/registers.go | 23 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 blinkm/blinkm.go create mode 100644 blinkm/registers.go diff --git a/blinkm/blinkm.go b/blinkm/blinkm.go new file mode 100644 index 0000000..5e7ec2c --- /dev/null +++ b/blinkm/blinkm.go @@ -0,0 +1,54 @@ +// Package blinkm implements a driver for the BlinkM I2C RGB LED. +// +// Datasheet: http://thingm.com/fileadmin/thingm/downloads/BlinkM_datasheet.pdf +package blinkm + +import ( + "machine" +) + +// Device wraps an I2C connection to a BlinkM device. +type Device struct { + bus machine.I2C +} + +// New creates a new BlinkM connection. The I2C bus must already be +// configured. +// +// This function only creates the Device object, it does not touch the device. +func New(bus machine.I2C) Device { + return Device{bus} +} + +// Version returns the version of firmware on the BlinkM. +func (d Device) Version() (major, minor byte, err error) { + version := []byte{0, 0} + d.bus.ReadRegister(Address, GET_FIRMWARE, version) + return version[0], version[1], nil +} + +// SetRGB sets the RGB color on the BlinkM. +func (d Device) SetRGB(r, g, b byte) error { + d.bus.WriteRegister(Address, TO_RGB, []byte{r, g, b}) + return nil +} + +// GetRGB gets the current RGB color on the BlinkM. +func (d Device) GetRGB() (r, g, b byte, err error) { + color := []byte{0, 0, 0} + d.bus.ReadRegister(Address, GET_RGB, color) + return color[0], color[1], color[2], nil +} + +// FadeToRGB sets the RGB color on the BlinkM by fading from the current color +// to the new color. +func (d Device) FadeToRGB(r, g, b byte) error { + d.bus.WriteRegister(Address, FADE_TO_RGB, []byte{r, g, b}) + return nil +} + +// StopScript stops whatever script is currently running on the BlinkM. +func (d Device) StopScript() error { + d.bus.WriteRegister(Address, STOP_SCRIPT, nil) + return nil +} diff --git a/blinkm/registers.go b/blinkm/registers.go new file mode 100644 index 0000000..6183477 --- /dev/null +++ b/blinkm/registers.go @@ -0,0 +1,23 @@ +package blinkm + +// Constants/addresses used for BlinkM. + +// The I2C address which this device listens to. +const Address = 0x09 + +// Registers, which in the case of the BlinkM are actually commands. +const ( + TO_RGB = 0x6e + FADE_TO_RGB = 0x63 + FADE_TO_HSB = 0x68 + FADE_TO_RND_RGB = 0x43 + FADE_TO_RND_HSB = 0x48 + PLAY_LIGHT_SCRIPT = 0x70 + STOP_SCRIPT = 0x6f + SET_FADE = 0x66 + SET_TIME = 0x74 + GET_RGB = 0x67 + GET_ADDRESS = 0x61 + SET_ADDRESS = 0x41 + GET_FIRMWARE = 0x5a +)