-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMeadowApp.cs
80 lines (68 loc) · 2.89 KB
/
MeadowApp.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
using Meadow;
using Meadow.Devices;
using Meadow.Hardware;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DigitalInputPort;
public class MeadowApp : App<F7FeatherV2>
{
private List<IDigitalInputPort> inputs = new List<IDigitalInputPort>();
public override Task Initialize()
{
// we'll create 3 inputs, with each of the available resistor modes
var d5 = Device.Pins.D05.CreateDigitalInputPort(resistorMode: ResistorMode.Disabled);
inputs.Add(d5);
var d6 = Device.Pins.D06.CreateDigitalInputPort(resistorMode: ResistorMode.InternalPullUp);
inputs.Add(d6);
var d7 = Device.Pins.D07.CreateDigitalInputPort(resistorMode: ResistorMode.InternalPullDown);
inputs.Add(d7);
TimeSpan debounceDuration = TimeSpan.FromMilliseconds(20);
var d4 = Device.Pins.D04.CreateDigitalInterruptPort(InterruptMode.EdgeBoth, ResistorMode.Disabled);
d4.DebounceDuration = debounceDuration;
d4.Changed += OnStateChangedHandler;
inputs.Add(d4);
// since we're looking for falling, pull it up
var d3 = Device.Pins.D03.CreateDigitalInterruptPort(InterruptMode.EdgeFalling, ResistorMode.InternalPullUp);
d3.DebounceDuration = debounceDuration;
d3.Changed += OnStateChangedHandler;
inputs.Add(d3);
// since we're looking for risinging, pull it down
var d2 = Device.Pins.D02.CreateDigitalInterruptPort(InterruptMode.EdgeRising, ResistorMode.InternalPullDown);
d2.DebounceDuration = debounceDuration;
d2.Changed += OnStateChangedHandler;
inputs.Add(d2);
return Task.CompletedTask;
}
public override async Task Run()
{
// Display the current input states
// The general idea here is that you have 1 floating, 1 pulled high, and 1 pulled low.
// With nothing connected, you should have inputs of:
// - D05: undetermined
// - D06: high
// - D07: low
// You can then drive the outputs with a jumper to either GND or VCC to change their states to high or low
while (true)
{
var line1 = string.Join(" ", inputs.Select(i => i.Pin.Name).ToArray());
var line2 = string.Join(" ", inputs.Select(i => $" {(i.State ? 1 : 0)} ").ToArray());
Resolver.Log.Info(line1);
Resolver.Log.Info(line2 + "\n");
await Task.Delay(2000);
}
}
private void OnStateChangedHandler(object sender, DigitalPortResult e)
{
var port = sender as IDigitalInputPort;
if (port == null)
{
Resolver.Log.Info($"sender is a {port.GetType().Name}");
}
else
{
Resolver.Log.Info($"{port.Pin.Name} state changed to {e.New.State}");
}
}
}