I ask because I do not understand why I get the following exception when creating a new PWM. I am not saying anything about right or wrong here.
Example using Expansion Port E13:
If I use (For the first time).
static PWM pwmExpansion;
pwmExpansion = new PWM(BrainPad.Expansion.PwmOutput.E13, 50, 0.05, false);
I receive the exception:
A first chance exception of type ‘System.InvalidOperationException’ occurred in Microsoft.SPOT.Hardware.dll
So in order to create the PWM I must do another:
static PWM pwmExpansion;
pwmExpansion = new PWM(BrainPad.Expansion.PwmOutput.E13, 50, 0.05, false);
This time I do not receive the exception and I have a useable PWM.
Is there something I need to do to prevent the exception on the first instance? Receiving the exception do not seem to cause any problem but it seems kind of crude coding. But it does work then.
Some sample code I used to show the issue.
There is not much code if you remove all the BrainPad.WriteDebugMessages.
namespace MFConsoleBrainPadQuestion
{
public class Program
{
static PWM pwmExpansion;
static bool Expansionstarted = false;
static bool isfirst = true;
public static void Main()
{
Debug.Print(Resources.GetString(Resources.StringResources.String1));
BrainPad.Display.Clear();
//InterruptPort is set as: Port.InterruptMode.InterruptEdgeBoth for all four buttons
//I only want to respond when the button is released
BrainPad.Button.ButtonReleased += Button_ButtonReleased;
BrainPad.WriteDebugMessage("BrainPad Is Running. Using PWM on Expansion E13");
BrainPad.WriteDebugMessage("Expansion pin E13 is high (1) at power on/reset\r\nPress the Left button to begin.");
while (BrainPad.Looping)
{
Button();
BrainPad.Wait.Milliseconds(200);
}
//
}
//
static bool Leftbuttonreleased = false;
static void Button_ButtonReleased(BrainPad.Button.DPad button, BrainPad.Button.State state)
{
Leftbuttonreleased = false;
if (button == BrainPad.Button.DPad.Left)
{
Leftbuttonreleased = true;
}
}
//
static void Button()
{
if (Leftbuttonreleased) //Left button was pressed and then released so continue on
{
Leftbuttonreleased = false;
BrainPad.WriteDebugMessage("\r\nMsg: Left button pressed and released.");
if (Expansionstarted)
{
pwmExpansion.Stop();
pwmExpansion.Dispose();
Expansionstarted = false;
BrainPad.WriteDebugMessage("Msg: if (Expansionstarted), pwmExpansion Dispose. NO PULSE. Press button again.");
return;
}
//
if (!isfirst)
{
pwmExpansion.Stop();
pwmExpansion = new PWM(BrainPad.Expansion.PwmOutput.E13, 50, 0.05, false);
pwmExpansion.Start();
BrainPad.WriteDebugMessage("Msg: if (!isfirst), new PWM() - We now have a pulse. Press button again.");
Expansionstarted = true;
return;
}
//
// I receive A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.SPOT.Hardware.dll
// The try/catch does not respond to this exception
try
{
if (isfirst)
{
// If I use two pwmExpansion = new PWM(BrainPad.Expansion.PwmOutput.E13, 50, 0.05, false);
// the pulse will start on the first button press
// 1ms pulse - 50Hz - 05.0%
pwmExpansion = new PWM(BrainPad.Expansion.PwmOutput.E13, 50, 0.05, false);
BrainPad.WriteDebugMessage("Msg: if (isfirst)");
BrainPad.WriteDebugMessage("Msg: pwmExpansion = new PWM(BrainPad.Expansion.PwmOutput.E13, 50, 0.05, false);");
BrainPad.WriteDebugMessage("Msg: new PWM() 1 - Exception when using new PWM()");
// pwmExpansion.Start(); //If used E13 pin would go low but have no pulse
pwmExpansion = new PWM(BrainPad.Expansion.PwmOutput.E13, 50, 0.05, false);
pwmExpansion.Start(); // E13 pin now has a pulse
Expansionstarted = true;
isfirst = false;
BrainPad.WriteDebugMessage("Msg: pwmExpansion = new PWM(BrainPad.Expansion.PwmOutput.E13, 50, 0.05, false);");
BrainPad.WriteDebugMessage("Msg:- new PWM() 2 - No Exception when using new PWM() - We now have a pulse.");
return;
}
//
}
catch (Exception e) // Never comes here
{
BrainPad.WriteDebugMessage("Msg: catch (Exception e)");
BrainPad.WriteDebugMessage(e.Message);
}
//end try/catch
} //end if Leftbuttonreleased
} //end Button()
} //end program
} //end namespace
//
Example output:
The thread ‘’ (0x2) has exited with code 0 (0x0).
Hello World!
BrainPad Is Running. Using PWM on Expansion E13
Expansion pin E13 is high (1) at power on/reset
Press the Left button to begin.
Msg: Left button pressed and released.
A first chance exception of type ‘System.InvalidOperationException’ occurred in Microsoft.SPOT.Hardware.dll
Msg: if (isfirst)
Msg: pwmExpansion = new PWM(BrainPad.Expansion.PwmOutput.E13, 50, 0.05, false);
Msg: new PWM() 1 - Exception when using new PWM()
Msg: pwmExpansion = new PWM(BrainPad.Expansion.PwmOutput.E13, 50, 0.05, false);
Msg:- new PWM() 2 - No Exception when using new PWM() - We now have a pulse.
Msg: Left button pressed and released.
Msg: if (Expansionstarted), pwmExpansion Dispose. NO PULSE. Press button again.
Msg: Left button pressed and released.
Msg: if (!isfirst), new PWM() - We now have a pulse. Press button again.
Msg: Left button pressed and released.
Msg: if (Expansionstarted), pwmExpansion Dispose. NO PULSE. Press button again.
Msg: Left button pressed and released.
Msg: if (!isfirst), new PWM() - We now have a pulse. Press button again.
Just a question I had to ask.
Thanks…