Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Assgining the same IO pin to multiple instances?


  • Please log in to reply
3 replies to this topic

#1 Uberhund

Uberhund

    New Member

  • Members
  • Pip
  • 2 posts

Posted 04 June 2012 - 01:56 PM

I wish to create a multiple instances of a class to control the Netduino onboard LED.

With only one instance of this class, my code executes perfectly. However, with more than one instance, execution stops with a "System Exception" error at the point where the Netduino hardware resource is allocated for the second time.

Consider the working example below of a single instance being created for the class, which I'll call "Morse"
Morse morse16 = new Morse(16);

The constructor for the class itself is defined as illustrated below:
public Morse(int iWPM)
        {
            iDotLength = 1200 / iWPM;
            iDashLength = 3 * iDotLength;
            ledOnBoard = new OutputPort(Pins.ONBOARD_LED, false); 
        }

Now, consider the example below of two instances being created for the class "Morse," which compiles fine, but which terminates with an execution error at the line where ledOnBoard is being defined for the second instance:
Morse morse16 = new Morse(16);
Morse morse24 = new Morse(24);
Execution stops here:
ledOnBoard = new OutputPort(Pins.ONBOARD_LED, false); //System exception occurs here with more than one instance

Is there a rule about assigning Netduino hardware resources to more than one instance that I have overlooked?

#2 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 04 June 2012 - 02:00 PM

Hi Uberhund and welcome to the forums. The .NET Micro Framework makes it impossible to assign a pin twice, to prevent some issues. Also, the garbage collector doesn't free up the pins. Therefor you can use the Dispose() method on the OutputPort class. You could also make the ledOnBoard static, or a global member of your class. I have the feeling that's what you want to do.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#3 Uberhund

Uberhund

    New Member

  • Members
  • Pip
  • 2 posts

Posted 04 June 2012 - 03:10 PM

Hi Uberhund and welcome to the forums.

The .NET Micro Framework makes it impossible to assign a pin twice, to prevent some issues. Also, the garbage collector doesn't free up the pins.
Therefor you can use the Dispose() method on the OutputPort class.

You could also make the ledOnBoard static, or a global member of your class. I have the feeling that's what you want to do.


Thanks for the welcome and excellent response, Stefan. Many thanks.

#4 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 04 June 2012 - 04:59 PM

You could also make the ledOnBoard static, or a global member of your class.

Or, you could do something like this:

public class Morse
{
  //...
  private OutputPort led;

  public Morse(int iWPM) : this(iWPM, null)
  {
  }

  public Morse(int iWPM, OutputPort led)
  {
    // Parameter checking omitted for sake of brevity
    iDotLength = ...
    ...
    this.led = led;
  }

  public void DoSomething()
  {
    if(led != null)
    {
      led.Write(true);
    }
    //...
  }
}
Then you can instantiate the LED output port and pass it to selected Morse objects

//...
led = new OutputPort(Pins.ONBOARD_LED, false);

morse16 = new Morse(16, led);
morse24 = new Morse(24, led);

morse16_noLed = new Morse(16); // LED output port is null





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.