Following the blog post to get you up and running with Android Things, Visual Studio and Xamarin. http://explorationspace.co.za/2017/02/26/using-xamarin-and-visual-studio-with-android-things/
Android Things has GPIO callbacks which are triggered on certain trigger events. This is great for event handling. I’ve added a sample to Xamarin bindings which illustrates this by means of a Grove PIR (Passive InfraRed) sensor much like what’s in your home alarm system. If we use that in conjunction with a buzzer, you have a primitive alarm system.
The source can be found here: https://github.com/apead/Xamarin-Android-Things
Gpio callbacks are classes implemented inheriting from the abstract class Gpio Callback. This call back is triggered on certain trigger types. This are set on the gpio pin with the settriggertype method.
EDGE_NONE
: No interrupt events. This is the default value.EDGE_RISING
: Interrupt on a transition from low to highEDGE_FALLING
: Interrupt on a transition from high to lowEDGE_BOTH
: Interrupt on all state transitions
public class AlarmCallback : Com.Google.Android.Things.Pio.GpioCallback { public Gpio BuzzerPin { get; set; } public override bool OnGpioEdge(Gpio p0) { if (p0.Value) { if (!BuzzerPin.Value) { BuzzerPin.Value = true; Thread.Sleep(1000); BuzzerPin.Value = false; } } return true; } public override void OnGpioError(Gpio p0, int p1) { Log.Info("Alarmcallback", "Error"); } }
The class has two methods to override. OnGpioEdge and OnGpioError. The OnGpioEdge method is called on a trigger event. The OnGpioError is called on an error.
This class is registered on the actual input gpio pin.
_callback = new AlarmCallback {BuzzerPin = _buzzerGpio}; _alarm1Gpio.RegisterGpioCallback(_callback);
Happy Android Things development with Xamarin!