forked from mlinnen/Netduino-Emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmulator2.cs
More file actions
142 lines (130 loc) · 5.65 KB
/
Emulator2.cs
File metadata and controls
142 lines (130 loc) · 5.65 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SPOT.Emulator;
using System.Threading;
using Microsoft.SPOT.Emulator.Gpio;
using Microsoft.SPOT.Hardware;
namespace Netduino.Core.Services
{
/// <summary>Extension of standard Emulator to provide events for key state changes</summary>
/// <remarks>The standard <see cref="emulator" /> and derived classes go through
/// several stages of initialization. However, external classes, like a view model
/// don't get to participate in the "notification" of state changes. Thus it is difficult
/// for a view model to know when it is safe to attach to the emulators LCD changed event.
/// <para>This class is designed to add events to the standard emulator for the Initialize
/// and Uninitialize states and to simplify registration of GPIO pins and LCD display components</para>
/// </remarks>
public class Emulator2 : Emulator
{
/// <summary>Creates a new instance of an <see cref="Emulator2"/></summary>
public Emulator2( )
{
}
/// <summary>Creates a new instance of an <see cref="Emulator2"/></summary>
/// <param name="Hal">Custom emulator HAL built from the .NET Micro Framework Porting kit</param>
/// <remarks>
/// This overload of the constructor allows setting the HAL/CLR implementation for the emulator.
/// You can create custom HAL/CLR configurations using the .NET Micro Framework Porting kit.
/// </remarks>
//public Emulator2(IEmulator Hal)
// : base( Hal )
//{
//}
#region Emulator Thread
/// <summary>Start the emulator engine on a new thread</summary>
/// <remarks>Uses Environment.GetCommandLineArguments()</remarks>
public void StartEmulator( )
{
new Thread( Start ).Start( );
}
/// <summary>Start the emulator engine on a new thread</summary>
/// <param name="Args">Command line arguments</param>
public void StartEmulator( string[ ] Args )
{
new Thread( Start ).Start( Args );
}
#endregion
#region Events
/// <summary>Event raised when the emulator has reached the Initialize state</summary>
/// <remarks>
/// This event is raised after the emulator is initialized but before it begins executing
/// code. All of the EmulatorComponentCollections are fully wired up at this point. This
/// event is normally used to trigger application code to attach to events fired by the
/// other emulator components, such as the LCD or a GPIO pin.
/// </remarks>
public event EventHandler Initialize = delegate { };
/// <summary>Event raised when the emulator is shutting down</summary>
/// <remarks>
/// This event indicates the emulator is shutting down and receivers should
/// detach events and handle any other cleanup work needed.
/// </remarks>
public event EventHandler Uninitialize = delegate { };
/// <summary>Triggers the <see cref="Initialize"/> event</summary>
public override void InitializeComponent( )
{
base.InitializeComponent( );
Initialize(this, null);
}
/// <summary>Triggers the <see cref="Uninitialize"/> event</summary>
public override void UninitializeComponent()
{
base.UninitializeComponent( );
Uninitialize( this, null );
}
#endregion
#region GPIO Pins
/// <summary>Registers a GPIO pin in the system</summary>
/// <param name="Pin">Pin number</param>
/// <param name="Name">Name for the pin</param>
/// <remarks>
/// <para>Useful method for registering a pin</para>
/// </para>NOTE: This should only be called from an overload of the
/// <see cref="LoadDefaultComponents()"/> Method</para>
/// </remarks>
protected GpioPort RegisterPin(string Name, Cpu.Pin Pin)
{
return RegisterPin(Name, Pin, GpioPortMode.InputOutputPort, GpioPortMode.InputOutputPort);
}
/// <summary>Registers a GPIO pin in the system</summary>
/// <param name="Pin">Pin number</param>
/// <param name="Name">Name for the pin</param>
/// <param name="Key">Virtual Key to attach to the pin</param>
/// <remarks>
/// <para>Useful method for registering a pin that acts as a button</para>
/// </para>NOTE: This should only be called from an overload of the
/// <see cref="LoadDefaultComponents()"/> Method</para>
/// </remarks>
protected GpioPort RegisterPin(string Name, Cpu.Pin Pin, VirtualKey Key)
{
GpioPort retVal = RegisterPin(Name, Pin);
retVal.VirtualKey = Key;
return retVal;
}
/// <summary>Registers a GPIO pin in the system</summary>
/// <param name="Name">Name for the pin</param>
/// <param name="Pin">Pin number</param>
/// <param name="ExpectedMode">Expected mode for the pin</param>
/// <param name="AllowedMode">Allowed modes for the pin</param>
/// <remarks>
/// <para>Useful method for registering a pin</para>
/// </para>NOTE: This should only be called from an overload of the
/// <see cref="LoadDefaultComponents()"/> Method</para>
/// </remarks>
protected GpioPort RegisterPin(string Name, Cpu.Pin Pin, GpioPortMode ExpectedMode, GpioPortMode AllowedMode)
{
if (this.State != EmulatorState.Configuration)
throw new InvalidOperationException(
"Cannot register GPIO Pins unless emulator is in the Configuration state");
GpioPort port = new GpioPort();
port.Pin = Pin;
port.ModesAllowed = AllowedMode;
port.ModesExpected = ExpectedMode;
port.ComponentId = Name;
RegisterComponent(port);
return port;
}
#endregion
}
}