jefffhaynes / XBee

A .NET library for XBee wireless controllers
MIT License
41 stars 17 forks source link

Feature Request: SampleReceivedEventArgs, Properties for accessing Digital and Analog Data #15

Closed PaulNoto closed 7 years ago

PaulNoto commented 8 years ago

Hi Jeff, I have been able to get at the Digital and Analog data coming from an XBee. I was hoping that you would add properties to simplify the process. I modified your source code to test my idea and I like the way it works. This is what I did, let me know what you think.

I did this for the DIO pins

class SampleReceivedEventArgs

    /// <summary>
    /// Returns the state of DIO0 from the sample.
    /// </summary>
    public bool D0 { get { return DigitalSampleState.HasFlag( DigitalSampleState.Input0 ); } }

so now I have properties for D0 thru D12 and I can code the Event Handler:

    void DoIt( object node, SampleReceivedEventArgs sample ) {
        if ( !sample.D0 )
            AddLog( "End_Grove: Button_0 Pressed" );
    }

I really like this.

I also did the same for the ADC inputs

    /// <summary>
    /// Returns the value of ADC0 from the sample.
    /// </summary>
    public ushort A0 {
        get {
            foreach ( AnalogSample s in AnalogSamples ) if ( s.Channel == 0 ) return s.Value;
            return 0;
        }
    }

I checked the digital pin mask, to make sure it worked, like this

    /// <summary>
    /// Returns the state of DIO0 from the sample.
    /// </summary>
    public bool D0 {
        get {
            if ( debug_verify_mask && !DigitalChannels.HasFlag( DigitalSampleChannels.Input0 ) )
                throw new InvalidOperationException( string.Format( "{0} is not configured as a digital pin", DigitalSampleChannels.Input0 ) );
            return DigitalSampleState.HasFlag( DigitalSampleState.Input0 );
        }
    }

It works. However, I don't like it because, #1 Even though It does throw an exception the debugger never sees it. #2 The value of debug_verify_mask needs to be set globally. #3 There is no way I could figure out to know which XBee has the problem from inside SampleReceivedEventArgs. I also did something similar for the ADC Mask and it works, but with these same issues.

Anyway, please consider some way of adding properties to SampleReceivedEventArgs. I really like how easy it makes writing code in the Event handler.

jefffhaynes commented 8 years ago

Just looking at this now. I'm curious why you say the debugger doesn't see your exception when it gets thrown. Are you calling it from inside a task or something? Under normal circumstances you should be able to catch that exception.

PaulNoto commented 8 years ago

I was calling it from a sample received event handler with no task involved. I never pursued why the exception never reached the debugger because, to me, it was meaningless without the context of which XBee threw the exception, because, I could never figure out how to get a reference to the node from inside SampleReceivedEventArgs. Even if the exception was coming out of SampleReceivedEventArgs the only way to deal with it, that I could think of, was to wrap every call to a property inside a try/catch block like so:

    void DoIt( object node, SampleReceivedEventArgs sample ) {
        try {
            if ( !sample.D0 )
                AddLog( "End_Grove: Button_0 Pressed" );
        }
        catch ( Exception ex ) {
            // Report problem from here because I know which node caused the exception
        }
    }

This just seems wrong. I think if, inside your library, SampleReceivedEventArgs had a reference to the node, SampleReceivedEventArgs could throw the exception with a context as to which node was the problem. At he end of the day, I consider this type of exception to be something I would want to resolve during test/debug and not even let get out, to a deployed application.

Anyway, I'm not sure if this type of exception needs to be addressed in this way, or at all. I just thought it would be helpful during test/debug, to know immediately which node was not configured correctly, but I may not be thinking it through correctly.

jefffhaynes commented 7 years ago

Glad to hear it, thanks!


From: PaulNoto notifications@github.com Sent: Friday, May 12, 2017 8:29:42 AM To: jefffhaynes/XBee Cc: Jeff Haynes; Comment Subject: Re: [jefffhaynes/XBee] Feature Request: SampleReceivedEventArgs, Properties for accessing Digital and Analog Data (#15)

Closed #15https://github.com/jefffhaynes/XBee/issues/15.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/jefffhaynes/XBee/issues/15#event-1080182480, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AJSKR9auaegiiPcafaadvovUAX0M9SJYks5r5HrmgaJpZM4ISIey.