Exemplo n.º 1
0
        private uint GetRawPressure()
        {
            WriteByte(Interop.CONTROL, (byte)(Interop.READPRESSURECMD + ((int)precision << 6)));

            switch (precision)
            {
            case Bmp085Precision.Low:
                HighResolutionTimer.Sleep(lowDelay);
                break;

            case Bmp085Precision.High:
                HighResolutionTimer.Sleep(highDelay);
                break;

            case Bmp085Precision.Highest:
                HighResolutionTimer.Sleep(highestDelay);
                break;

            default:
                HighResolutionTimer.Sleep(defaultDelay);
                break;
            }

            var msb  = ReadByte(Interop.PRESSUREDATA);
            var lsb  = ReadByte(Interop.PRESSUREDATA + 1);
            var xlsb = ReadByte(Interop.PRESSUREDATA + 2);
            var raw  = ((msb << 16) + (lsb << 8) + xlsb) >> (8 - (int)precision);

            return((uint)raw);
        }
Exemplo n.º 2
0
        private ushort GetRawTemperature()
        {
            WriteByte(Interop.CONTROL, Interop.READTEMPCMD);
            HighResolutionTimer.Sleep(lowDelay);

            return(ReadUInt16(Interop.TEMPDATA));
        }
Exemplo n.º 3
0
        private void SendByte(byte data)
        {
            // Send one bit at a time, starting with the MSB
            for (byte i = 0; i < 8; i++)
            {
                // If MSB is 1, write one and clock it, else write 0 and clock
                if ((data & 0x80) != 0)
                {
                    dataPin.Write(true);
                }
                else
                {
                    dataPin.Write(false);
                }

                // clk():
                clockPin.Write(false);
                HighResolutionTimer.Sleep(0.02m);
                clockPin.Write(true);
                HighResolutionTimer.Sleep(0.02m);

                // Advance to the next bit to send
                data <<= 1;
            }
        }
Exemplo n.º 4
0
 private void LatchData()
 {
     dataPin.Write(false);
     HighResolutionTimer.Sleep(delay);
     for (int i = 0; i < 4; i++)
     {
         dataPin.Write(true);
         dataPin.Write(false);
     }
 }
        /// <summary>
        /// Senses the distance.
        /// </summary>
        public void SenseDistance()
        {
            var timer = new HighResolutionTimer();
            this.EnsureReady();

            _timer = new Stopwatch();
            _trigPin.Write(GpioPinValue.High);
            timer.Sleep(0.01d);
            _trigPin.Write(GpioPinValue.Low);
        }
        /// <summary>
        /// Sets the pin resistor.
        /// </summary>
        /// <param name="pin">The pin.</param>
        /// <param name="resistor">The resistor.</param>
        public void SetPinResistor(ProcessorPin pin, PinResistor resistor)
        {
            // Set the pullup/down resistor for a pin
            //
            // The GPIO Pull-up/down Clock Registers control the actuation of internal pull-downs on
            // the respective GPIO pins. These registers must be used in conjunction with the GPPUD
            // register to effect GPIO Pull-up/down changes. The following sequence of events is
            // required:
            // 1. Write to GPPUD to set the required control signal (i.e. Pull-up or Pull-Down or neither
            // to remove the current Pull-up/down)
            // 2. Wait 150 cycles ? this provides the required set-up time for the control signal
            // 3. Write to GPPUDCLK0/1 to clock the control signal into the GPIO pads you wish to
            // modify ? NOTE only the pads which receive a clock will be modified, all others will
            // retain their previous state.
            // 4. Wait 150 cycles ? this provides the required hold time for the control signal
            // 5. Write to GPPUD to remove the control signal
            // 6. Write to GPPUDCLK0/1 to remove the clock
            //
            // RPi has P1-03 and P1-05 with 1k8 pullup resistor

            uint pud;

            switch (resistor)
            {
            case PinResistor.None:
                pud = Interop.BCM2835_GPIO_PUD_OFF;
                break;

            case PinResistor.PullDown:
                pud = Interop.BCM2835_GPIO_PUD_DOWN;
                break;

            case PinResistor.PullUp:
                pud = Interop.BCM2835_GPIO_PUD_UP;
                break;

            default:
                throw new ArgumentOutOfRangeException("resistor", resistor, string.Format(CultureInfo.InvariantCulture, "{0} is not a valid value for pin resistor", resistor));
            }

            WriteResistor(pud);
            HighResolutionTimer.Sleep(resistorSetDelay);
            SetPinResistorClock(pin, true);
            HighResolutionTimer.Sleep(resistorSetDelay);
            WriteResistor(Interop.BCM2835_GPIO_PUD_OFF);
            SetPinResistorClock(pin, false);

            pinResistors[pin] = PinResistor.None;
        }
Exemplo n.º 7
0
            public double GetData()
            {
                Connection.Write(0x10);
                HighResolutionTimer.Sleep(TimeSpanUtility.FromMicroseconds(150 * 1000));
                byte[] readBuf = Connection.Read(2);

                var valf = readBuf[0] << 8;

                valf |= readBuf[1];
                return(valf / 1.2 * (69 / 69) / 1);

                // var valf = ((readBuf[0] << 8) | readBuf[1]) / 1.2;
                // return valf;

                // return Math.Round(valf / (2 * 1.2), 2);
            }
        private DhtData TryGetData()
        {
            // Prepare buffer
            var data = new byte[5];

            for (var i = 0; i < 5; i++)
            {
                data[i] = 0;
            }

            var remainingSamplingInterval = SamplingInterval - (DateTime.UtcNow - previousRead);

            if (remainingSamplingInterval > TimeSpan.Zero)
            {
                HighResolutionTimer.Sleep((int)remainingSamplingInterval.TotalMilliseconds);
            }

            // Prepare for reading
            try
            {
                // Measure required by host : pull down then pull up
                pin.Write(false);
                HighResolutionTimer.Sleep((decimal)WakeupInterval.TotalMilliseconds);
                pin.Write(true);

                // Read acknowledgement from DHT
                pin.Wait(true, timeout);
                pin.Wait(false, timeout);

                // Read 40 bits output, or time-out
                var cnt = 7;
                var idx = 0;
                for (var i = 0; i < 40; i++)
                {
                    pin.Wait(true, timeout);
                    var start = DateTime.UtcNow;
                    pin.Wait(false, timeout);

                    // Determine whether bit is "1" or "0"
                    if (DateTime.UtcNow - start > bitSetUptime)
                    {
                        data[idx] |= (byte)(1 << cnt);
                    }

                    if (cnt == 0)
                    {
                        idx++;      // next byte
                        cnt = 7;    // restart at MSB
                    }
                    else
                    {
                        cnt--;
                    }
                }
            }
            finally
            {
                // Prepare for next reading
                previousRead = DateTime.UtcNow;
                pin.Write(true);
            }

            var checkSum = data[0] + data[1] + data[2] + data[3];

            if ((checkSum & 0xff) != data[4])
            {
                throw new InvalidChecksumException("Invalid checksum on DHT data", data[4], (checkSum & 0xff));
            }

            var sign = 1;

            if ((data[2] & 0x80) != 0) // negative temperature
            {
                data[2] = (byte)(data[2] & 0x7F);
                sign    = -1;
            }

            var humidity    = (data[0] << 8) + data[1];
            var temperature = sign * ((data[2] << 8) + data[3]);

            return(GetDhtData(temperature, humidity));
        }