Ich möchte gerne mithilfe von Mono (C#) GPIOs einlesen.
Dank einer vorangegangenen Forum Antwort bin ich auf ein Beispielprojekt gestoßen.
Allerdings fällt es mir schwer die relevanten Zeilen für das einlesen zu Identifizieren, da es sich um ein umfangreiches Projekt handelt.
Vielleicht fällt es einem geschulten Auge von euch leichter?
C#
Skip to content
Sign up Sign in
This repository
Search
Explore
Features
Enterprise
Pricing
Watch 47 Star 134 Fork 53 raspberry-sharp/raspberry-sharp-io
Code Issues 7 Pull requests 0 Wiki Pulse Graphs
Branch: master Find file Copy pathraspberry-sharp-io/Tests/Test.Gpio.Chaser/Program.cs
739666a on 1 Apr
@ebezine ebezine Use Raspberry.System 2.0
1 contributor
RawBlameHistory 104 lines (89 sloc) 4.21 KB
using System;
using System.Threading;
using Raspberry.IO.GeneralPurpose;
using Raspberry.IO.GeneralPurpose.Behaviors;
namespace Test.Gpio.Chaser
{
/// <summary>
/// This is a sample program. Must be modified to match your GPIO project.
/// </summary>
class Program
{
static void Main(string[] args)
{
const ConnectorPin led1Pin = ConnectorPin.P1Pin26;
const ConnectorPin led2Pin = ConnectorPin.P1Pin24;
const ConnectorPin led3Pin = ConnectorPin.P1Pin22;
const ConnectorPin led4Pin = ConnectorPin.P1Pin15;
const ConnectorPin led5Pin = ConnectorPin.P1Pin13;
const ConnectorPin led6Pin = ConnectorPin.P1Pin11;
const ConnectorPin buttonPin = ConnectorPin.P1Pin03;
Console.WriteLine("Chaser Sample: Sample a LED chaser with a switch to change behavior");
Console.WriteLine();
Console.WriteLine("\tLed 1: {0}", led1Pin);
Console.WriteLine("\tLed 2: {0}", led2Pin);
Console.WriteLine("\tLed 3: {0}", led3Pin);
Console.WriteLine("\tLed 4: {0}", led4Pin);
Console.WriteLine("\tLed 5: {0}", led5Pin);
Console.WriteLine("\tLed 6: {0}", led6Pin);
Console.WriteLine("\tSwitch: {0}", buttonPin);
Console.WriteLine();
var driver = args.GetDriver();
// Declare outputs (leds)
var leds = new PinConfiguration[]
{
led1Pin.Output().Name("Led1").Enable(),
led2Pin.Output().Name("Led2"),
led3Pin.Output().Name("Led3").Enable(),
led4Pin.Output().Name("Led4"),
led5Pin.Output().Name("Led5").Enable(),
led6Pin.Output().Name("Led6")
};
// Assign a behavior to the leds
var behavior = new ChaserBehavior(leds)
{
Loop = args.GetLoop(),
RoundTrip = args.GetRoundTrip(),
Width = args.GetWidth(),
Interval = TimeSpan.FromMilliseconds(args.GetSpeed())
};
// Alternate behaviors...
/*
var random = new Random();
var behavior = new PatternBehavior(leds, Enumerable.Range(0, 5).Select(i => random.Next(511)))
{
Loop = Helpers.GetLoop(args),
RoundTrip = Helpers.GetRoundTrip(args),
Interval = Helpers.GetSpeed(args)
};*/
/*
var behavior = new BlinkBehavior(leds)
{
Count = args.GetWidth(),
Interval = args.GetSpeed()
};*/
// Declare input (switchButton) interacting with the leds behavior
var switchButton = buttonPin.Input()
.Name("Switch")
.Revert()
.Switch()
.Enable()
.OnStatusChanged(b =>
{
behavior.RoundTrip = !behavior.RoundTrip;
Console.WriteLine("Button switched {0}", b ? "on" : "off");
});
// Create connection
var settings = new GpioConnectionSettings {Driver = driver};
using (var connection = new GpioConnection(settings, leds))
{
Console.WriteLine("Using {0}, frequency {1:0.##}hz", settings.Driver.GetType().Name, 1000.0/args.GetSpeed());
Thread.Sleep(1000);
connection.Add(switchButton);
connection.Start(behavior); // Starting the behavior automatically registers the pins to the connection, if needed.
Console.ReadKey(true);
connection.Stop(behavior);
}
}
}
}
Status API Training Shop Blog About Pricing
© 2015 GitHub, Inc. Terms Privacy Security Contact Help
Alles anzeigen
C#
#region References
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Raspberry.IO.GeneralPurpose;
#endregion
namespace Test.Gpio.Chaser
{
internal static class CommandLineExtensionMethods
{
#region Methods
public static bool GetLoop(this IEnumerable<string> args)
{
return args.SkipWhile(a => a != "-loop").Any();
}
public static bool GetRoundTrip(this IEnumerable<string> args)
{
return args.SkipWhile(a => a != "-roundTrip").Any();
}
public static int GetWidth(this IEnumerable<string> args)
{
return args.SkipWhile(a => a != "-width").Skip(1).Select(int.Parse).DefaultIfEmpty(1).First();
}
public static int GetSpeed(this IEnumerable<string> args)
{
return args.SkipWhile(a => a != "-speed").Skip(1).Select(int.Parse).DefaultIfEmpty(250).First();
}
public static IGpioConnectionDriver GetDriver(this IEnumerable<string> args)
{
var driverName = args.SkipWhile(a => a != "-driver").Skip(1).DefaultIfEmpty("").First();
return GetDriver(driverName);
}
#endregion
#region Private Helpers
private static IGpioConnectionDriver GetDriver(string driverName)
{
switch (driverName)
{
case "default":
return new GpioConnectionDriver();
case "memory":
return new MemoryGpioConnectionDriver();
case "file":
return new FileGpioConnectionDriver();
case "":
return null;
default:
throw new ArgumentOutOfRangeException("driverName", driverName,
string.Format(CultureInfo.InvariantCulture, "{0} is not a valid driver name", driverName));
}
}
#endregion
}
}
Alles anzeigen
Sobald ich an Pin 3 3,3V anlege, ändert sich die Ausgabe in der Konsole von Button switched on zu Botton switched off.
Sobald ich die Verbindung wieder trenne und sie neu anschließe wechselt die Anzeige von Botton swichted off zu Botton switched on.
Hoffe jemand kann mir helfen