Ejemplo n.º 1
0
        /// <summary>
        /// Calibrates the probe using a single point using a mV value.
        /// </summary>
        /// <param name="solutionmV">mV value</param>
        public void CalibrateSingle(float solutionmV)
        {
            Write_register((byte)Register.ISE_SOLUTION_REGISTER, solutionmV);
            Send_command((byte)Command.ISE_CALIBRATE_SINGLE);

            DelayHelper.DelayMilliseconds(ISE_MV_MEASURE_TIME, allowThreadYield: true);
        }
Ejemplo n.º 2
0
        private byte Read_byte(byte register)
        {
            Change_register(register);
            DelayHelper.DelayMilliseconds(10, allowThreadYield: true);

            return(_device.ReadByte());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Write a PiJuice command
        /// </summary>
        /// <param name="command">The PiJuice command</param>
        /// <param name="data">The data to write</param>
        internal void WriteCommand(PiJuiceCommand command, ReadOnlySpan <byte> data)
        {
            byte        tries  = 0;
            Span <byte> buffer = stackalloc byte[data.Length + 2];

            data.CopyTo(buffer.Slice(1));

            buffer[0] = (byte)command;
            buffer[buffer.Length - 1] = GetCheckSum(data, checkLastByte: true);

            // When writing/reading to the I2C port, PiJuice doesn't respond on time in some cases
            // So we wait a little bit before retrying
            // In most cases, the I2C read/write can go thru without waiting
            while (tries < MaxRetries)
            {
                try
                {
                    _i2cDevice.Write(buffer);
                    return;
                }
                catch (IOException ex)
                {
                    tries++;

                    if (tries >= MaxRetries)
                    {
                        throw new IOException($"{nameof(WriteCommand)}: Failed to write command {command}", ex);
                    }

                    DelayHelper.DelayMilliseconds(ReadRetryDelay, false);
                }
            }
        }
Ejemplo n.º 4
0
        public static void MotorTest(List <string> argsList, DCMotor motorL, DCMotor motorR)
        {
            double delay;

            if (argsList.Count > 1)
            {
                delay = Convert.ToDouble(argsList[1]);
            }
            else
            {
                delay = 10;
            }

            const double Period = 20.0;

            Console.WriteLine($"Motor Test");
            Stopwatch sw            = Stopwatch.StartNew();
            string    lastSpeedDisp = null;

            while (sw.ElapsedMilliseconds < (Math.PI * 2000))
            {
                double time = sw.ElapsedMilliseconds / 1000.0;

                // Note: range is from -1 .. 1 (for 1 pin setup 0 .. 1)
                motorL.Speed = Math.Sin(2.0 * Math.PI * time / Period);
                motorR.Speed = Math.Sin(2.0 * Math.PI * time / Period);
                string disp = $"Speed[L, R] = [{motorL.Speed:0.00}, {motorR.Speed:0.00}]";
                if (disp != lastSpeedDisp)
                {
                    lastSpeedDisp = disp;
                    Console.WriteLine(disp);
                }
                DelayHelper.DelayMilliseconds((int)delay, true);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initializes the bit mode settings.
        /// </summary>
        protected override void InitializeBitMode()
        {
            // Prep the pins
            _controller.OpenPin(_rsPin, PinMode.Output);

            if (_rwPin != -1)
            {
                _controller.OpenPin(_rwPin, PinMode.Output);
            }
            if (_backlight != -1)
            {
                _controller.OpenPin(_backlight, PinMode.Output);
                if (_backlightBrightness > 0)
                {
                    // Turn on the backlight
                    _controller.Write(_backlight, PinValue.High);
                }
            }
            _controller.OpenPin(_enablePin, PinMode.Output);

            for (int i = 0; i < _dataPins.Length; ++i)
            {
                _controller.OpenPin(_dataPins[i], PinMode.Output);
            }

            // The HD44780 self-initializes when power is turned on to the following settings:
            //
            //  - 8 bit, 1 line, 5x7 font
            //  - Display, cursor, and blink off
            //  - Increment with no shift
            //
            // It is possible that the initialization will fail if the power is not provided
            // within specific tolerances. As such, we'll always perform the software based
            // initialization as described on pages 45/46 of the HD44780 data sheet. We give
            // a little extra time to the required waits.

            if (_dataPins.Length == 8)
            {
                // Init to 8 bit mode
                DelayHelper.DelayMilliseconds(50, allowThreadYield: true);
                Send(0b0011_0000);
                DelayHelper.DelayMilliseconds(5, allowThreadYield: true);
                Send(0b0011_0000);
                DelayHelper.DelayMicroseconds(100, allowThreadYield: true);
                Send(0b0011_0000);
            }
            else
            {
                // Init to 4 bit mode, setting _rspin to low as we're writing 4 bits directly.
                // (Send writes the whole byte in two 4bit/nybble chunks)
                _controller.Write(_rsPin, PinValue.Low);
                DelayHelper.DelayMilliseconds(50, allowThreadYield: true);
                WriteBits(0b0011, 4);
                DelayHelper.DelayMilliseconds(5, allowThreadYield: true);
                WriteBits(0b0011, 4);
                DelayHelper.DelayMicroseconds(100, allowThreadYield: true);
                WriteBits(0b0011, 4);
                WriteBits(0b0010, 4);
            }
        }
Ejemplo n.º 6
0
        public static void IrTest(List <string> argsList, IrReceiver ir)
        {
            Console.WriteLine($"Ir Test");

            double delay;

            if (argsList.Count > 1)
            {
                delay = Convert.ToDouble(argsList[1]);
            }
            else
            {
                delay = 10;
            }

            while (true)
            {
                int data = ir.GetKey();
                if (data == 0 & data != 999)
                {
                    Console.Write($"_");
                }
                else if (data == 999)
                {
                    Console.WriteLine($"data: repeated last");
                }
                else
                {
                    Console.WriteLine($"data: {data} ");
                }
                DelayHelper.DelayMilliseconds((int)delay, true);
            }
        }
Ejemplo n.º 7
0
        public static void AdcTest(List <string> argsList, Tlc1543 adc)
        {
            Console.WriteLine($"ADC Test");
            double delay;
            byte   sensorNumber;

            if (argsList.Count > 1)
            {
                delay = Convert.ToDouble(argsList[1]);
            }
            else
            {
                delay = 10;
            }
            if (argsList.Count > 2)
            {
                sensorNumber = Convert.ToByte(argsList[2]);
            }
            else
            {
                sensorNumber = 11;
            }

            while (true)
            {
                for (int i = 0; i < sensorNumber; i++)
                {
                    Console.Write($"{i}: {adc.ReadChannel((Tlc1543.Channel)i),4} ");
                    DelayHelper.DelayMilliseconds((int)delay, true);
                }
                Console.WriteLine();
            }
        }
Ejemplo n.º 8
0
        private void Write_byte(byte register, byte value)
        {
            Change_register(register);

            _device.WriteByte(value);

            DelayHelper.DelayMilliseconds(10, allowThreadYield: true);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Create a DHT10 sensor through I2C
 /// </summary>
 /// <param name="i2cDevice">I2C Device</param>
 public Dht10(I2cDevice i2cDevice)
     : base(i2cDevice)
 {
     i2cDevice.WriteByte(DHT10_CMD_SOFTRESET);
     // make sure DHT10 stable (in the datasheet P7)
     DelayHelper.DelayMilliseconds(20, true);
     i2cDevice.WriteByte(DHT10_CMD_INIT);
 }
Ejemplo n.º 10
0
        private void Send_command(byte data)
        {
            Span <byte> bytes = stackalloc byte[2];

            bytes[0] = (byte)Register.ISE_TASK_REGISTER;
            bytes[1] = data;
            _device.Write(bytes);
            DelayHelper.DelayMilliseconds(10, allowThreadYield: true);
        }
Ejemplo n.º 11
0
 /// <summary>
 /// Initializes the bit mode settings.
 /// </summary>
 protected override void InitializeBitMode()
 {
     // Init to 8 bit mode
     DelayHelper.DelayMilliseconds(50, allowThreadYield: true);
     Send(0b0011_0000);
     DelayHelper.DelayMilliseconds(5, allowThreadYield: true);
     Send(0b0011_0000);
     DelayHelper.DelayMicroseconds(100, allowThreadYield: true);
     Send(0b0011_0000);
 }
Ejemplo n.º 12
0
        /// <summary>
        /// Measure mV
        /// </summary>
        /// <returns>mV read from ISE Probe Interface</returns>
        public float MeasuremV()
        {
            Send_command((byte)Command.ISE_MEASURE_MV);

            DelayHelper.DelayMilliseconds(ISE_MV_MEASURE_TIME, allowThreadYield: true);

            _mV = Read_register((byte)Register.ISE_MV_REGISTER);
            _mV = Convert.ToSingle(Math.Round(_mV, 2));

            return(_mV);
        }
Ejemplo n.º 13
0
        internal override void ReadThroughI2c()
        {
            // DHT10 has no calibration bits
            IsLastReadSuccessful = true;

            _i2cDevice.WriteByte(DHT10_CMD_START);
            // make sure DHT10 ends measurement (in the datasheet P7)
            DelayHelper.DelayMilliseconds(75, true);

            _i2cDevice.Read(_dht10ReadBuff);
        }
Ejemplo n.º 14
0
        private void Write(Register register)
        {
            Span <byte> writeBuff = stackalloc byte[2];

            BinaryPrimitives.WriteUInt16BigEndian(writeBuff, (ushort)register);

            _i2cDevice.Write(writeBuff);

            // wait SCL free
            DelayHelper.DelayMilliseconds(20, false);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Read data from PiJuice
        /// </summary>
        /// <param name="command">The PiJuice command</param>
        /// <param name="length">The length of the data to be read</param>
        /// <returns>Returns an array of the bytes read</returns>
        internal byte[] ReadCommand(PiJuiceCommand command, byte length)
        {
            byte        tries    = 0;
            Span <byte> outArray = stackalloc byte[length + 1];

            outArray.Clear();

            // When writing/reading the I2C port, PiJuice doesn't respond on time in some cases
            // So we wait a little bit before retrying
            // In most cases, the I2C read/write can go thru without waiting
            while (tries < MaxRetries)
            {
                try
                {
                    _i2cDevice.Write(new byte[] { (byte)command });
                    _i2cDevice.Read(outArray);

                    var checksum = GetCheckSum(outArray);
                    if (checksum != outArray[length])
                    {
                        outArray[0] |= 0x80;
                        checksum     = GetCheckSum(outArray);
                        if (checksum != outArray[length])
                        {
                            tries++;

                            if (tries >= MaxRetries)
                            {
                                throw new InvalidDataException($"{nameof(ReadCommand)}: Invalid checksum read command {command}, checksum {checksum}");
                            }

                            DelayHelper.DelayMilliseconds(ReadRetryDelay, false);
                            continue;
                        }
                    }

                    break;
                }
                catch (IOException ex)
                {
                    tries++;

                    if (tries >= MaxRetries - 1)
                    {
                        throw new IOException($"{nameof(ReadCommand)}: Failed to read command {command}", ex);
                    }

                    DelayHelper.DelayMilliseconds(ReadRetryDelay, false);
                }
            }

            return(outArray.Slice(0, length).ToArray());
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Initializes the Seesaw device.
        /// <param name="i2cDevice">The I2cDevice to initialize the Seesaw device with.</param>
        /// </summary>
        protected void Initialize(I2cDevice i2cDevice)
        {
            SoftwareReset();
            DelayHelper.DelayMilliseconds(10, true);

            if (ReadByte(SeesawModule.Status, SeesawFunction.StatusHwId) != SessawHardwareId)
            {
                throw new NotSupportedException($"The hardware on I2C Bus {I2cDevice.ConnectionSettings.BusId}, Address 0x{I2cDevice.ConnectionSettings.DeviceAddress:X2} does not appear to be an Adafruit SeeSaw module");
            }

            _options = GetOptions();
            Version  = GetVersion();
        }
Ejemplo n.º 17
0
        internal override void ReadThroughI2c()
        {
            if (_i2cDevice is null)
            {
                throw new Exception("I2C decvice not configured.");
            }

            // DHT10 has no calibration bits
            IsLastReadSuccessful = true;

            _i2cDevice.WriteByte(DHT10_CMD_START);
            // make sure DHT10 ends measurement (in the datasheet P7)
            DelayHelper.DelayMilliseconds(75, true);

            _i2cDevice.Read(_dht10ReadBuff);
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Write a PiJuice command and verify
        /// </summary>
        /// <param name="command">The PiJuice command</param>
        /// <param name="data">The data to write</param>
        /// <param name="delay">The delay before reading the data</param>
        internal void WriteCommandVerify(PiJuiceCommand command, ReadOnlySpan <byte> data, int delay = 0)
        {
            WriteCommand(command, data);

            if (delay > 0)
            {
                DelayHelper.DelayMilliseconds(delay, false);
            }

            var response = ReadCommand(command, (byte)data.Length);

            if (!data.SequenceEqual(response))
            {
                throw new IOException($"{nameof(WriteCommandVerify)}: Failed to write command {command}");
            }
        }
Ejemplo n.º 19
0
        private void updateSizeLoop()
        {
            try
            {
                while (resizeDown || resizeLeft || resizeRight || resizeUp)
                {
                    _window.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, new RefreshDelegate(updateSize));
                    _window.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, new RefreshDelegate(updateMouseDown));
                    DelayHelper.DelayMilliseconds(10);
                }

                _window.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Render, new RefreshDelegate(setArrowCursor));
            }
            catch (Exception)
            {
            }
        }
Ejemplo n.º 20
0
        /// <summary>
        /// Measure temperature
        /// </summary>
        /// <returns>Temperature in celsius</returns>
        public float MeasureTemp()
        {
            Send_command((byte)Command.ISE_MEASURE_TEMP);

            DelayHelper.DelayMilliseconds(ISE_TEMP_MEASURE_TIME, allowThreadYield: true);
            _tempC = Read_register((byte)Register.ISE_TEMP_REGISTER);

            if (_tempC == -127.0)
            {
                _tempF = -127.0F;
            }
            else
            {
                _tempF = ((_tempC * 9) / 5) + 32;
            }

            return(_tempC);
        }
Ejemplo n.º 21
0
        public static void CameraTest(List <string> argsList, Camera camera)
        {
            Console.WriteLine($"Camera Test");
            double delay;

            if (argsList.Count > 1)
            {
                delay = Convert.ToDouble(argsList[1]);
            }
            else
            {
                delay = 100;
            }

            camera.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps, 30);
            camera.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth, 1920);
            camera.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight, 1080);

            //int fourcc = VideoWriter.Fourcc('I', 'Y', 'U', 'V');
            //
            //try
            //{
            //    VideoW = new VideoWriter(@"/home/pi/test_%02d.avi",
            //        frameRate, fourcc,
            //        new System.Drawing.Size(
            //            (int)camera.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameWidth),
            //            (int)camera.GetCaptureProperty(Emgu.CV.CvEnum.CapProp.FrameHeight)),
            //        true);
            //}
            //catch (Exception e)
            //{
            //    Console.WriteLine(e.Message);
            //}
            //
            //VideoW = new VideoWriter(@"/home/pi/test.avi", 30, fourcc, new System.Drawing.Size(1280, 720), true);

            camera.Start();

            while (true)
            {
                DelayHelper.DelayMilliseconds((int)delay, true);
            }
        }
Ejemplo n.º 22
0
        private void Write_register(byte register, float?data)
        {
            byte[] dataArray = { 0, 0, 0, 0 };

            if (data != null)
            {
                dataArray = BitConverter.GetBytes(Round_total_digits(data.Value));
            }

            Span <byte> bytes = new Span <byte>(new byte[4]);

            bytes[0] = dataArray[0];
            bytes[1] = dataArray[1];
            bytes[2] = dataArray[2];
            bytes[3] = dataArray[3];

            Change_register(register);
            _device.Write(bytes);
            DelayHelper.DelayMilliseconds(10, allowThreadYield: true);
        }
Ejemplo n.º 23
0
        public static void IrTest1(List <string> argsList, IrReceiver ir)
        {
            Console.WriteLine($"Ir Test1");

            double delay;

            if (argsList.Count > 1)
            {
                delay = Convert.ToDouble(argsList[1]);
            }
            else
            {
                delay = 10;
            }

            while (true)
            {
                Console.WriteLine($"{ir.GetKeyTemp()}");
                DelayHelper.DelayMilliseconds((int)delay, true);
            }
        }
Ejemplo n.º 24
0
            private void Initialize()
            {
                // Prep the pins
                _controller.OpenPin(_rsPin, PinMode.Output);

                if (_rwPin != -1)
                {
                    _controller.OpenPin(_rwPin, PinMode.Output);

                    // Set to write. Once we enable reading have reading pull high and reset
                    // after reading to give maximum performance to write (i.e. assume that
                    // the pin is low when writing).
                    _controller.Write(_rwPin, PinValue.Low);
                }

                if (_backlight != -1)
                {
                    _controller.OpenPin(_backlight, PinMode.Output);
                    if (_backlightBrightness > 0)
                    {
                        // Turn on the backlight
                        _controller.Write(_backlight, PinValue.High);
                    }
                }

                _controller.OpenPin(_enablePin, PinMode.Output);

                for (int i = 0; i < _dataPins.Length; ++i)
                {
                    _controller.OpenPin(_dataPins[i], PinMode.Output);
                }

                // The HD44780 self-initializes when power is turned on to the following settings:
                //
                //  - 8 bit, 1 line, 5x7 font
                //  - Display, cursor, and blink off
                //  - Increment with no shift
                //
                // It is possible that the initialization will fail if the power is not provided
                // within specific tolerances. As such, we'll always perform the software based
                // initialization as described on pages 45/46 of the HD44780 data sheet. We give
                // a little extra time to the required waits as described.
                if (_dataPins.Length == 8)
                {
                    // Init to 8 bit mode (this is the default, but other drivers
                    // may set the controller to 4 bit mode, so reset to be safe.)
                    DelayHelper.DelayMilliseconds(50, allowThreadYield: true);
                    WriteBits(0b0011_0000, 8);
                    DelayHelper.DelayMilliseconds(5, allowThreadYield: true);
                    WriteBits(0b0011_0000, 8);
                    DelayHelper.DelayMicroseconds(100, allowThreadYield: true);
                    WriteBits(0b0011_0000, 8);
                }
                else
                {
                    // Init to 4 bit mode, setting _rspin to low as we're writing 4 bits directly.
                    // (Send writes the whole byte in two 4bit/nybble chunks)
                    _controller.Write(_rsPin, PinValue.Low);
                    DelayHelper.DelayMilliseconds(50, allowThreadYield: true);
                    WriteBits(0b0011, 4);
                    DelayHelper.DelayMilliseconds(5, allowThreadYield: true);
                    WriteBits(0b0011, 4);
                    DelayHelper.DelayMicroseconds(100, allowThreadYield: true);
                    WriteBits(0b0011, 4);
                    WriteBits(0b0010, 4);
                }

                // The busy flag can NOT be checked until this point.
            }
Ejemplo n.º 25
0
 private void Change_register(byte register)
 {
     _device.WriteByte(register);
     DelayHelper.DelayMilliseconds(10, allowThreadYield: true);
 }
Ejemplo n.º 26
0
        /// <summary>
        /// Read through One-Wire
        /// </summary>
        internal virtual void ReadThroughOneWire()
        {
            byte readVal = 0;
            uint count;

            // keep data line HIGH
            _controller.SetPinMode(_pin, PinMode.Output);
            _controller.Write(_pin, PinValue.High);
            DelayHelper.DelayMilliseconds(20, true);

            // send trigger signal
            _controller.Write(_pin, PinValue.Low);
            // wait at least 18 milliseconds
            // here wait for 18 milliseconds will cause sensor initialization to fail
            DelayHelper.DelayMilliseconds(20, true);

            // pull up data line
            _controller.Write(_pin, PinValue.High);
            // wait 20 - 40 microseconds
            DelayHelper.DelayMicroseconds(30, true);

            _controller.SetPinMode(_pin, PinMode.InputPullUp);

            // DHT corresponding signal - LOW - about 80 microseconds
            count = _loopCount;
            while (_controller.Read(_pin) == PinValue.Low)
            {
                if (count-- == 0)
                {
                    IsLastReadSuccessful = false;
                    return;
                }
            }

            // HIGH - about 80 microseconds
            count = _loopCount;
            while (_controller.Read(_pin) == PinValue.High)
            {
                if (count-- == 0)
                {
                    IsLastReadSuccessful = false;
                    return;
                }
            }

            // the read data contains 40 bits
            for (int i = 0; i < 40; i++)
            {
                // beginning signal per bit, about 50 microseconds
                count = _loopCount;
                while (_controller.Read(_pin) == PinValue.Low)
                {
                    if (count-- == 0)
                    {
                        IsLastReadSuccessful = false;
                        return;
                    }
                }

                // 26 - 28 microseconds represent 0
                // 70 microseconds represent 1
                _stopwatch.Restart();
                count = _loopCount;
                while (_controller.Read(_pin) == PinValue.High)
                {
                    if (count-- == 0)
                    {
                        IsLastReadSuccessful = false;
                        return;
                    }
                }

                _stopwatch.Stop();

                // bit to byte
                // less than 40 microseconds can be considered as 0, not necessarily less than 28 microseconds
                // here take 30 microseconds
                readVal <<= 1;
                if (!(_stopwatch.ElapsedTicks * 1000000F / Stopwatch.Frequency <= 30))
                {
                    readVal |= 1;
                }

                if (((i + 1) % 8) == 0)
                {
                    _readBuff[i / 8] = readVal;
                }
            }

            _lastMeasurement = Environment.TickCount;

            if ((_readBuff[4] == ((_readBuff[0] + _readBuff[1] + _readBuff[2] + _readBuff[3]) & 0xFF)))
            {
                IsLastReadSuccessful = (_readBuff[0] != 0) || (_readBuff[2] != 0);
            }
            else
            {
                IsLastReadSuccessful = false;
            }
        }