How to stream analog data?

I've chosen to put this up as a small 'thank you' to mbient, hailing from this question: https://mbientlab.com/community/discussion/2913/supplying-mbl-mw-datasignal-read-with-parameters-with-unsaferawpointer-to-struct

After connecting to the device and performing segue to another ViewController (SceneKit):

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);

        print("Restoring")
        if let state = DeviceState.loadForDevice(device) {
            // Initialize the device
            device.deserialize(state.serializedState)
            print("Connecting")
            device.connectAndSetup().continueWith { t in
                if let error = t.error {
                    // Sorry we couldn't connect
                    //self.deviceStatus.text = error.localizedDescription
                    print("ERROR: Could not connect!")
                } else {
                    // The result of a connectAndSetup call is a task which completes upon disconnection.
                    t.result!.continueWith {
                        state.serializedState = self.device.serialize()
                        state.saveToUrl(self.device.uniqueUrl)
                        print($0.error?.localizedDescription ?? "Disconnected")
                    }
                    print("Connected")
                    self.device.flashLED(color: .green, intensity: 1.0, _repeat: 3)
                    self.fusionTest()
                    print("Steaming")
                }
            }
        }
    }

func fusionTest() {
        configureFusion()
        let incomming = mbl_mw_sensor_fusion_get_data_signal(device.board, MBL_MW_SENSOR_FUSION_DATA_QUATERNION)
        mbl_mw_datasignal_subscribe(incomming!, bridge(obj: self)) { (context, data) in
            let dataSignal = data!.pointee.valueAs() as MblMwQuaternion
            DispatchQueue.main.async{
                let mySelf = Unmanaged<GraphicsViewController>.fromOpaque(context!).takeUnretainedValue()
                let myQuatanion = SCNQuaternion(x: dataSignal.x, y:dataSignal.y, z:dataSignal.z, w:dataSignal.w)
                mySelf.pyramidNode.worldOrientation = myQuatanion
            }
        }

        mbl_mw_sensor_fusion_enable_data(device.board, MBL_MW_SENSOR_FUSION_DATA_QUATERNION)
        mbl_mw_sensor_fusion_start(device.board)

        device.timerCreate(period: 1000)
            .continueOnSuccessWithTask { timer -> Task<Void> in
            // Start the timer, this even will fire every temperaturePeriodMsec
            mbl_mw_timer_start(timer)
            // Now put the SDK in recording mode, all SDK calls between
            // mbl_mw_event_record_commands and eventEndRecord will be
            // programmed to the MetaWear and invoked each time 'timer' fires
            mbl_mw_event_record_commands(timer)
            // All we want the timer to do is read the temperature
            //self.device.flashLED(color: .green, intensity: 1.0, _repeat: 1)
            let tempSignal = mbl_mw_gpio_get_analog_input_data_signal(self.device.board, 0, MBL_MW_GPIO_ANALOG_READ_MODE_ADC)

            mbl_mw_datasignal_subscribe(tempSignal!, bridge(obj: self)) { (context, data) in
                let dataSignal = data!.pointee.valueAs() as UInt32
                DispatchQueue.main.async{
                    let mySelf = Unmanaged<GraphicsViewController>.fromOpaque(context!).takeUnretainedValue()
                    mySelf.printData(data: dataSignal)
                }
            }
            var readParams = MblMwGpioAnalogReadParameters.init(pullup_pin: 0, pulldown_pin: 1, virtual_pin: 0, delay_us: 10)
            mbl_mw_datasignal_read_with_parameters(tempSignal, &readParams)
            // Finish up the event
            return timer.eventEndRecord()
        }
    }

    func configureFusion() {
        // set fusion mode to ndof (n degress of freedom)
        mbl_mw_sensor_fusion_set_mode(device.board, MBL_MW_SENSOR_FUSION_MODE_NDOF);
        // set acceleration range to +/-8G, note accelerometer is configured here
        mbl_mw_sensor_fusion_set_acc_range(device.board, MBL_MW_SENSOR_FUSION_ACC_RANGE_8G);
        // write changes to the board
        mbl_mw_sensor_fusion_write_config(device.board);
    }

This should in essence set up a NSNode object in a SCNScene to turn in unison with the sensor, as well as print the voltage on pin 0 every second.

No answers necessary, but comments and improvements are more than welcome.

Sign In or Register to comment.