Skip to content

Instantly share code, notes, and snippets.

@sanme98
Created September 29, 2021 14:53
Show Gist options
  • Save sanme98/d5b698529dbdb91d3a95c687f85f8eec to your computer and use it in GitHub Desktop.
Save sanme98/d5b698529dbdb91d3a95c687f85f8eec to your computer and use it in GitHub Desktop.
Sample code for Maker pHAT in .NET C#
using System;
using System.Device.Gpio;
using System.Threading;
using System.Diagnostics;
//LED
int led1 = 17;
int led2 = 18;
int led3 = 27;
int led4 = 22;
int led5 = 25;
int led6 = 12;
int led7 = 13;
int led8 = 19;
//Button
int sw1 = 21;
int sw2 = 16;
int sw3 = 20;
int buzzer = 26;
int none = 0;
int decrease = 1;
int increase = 2;
int all_off = 9;
int all_on = 10;
using GpioController controller = new();
controller.OpenPin(led1, PinMode.Output);
controller.OpenPin(led2, PinMode.Output);
controller.OpenPin(led3, PinMode.Output);
controller.OpenPin(led4, PinMode.Output);
controller.OpenPin(led5, PinMode.Output);
controller.OpenPin(led6, PinMode.Output);
controller.OpenPin(led7, PinMode.Output);
controller.OpenPin(led8, PinMode.Output);
controller.OpenPin(sw1, PinMode.InputPullDown);
controller.OpenPin(sw2, PinMode.InputPullDown);
controller.OpenPin(sw3, PinMode.InputPullDown);
controller.OpenPin(buzzer, PinMode.Output);
Console.CancelKeyPress += new ConsoleCancelEventHandler(keyboardInterruptHandler);
void keyboardInterruptHandler(object sender, ConsoleCancelEventArgs args)
{
LED(all_off);
GpioOff(buzzer);
}
void GpioOn(int gpio)
{
controller.Write(gpio, PinValue.High);
}
void GpioOff(int gpio)
{
controller.Write(gpio, PinValue.Low);
}
void Beep(int times, int milliSecond)
{
for (int i = 0; i < times; i++)
{
GpioOn(buzzer);
Thread.Sleep(milliSecond);
GpioOff(buzzer);
Thread.Sleep(milliSecond);
}
}
void LED(int ledNumber)
{
if (ledNumber == 1)
{
GpioOn(led1);
}
else
{
GpioOff(led1);
}
if (ledNumber == 2)
{
GpioOn(led2);
}
else
{
GpioOff(led2);
}
if (ledNumber == 3)
{
GpioOn(led3);
}
else
{
GpioOff(led3);
}
if (ledNumber == 4)
{
GpioOn(led4);
}
else
{
GpioOff(led4);
}
if (ledNumber == 5)
{
GpioOn(led5);
}
else
{
GpioOff(led5);
}
if (ledNumber == 6)
{
GpioOn(led6);
}
else
{
GpioOff(led6);
}
if (ledNumber == 7)
{
GpioOn(led7);
}
else
{
GpioOff(led7);
}
if (ledNumber == 8)
{
GpioOn(led8);
}
else
{
GpioOff(led8);
}
if (ledNumber == all_off)
{
GpioOff(led1);
GpioOff(led2);
GpioOff(led3);
GpioOff(led4);
GpioOff(led5);
GpioOff(led6);
GpioOff(led7);
GpioOff(led8);
}
else if (ledNumber == all_on)
{
GpioOn(led1);
GpioOn(led2);
GpioOn(led3);
GpioOn(led4);
GpioOn(led5);
GpioOn(led6);
GpioOn(led7);
GpioOn(led8);
}
}
int mode = none;
int ledPosition = 0;
LED(all_on);
Beep(1, 100);
LED(all_off);
while (true)
{
if (controller.Read(sw1) == PinValue.Low && mode != decrease)
{
Beep(2, 70);
mode = decrease;
}
else if (controller.Read(sw2) == PinValue.Low && controller.Read(sw3) == PinValue.Low)
{
Thread.Sleep(500);
for (int i = 0; i < 3; i++)
{
LED(all_on);
GpioOn(buzzer);
Thread.Sleep(200);
LED(all_off);
GpioOff(buzzer);
Thread.Sleep(200);
}
Thread.Sleep(1000);
Process.Start("/bin/bash", "-c \"sudo shutdown -h now\"");
}
else if (controller.Read(sw2) == PinValue.Low && mode != increase)
{
Beep(2, 70);
mode = increase;
}
else if (controller.Read(sw3) == PinValue.Low && mode != none)
{
Beep(1, 70);
mode = none;
}
if (mode == increase)
{
if (ledPosition < 8)
{
ledPosition = ledPosition + 1;
}
else
{
ledPosition = 0;
}
}
else if (mode == decrease)
{
if (ledPosition > 0)
{
ledPosition = ledPosition - 1;
}
else
{
ledPosition = 8;
}
}
LED(ledPosition);
Thread.Sleep(50);
}
@sanme98
Copy link
Author

sanme98 commented Oct 1, 2021

Please visit Maker pHAT Sample Code in .NET C# for some explainations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment