-
Notifications
You must be signed in to change notification settings - Fork 573
/
Program.cs
230 lines (203 loc) · 8.59 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// 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.Device.Spi;
using System.Threading;
using Iot.Device.Adc;
using Iot.Device.Board;
namespace BoardSample
{
/// <summary>
/// Test program main class
/// </summary>
public class Program
{
/// <summary>
/// Example program for Windows board class (execute on desktop)
/// </summary>
/// <param name="args">Command line arguments</param>
public static void Main(string[] args)
{
Console.WriteLine("Board abstraction test. Press any key to start.");
Console.ReadKey(true);
var os = Environment.OSVersion;
if (os.Platform == PlatformID.Win32NT)
{
// TODO: Add wrapper for CPU type property (see https://stackoverflow.com/questions/6944779/determine-operating-system-and-processor-type-in-c-sharp)
// We will eventually need to run different code based on whether this is an X86/X64 CPU or an ARM
WindowsDesktop();
}
else if (os.Platform == PlatformID.Unix)
{
RaspberryPiTest();
}
}
private static void WindowsDesktop()
{
const int led0 = 0;
const int led1 = 1;
const int led2 = 2;
using Board b = Board.Create();
Console.WriteLine("Hardware detected: ");
Console.WriteLine(b.QueryComponentInformation());
using GpioController controller = b.CreateGpioController();
if (controller.PinCount > 0)
{
Console.WriteLine("Blinking keyboard test. Press ESC to quit");
controller.OpenPin(led0);
controller.OpenPin(led1);
controller.OpenPin(led2);
controller.SetPinMode(led0, PinMode.Output);
controller.SetPinMode(led1, PinMode.Output);
controller.SetPinMode(led2, PinMode.Output);
PinValue state = PinValue.Low;
ConsoleKey key = ConsoleKey.NoName;
while (key != ConsoleKey.Escape)
{
state = !state;
controller.Write(led0, state);
controller.Write(led1, state);
controller.Write(led2, state);
Thread.Sleep(500);
if (Console.KeyAvailable)
{
key = Console.ReadKey(true).Key;
}
}
while (Console.KeyAvailable)
{
Console.ReadKey(true);
}
key = ConsoleKey.NoName;
Console.WriteLine($"Input test: This polls the 'A' key, ESC to quit");
int pinNumber = (int)ConsoleKey.A;
controller.OpenPin(pinNumber);
controller.SetPinMode(pinNumber, PinMode.Input);
// Note that the Console access is independent of what we actually demonstrate here: The use of the keyboard as "input pins"
while (key != ConsoleKey.Escape)
{
if (controller.Read(pinNumber) == PinValue.High)
{
Console.WriteLine("Key is pressed");
}
else
{
Console.WriteLine("Key is not pressed");
}
Thread.Sleep(100);
if (Console.KeyAvailable)
{
key = Console.ReadKey(true).Key;
}
}
key = ConsoleKey.NoName;
Console.WriteLine($"Interrupt test: This listens on the 'A' key, ESC to quit");
controller.SetPinMode(pinNumber, PinMode.Input);
controller.RegisterCallbackForPinValueChangedEvent(pinNumber, PinEventTypes.Falling | PinEventTypes.Rising, Callback);
// Note that the Console access is independent of what we actually demonstrate here: The use of the keyboard as "input pins"
while (key != ConsoleKey.Escape)
{
// Now we can do a blocking read here
key = Console.ReadKey(true).Key;
}
controller.UnregisterCallbackForPinValueChangedEvent(pinNumber, Callback);
controller.ClosePin(pinNumber);
}
}
private static void Callback(object sender, PinValueChangedEventArgs e)
{
if (e.ChangeType == PinEventTypes.Rising)
{
Console.WriteLine("Key pressed");
}
else
{
Console.WriteLine("Key released");
}
}
private static void RaspberryPiTest()
{
using var raspi = Board.Create();
Console.WriteLine("Hardware detected: ");
Console.WriteLine(raspi.QueryComponentInformation());
PwmRaspiTest(raspi);
SpiRaspiTestWithSoftwareCs(raspi);
SpiRaspiTestWithHardwareCs(raspi);
}
private static void PwmRaspiTest(Board raspi)
{
int pinNumber = 12; // PWM0 pin
Console.WriteLine("Blinking and dimming an LED - Press any key to quit");
while (!Console.KeyAvailable)
{
GpioController ctrl = raspi.CreateGpioController();
ctrl.OpenPin(pinNumber);
ctrl.SetPinMode(pinNumber, PinMode.Output);
ctrl.Write(pinNumber, PinValue.Low);
Thread.Sleep(500);
ctrl.Write(pinNumber, PinValue.High);
Thread.Sleep(1000);
ctrl.ClosePin(pinNumber);
ctrl.Dispose();
var pwm = raspi.CreatePwmChannel(0, 0, 9000, 0.1);
pwm.Start();
for (int i = 0; i < 10; i++)
{
pwm.DutyCycle = i * 0.1;
Thread.Sleep(500);
}
pwm.Stop();
pwm.Dispose();
}
Console.ReadKey(true);
}
private static void SpiRaspiTestWithSoftwareCs(Board raspi)
{
Console.WriteLine("MCP3008 SPI Software CS Test");
// Runs a test communication against an MCP3008. The CS pin 8 is controlled explicitly by software (this binding
// fails to have support for this)
SpiConnectionSettings spiSettings = new SpiConnectionSettings(0, -1) { ChipSelectLineActiveState = PinValue.Low };
using SpiDevice dev = raspi.CreateSpiDevice(spiSettings);
using Mcp3008 mcp = new Mcp3008(dev);
using GpioController ctrl = raspi.CreateGpioController();
// Note that if we have only a single device attached to the SPI bus, we could just as well pull the CS line
// hard to low, but this is to show the concept.
ctrl.OpenPin(8, PinMode.Output);
ctrl.Write(8, PinValue.High);
while (!Console.KeyAvailable)
{
for (int i = 0; i < 8; i++)
{
ctrl.Write(8, PinValue.Low);
int value = mcp.Read(i);
ctrl.Write(8, PinValue.High);
Console.WriteLine($"Channel {i} has value {value}.");
Thread.Sleep(100);
}
Thread.Sleep(500);
}
Console.ReadKey(true);
}
private static void SpiRaspiTestWithHardwareCs(Board raspi)
{
Console.WriteLine("MCP3008 SPI Hardware CS Test");
// Runs a test communication against an MCP3008. The CS pin 8 is expected to be controlled by the driver, as
// we specify it here (SPI0_CE0 is BCM pin 8 on ALT0)
SpiConnectionSettings spiSettings = new SpiConnectionSettings(0, 0) { ChipSelectLineActiveState = PinValue.Low };
using SpiDevice dev = raspi.CreateSpiDevice(spiSettings);
using Mcp3008 mcp = new Mcp3008(dev);
while (!Console.KeyAvailable)
{
for (int i = 0; i < 8; i++)
{
int value = mcp.Read(i);
Console.WriteLine($"Channel {i} has value {value}.");
Thread.Sleep(100);
}
Thread.Sleep(500);
}
Console.ReadKey(true);
}
}
}