gobot.io/x/gobot@v1.16.0/drivers/i2c/mma7660_driver.go (about) 1 package i2c 2 3 import ( 4 "gobot.io/x/gobot" 5 ) 6 7 const mma7660Address = 0x4c 8 9 const ( 10 MMA7660_X = 0x00 11 MMA7660_Y = 0x01 12 MMA7660_Z = 0x02 13 MMA7660_TILT = 0x03 14 MMA7660_SRST = 0x04 15 MMA7660_SPCNT = 0x05 16 MMA7660_INTSU = 0x06 17 MMA7660_MODE = 0x07 18 MMA7660_STAND_BY = 0x00 19 MMA7660_ACTIVE = 0x01 20 MMA7660_SR = 0x08 21 MMA7660_AUTO_SLEEP_120 = 0x00 22 MMA7660_AUTO_SLEEP_64 = 0x01 23 MMA7660_AUTO_SLEEP_32 = 0x02 24 MMA7660_AUTO_SLEEP_16 = 0x03 25 MMA7660_AUTO_SLEEP_8 = 0x04 26 MMA7660_AUTO_SLEEP_4 = 0x05 27 MMA7660_AUTO_SLEEP_2 = 0x06 28 MMA7660_AUTO_SLEEP_1 = 0x07 29 MMA7660_PDET = 0x09 30 MMA7660_PD = 0x0A 31 ) 32 33 type MMA7660Driver struct { 34 name string 35 connector Connector 36 connection Connection 37 Config 38 } 39 40 // NewMMA7660Driver creates a new driver with specified i2c interface 41 // Params: 42 // conn Connector - the Adaptor to use with this Driver 43 // 44 // Optional params: 45 // i2c.WithBus(int): bus to use with this driver 46 // i2c.WithAddress(int): address to use with this driver 47 // 48 func NewMMA7660Driver(a Connector, options ...func(Config)) *MMA7660Driver { 49 m := &MMA7660Driver{ 50 name: gobot.DefaultName("MMA7660"), 51 connector: a, 52 Config: NewConfig(), 53 } 54 55 for _, option := range options { 56 option(m) 57 } 58 59 // TODO: add commands for API 60 return m 61 } 62 63 // Name returns the Name for the Driver 64 func (h *MMA7660Driver) Name() string { return h.name } 65 66 // SetName sets the Name for the Driver 67 func (h *MMA7660Driver) SetName(n string) { h.name = n } 68 69 // Connection returns the connection for the Driver 70 func (h *MMA7660Driver) Connection() gobot.Connection { return h.connector.(gobot.Connection) } 71 72 // Start initialized the mma7660 73 func (h *MMA7660Driver) Start() (err error) { 74 bus := h.GetBusOrDefault(h.connector.GetDefaultBus()) 75 address := h.GetAddressOrDefault(mma7660Address) 76 77 h.connection, err = h.connector.GetConnection(address, bus) 78 if err != nil { 79 return err 80 } 81 82 if _, err := h.connection.Write([]byte{MMA7660_MODE, MMA7660_STAND_BY}); err != nil { 83 return err 84 } 85 86 if _, err := h.connection.Write([]byte{MMA7660_SR, MMA7660_AUTO_SLEEP_32}); err != nil { 87 return err 88 } 89 90 if _, err := h.connection.Write([]byte{MMA7660_MODE, MMA7660_ACTIVE}); err != nil { 91 return err 92 } 93 94 return 95 } 96 97 // Halt returns true if devices is halted successfully 98 func (h *MMA7660Driver) Halt() (err error) { return } 99 100 // Acceleration returns the acceleration of the provided x, y, z 101 func (h *MMA7660Driver) Acceleration(x, y, z float64) (ax, ay, az float64) { 102 return x / 21.0, y / 21.0, z / 21.0 103 } 104 105 // XYZ returns the raw x,y and z axis from the mma7660 106 func (h *MMA7660Driver) XYZ() (x float64, y float64, z float64, err error) { 107 buf := []byte{0, 0, 0} 108 bytesRead, err := h.connection.Read(buf) 109 if err != nil { 110 return 111 } 112 113 if bytesRead != 3 { 114 err = ErrNotEnoughBytes 115 return 116 } 117 118 for _, val := range buf { 119 if ((val >> 6) & 0x01) == 1 { 120 err = ErrNotReady 121 return 122 } 123 } 124 125 x = float64((int8(buf[0]) << 2)) / 4.0 126 y = float64((int8(buf[1]) << 2)) / 4.0 127 z = float64((int8(buf[2]) << 2)) / 4.0 128 129 return 130 }