|
|
|
|
@ -0,0 +1,118 @@ |
|
|
|
|
using System; |
|
|
|
|
using System.Threading; |
|
|
|
|
using System.Device.Gpio; |
|
|
|
|
using System.Device.Gpio.Drivers; |
|
|
|
|
using System.Net; |
|
|
|
|
using System.Net.Sockets; |
|
|
|
|
using System.Text; |
|
|
|
|
|
|
|
|
|
namespace BAT_UdpLedTestApp { |
|
|
|
|
|
|
|
|
|
internal class Program { |
|
|
|
|
public static GpioController Ctrl { get; set; } |
|
|
|
|
|
|
|
|
|
static void Main( string[] args ) { |
|
|
|
|
Console.WriteLine( "Application started!" ); |
|
|
|
|
|
|
|
|
|
// Set up GPIO |
|
|
|
|
|
|
|
|
|
int gpioBank = 0; |
|
|
|
|
Console.WriteLine( "in Joystick() - create LibGpiodDriver" ); |
|
|
|
|
LibGpiodDriver libGpiodDriver = new LibGpiodDriver( gpioBank ); |
|
|
|
|
|
|
|
|
|
Console.WriteLine( "in Joystick() - create GpioController" ); |
|
|
|
|
GpioController ctrl = new GpioController( PinNumberingScheme.Logical, libGpiodDriver ); |
|
|
|
|
Ctrl = ctrl; // set in public property so joystick and leds can access it |
|
|
|
|
|
|
|
|
|
Thread t = new Thread( Run ); |
|
|
|
|
// Background thread, ends when Main() ends |
|
|
|
|
t.IsBackground = false; |
|
|
|
|
t.Start(); |
|
|
|
|
// Program ends, but Run-Thread keeps working |
|
|
|
|
} |
|
|
|
|
protected static void Run() { |
|
|
|
|
Joystick joystick = Joystick.GetInstance(); |
|
|
|
|
Leds leds = Leds.GetInstance(); |
|
|
|
|
JoystickButtons oldState = JoystickButtons.None; |
|
|
|
|
JoystickButtons newState; |
|
|
|
|
|
|
|
|
|
IPEndPoint ipEndpoint = new IPEndPoint(IPAddress.Any, 10001); |
|
|
|
|
UdpClient udpClient = new UdpClient(ipEndpoint); |
|
|
|
|
udpClient.Client.ReceiveTimeout = 100; // set timeout to 100ms |
|
|
|
|
byte[] data = new byte[1024]; |
|
|
|
|
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0); |
|
|
|
|
|
|
|
|
|
Console.WriteLine("Waiting for clients..."); |
|
|
|
|
while( true ) { |
|
|
|
|
// reset array |
|
|
|
|
data = Array.Empty<byte>(); |
|
|
|
|
// try receive data |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
data = udpClient.Receive(ref sender); |
|
|
|
|
} |
|
|
|
|
catch (SocketException sockEx) |
|
|
|
|
{ |
|
|
|
|
// timeout = normal use case |
|
|
|
|
if (sockEx.SocketErrorCode != SocketError.TimedOut) |
|
|
|
|
{ |
|
|
|
|
throw; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// decode message, if any received => Handle it |
|
|
|
|
string receivedMessage = Encoding.ASCII.GetString(data, 0, data.Length); |
|
|
|
|
if(receivedMessage.Length > 0) |
|
|
|
|
{ |
|
|
|
|
Console.WriteLine("Message received from {0}:", sender.ToString()); |
|
|
|
|
Console.WriteLine(receivedMessage); |
|
|
|
|
|
|
|
|
|
if(receivedMessage.StartsWith("led on")) |
|
|
|
|
{ |
|
|
|
|
leds.Set( LedColor.Green, true ); |
|
|
|
|
ReplyMessage(udpClient, sender, "Led turned on"); |
|
|
|
|
} |
|
|
|
|
else if(receivedMessage.StartsWith("led off")) |
|
|
|
|
{ |
|
|
|
|
leds.Set( LedColor.Green, false ); |
|
|
|
|
ReplyMessage(udpClient, sender, "Led turned on"); |
|
|
|
|
} |
|
|
|
|
else if(receivedMessage.StartsWith("joystick state")) |
|
|
|
|
{ |
|
|
|
|
JoystickButtons state = joystick.State; |
|
|
|
|
ReplyMessage(udpClient, sender, $"Current Joystick state: {state}"); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
ReplyMessage(udpClient, sender, "Unknown command"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// check if the joystick state changed, send to last sender |
|
|
|
|
newState = joystick.State; |
|
|
|
|
if( oldState != newState ) { |
|
|
|
|
Console.WriteLine($"New Joystick state detected: {newState}"); |
|
|
|
|
ReplyMessage(udpClient, sender, $"New Joystick state: {newState}"); |
|
|
|
|
oldState = newState; |
|
|
|
|
} |
|
|
|
|
Thread.Sleep( 50 ); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static void ReplyMessage(UdpClient client, IPEndPoint sender, string replyMessage) |
|
|
|
|
{ |
|
|
|
|
// would lead to exception |
|
|
|
|
if(sender.Address == IPAddress.Any) { return; } |
|
|
|
|
|
|
|
|
|
// terminate message with LF |
|
|
|
|
string terminatedMessage = $"{replyMessage}\n"; |
|
|
|
|
|
|
|
|
|
// send data |
|
|
|
|
byte[] data = new byte[1024]; |
|
|
|
|
data = Encoding.ASCII.GetBytes(terminatedMessage); |
|
|
|
|
client.Send(data, data.Length, sender); |
|
|
|
|
Console.WriteLine($"Sent message: {replyMessage}"); |
|
|
|
|
} |
|
|
|
|
} // end class Program |
|
|
|
|
|
|
|
|
|
} // end namespace |