示例#1
0
        public static void TestDht(ArduinoBoard board)
        {
            Console.WriteLine("Reading DHT11. Any key to quit.");
            DhtSensor?handler = board.GetCommandHandler <DhtSensor>();

            if (handler == null)
            {
                Console.WriteLine("DHT Command handler not available.");
                return;
            }

            while (!Console.KeyAvailable)
            {
                // Read from DHT11 at pin 3
                if (handler.TryReadDht(3, 11, out var temperature, out var humidity))
                {
                    Console.WriteLine($"Temperature: {temperature}, Humidity {humidity}");
                }
示例#2
0
        private static void TestFrequency(ArduinoBoard board)
        {
            Console.Write("Which pin number to use? ");
            string?input = Console.ReadLine();

            if (input == null)
            {
                return;
            }

            if (!int.TryParse(input, out int pin))
            {
                return;
            }

            FrequencySensor?sensor = board.GetCommandHandler <FrequencySensor>();

            if (sensor == null)
            {
                Console.WriteLine("Frequency handling software module missing");
                return;
            }

            try
            {
                sensor.EnableFrequencyReporting(pin, FrequencyMode.Rising, 500);

                while (!Console.KeyAvailable)
                {
                    var f = sensor.GetMeasuredFrequency();
                    Console.Write($"\rFrequency at GPIO{pin}: {f}                       ");
                    Thread.Sleep(100);
                }
            }
            finally
            {
                sensor.DisableFrequencyReporting(pin);
            }

            Console.ReadKey(true);
            Console.WriteLine();
        }
示例#3
0
        public static void DisplayModes(ArduinoBoard board)
        {
            const int ButtonPin       = 2;
            const int MaxMode         = 10;
            Length    stationAltitude = Length.FromMeters(650);
            int       mode            = 0;
            var       gpioController  = board.CreateGpioController();

            GpioButton       button = new GpioButton(ButtonPin, gpioController, false, PinMode.Input, TimeSpan.FromMilliseconds(200));
            CharacterDisplay disp   = new CharacterDisplay(board);

            Console.WriteLine("Display output test");
            Console.WriteLine("The button on GPIO 2 changes modes");
            Console.WriteLine("Press x to exit");
            disp.Output.ScrollUpDelay = TimeSpan.FromMilliseconds(500);
            AutoResetEvent buttonClicked = new AutoResetEvent(false);

            void ChangeMode(object?sender, EventArgs pinValueChangedEventArgs)
            {
                mode++;
                if (mode > MaxMode)
                {
                    // Don't change back to 0
                    mode = 1;
                }

                buttonClicked.Set();
            }

            button.Press += ChangeMode;

            var    device = board.CreateI2cDevice(new I2cConnectionSettings(0, Bmp280.DefaultI2cAddress));
            Bmp280?bmp;

            try
            {
                bmp             = new Bmp280(device);
                bmp.StandbyTime = StandbyTime.Ms250;
                bmp.SetPowerMode(Bmx280PowerMode.Normal);
            }
            catch (IOException)
            {
                bmp = null;
                Console.WriteLine("BMP280 not available");
            }

            DhtSensor?dht = board.GetCommandHandler <DhtSensor>();

            if (dht == null)
            {
                // Note that this is a software error, hardware support is not tested here.
                Console.WriteLine("DHT Sensor module missing");
                return;
            }

            OpenHardwareMonitor hardwareMonitor = new OpenHardwareMonitor();

            hardwareMonitor.EnableDerivedSensors();
            TimeSpan sleeptime        = TimeSpan.FromMilliseconds(500);
            string   modeName         = string.Empty;
            string   previousModeName = string.Empty;
            int      firstCharInText  = 0;

            while (true)
            {
                if (Console.KeyAvailable && Console.ReadKey(true).KeyChar == 'x')
                {
                    break;
                }

                // Default
                sleeptime = TimeSpan.FromMilliseconds(500);

                switch (mode)
                {
                case 0:
                    modeName = "Display ready";
                    disp.Output.ReplaceLine(1, "Button for mode");
                    // Just text
                    break;

                case 1:
                {
                    modeName = "Time";
                    disp.Output.ReplaceLine(1, DateTime.Now.ToLongTimeString());
                    sleeptime = TimeSpan.FromMilliseconds(200);
                    break;
                }

                case 2:
                {
                    modeName = "Date";
                    disp.Output.ReplaceLine(1, DateTime.Now.ToShortDateString());
                    break;
                }

                case 3:
                    modeName = "Temperature / Barometric Pressure";
                    if (bmp != null && bmp.TryReadTemperature(out Temperature temp) && bmp.TryReadPressure(out Pressure p2))
                    {
                        Pressure p3 = WeatherHelper.CalculateBarometricPressure(p2, temp, stationAltitude);
                        disp.Output.ReplaceLine(1, string.Format(CultureInfo.CurrentCulture, "{0:s1} {1:s1}", temp, p3));
                    }