Beispiel #1
0
 public override void DoSave()
 {
     if (Context.Player != null && !ChildModel)
     {
         Player p = Context.Player;
         if (Saving.Enter())
         {
             SaveLock.WaitOne();
             try
             {
                 if (p.FilePath is IFile file)
                 {
                     Context.UnsavedChanges = 0;
                     using (Stream s = file.OpenAsync(FileAccess.ReadAndWrite).Result)
                     {
                         Player.Serializer.Serialize(s, p);
                         s.SetLength(s.Position);
                     }
                 }
             } catch (Exception e)
             {
                 ConfigManager.LogError("Error Saving", e);
             }
             SaveLock.ReleaseMutex();
             Saving.Exit();
         }
     }
 }
        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();
        }
Beispiel #3
0
        void SerialDataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                queuedLock.Enter();
                _dataViewParsedBuilder.Append(Encoding.ASCII.GetString(e.Data));

                foreach (byte data in e.Data)
                {
                    char character = (char)data;
                    if (char.IsControl(character))
                    {
                        character = '.';
                    }

                    _dataViewHexBuilder.Append(string.Format("{0:x2} ", data));
                    _dataViewRawBuilder.Append(character);

                    if (++_rawDataCounter == 16)
                    {
                        _dataViewHexBuilder.Append("\r\n");
                        _dataViewRawBuilder.Append("\r\n");
                        _rawDataCounter = 0;
                    }
                }
            }
            finally
            {
                queuedLock.Exit();
            }
        }
Beispiel #4
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);
        }
Beispiel #6
0
        public void Reset()
        {
            QueuedLock.Enter();

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

            QueuedLock.Exit();

            SetAllPwm(4096, 0);
        }
Beispiel #7
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();
        }
Beispiel #8
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();
        }
Beispiel #9
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();
        }
Beispiel #10
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);
        }