public async Task Initialize()
        {
            var settings = new I2cConnectionSettings(I2C_ADDRESS)
            {
                BusSpeed = I2cBusSpeed.FastMode, SharingMode = I2cSharingMode.Shared
            };
            var controller = await I2cController.GetDefaultAsync();

            _accelerometer = controller.GetDevice(settings);

            QueuedLock.Enter();

            //Enable all axes with normal mode
            _accelerometer.Write(new byte[] { REGISTER_POWER_MANAGEMENT_1, 0 });    //Wake up device
            _accelerometer.Write(new byte[] { REGISTER_POWER_MANAGEMENT_1, 0x80 }); //Reset the device

            QueuedLock.Exit();

            await Task.Delay(20);

            QueuedLock.Enter();

            _accelerometer.Write(new byte[] { REGISTER_POWER_MANAGEMENT_1, 1 });   //Set clock source to gyro x
            _accelerometer.Write(new byte[] { REGISTER_GYROSCOPE_CONFIG, 0 });     //+/- 250 degrees sec
            _accelerometer.Write(new byte[] { REGISTER_ACCELEROMETER_CONFIG, 0 }); //+/- 2g

            _accelerometer.Write(new byte[] { REGISTER_CONFIG, 1 });               //184 Hz, 2ms delay
            _accelerometer.Write(new byte[] { REGISTER_SAMPLE_RATE_DIVIDER, 19 }); //Set rate 50Hz
            _accelerometer.Write(new byte[] { REGISTER_POWER_MANAGEMENT_1, 0 });   //Wake up device

            QueuedLock.Exit();
        }
示例#2
0
        public async Task <int> ReadDistanceInCm()
        {
            int range = 0;

            byte[] range_highLowByte = new byte[2];

            QueuedLock.Enter();

            //Call device measurement
            _distanceMeasurementSensor.Write(new byte[] { 0x51 });

            QueuedLock.Exit();

            //Wait device measured
            await Task.Delay(100);

            QueuedLock.Enter();

            //Read measurement
            _distanceMeasurementSensor.WriteRead(new byte[] { 0xe1 }, range_highLowByte);

            QueuedLock.Exit();

            range = (range_highLowByte[0] * 256) + range_highLowByte[1];

            return(range);
        }
        private AccelerationGyroleration ReadInternal()
        {
            var data        = new byte[14];                            //6 bytes equals 2 bytes * 3 axes
            var readAddress = new byte[] { REGISTER_ACCELEROMETER_X }; //0x80 for autoincrement, read from register x all three axis

            QueuedLock.Enter();

            _accelerometer.WriteRead(readAddress, data);

            QueuedLock.Exit();

            var xa = (short)(data[0] << 8 | data[1]);
            var ya = (short)(data[2] << 8 | data[3]);
            var za = (short)(data[4] << 8 | data[5]);

            var temperature = (short)(data[6] << 8 | data[7]);

            var xg = (short)(data[8] << 8 | data[9]);
            var yg = (short)(data[10] << 8 | data[11]);
            var zg = (short)(data[12] << 8 | data[13]);

            var acceleration = new AccelerationGyroleration
            {
                AccelerationX  = xa / (float)16384,
                AccelerationY  = ya / (float)16384,
                AccelerationZ  = za / (float)16384,
                TemperatureInC = temperature / 340.00 + 36.53,
                GyroX          = xg / (float)131,
                GyroY          = yg / (float)131,
                GyroZ          = zg / (float)131
            };

            return(acceleration);
        }
示例#4
0
        public void Reset()
        {
            QueuedLock.Enter();

            _pwmDevice.Write(new byte[] { (byte)Registers.MODE1, 0x0 }); // reset the device

            QueuedLock.Exit();

            SetAllPwm(4096, 0);
        }
