How do you use the Onboard Button - Netduino Go - Netduino Forums
   
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

How do you use the Onboard Button


  • Please log in to reply
9 replies to this topic

#1 Jason Stevenson

Jason Stevenson

    New Member

  • Members
  • Pip
  • 2 posts
  • LocationCoal Valley, IL

Posted 29 August 2012 - 09:37 PM

Hello all, I'm new to this whole Arduino/Netduino thing and have been taking my son (age 10) through all of the components and writing some fun scripts today.

We have the Button, Potentiometer, RGBLed, Shield and the Go itself. We wanted to see if we could use the on board button instead of a socket attached button module in our project.

I read about the Button class, and it "find the first available button" when the constructor was empty, but I get a "System.Exception" within the button.cs class.

Here is our code:

    public class Program
    {
        public static void Main()
        {
            byte redValue = 0;
            byte greenValue = 0;
            byte blueValue = 0;

            var led = new RgbLed(GoSockets.Socket3);

            var button = new NetduinoGo.Button();
            button.ButtonPressed += new NetduinoGo.Button.ButtonEventHandler(button_ButtonPressed);

            var controlledColor = 0;

            var pot = new Potentiometer(GoSockets.Socket5);

            while (true)
            {
                switch (controlledColor)
                {
                    case 0:
                        redValue = (byte)(255 * pot.GetValue());
                        break;
                    case 1:
                        greenValue = (byte)(255 * pot.GetValue());
                        break;
                    case 2:
                        blueValue = (byte)(255 * pot.GetValue());
                        break;
                }

                led.SetColor(redValue, greenValue, blueValue);
            }

        }

        static void button_ButtonPressed(object sender, bool isPressed)
        {
            // Change The LED Flag that tells the program which color to be controlling
        }

I thought it could be done perhaps using the InputPort object, but wasn't sure after that.

Any help would be appreciated, I apologize if this is answered in any of the faq's or other posts.

#2 Matt Isenhower

Matt Isenhower

    Advanced Member

  • Members
  • PipPipPip
  • 74 posts
  • LocationSan Diego, CA

Posted 30 August 2012 - 06:25 AM

Hi Jason,

Welcome to the Netduino community!

The NetduinoGo.Button class can only be used with the Button module. This class inherits from the GoModule class, which is the base class for all GoBus module drivers. "First available button" here actually refers to the first uninitialized button module connected to the GoBus sockets and doesn't include the onboard button.

The onboard button is treated as a standard GPIO pin, similar to the I/O pins on the Netduino and Netduino Plus. Netduino Go only makes two standard GPIO pins available, including the onboard button and the power LED (which can be turned on or off by initializing it in an OutputPort).

Here's a modified version of your program that accepts input from either the onboard button or a button module (if one is attached):

static int controlledColor;

public static void Main()
{
    byte redValue = 0;
    byte greenValue = 0;
    byte blueValue = 0;

    var led = new RgbLed();

    NetduinoGo.Button button;
    try
    {
        button = new NetduinoGo.Button();
        button.ButtonPressed += new NetduinoGo.Button.ButtonEventHandler(button_ButtonPressed);
    }
    catch { }

    var onboardButton = new InterruptPort(Pins.Button, true, Port.ResistorMode.PullDown, Port.InterruptMode.InterruptEdgeHigh);
    onboardButton.OnInterrupt += new NativeEventHandler(onboardButton_OnInterrupt);

    controlledColor = 0;
    var pot = new Potentiometer();

    while (true)
    {
        switch (controlledColor)
        {
            case 0:
                redValue = (byte)(255 * pot.GetValue());
                break;
            case 1:
                greenValue = (byte)(255 * pot.GetValue());
                break;
            case 2:
                blueValue = (byte)(255 * pot.GetValue());
                break;
        }

        led.SetColor(redValue, greenValue, blueValue);
    }

}

static void NextColor()
{
    // Change The LED Flag that tells the program which color to be controlling
    controlledColor++;
    if (controlledColor > 2)
        controlledColor = 0;
}

static void button_ButtonPressed(object sender, bool isPressed)
{
    NextColor();
}

static void onboardButton_OnInterrupt(uint data1, uint data2, DateTime time)
{
    NextColor();
}

Hope that helps :) Let me know if you have any other questions.

Matt

Edit: Just realized I talked with you in chat about this earlier, I think your post got delayed because of the spam filter... I'll leave the answer though just in case anyone else has the same problem :)
Komodex Labs
Follow me on Twitter: @mattisenhower

#3 CW2

CW2

    Advanced Member

  • Members
  • PipPipPip
  • 1592 posts
  • LocationCzech Republic

Posted 30 August 2012 - 09:20 AM

One tip: You'd want to have objects with event handlers (button, onboardButton) instantiated declared outside the Main() method, so they are not garbage collected eventually (and the event handlers stop working).

Edit: fixed, as neslekkim pointed out (thanks).

Edited by CW2, 30 August 2012 - 10:47 AM.


#4 neslekkim

neslekkim

    Advanced Member

  • Members
  • PipPipPip
  • 350 posts
  • LocationOslo, Norway

