Created
July 24, 2020 15:32
-
-
Save TrabacchinLuigi/a9907169ca5da545daacf5ac361ca60e to your computer and use it in GitHub Desktop.
Raspberry Pi 3 Pin benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Concurrent; | |
using System.Collections.Generic; | |
using System.Device.Gpio; | |
using System.Device.Gpio.Drivers; | |
using System.Linq; | |
using System.Threading; | |
using System.Threading.Tasks; | |
namespace IRLLogger | |
{ | |
class Program | |
{ | |
static readonly String resistorArgName = "resistor="; | |
static readonly String driverArgName = "driver="; | |
static readonly BlockingCollection<PinEvent> PinEvents = new BlockingCollection<PinEvent>(); | |
static void Main(String[] args) | |
{ | |
var resistor = GetArg( | |
args, | |
resistorArgName, | |
x => | |
{ | |
return String.Equals(x, "pullup", StringComparison.OrdinalIgnoreCase) | |
? PinMode.InputPullUp | |
: String.Equals(x, "pulldown", StringComparison.OrdinalIgnoreCase) | |
? PinMode.InputPullDown | |
: PinMode.Input; | |
}); | |
var driver = GetArg( | |
args, | |
driverArgName, | |
x => | |
{ | |
return String.Equals(x, "LibGpiod", StringComparison.OrdinalIgnoreCase) | |
? LibGpiodDriver.Create() | |
: String.Equals(x, "RaspberryPi", StringComparison.OrdinalIgnoreCase) | |
? new RaspberryPi3Driver() | |
: new SysFsDriver() as GpioDriver; | |
}); | |
Console.WriteLine($"Welcome to infra red light logger! resistor mode: {resistor}, driver {driver.GetType().Name}"); | |
using (var controller = new GpioController(PinNumberingScheme.Logical, driver)) | |
{ | |
Console.WriteLine($"controller started with {controller.GetType()}"); | |
var inputPins = new[] { 21 }; | |
foreach (var pin in inputPins) | |
{ | |
Console.WriteLine($"Setupping pin {pin}"); | |
controller.OpenPin(pin, resistor); | |
controller.RegisterCallbackForPinValueChangedEvent(pin, PinEventTypes.Rising, HandlePinChanged); | |
controller.RegisterCallbackForPinValueChangedEvent(pin, PinEventTypes.Falling, HandlePinChanged); | |
} | |
var thread = new Thread(() => | |
{ | |
foreach (var e in PinEvents.GetConsumingEnumerable()) | |
{ | |
WriteForExcell(e.Time, e.EventType); | |
} | |
}); | |
thread.Priority = ThreadPriority.Lowest; | |
thread.Start(); | |
while (true) | |
{ | |
var keyInfo = Console.ReadKey(); | |
if (keyInfo.Key == ConsoleKey.Delete) | |
{ | |
Console.Clear(); | |
} | |
else if (keyInfo.Key == ConsoleKey.Escape) | |
{ | |
break; | |
} | |
} | |
Console.WriteLine("Closing..."); | |
PinEvents.CompleteAdding(); | |
if (!thread.Join(1000)) Console.WriteLine("t did not stop in suitable time"); | |
Console.WriteLine("Bye."); | |
} | |
} | |
private static T GetArg<T>(String[] args, String argStart, Func<String, T> parseFn) | |
{ | |
var arg = args.SingleOrDefault(x => x.StartsWith(argStart, StringComparison.OrdinalIgnoreCase)); | |
var argValue = arg?.Substring(argStart.Length); | |
var result = parseFn(argValue); | |
return result; | |
} | |
private static void HandlePinChanged(Object sender, PinValueChangedEventArgs pinValueChangedEventArgs) | |
{ | |
var time = DateTime.Now; | |
var pinevent = new PinEvent | |
{ | |
EventType = pinValueChangedEventArgs.ChangeType, | |
Time = time | |
}; | |
if (PinEvents.IsAddingCompleted) return; | |
PinEvents.Add(pinevent); | |
} | |
private static void WriteForExcell(DateTime time, PinEventTypes evtype) | |
{ | |
WriteVal(time, evtype == PinEventTypes.Falling, evtype); | |
WriteVal(time, evtype == PinEventTypes.Rising, evtype); | |
} | |
private static void WriteVal(DateTime time, Boolean val, PinEventTypes evtype) | |
{ | |
Console.Write(time.ToString("HHmmss,fffffff")); | |
Console.Write('\t'); | |
Console.Write(val ? '1' : '0'); | |
Console.Write('\t'); | |
Console.Write(evtype.ToString()); | |
Console.WriteLine(); | |
} | |
internal class PinEvent | |
{ | |
public PinEventTypes EventType { get; set; } | |
public DateTime Time { get; set; } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment