forked from tinygo-org/drivers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blinkm.go
58 lines (49 loc) · 1.66 KB
/
blinkm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Package blinkm implements a driver for the BlinkM I2C RGB LED.
//
// Datasheet: http://thingm.com/fileadmin/thingm/downloads/BlinkM_datasheet.pdf
package blinkm // import "tinygo.org/x/drivers/blinkm"
import "tinygo.org/x/drivers"
// Device wraps an I2C connection to a BlinkM device.
type Device struct {
bus drivers.I2C
Address uint16
}
// 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 drivers.I2C) Device {
return Device{bus, Address}
}
// Configure sets up the device for communication
func (d *Device) Configure() {
d.bus.Tx(d.Address, []byte{'o'}, nil)
}
// Version returns the version of firmware on the BlinkM.
func (d Device) Version() (major, minor byte, err error) {
version := []byte{0, 0}
d.bus.Tx(d.Address, []byte{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.Tx(d.Address, []byte{TO_RGB, r, g, b}, nil)
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.Tx(d.Address, []byte{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.Tx(d.Address, []byte{FADE_TO_RGB, r, g, b}, nil)
return nil
}
// StopScript stops whatever script is currently running on the BlinkM.
func (d Device) StopScript() error {
d.bus.Tx(d.Address, []byte{STOP_SCRIPT}, nil)
return nil
}