forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
69 lines (53 loc) · 2.06 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System;
using System.Diagnostics;
using System.Threading;
using System.Device.Gpio;
namespace TimerSample
{
public class Program
{
static GpioPin _led;
public static void Main()
{
// mind to set a pin that exists on the board being tested
// PJ5 is LD2 in STM32F769I_DISCO
_led = new GpioController().OpenPin(PinNumber('J', 5), PinMode.Output);
// PG14 is LEDLD4 in F429I_DISCO
//_led = new GpioController().OpenPin(PinNumber('G', 14), PinMode.Output);
// If you are using an ESP32, adjust the number for the proper GPIO
//_led = new GpioController().OpenPin(4, PinMode.Output);
// create timer
Debug.WriteLine(DateTime.UtcNow.ToString() + ": creating timer, due in 1 second");
Timer testTimer = new Timer(CheckStatusTimerCallback, null, 1000, 1000);
// let it run for 5 seconds (will blink 5 times)
Thread.Sleep(5000);
Debug.WriteLine(DateTime.UtcNow.ToString() + ": changing period to 2 seconds");
// change timer period
testTimer.Change(0, 2000);
// let it run for 10 seconds (will blink 5 times)
Thread.Sleep(10000);
Debug.WriteLine(DateTime.UtcNow.ToString() + ": destroying timer");
// dispose timer
testTimer.Dispose();
// loop forever
Thread.Sleep(Timeout.Infinite);
}
private static void CheckStatusTimerCallback(object state)
{
Debug.WriteLine(DateTime.UtcNow.ToString() + ": blink");
_led.Write(PinValue.High);
Thread.Sleep(125);
_led.Write(PinValue.Low);
}
static int PinNumber(char port, byte pin)
{
if (port < 'A' || port > 'J')
throw new ArgumentException();
return ((port - 'A') * 16) + pin;
}
}
}