forked from mlinnen/Netduino-Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmulatorService.cs
More file actions
201 lines (183 loc) · 6.73 KB
/
EmulatorService.cs
File metadata and controls
201 lines (183 loc) · 6.73 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SPOT.Emulator;
using Microsoft.SPOT.Emulator.Gpio;
using Microsoft.SPOT.Hardware;
using Caliburn.Micro;
using System.ComponentModel.Composition;
using Netduino.Core;
namespace Netduino.Core.Services
{
[Export(typeof(IEmulatorService))]
public class EmulatorService:Emulator2,IEmulatorService,IHandle<InputGpioEventArgs>,IHandle<OutputGpioEventArgs>
{
//private readonly Emulator _emulator;
private GpioPort _onBoardLedPort;
private GpioPort _onBoardSwitch1;
private GpioPort _gpio_d0Port;
private GpioPort _gpio_d1Port;
private GpioPort _gpio_d2Port;
private GpioPort _gpio_d3Port;
private GpioPort _gpio_d4Port;
private GpioPort _gpio_d5Port;
private GpioPort _gpio_d6Port;
private GpioPort _gpio_d7Port;
private GpioPort _gpio_d8Port;
private GpioPort _gpio_d9Port;
private GpioPort _gpio_d10Port;
private GpioPort _gpio_d11Port;
private GpioPort _gpio_d12Port;
private GpioPort _gpio_d13Port;
//private GpioPort _gpio_a0Port;
private readonly IEventAggregator _eventAggregator;
private readonly ILog _log = LogManager.GetLog(typeof(EmulatorService));
public EmulatorService(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
}
public override void Configure(System.Xml.XmlReader reader)
{
try
{
base.Configure(reader);
}
catch (Exception ex)
{
_log.Error(ex);
}
}
public override void Run()
{
base.Run();
}
/// <summary>Registers default components and settings for this emulator</summary>
protected override void LoadDefaultComponents()
{
base.LoadDefaultComponents();
}
public override void SetupComponent()
{
base.SetupComponent();
}
public override void InitializeComponent()
{
base.InitializeComponent();
_gpio_d0Port = RegisterOutput("GPIO_PIN_D0");
_gpio_d1Port = RegisterOutput("GPIO_PIN_D1");
_gpio_d2Port = RegisterOutput("GPIO_PIN_D2");
_gpio_d3Port = RegisterOutput("GPIO_PIN_D3");
_gpio_d4Port = RegisterOutput("GPIO_PIN_D4");
_gpio_d5Port = RegisterOutput("GPIO_PIN_D5");
_gpio_d6Port = RegisterOutput("GPIO_PIN_D6");
_gpio_d7Port = RegisterOutput("GPIO_PIN_D7");
_gpio_d8Port = RegisterOutput("GPIO_PIN_D8");
_gpio_d9Port = RegisterOutput("GPIO_PIN_D9");
_gpio_d10Port = RegisterOutput("GPIO_PIN_D10");
_gpio_d11Port = RegisterOutput("GPIO_PIN_D11");
_gpio_d12Port = RegisterOutput("GPIO_PIN_D12");
_gpio_d13Port = RegisterOutput("GPIO_PIN_D13");
_onBoardLedPort = RegisterOutput("ONBOARD_LED");
_onBoardSwitch1 = this.FindComponentById("ONBOARD_SW1") as GpioPort;
_eventAggregator.Subscribe(this);
}
private GpioPort RegisterOutput(string id)
{
GpioPort port = this.FindComponentById(id) as GpioPort;
if (port != null)
{
if (port.ModesAllowed == GpioPortMode.InputOutputPort || port.ModesAllowed == GpioPortMode.OutputPort)
{
_log.Info("Id={0} is an output pin so register it for events", id);
port.OnGpioActivity += new GpioActivity(Port_OnGpioActivity);
}
else
{
_log.Warn("Id={0} must not be an output pin", id);
}
}
else
{
_log.Warn("Output was not registered Id={0}", id);
}
return port;
}
public override void UninitializeComponent()
{
base.UninitializeComponent();
}
void Port_OnGpioActivity(GpioPort sender, bool edge)
{
_log.Info("Emulator Service GpioPort Fired {0} {1}",sender.Pin,edge);
_eventAggregator.Publish<OutputGpioEventArgs>(new OutputGpioEventArgs((int)sender.Pin, edge));
}
public void Handle(InputGpioEventArgs message)
{
if (message != null)
{
_log.Info("Emulator Service: Handle Input {0} {1}",message.Pin,message.Edge);
if (message.Pin == Pins.ONBOARD_SW1 && _onBoardSwitch1 != null)
{
_onBoardSwitch1.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D1 && _gpio_d1Port != null)
{
_gpio_d1Port.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D2 && _gpio_d2Port != null)
{
_gpio_d2Port.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D3 && _gpio_d3Port != null)
{
_gpio_d3Port.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D4 && _gpio_d4Port != null)
{
_gpio_d4Port.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D5 && _gpio_d5Port != null)
{
_gpio_d5Port.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D6 && _gpio_d6Port != null)
{
_gpio_d6Port.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D7 && _gpio_d7Port != null)
{
_gpio_d7Port.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D8 && _gpio_d8Port != null)
{
_gpio_d8Port.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D9 && _gpio_d9Port != null)
{
_gpio_d9Port.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D10 && _gpio_d10Port != null)
{
_gpio_d10Port.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D11 && _gpio_d11Port != null)
{
_gpio_d11Port.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D12 && _gpio_d12Port != null)
{
_gpio_d12Port.Write(message.Edge);
}
if (message.Pin == Pins.GPIO_PIN_D13 && _gpio_d13Port != null)
{
_gpio_d13Port.Write(message.Edge);
}
}
}
public void Handle(OutputGpioEventArgs message)
{
_log.Info("Emulator Service Handle Output ");
}
}
}