Posted 30 August 2012 - 10:15 AM

No, you want to have the member-variables holding those objects defined outside the main method, but still instantiate them inside the main method. Lots of problems have been discussed about that earlier, when it comes to the sequence of instantiating them,.

--
Asbjørn


#5 Lunddahl

Lunddahl

    Advanced Member

  • Members
  • PipPipPip
  • 152 posts
  • LocationEurope, Denmark

Posted 30 August 2012 - 12:02 PM

No, you want to have the member-variables holding those objects defined outside the main method, but still instantiate them inside the main method.
Lots of problems have been discussed about that earlier, when it comes to the sequence of instantiating them,.


Ahhh, that explains some weird behavior i have seen, i knew the variables should go outside of main, but did not know where to instantiate them, so i did that outside of main too.

Nice to know how is should be done right , tnx.

- Ulrik

#6 Jason Stevenson

Jason Stevenson

    New Member

  • Members
  • Pip
  • 2 posts
  • LocationCoal Valley, IL

Posted 30 August 2012 - 10:39 PM

Thank you all for the help. We got it working thanks to all your suggestions. Really appreciate the community help.

#7 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 30 August 2012 - 11:04 PM

No, you want to have the member-variables holding those objects defined outside the main method, but still instantiate them inside the main method.
Lots of problems have been discussed about that earlier, when it comes to the sequence of instantiating them,.

Have you had troubles with this? We should be able to declare and instantiate objects in the Main method without troubles, since Main never terminates so its objects stay in scope (unless disposed).

I know that declaring objects in constructors won't work out so well, since those references are garbage-collectable once the constructor finishes; but if there's an issue with creating objects in Main we should really dig into that more.

Chris

#8 neslekkim

neslekkim

    Advanced Member

  • Members
  • PipPipPip
  • 350 posts
  • LocationOslo, Norway

Posted 31 August 2012 - 03:59 AM

Read that one more time. Instantiating objects in Main is NO problem. Instatiating objects outside of Main is problem, even you have said so. C# cannot guarantee the sequence of instatiation on global variables, so, declare outside main, that is, in the class scope. Instatiate these variables INSIDE main, to make sure that you get things sequenced correctly. If you declare and instatiate variable inside Main, you have no clue about when garbage collection are comming to get you.. I really hope that .net mf developers have a bit experience in normal .net development..

--
Asbjørn


#9 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 31 August 2012 - 05:23 AM

Hi neslekkim,

Read that one more time.
Instantiating objects in Main is NO problem.
Instatiating objects outside of Main is problem, even you have said so.
C# cannot guarantee the sequence of instatiation on global variables, so, declare outside main, that is, in the class scope. Instatiate these variables INSIDE main, to make sure that you get things sequenced correctly.
If you declare and instatiate variable inside Main, you have no clue about when garbage collection are comming to get you..

I really hope that .net mf developers have a bit experience in normal .net development..

I'm following along here, and I generally agree, but I'm still a bit confused regarding declaration and instantiation in the Main function.

I've never had any issues with declaring objects in the Main function...and then having them garbage collected. Whether declared at a class-level scope and instantiated in the Main function...or declared in the Main function and then instantiated in main...I'm not quite clear on how these variables could go out of scope and therefore be garbage collected.

I have roughly eleven years of .NET development experience, and I'm looking forward to learning something new here :) Thanks for your insights, neslekkim.

Chris

P.S. We generally declare objects at a class-level if they're going to be used outside of Main anyway...but that's more of a design decision that we've made instead of something based on garbage collection concerns.

#10 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 31 August 2012 - 08:48 AM

Read that one more time.
Instantiating objects in Main is NO problem.
Instatiating objects outside of Main is problem, even you have said so.
C# cannot guarantee the sequence of instatiation on global variables, so, declare outside main, that is, in the class scope. Instatiate these variables INSIDE main, to make sure that you get things sequenced correctly.
If you declare and instatiate variable inside Main, you have no clue about when garbage collection are comming to get you..

I really hope that .net mf developers have a bit experience in normal .net development..


I think that really depends on how you have structured your code.

Perhaps some snippets would help illustrate and provide some valuable learning materials for beginners :)

Personally i nearly always declare class level variables outside of the main method and only ever assign to them. Although a lot of garbage collection issues stem from a lack of understanding of variable scope, which is an issue in any language, and is one of the most fundamental concepts that's the hardest to learn other than through trail and error and lots of practice.

Ultimately 99% of the time in the .net world you don't need to worry about the GC it just does what its supposed to do (other than when doing COM integrations or using unmanaged code). However the netmf GC is sufficiently different to the standard framework GC to warrant additional study. But its hardly a concept i would recommend to a beginner (or anyone really unless they are having GC issues that cant be solved by a simple refactor/code review), as the time spent being confused by the workings of the GC would be better spent learning generic algorithmics (iteration, selection, recursion etc.), OO principles, studying the .net and netmf api's and documentation and just writing code and making things happen.

My $0.02 on the subject...

Nak.




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.