forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
51 lines (42 loc) · 1.23 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Device.Gpio;
using System.Threading;
int lightTime = 1000;
int dimTime = 200;
int[] pins = new int[] {18, 24, 25};
using GpioController controller = new();
CancellationTokenSource cts = new();
CancellationToken ct = cts.Token;
// configure pins
foreach (int pin in pins)
{
controller.OpenPin(pin, PinMode.Output);
controller.Write(pin, 0);
Console.WriteLine($"GPIO pin enabled for use: {pin}");
}
// enable program to be safely terminated via CTRL-c
Console.CancelKeyPress += (s, e) =>
{
cts.Cancel();
controller.Dispose();
};
// turn LEDs on and off
int index = 0;
while (!ct.IsCancellationRequested)
{
int pin = pins[index];
Console.WriteLine($"Light pin {pin} for {lightTime}ms");
controller.Write(pin, PinValue.High);
Thread.Sleep(lightTime);
if (ct.IsCancellationRequested) break;
Console.WriteLine($"Dim pin {pin} for {dimTime}ms");
controller.Write(pin, PinValue.Low);
Thread.Sleep(dimTime);
index++;
if (index >= pins.Length)
{
index = 0;
}
}