示例#1
0
        static async Task Main(string[] args)
        {
            // Using same example as what's described at:
            // http://hertaville.com/introduction-to-accessing-the-raspberry-pis-gpio-in-c.html

            var taskSchedulerUtility = new TaskSchedulerUtility();
            var fileUtils            = new AsyncFileUtil();
            var consoleLogger        = new ConsoleLogger
            {
                IsLoggingErrors = true,
                IsLoggingInfo   = true
            };

            using (var controller = new LinuxPinController(fileUtils, consoleLogger, taskSchedulerUtility))
            {
                var buttonInputPin = controller.GetOrCreateInputPin(17);
                var button         = new Button(buttonInputPin);
                button.StateChanged += Button_StateChanged;

                try
                {
                    var ledPin = controller.GetOrCreateOutputPin(4);
                    controller.StartContinuouslyCheckingInputPins();

                    for (int i = 0; i < 1000; i++)
                    {
                        //LED stuff stopped worked for some reason. Not sure why. Might need to make sure file is closed first?
                        if (i % 2 == 0)
                        {
                            await ledPin.SetOutputModeAsync(OutputModeType.High);
                        }
                        else
                        {
                            await ledPin.SetOutputModeAsync(OutputModeType.Low);
                        }

                        System.Threading.Thread.Sleep(500);
                    }

                    Console.WriteLine("Done...");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Caught Exception: {ex.Message}");
                    Console.WriteLine(Environment.NewLine);
                    Console.WriteLine(ex.ToString());
                }
            }
        }
示例#2
0
        static void Main()
        {
            //Compile with:
            //  dotnet publish ./RaspbianNetCoreDemo -c Release -r linux-arm --self-contained

            var taskSchedulerUtility = new TaskSchedulerUtility();
            var fileUtils            = new AsyncFileUtil();

            var pinController = new LinuxPinController(fileUtils, taskSchedulerUtility);
            var ledPin        = pinController.GetOrCreateOutputPin(LedPinNumber);

            BlockingTimer sleepTimer = new BlockingTimer(TimeSpan.FromMilliseconds(100));
            LedControl    ledControl = new LedControl(ledPin, sleepTimer);

            //while (true)
            //{
            //    ledControl.Blink();
            //}

            var lightSensorDevice = new I2cDevice(I2cDevicePath, LightSensorDeviceAddress);
            var lightSensor       = new APDS9301_LightSensor(lightSensorDevice, APDS9301_LightSensor.MinimumPollingPeriod);

            while (true)
            {
                float currentLuminosity = lightSensor.Luminosity;

                if (!ledControl.State && currentLuminosity <= OnMinimumLuminosity)
                {
                    ledControl.Blink();
                    ledControl.Blink();
                    ledControl.TurnOnLed();
                    System.Diagnostics.Debug.WriteLine(currentLuminosity.ToString());
                }
                else if (ledControl.State && currentLuminosity > OnMinimumLuminosity)
                {
                    ledControl.TurnOffLed();
                    System.Diagnostics.Debug.WriteLine(currentLuminosity.ToString());
                }

                Thread.Sleep(10);
            }

            //TODO: Dispose these somehow
            //      Only care if exception is thrown on startup when the I2C Bus and IO Pins are already setup I guess
            //lightSensor.Dispose();
            //pinController.Dispose();
        }