tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/blinkm/blinkm.go (about) 1 // Package blinkm implements a driver for the BlinkM I2C RGB LED. 2 // 3 // Datasheet: http://thingm.com/fileadmin/thingm/downloads/BlinkM_datasheet.pdf 4 package blinkm // import "tinygo.org/x/drivers/blinkm" 5 6 import "tinygo.org/x/drivers" 7 8 // Device wraps an I2C connection to a BlinkM device. 9 type Device struct { 10 bus drivers.I2C 11 Address uint16 12 } 13 14 // New creates a new BlinkM connection. The I2C bus must already be 15 // configured. 16 // 17 // This function only creates the Device object, it does not touch the device. 18 func New(bus drivers.I2C) Device { 19 return Device{bus, Address} 20 } 21 22 // Configure sets up the device for communication 23 func (d *Device) Configure() { 24 d.bus.Tx(d.Address, []byte{'o'}, nil) 25 } 26 27 // Version returns the version of firmware on the BlinkM. 28 func (d Device) Version() (major, minor byte, err error) { 29 version := []byte{0, 0} 30 d.bus.Tx(d.Address, []byte{GET_FIRMWARE}, version) 31 return version[0], version[1], nil 32 } 33 34 // SetRGB sets the RGB color on the BlinkM. 35 func (d Device) SetRGB(r, g, b byte) error { 36 d.bus.Tx(d.Address, []byte{TO_RGB, r, g, b}, nil) 37 return nil 38 } 39 40 // GetRGB gets the current RGB color on the BlinkM. 41 func (d Device) GetRGB() (r, g, b byte, err error) { 42 color := []byte{0, 0, 0} 43 d.bus.Tx(d.Address, []byte{GET_RGB}, color) 44 return color[0], color[1], color[2], nil 45 } 46 47 // FadeToRGB sets the RGB color on the BlinkM by fading from the current color 48 // to the new color. 49 func (d Device) FadeToRGB(r, g, b byte) error { 50 d.bus.Tx(d.Address, []byte{FADE_TO_RGB, r, g, b}, nil) 51 return nil 52 } 53 54 // StopScript stops whatever script is currently running on the BlinkM. 55 func (d Device) StopScript() error { 56 d.bus.Tx(d.Address, []byte{STOP_SCRIPT}, nil) 57 return nil 58 }