diff --git a/main.go b/main.go index ecefd68..d8d9229 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,7 @@ import ( "log" "time" - "github.com/CGSG-2021-AE4/mpu-test/mpu6050" + "github.com/CGSG-2021-AE4/mpu-test/pkg/mpu6050" ) func main() { @@ -26,7 +26,7 @@ func mainE() error { for { // temp, err := mpu.GetTemp() // if err != nil { - // return err + // return err // } accel, err := mpu.GetAccel(false) if err != nil { diff --git a/mpu/mpu.go b/mpu/mpu.go new file mode 100644 index 0000000..ae1369c --- /dev/null +++ b/mpu/mpu.go @@ -0,0 +1,74 @@ +package mpu + +import ( + "fmt" + "io" + + "github.com/CGSG-2021-AE4/mpu-test/pkg/mpu6050" +) + +type MpuData struct { + TempData float32 `json:"Temperature"` + AccelData [3]float32 `json:"Accelerometer"` + GyroData [3]float32 `json:"Gyroscope"` +} + +type mpu struct { + device mpu6050.Device + + data MpuData +} + +type Mpu interface { + Init() error + Update() error + Validate() bool + GetData() MpuData + io.Closer +} + +func New() Mpu { + return &mpu{ + device: nil, + } +} + +func (m *mpu) Init() error { + d, err := mpu6050.Open() + if err != nil { + return fmt.Errorf("open mpu connection: %w", err) + } + m.device = d + return nil +} + +func (m *mpu) Update() (err error) { + m.data.TempData, err = m.device.GetTemp() + if err != nil { + return fmt.Errorf("get temp data: %w", err) + } + m.data.AccelData, err = m.device.GetAccel(false) + if err != nil { + return fmt.Errorf("get accel data: %w", err) + } + m.data.GyroData, err = m.device.GetGyro() + if err != nil { + return fmt.Errorf("get gyro data: %w", err) + } + return nil +} + +func (m *mpu) Validate() bool { + return m.device != nil +} + +func (m *mpu) GetData() MpuData { + return m.data +} + +func (m *mpu) Close() error { + defer func() { + m.device = nil + }() + return m.device.Close() +} diff --git a/mpu6050/i2c.go b/pkg/mpu6050/i2c.go similarity index 100% rename from mpu6050/i2c.go rename to pkg/mpu6050/i2c.go diff --git a/mpu6050/mpu.go b/pkg/mpu6050/mpu.go similarity index 94% rename from mpu6050/mpu.go rename to pkg/mpu6050/mpu.go index 72bc785..eeb39f2 100644 --- a/mpu6050/mpu.go +++ b/pkg/mpu6050/mpu.go @@ -2,7 +2,7 @@ package mpu6050 import ( "fmt" - "log" + "io" "golang.org/x/exp/io/i2c" ) @@ -70,10 +70,14 @@ type device struct { type Device interface { GetTemp() (float32, error) + SetAccelRange(r byte) error GetAccel(inG bool) ([3]float32, error) + SetGyroRange(r byte) error GetGyro() ([3]float32, error) + + io.Closer } func Open() (Device, error) { @@ -140,8 +144,8 @@ func (d *device) GetAccel(inG bool) ([3]float32, error) { scale = AccelScaleModifier2G case AccelRange16G: scale = AccelScaleModifier2G - default: - log.Println("not foudn accel range:", accelRange) + //default: + // log.Println("not foudn accel range:", accelRange) } scale = 1 / scale @@ -201,8 +205,8 @@ func (d *device) GetGyro() ([3]float32, error) { scale = GyroScaleModifier1000DEG case GyroRange2000DEG: scale = GyroScaleModifier2000DEG - default: - log.Println("not found gyro range:", gyroRange) + // default: + // log.Println("not found gyro range:", gyroRange) } return [3]float32{ float32(x) / scale, @@ -210,3 +214,10 @@ func (d *device) GetGyro() ([3]float32, error) { float32(z) / scale, }, nil } + +func (d *device) Close() error { + defer func() { + d.device = nil + }() + return d.device.Close() +}