 
 class MScannerMotion
 {
+    private enum MotionOrientation
+    {
+        case portrait
+        case landscapeRight
+        case landscapeLeft
+    }
+    
     private weak var controller:CScanner!
+    private var motionOrientation:MotionOrientation
     private let manager:CMMotionManager!
-    private let kUpdatesInterval:TimeInterval = 0.09
+    private let kRotationThreshold:Double = 2.7
+    private let k180Deg:Float = 180
+    private let kUpdatesInterval:TimeInterval = 0.07
     
     init(controller:CScanner)
     {
         self.controller = controller
+        motionOrientation = MotionOrientation.portrait
         
         let manager:CMMotionManager = CMMotionManager()
         manager.deviceMotionUpdateInterval = kUpdatesInterval

        if manager.isDeviceMotionAvailable
        {
            manager.startDeviceMotionUpdates(
                using:CMAttitudeReferenceFrame.xTrueNorthZVertical,
                to:OperationQueue.main)
            { (data:CMDeviceMotion?, error:Error?) in
                
                if error == nil
                {
                    guard
                        
                        let dataMotion:CMDeviceMotion = data
                        
                    else
                    {
                        return
                    }
                    
                    let acceleration:CMAcceleration = dataMotion.gravity
                    let attitude:CMAttitude = dataMotion.attitude
                    let rotation:CMRotationMatrix = attitude.rotationMatrix
                    let quaternion:CMQuaternion = attitude.quaternion
                    
                    self.gravityCompute(acceleration:acceleration)
                    
                    //                        print((attitude.yaw + (Double.pi / 2)) * -180.0 / Double.pi)
                    
                    //let heading = Double.atan2(rotation.m22, rotation.m12)
                    let xy = atan2(quaternion.x, quaternion.y)
                    let heading = atan2(quaternion.z, xy)
                    //let heading = (quaternion.z + ((quaternion.x/2) + (quaternion.y))) * Double.pi
                    let headingDegrees = heading * 180.0 / Double.pi
                    
                    //controller.modelRender?.mines.userHeading = -headingDegrees
                    
                    //print("\(quaternion.x) : \(quaternion.y) : \(quaternion.z)")
                }
            }
        }
    }
    
    //MARK: private
    
    private func gravityCompute(acceleration:CMAcceleration)
    {

        
        if zRotationFloat >= 0
        {
            normalizedZRotation = -(k180Deg - zRotationFloat)
        }
        else
        {
            normalizedZRotation = k180Deg + zRotationFloat
        }
        
        if rawRotation >= 0
        {
            if rawRotation < kRotationThreshold
            {
                orientationLandscapeRight()
            }
            else
            {
                orientationPortrait()
            }
        }
        else
        {
            if rawRotation > -kRotationThreshold
            {
                orientationLandscapeLeft()
            }
            else
            {
                orientationPortrait()
            }
        }
        
