Esempio n. 1
0
        public async Task SetModeAsync(MagnetometerMeasurementMode measurementMode)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException("HMC5883L");
            }
            if (!IsInitialized)
            {
                await InitializeAsync();
            }

            byte[] writeBuffer;

            // Write CRA (00) – send 0x3C 0x00 0x70 (8-average, 15 Hz default, normal measurement)
            writeBuffer = new byte[] { CONFIGURATION_REGISTER_A, SAMPLES_AVERAGED_8 | OUTPUT_RATE_15 | MEASUREMENT_MODE_NORMAL };
            _i2cController.Write(writeBuffer);

            // Write CRB (01) – send 0x3C 0x01 0xA0 (Gain=5, or any other desired gain)
            writeBuffer = new byte[] { CONFIGURATION_REGISTER_B, GAIN_390 };
            _i2cController.Write(writeBuffer);

            switch (measurementMode)
            {
            case MagnetometerMeasurementMode.Continuous:

                // Write Mode (02) – send 0x3C 0x02 0x00 (Continuous-measurement mode)
                writeBuffer = new byte[] { MODE_REGISTER, OPERATING_MODE_CONTINUOUS };
                _i2cController.Write(writeBuffer);

                // Wait 6 ms for setup then take a reading every 200 ms
                _timer = new Timer(OnMeasurementReady, null, 6, 200);

                break;

            case MagnetometerMeasurementMode.Single:

                // Write Mode (02) – send 0x3C 0x02 0x01 (Single measurement mode)
                writeBuffer = new byte[] { MODE_REGISTER, OPERATING_MODE_SINGLE };
                _i2cController.Write(writeBuffer);

                await Task.Delay(100);

                OnMeasurementReady(null);

                break;

            case MagnetometerMeasurementMode.Idle:

                // Write Mode (02) – send 0x3C 0x02 0x02 (Idle mode)
                writeBuffer = new byte[] { MODE_REGISTER, OPERATING_MODE_IDLE };
                _i2cController.Write(writeBuffer);

                break;
            }

            MeasurementMode = measurementMode;
        }
Esempio n. 2
0
        public async Task SetModeAsync(MagnetometerMeasurementMode measurementMode)
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException("HMC5883L");
            }
            if (!IsInitialized)
            {
                await InitializeAsync();
            }

            byte[] writeBuffer;

            // Write CRA (00) – send 0x3C 0x00 0x70 (8-average, 15 Hz default, normal measurement)
            writeBuffer = new byte[] { CONFIGURATION_REGISTER_A, SAMPLES_AVERAGED_8 | OUTPUT_RATE_15 | MEASUREMENT_MODE_NORMAL };
            _i2cController.Write(writeBuffer);

            // Write CRB (01) – send 0x3C 0x01 0xA0 (Gain=5, or any other desired gain)
            writeBuffer = new byte[] { CONFIGURATION_REGISTER_B, GAIN_390 };
            _i2cController.Write(writeBuffer);

            switch (measurementMode)
            {
                case MagnetometerMeasurementMode.Continuous:

                    // Write Mode (02) – send 0x3C 0x02 0x00 (Continuous-measurement mode)
                    writeBuffer = new byte[] { MODE_REGISTER, OPERATING_MODE_CONTINUOUS };
                    _i2cController.Write(writeBuffer);

                    // Wait 6 ms for setup then take a reading every 200 ms
                    _timer = new Timer(OnMeasurementReady, null, 6, 200);

                    break;

                case MagnetometerMeasurementMode.Single:

                    // Write Mode (02) – send 0x3C 0x02 0x01 (Single measurement mode)
                    writeBuffer = new byte[] { MODE_REGISTER, OPERATING_MODE_SINGLE };
                    _i2cController.Write(writeBuffer);

                    await Task.Delay(100);

                    OnMeasurementReady(null);

                    break;

                case MagnetometerMeasurementMode.Idle:

                    // Write Mode (02) – send 0x3C 0x02 0x02 (Idle mode)
                    writeBuffer = new byte[] { MODE_REGISTER, OPERATING_MODE_IDLE };
                    _i2cController.Write(writeBuffer);

                    break;
            }

            MeasurementMode = measurementMode;
        }