using System;
using System.Linq;
using Raspberry.IO.GeneralPurpose;
using System.Threading;
using System.Threading.Tasks;
namespace Test.Gpio.WatchPin
{
class Program
{
static void Main()
{
Console.WriteLine("WatchPin Sample: log the state changes on input pin. \nTry to run it in parallel with i.e. Test.Gpio.HCSR501 or to test HCSR04 (IR motion detector) or SW-18020P (shake detector)");
Console.WriteLine();
Task monPin18 = Task.Factory.StartNew(() => Watcher("P1Pin18"));
Task monPin16 = Task.Factory.StartNew(() => Watcher("P1Pin16"));
Task monPin15 = Task.Factory.StartNew(() => Watcher("P1Pin15"));
Task monPin22 = Task.Factory.StartNew(() => Watcher("P1Pin22"));
Task.WaitAll(monPin15,monPin16,monPin18,monPin22);
Console.ReadKey();
//TODO: Allow watch multiple pins simultaneously
//TODO: Allow to specify pin in different ways, ie, by name, by wiringPi, etc.
}
private static void Watcher(string pinName)
{
ConnectorPin userPin;
if (!Enum.TryParse(pinName, true, out userPin))
{
Console.WriteLine("Could not find pin: " + pinName);
PrintUsage();
return;
}
Console.WriteLine("\tWatching Pin: {0}", userPin);
Console.WriteLine("Press CTRL-C to stop");
Console.WriteLine();
var procPin = userPin.ToProcessor();
var driver = GpioConnectionSettings.DefaultDriver;
try
{
driver.Allocate(procPin, PinDirection.Input);
var isHigh = driver.Read(procPin);
while (true)
{
var now = DateTime.Now;
Console.WriteLine(now + "." + now.Millisecond.ToString("000") + ": " +pinName + (isHigh ? ": HIGH" : ": LOW"));
driver.Wait(procPin, !isHigh, TimeSpan.FromDays(7)); //TODO: infinite
isHigh = !isHigh;
Thread.Sleep(500);
}
Task.Delay(500);
}
finally
{
// Leaves the pin unreleased so that other processes can keep reading
//driver.Release(procPin);
}
}
private static void PrintUsage()
{
Console.WriteLine("Usage: Test.Gpio.WatchPin [pin]");
Console.WriteLine("Available pins:");
Enum.GetNames(typeof(ConnectorPin)).ToList().ForEach(Console.WriteLine);
Console.WriteLine("//todo allow watch multiple pins simultanously");
Console.WriteLine("//todo allow to specify pin in diffrent ways, ie, by name, by wiringPi, etc");
Console.WriteLine("I.e.: sudo mono Test.Gpio.WatchPin.exe P1Pin23");
Console.WriteLine();
}
}
using System; using System.Linq; using Raspberry.IO.GeneralPurpose; using System.Threading; using System.Threading.Tasks;
namespace Test.Gpio.WatchPin { class Program { static void Main() { Console.WriteLine("WatchPin Sample: log the state changes on input pin. \nTry to run it in parallel with i.e. Test.Gpio.HCSR501 or to test HCSR04 (IR motion detector) or SW-18020P (shake detector)"); Console.WriteLine(); Task monPin18 = Task.Factory.StartNew(() => Watcher("P1Pin18")); Task monPin16 = Task.Factory.StartNew(() => Watcher("P1Pin16")); Task monPin15 = Task.Factory.StartNew(() => Watcher("P1Pin15")); Task monPin22 = Task.Factory.StartNew(() => Watcher("P1Pin22")); Task.WaitAll(monPin15,monPin16,monPin18,monPin22); Console.ReadKey(); //TODO: Allow watch multiple pins simultaneously //TODO: Allow to specify pin in different ways, ie, by name, by wiringPi, etc.
}