Example #1
0
 void ReadLoop()
 {
     while (true)
     {
         var newVal = analogInputPort.ReadValue();
         if (currentValue != newVal)
         {
             currentValue = newVal;
             AnalogValueChanged(newVal);
         }
         Thread.Sleep(200);
     }
 }
Example #2
0
 /// <summary>
 /// Read state of Joystick
 /// </summary>
 public void Read()
 {
     X = _xChannel.ReadValue();
     Y = _yChannel.ReadValue();
     //Debug.WriteLine($"x: {X}, y: {Y}");
     _bReadButtonClick = CheckEvents(X > _clickThreshold, _bReadButtonClick, Click);
     _bReadButtonDown  = CheckEvents(Y < _yMinDead, _bReadButtonDown, Down);
     _bReadButtonUp    = CheckEvents(Y > _yMaxDead, _bReadButtonUp, Up);
     _bReadButtonLeft  = CheckEvents(X < _xMinDead, _bReadButtonLeft, Left);
     _bReadButtonRight = CheckEvents(X > _xMaxDead, _bReadButtonRight, Right);
 }
Example #3
0
        static async Task adc(int channelint)
        {
            try
            {
                UpBridge.Up   upb        = new UpBridge.Up();
                AdcController controller = await AdcController.GetDefaultAsync();

                AdcChannel channel = controller.OpenChannel(channelint);
                Console.WriteLine(channel.ReadValue());
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
Example #4
0
        void Setup(AdcChannel analogPort, GpioPin digitalInputPort)
        {
            this.analogPort       = analogPort;
            this.digitalInputPort = digitalInputPort;

            digitalInputPort.ValueChanged += DigitalInputPort_ValueChanged;

            //analogPort.StartSampling();

            while (true)
            {
                Debug.WriteLine($"Analog: {analogPort.ReadValue()}");
                Thread.Sleep(250);
            }
        }
Example #5
0
        /// <summary>
        /// Reads the light intensity in mW/cm2
        /// </summary>
        /// <param name="numberOfSamples">Read the Ambient click n-times to smooth out stray values.</param>
        /// <example>Example usage:
        /// <code language = "C#">
        /// Debug.WriteLine("Light intensity in mW/cm2 is {_ambient.ReadSensor(10):F0}");
        /// </code>
        /// <code language = "VB">
        /// Debug.WriteLine("Light intensity in mW/cm2 is " <![CDATA[&]]> _ambient.ReadSensor(10).ToString("F0"))
        /// </code>
        /// </example>
        public Double ReadSensor(UInt16 numberOfSamples = 10)
        {
            if (numberOfSamples == 0)
            {
                numberOfSamples = 1;                       // Don't want to divide by Zero.
            }
            var average = 0.00;

            for (var i = 0; i < numberOfSamples - 1; i++) // Read n samples for smoothing.
            {
                average += _ambient.ReadValue();
                Thread.Sleep(1);
            }
            average /= numberOfSamples;
            return(((average * 3300) / 4095) / 7);
        }
Example #6
0
        private void Update(bool raiseEvent)
        {
            // Read X and Y values
            var val   = adcChannel.ReadValue();
            var ratio = adcChannel.ReadRatio(); // TODO: Support customized value scaling

            // Update current value
            lock (currentReading)
            {
                currentReading = new AnalogSensorReading(val, ratio);
            }

            // Notify?
            if (raiseEvent)
            {
                readingChangedEvent.Raise(this, new AnalogSensorReadingChangedEventArgs(currentReading));
            }
        }
Example #7
0
        public static void Main()
        {
            try
            {
                // See if any analog stuff is still out there
                Console.WriteLine(AdcController.GetDeviceSelector());

                // Assign first ADC
                AdcController _ctl1 = AdcController.GetDefault();

                // Some stats to see we are alive
                Console.WriteLine("Channels    : " + _ctl1.ChannelCount.ToString());
                Console.WriteLine("Active mode : " + _ctl1.ChannelMode.ToString()); // 0=SingleEnded, 1=Differential
                Console.WriteLine("Resolution  : " + _ctl1.ResolutionInBits.ToString() + " bits");
                Console.WriteLine("Min value   : " + _ctl1.MinValue);
                Console.WriteLine("Max value   : " + _ctl1.MaxValue);

                // Now open a channel.
                // We don't need additional HW to test ADC.
                // if we use the internal temp sensor
                AdcChannel _ac0 = _ctl1.OpenChannel(AdcChannels.Channel_0);

                int _val1 = -1;

                // Loopie
                for (; ;)
                {
                    _val1 = _ac0.ReadValue();
                    Console.WriteLine("Value read from ADC = " + _val1);

                    Thread.Sleep(1000);
                }
            }
            catch (Exception ex)
            {
                // Do whatever please you with the exception caught
                Console.WriteLine(ex.ToString());
                // Loopie
                for (; ;)
                {
                    Thread.Sleep(1000);
                }
            }
        }
Example #8
0
        public float GetMcuTemperature()
        {
            AdcController adc1 = AdcController.GetDefault();

            adcTemp = adc1.OpenChannel(Pinout.AdcChannel.ADC_CHANNEL_SENSOR);
            return(adcTemp.ReadValue() / 100.00f);

            //https://www.st.com/resource/en/datasheet/stm32f769ni.pdf
            //https://electronics.stackexchange.com/questions/324321/reading-internal-temperature-sensor-stm32
            //const int ADC_TEMP_3V3_30C = 0x1FF0F44C //0x1FFF7A2C;
            //const int ADC_TEMP_3V3_110C = 0x1FF0 F44E //0x1FFF7A2E;
            //const float CALIBRATION_REFERENCE_VOLTAGE = 3.3F;
            //const float REFERENCE_VOLTAGE = 3.0F; // supplied with Vref+ or VDDA

            // scale constants to current reference voltage
            //float adcCalTemp30C = getRegisterValue(ADC_TEMP_3V3_30C) * (REFERENCE_VOLTAGE / CALIBRATION_REFERENCE_VOLTAGE);
            //float adcCalTemp110C = getRegisterValue(ADC_TEMP_3V3_110C) * (REFERENCE_VOLTAGE / CALIBRATION_REFERENCE_VOLTAGE);

            // return (adcTemp.ReadValue() - adcCalTemp30C)/(adcCalTemp110C - adcCalTemp30C) *(110.0F - 30.0F) + 30.0F);
        }
Example #9
0
        public double GetTemperatureOnBoard()
        {
            AdcController adc1 = AdcController.GetDefault();

            adcTemp = adc1.OpenChannel(Pinout.AdcChannel.ADC1_IN13_TEMP);

            double tempInCent = 0;

            try
            {
                var    maximumValue     = 4095;
                var    analogReference  = 3300;
                double adcTempCalcValue = (analogReference * adcTemp.ReadValue()) / maximumValue;
                tempInCent = ((13.582f - Math.Sqrt(184.470724f + (0.01732f * (2230.8f - adcTempCalcValue)))) / (-0.00866f)) + 30;
                // double tempInF = ((9f / 5f) * tempInCent) + 32f;
            }
            catch { }

            return(tempInCent);
        }
        private async void timerCallback(object state)
        {
            Debug.WriteLine("\nMainPage::timerCallback");
            if (adcController == null)
            {
                Debug.WriteLine("MainPage::timerCallback not ready");
                return;
            }

            // The new light state, assume it's just right to start.
            eState newState = eState.JustRight;

            // Read from the ADC chip the current values of the two pots and the photo cell.
            int lowPotReadVal  = LowPotAdcChannel.ReadValue();
            int highPotReadVal = HighPotAdcChannel.ReadValue();
            int cdsReadVal     = CdsAdcChannel.ReadValue();

            // convert the ADC readings to voltages to make them more friendly.
            float lowPotVoltage  = ADCToVoltage(lowPotReadVal);
            float highPotVoltage = ADCToVoltage(highPotReadVal);
            float cdsVoltage     = ADCToVoltage(cdsReadVal);

            // Let us know what was read in.
            Debug.WriteLine(String.Format("Read values {0}, {1}, {2} ", lowPotReadVal, highPotReadVal, cdsReadVal));
            Debug.WriteLine(String.Format("Voltages {0}, {1}, {2} ", lowPotVoltage, highPotVoltage, cdsVoltage));

            // Compute the new state by first checking if the light level is too low
            if (cdsVoltage < lowPotVoltage)
            {
                newState = eState.TooDark;
            }

            // And now check if it too high.
            if (cdsVoltage > highPotVoltage)
            {
                newState = eState.TooBright;
            }

            // Use another method to determine what to do with the state.
            await CheckForStateChange(newState);
        }
Example #11
0
        //private AdcChannel adc420mA;

        /// <summary>
        /// Returns the value of the 12V battery voltage coming in the system
        /// </summary>
        /// <returns></returns>
        public double GetBatteryUnregulatedVoltage()
        {
            float voltage = 0;

            if (adcVBAT == null)
            {
                AdcController adc1 = AdcController.GetDefault();
                adcVBAT = adc1.OpenChannel(Pinout.AdcChannel.ADC1_IN8_VBAT);
            }

            var average = 0;

            for (byte i = 0; i < 5; i++)
            {
                average += adcVBAT.ReadValue();

                Thread.Sleep(50);//pause to stabilize
            }

            try
            {
                average /= 5;

                //maximumValue = 4095;
                //analogReference = 3300;
                //VBat = 0.25 x VIN adc count
                //float voltage = ((3300 * average) / 4096)* 4;

                voltage = ((3300 * average) / 4096) * 0.004f;

                voltage += 0.25f;//small offset calibration factor for board to even drop on measure
            }
            catch
            {
            }

            return(voltage);
        }
Example #12
0
        public static void Main()
        {
            string devs = AdcController.GetDeviceSelector();

            Console.WriteLine("devs=" + devs);

            AdcController adc1 = AdcController.GetDefault();

            int max1 = adc1.MaxValue;
            int min1 = adc1.MinValue;

            Console.WriteLine("min1=" + min1.ToString() + " max1=" + max1.ToString());

            AdcChannel ac0 = adc1.OpenChannel(0);

            // the following indexes are valid for STM32F769I-DISCO board
            AdcChannel vref = adc1.OpenChannel(0);
            AdcChannel vbat = adc1.OpenChannel(8);

            // VP
            //AdcChannel ac3 = adc1.OpenChannel(3);
            // VN
            while (true)
            {
                int value     = ac0.ReadValue();
                int valueVref = vref.ReadValue();
                int valueVbat = vbat.ReadValue();

                double percent = ac0.ReadRatio();

                Console.WriteLine("value0=" + value.ToString() + " ratio=" + percent.ToString());
                Console.WriteLine("verf" + valueVref.ToString() + " ratio=" + percent.ToString());
                Console.WriteLine("vbat" + valueVbat.ToString() + " ratio=" + percent.ToString());

                Thread.Sleep(1000);
            }
        }
Example #13
0
 private void Timer_Tick(object sender, object e)
 {
     valueTextBox.Text = _channel.ReadValue().ToString();
 }
 public int GetRawData()
 {
     return(_adcChannel.ReadValue());
 }
Example #15
0
 /// <summary>
 ///     Read the sensor output and convert the sensor readings into acceleration values.
 /// </summary>
 public void Update()
 {
     Conditions.XAcceleration = (_xPort.ReadValue() - _zeroGVoltage) / XVoltsPerG;
     Conditions.YAcceleration = (_yPort.ReadValue() - _zeroGVoltage) / YVoltsPerG;
     Conditions.ZAcceleration = (_zPort.ReadValue() - _zeroGVoltage) / ZVoltsPerG;
 }
        public void StartMeasurement(WindVaneMeasurementResolution measurementResolution)
        {
            if (_measuringThread.IsAlive)
            {
                throw new InvalidOperationException(
                          "Cannot start measurement because another measurement process is in progress.");
            }

            _isMeasuring = true;

            _measuringThread = new Thread(() =>
            {
                while (_isMeasuring)
                {
                    ushort adcVal = (ushort)_adcChannel.ReadValue();
                    if (adcVal >= (ushort)WindDirectionAdcResponse.N - ADC_SIGNAL_UNCERTAINTY &&
                        adcVal <= (ushort)WindDirectionAdcResponse.N + ADC_SIGNAL_UNCERTAINTY)
                    {
                        _windDirectionCounters[(ushort)WindDirection.N]++;
                        _currentWindDirection = WindDirection.N;
                    }

                    else if (adcVal >= (ushort)WindDirectionAdcResponse.NE - ADC_SIGNAL_UNCERTAINTY &&
                             adcVal <= (ushort)WindDirectionAdcResponse.NE + ADC_SIGNAL_UNCERTAINTY)
                    {
                        _windDirectionCounters[(ushort)WindDirection.NE]++;
                        _currentWindDirection = WindDirection.NE;
                    }

                    else if (adcVal >= (ushort)WindDirectionAdcResponse.E - ADC_SIGNAL_UNCERTAINTY &&
                             adcVal <= (ushort)WindDirectionAdcResponse.E + ADC_SIGNAL_UNCERTAINTY)
                    {
                        _windDirectionCounters[(ushort)WindDirection.E]++;
                        _currentWindDirection = WindDirection.E;
                    }

                    else if (adcVal >= (ushort)WindDirectionAdcResponse.SE - ADC_SIGNAL_UNCERTAINTY &&
                             adcVal <= (ushort)WindDirectionAdcResponse.SE + ADC_SIGNAL_UNCERTAINTY)
                    {
                        _windDirectionCounters[(ushort)WindDirection.SE]++;
                        _currentWindDirection = WindDirection.SE;
                    }

                    else if (adcVal >= (ushort)WindDirectionAdcResponse.S - ADC_SIGNAL_UNCERTAINTY &&
                             adcVal <= (ushort)WindDirectionAdcResponse.S + ADC_SIGNAL_UNCERTAINTY)
                    {
                        _windDirectionCounters[(ushort)WindDirection.S]++;
                        _currentWindDirection = WindDirection.S;
                    }

                    else if (adcVal >= (ushort)WindDirectionAdcResponse.SW - ADC_SIGNAL_UNCERTAINTY &&
                             adcVal <= (ushort)WindDirectionAdcResponse.SW + ADC_SIGNAL_UNCERTAINTY)
                    {
                        _windDirectionCounters[(ushort)WindDirection.SW]++;
                        _currentWindDirection = WindDirection.SW;
                    }

                    //esp32 ADC output is not linear - for voltages in range 3,2 - 3,3 the output value has same, maximum value = 4095. For wind direction "W" input from wind vane = 3,2V
                    else if (adcVal >= (ushort)WindDirectionAdcResponse.W - 3 &&
                             adcVal <= (ushort)WindDirectionAdcResponse.W)
                    {
                        _windDirectionCounters[(int)WindDirection.W]++;
                        _currentWindDirection = WindDirection.W;
                    }

                    //because for wind direction NW input from wind vane = 3,10 V is near to maximum voltage which can be measured by ADC (3,2V) we must to use lower value uncertainty range
                    else if (adcVal >= (ushort)WindDirectionAdcResponse.NW - 60 &&
                             adcVal <= (ushort)WindDirectionAdcResponse.NW + 20)
                    {
                        _windDirectionCounters[(ushort)WindDirection.NW]++;
                        _currentWindDirection = WindDirection.NW;
                    }

                    else
                    {
                        _currentWindDirection = WindDirection.Undefined;
                    }

                    Logger.Log(() => $"Current wind vane direction: {_currentWindDirection } {adcVal}");


                    Thread.Sleep((ushort)measurementResolution * 1000);
                }
            });

            _measuringThread.Start();
        }
Example #17
0
 /// <summary>
 ///     Voltage being output by the sensor.
 /// </summary>
 public float GetVoltage()
 {
     return(sensor.ReadValue());
 }
Example #18
0
 /// <summary>Gets the current voltage reading of the potentiometer.</summary>
 public double ReadVoltage()
 {
     return(_input.ReadValue() / 1000.0);
 }