func (b *Client) I2cWrite(address int, data []byte) error {
	ret := []byte{I2CRequest, byte(address), (I2CModeWrite << 3)}
	for _, val := range data {
		ret = append(ret, byte(val&0x7F))
		ret = append(ret, byte((val>>7)&0x7F))
	}
	return b.writeSysex(ret)
}
I2cWrite writes data to address.
package client
import (
	"errors"
	"fmt"
	"io"
	"math"
	"time"
	"github.com/hybridgroup/gobot"
)
const (
	Input  = 0x00
	Output = 0x01
	Analog = 0x02
	Pwm    = 0x03
	Servo  = 0x04
)
const (
	ProtocolVersion          byte = 0xF9
	SystemReset              byte = 0xFF
	DigitalMessage           byte = 0x90
	DigitalMessageRangeStart byte = 0x90
	DigitalMessageRangeEnd   byte = 0x9F
	AnalogMessage            byte = 0xE0
	AnalogMessageRangeStart  byte = 0xE0
	AnalogMessageRangeEnd    byte = 0xEF
	ReportAnalog             byte = 0xC0
	ReportDigital            byte = 0xD0
	PinMode                  byte = 0xF4
	StartSysex               byte = 0xF0
	EndSysex                 byte = 0xF7
	CapabilityQuery          byte = 0x6B
	CapabilityResponse       byte = 0x6C
	PinStateQuery            byte = 0x6D
	PinStateResponse         byte = 0x6E
	AnalogMappingQuery       byte = 0x69
	AnalogMappingResponse    byte = 0x6A
	StringData               byte = 0x71
	I2CRequest               byte = 0x76
	I2CReply                 byte = 0x77
	I2CConfig                byte = 0x78
	FirmwareQuery            byte = 0x79
	I2CModeWrite             byte = 0x00
	I2CModeRead              byte = 0x01
	I2CModeContinuousRead    byte = 0x02
	I2CModeStopReading       byte = 0x03
	ServoConfig              byte = 0x70
)
var (
	ErrConnected = errors.New("client is already connected")
)
type Client struct {
	pins             []Pin
	FirmwareName     string
	ProtocolVersion  string
	connected        bool
	connection       io.ReadWriteCloser
	analogPins       []int
	initTimeInterval time.Duration
	gobot.Eventer
}
type Pin struct {
	SupportedModes []int
	Mode           int
	Value          int
	State          int
	AnalogChannel  int
}
type I2cReply struct {
	Address  int
	Register int
	Data     []byte
}
func New() *Client {
	c := &Client{
		ProtocolVersion: "",
		FirmwareName:    "",
		connection:      nil,
		pins:            []Pin{},
		analogPins:      []int{},
		connected:       false,
		Eventer:         gobot.NewEventer(),
	}
	for _, s := range []string{
		"FirmwareQuery",
		"CapabilityQuery",
		"AnalogMappingQuery",
		"ProtocolVersion",
		"I2cReply",
		"StringData",
		"Error",
	} {
		c.AddEvent(s)
	}
	return c
}
func (b *Client) Disconnect() (err error) {
	b.connected = false
	return b.connection.Close()
}
func (b *Client) Connected() bool {
	return b.connected
}
func (b *Client) Pins() []Pin {
	return b.pins
}
func (b *Client) Connect(conn io.ReadWriteCloser) (err error) {
	if b.connected {
		return ErrConnected
	}
	b.connection = conn
	b.Reset()
	initFunc := b.ProtocolVersionQuery
	b.Once(b.Event("ProtocolVersion"), func(data interface{}) {
		initFunc = b.FirmwareQuery
	})
	b.Once(b.Event("FirmwareQuery"), func(data interface{}) {
		initFunc = b.CapabilitiesQuery
	})
	b.Once(b.Event("CapabilityQuery"), func(data interface{}) {
		initFunc = b.AnalogMappingQuery
	})
	b.Once(b.Event("AnalogMappingQuery"), func(data interface{}) {
		initFunc = func() error { return nil }
		b.ReportDigital(0, 1)
		b.ReportDigital(1, 1)
		b.connected = true
	})
	for {
		if err := initFunc(); err != nil {
			return err
		}
		if err := b.process(); err != nil {
			return err
		}
		if b.connected {
			go func() {
				for {
					if !b.connected {
						break
					}
					if err := b.process(); err != nil {
						b.Publish(b.Event("Error"), err)
					}
				}
			}()
			break
		}
	}
	return
}
func (b *Client) Reset() error {
	return b.write([]byte{SystemReset})
}
func (b *Client) SetPinMode(pin int, mode int) error {
	b.pins[byte(pin)].Mode = mode
	return b.write([]byte{PinMode, byte(pin), byte(mode)})
}
func (b *Client) DigitalWrite(pin int, value int) error {
	port := byte(math.Floor(float64(pin) / 8))
	portValue := byte(0)
	b.pins[pin].Value = value
	for i := byte(0); i < 8; i++ {
		if b.pins[8*port+i].Value != 0 {
			portValue = portValue | (1 << i)
		}
	}
	return b.write([]byte{DigitalMessage | port, portValue & 0x7F, (portValue >> 7) & 0x7F})
}
func (b *Client) ServoConfig(pin int, max int, min int) error {
	ret := []byte{
		ServoConfig,
		byte(pin),
		byte(min & 0x7F),
		byte((min >> 7) & 0x7F),
		byte(max & 0x7F),
		byte((max >> 7) & 0x7F),
	}
	return b.writeSysex(ret)
}
func (b *Client) AnalogWrite(pin int, value int) error {
	b.pins[pin].Value = value
	return b.write([]byte{AnalogMessage | byte(pin), byte(value & 0x7F), byte((value >> 7) & 0x7F)})
}
func (b *Client) FirmwareQuery() error {
	return b.writeSysex([]byte{FirmwareQuery})
}
func (b *Client) PinStateQuery(pin int) error {
	return b.writeSysex([]byte{PinStateQuery, byte(pin)})
}
func (b *Client) ProtocolVersionQuery() error {
	return b.write([]byte{ProtocolVersion})
}
func (b *Client) CapabilitiesQuery() error {
	return b.writeSysex([]byte{CapabilityQuery})
}
func (b *Client) AnalogMappingQuery() error {
	return b.writeSysex([]byte{AnalogMappingQuery})
}
func (b *Client) ReportDigital(pin int, state int) error {
	return b.togglePinReporting(pin, state, ReportDigital)
}
func (b *Client) ReportAnalog(pin int, state int) error {
	return b.togglePinReporting(pin, state, ReportAnalog)
}
func (b *Client) I2cRead(address int, numBytes int) error {
	return b.writeSysex([]byte{I2CRequest, byte(address), (I2CModeRead << 3),
		byte(numBytes) & 0x7F, (byte(numBytes) >> 7) & 0x7F})
}