示例#5
0
        public void SetAllPwm(ushort on, ushort off)
        {
            QueuedLock.Enter();

            _pwmDevice.Write(new byte[] { (byte)Registers.ALL_LED_ON_L, (byte)(on & 0xFF) });
            _pwmDevice.Write(new byte[] { (byte)Registers.ALL_LED_ON_H, (byte)(on >> 8) });
            _pwmDevice.Write(new byte[] { (byte)Registers.ALL_LED_OFF_L, (byte)(off & 0xFF) });
            _pwmDevice.Write(new byte[] { (byte)Registers.ALL_LED_OFF_H, (byte)(off >> 8) });

            QueuedLock.Exit();
        }
示例#6
0
        /// <summary>
        /// Set pwm values
        /// </summary>
        /// <param name="channel">The pin that should updated</param>
        /// <param name="on">The tick (between 0..4095) when the signal should change from low to high</param>
        /// <param name="off">the tick (between 0..4095) when the signal should change from high to low</param>
        public void SetPwm(byte channel, ushort on, ushort off)
        {
            QueuedLock.Enter();

            _pwmDevice.Write(new byte[] { (byte)(Registers.LED0_ON_L + 4 * channel), (byte)(on & 0xFF) });
            _pwmDevice.Write(new byte[] { (byte)(Registers.LED0_ON_H + 4 * channel), (byte)(on >> 8) });
            _pwmDevice.Write(new byte[] { (byte)(Registers.LED0_OFF_L + 4 * channel), (byte)(off & 0xFF) });
            _pwmDevice.Write(new byte[] { (byte)(Registers.LED0_OFF_H + 4 * channel), (byte)(off >> 8) });

            QueuedLock.Exit();
        }
示例#7
0
        public void ChangeI2cAddress()
        {
            //Change the I2c Address of "I2CXL-MaxSonar- EZ MB1202" because his adrees is conflicting with the adress of
            //"Adafruit 16-Channel PWM/Servo HAT for Raspberry Pi". The addresses of "I2CXL-MaxSonar- EZ MB1202" and
            //"Adafruit 16-Channel PWM/Servo HAT for Raspberry Pi" are not the same, but it conflicting because the
            //"Adafruit 16-Channel PWM/Servo HAT for Raspberry Pi" has a bug.

            QueuedLock.Enter();

            _distanceMeasurementSensor.Write(new byte[] { 0xe0, 0xAA, 0xA5, 0x71 }); //0x71 is the 8 bit address of the I2c device

            QueuedLock.Exit();
        }
示例#8
0
        /// <summary>
        /// Set the frequency (defaults to 60Hz if not set). 1 Hz equals 1 full pwm cycle per second.
        /// </summary>
        /// <param name="frequency">Frequency in Hz</param>
        public double SetDesiredFrequency(double frequency)
        {
            if (frequency > MaxFrequency || frequency < MinFrequency)
            {
                throw new ArgumentOutOfRangeException(nameof(frequency), "Frequency must be between 40 and 1000hz");
            }

            frequency *= 0.9f; //Correct for overshoot in the frequency setting (see issue #11).
            double prescaleval = 25000000f;

            prescaleval /= 4096;
            prescaleval /= frequency;
            prescaleval -= 1;

            byte prescale = (byte)Math.Floor(prescaleval + 0.5f);

            QueuedLock.Enter();

            var readBuffer = new byte[1];

            _pwmDevice.WriteRead(new byte[] { (byte)Registers.MODE1 }, readBuffer);

            byte oldmode = readBuffer[0];
            byte newmode = (byte)((oldmode & 0x7F) | 0x10); //sleep

            _pwmDevice.Write(new byte[] { (byte)Registers.MODE1, newmode });
            _pwmDevice.Write(new byte[] { (byte)Registers.PRESCALE, prescale });
            _pwmDevice.Write(new byte[] { (byte)Registers.MODE1, oldmode });
            Task.Delay(TimeSpan.FromMilliseconds(5)).Wait();
            _pwmDevice.Write(new byte[] { (byte)Registers.MODE1, (byte)(oldmode | 0xa1) });

            QueuedLock.Exit();

            ActualFrequency = frequency;

            return(ActualFrequency);
        }