gobot.io/x/gobot@v1.16.0/drivers/i2c/blinkm_driver.go (about)

     1  package i2c
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"gobot.io/x/gobot"
     7  )
     8  
     9  const blinkmAddress = 0x09
    10  
    11  // BlinkMDriver is a Gobot Driver for a BlinkM LED
    12  type BlinkMDriver struct {
    13  	name       string
    14  	connector  Connector
    15  	connection Connection
    16  	Config
    17  	gobot.Commander
    18  }
    19  
    20  // NewBlinkMDriver creates a new BlinkMDriver.
    21  //
    22  // Params:
    23  //		conn Connector - the Adaptor to use with this Driver
    24  //
    25  // Optional params:
    26  //		i2c.WithBus(int):	bus to use with this driver
    27  //		i2c.WithAddress(int):	address to use with this driver
    28  //
    29  func NewBlinkMDriver(a Connector, options ...func(Config)) *BlinkMDriver {
    30  	b := &BlinkMDriver{
    31  		name:      gobot.DefaultName("BlinkM"),
    32  		Commander: gobot.NewCommander(),
    33  		connector: a,
    34  		Config:    NewConfig(),
    35  	}
    36  
    37  	for _, option := range options {
    38  		option(b)
    39  	}
    40  
    41  	b.AddCommand("Rgb", func(params map[string]interface{}) interface{} {
    42  		red := byte(params["red"].(float64))
    43  		green := byte(params["green"].(float64))
    44  		blue := byte(params["blue"].(float64))
    45  		return b.Rgb(red, green, blue)
    46  	})
    47  
    48  	b.AddCommand("Fade", func(params map[string]interface{}) interface{} {
    49  		red := byte(params["red"].(float64))
    50  		green := byte(params["green"].(float64))
    51  		blue := byte(params["blue"].(float64))
    52  		return b.Fade(red, green, blue)
    53  	})
    54  
    55  	b.AddCommand("FirmwareVersion", func(params map[string]interface{}) interface{} {
    56  		version, err := b.FirmwareVersion()
    57  		return map[string]interface{}{"version": version, "err": err}
    58  	})
    59  
    60  	b.AddCommand("Color", func(params map[string]interface{}) interface{} {
    61  		color, err := b.Color()
    62  		return map[string]interface{}{"color": color, "err": err}
    63  	})
    64  
    65  	return b
    66  }
    67  
    68  // Name returns the Name for the Driver
    69  func (b *BlinkMDriver) Name() string { return b.name }
    70  
    71  // SetName sets the Name for the Driver
    72  func (b *BlinkMDriver) SetName(n string) { b.name = n }
    73  
    74  // Connection returns the connection for the Driver
    75  func (b *BlinkMDriver) Connection() gobot.Connection { return b.connection.(gobot.Connection) }
    76  
    77  // Start starts the Driver up, and writes start command
    78  func (b *BlinkMDriver) Start() (err error) {
    79  	bus := b.GetBusOrDefault(b.connector.GetDefaultBus())
    80  	address := b.GetAddressOrDefault(blinkmAddress)
    81  
    82  	b.connection, err = b.connector.GetConnection(address, bus)
    83  	if err != nil {
    84  		return
    85  	}
    86  
    87  	if _, err := b.connection.Write([]byte("o")); err != nil {
    88  		return err
    89  	}
    90  	return
    91  }
    92  
    93  // Halt returns true if device is halted successfully
    94  func (b *BlinkMDriver) Halt() (err error) { return }
    95  
    96  // Rgb sets color using r,g,b params
    97  func (b *BlinkMDriver) Rgb(red byte, green byte, blue byte) (err error) {
    98  	if _, err = b.connection.Write([]byte("n")); err != nil {
    99  		return
   100  	}
   101  	_, err = b.connection.Write([]byte{red, green, blue})
   102  	return
   103  }
   104  
   105  // Fade removes color using r,g,b params
   106  func (b *BlinkMDriver) Fade(red byte, green byte, blue byte) (err error) {
   107  	if _, err = b.connection.Write([]byte("c")); err != nil {
   108  		return
   109  	}
   110  	_, err = b.connection.Write([]byte{red, green, blue})
   111  	return
   112  }
   113  
   114  // FirmwareVersion returns version with MAYOR.minor format
   115  func (b *BlinkMDriver) FirmwareVersion() (version string, err error) {
   116  	if _, err = b.connection.Write([]byte("Z")); err != nil {
   117  		return
   118  	}
   119  	data := []byte{0, 0}
   120  	read, err := b.connection.Read(data)
   121  	if read != 2 || err != nil {
   122  		return
   123  	}
   124  	return fmt.Sprintf("%v.%v", data[0], data[1]), nil
   125  }
   126  
   127  // Color returns an array with current rgb color
   128  func (b *BlinkMDriver) Color() (color []byte, err error) {
   129  	if _, err = b.connection.Write([]byte("g")); err != nil {
   130  		return
   131  	}
   132  	data := []byte{0, 0, 0}
   133  	read, err := b.connection.Read(data)
   134  	if read != 3 || err != nil {
   135  		return []byte{}, err
   136  	}
   137  	return []byte{data[0], data[1], data[2]}, nil
   138  }