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.

Adam Clifford

Member Since 31 Jan 2011
Offline Last Active Oct 28 2013 09:49 PM
-----

Topics I've Started

Socket Exception

06 June 2012 - 10:00 PM

Hi All,

I am trying to run a web service that I have built in connection with a project I am building. However I cannot for the life of me get my netduino to actually talk to my service.

Here is the code, does it look bad?
using (var request = (HttpWebRequest)WebRequest.Create(url))
            {
                request.Method = "GET";
                request.Accept = "application/json";
                request.UserAgent = "OomooTweetNetduino";
                try
                {
                    using (var response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            Debug.Print(response.StatusCode.ToString());

                            var textStream = response.GetResponseStream();

                            byte[] buffer = new byte[textStream.Length];

                            var count = textStream.Read(buffer, 0, int.Parse(textStream.Length.ToString()));

                            char[] chars = Encoding.UTF8.GetChars(buffer);

                            //var s = new string(chars).TrimEnd('

                            Debug.Print(new string(chars));
                        }

                    }

                    return true;

                }
                catch (Exception e)
                {
                    WriteAllErrors(e);
                    return false;
                }

            }

Rotary Encoder Switch class

26 July 2011 - 03:33 PM

Hi All,

Here is a state machine based rotary encoder switch with push button that is interrupt bassed.

you supply the two pins that output the pulses (grey code) and the pin that is used for the push switch, and when the the switch turns the class fires an event based upon the direction of motion, (clockwise or anti clockwise)

here is the code any improvements feel free to point out.
using System;
using Microsoft.SPOT.Hardware;

namespace RotaryEncoderSwitch
{
    public class RotarySwitch
    {
        private static readonly bool[] stateOne = new bool[2] {false, false};
        private static readonly bool[] stateTwo = new bool[2] {false, true};
        private static readonly bool[] stateThree = new bool[2] {true,true};
        private static readonly bool[] stateFour = new bool[2] {true,false};

        private static bool[] oldState = new bool[2];
        private static bool[] newState = new bool[2];

        private static InterruptPort _pinAInterrupt;
        private static InterruptPort _pinBInterrupt;
        private static InterruptPort _switchPinInterrupt;

        
        public delegate void ClockwiseEventHandler();
        public delegate void CounterClockWiseEventHandler();
        public delegate void ButtonPushedEventHandler();

        public static event ClockwiseEventHandler ClockWise;
        public static event CounterClockWiseEventHandler CounterClockWise;
        public static event ButtonPushedEventHandler ButtonPushed;

        public RotarySwitch(Cpu.Pin pinA, Cpu.Pin pinB, Cpu.Pin switchPin)
        {
            _pinAInterrupt = new InterruptPort(pinA, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
            _pinBInterrupt = new InterruptPort(pinB, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);
            _switchPinInterrupt = new InterruptPort(switchPin, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);

            _pinAInterrupt.OnInterrupt += new NativeEventHandler(_pinAInterrupt_OnInterrupt);
            _pinBInterrupt.OnInterrupt += new NativeEventHandler(_pinBInterrupt_OnInterrupt);

            _switchPinInterrupt.OnInterrupt += new NativeEventHandler(_switchPinInterrupt_OnInterrupt);

            oldState[0] = _pinAInterrupt.Read();
            oldState[1] = _pinBInterrupt.Read();
        }


        static void _switchPinInterrupt_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            if (ButtonPushed != null)
            {
                ButtonPushed();
            }
        }

        static void _pinAInterrupt_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            newState[0] = data2 == 1;
            newState[1] = _pinBInterrupt.Read();

            CheckDirection();
        }

        static void _pinBInterrupt_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            newState[0] = _pinBInterrupt.Read();
            newState[1] = data2 == 1;

            CheckDirection();
        }

        private static void CheckDirection()
        {
            int newStateInt = 0;
            int oldStateInt = 0;
            int direction = 0;
            
            if (newState == stateOne)
                newStateInt = 1;
            if (newState == stateTwo)
                newStateInt = 2;
            if (newState == stateThree)
                newStateInt = 3;
            if (newState == stateFour)
                newStateInt = 4;

            if (oldState == stateOne)
                oldStateInt = 1;
            if (oldState == stateTwo)
                oldStateInt = 2;
            if (oldState == stateThree)
                oldStateInt = 3;
            if (oldState == stateFour)
                oldStateInt = 4;

            direction = newStateInt - oldStateInt;

            if(direction > 0)
            {
                
                if (ClockWise != null)
                    ClockWise();
            }

            if(direction < 0)
            {
                oldState = newState;
                newState = null;
                if(CounterClockWise != null)
                {
                    CounterClockWise();
                }
            }

            oldState = newState;
            newState = null;

            return;
        }
    }
}

Adding an interupt port to a class

26 July 2011 - 11:12 AM

Hi Guys and Gals,

I am trying to write an interrupt based rotary encoder switch library that has three events, clockwise, counterclockwise and button push.

However when I try to add an interupt port to the class it just doesnt show up.

Here is my code so far
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using SecretLabs.NETMF;

namespace RotaryEncoderSwitch
{
    public class RotarySwitch
    {
        private int _pinA;
        private int _pinB;
        private int _switchPin;
        private bool _positiveEdge;
        
        public delegate void ClockwiseEventHandler();
        public delegate void CounterClockWiseEventHandler();
        public delegate void ButtonPushedEventHandler();

        public event ClockwiseEventHandler ClockWise;
        public event CounterClockWiseEventHandler CounterClockWise;
        public event ButtonPushedEventHandler ButtonPushed;

        public RotarySwitch(int pinA, int pinB, int switchPin, bool positiveEdge)
        {
            _pinA = pinA;
            _pinB = pinB;
            _switchPin = switchPin;
            _positiveEdge = positiveEdge;
        }
    }
}

I would like to add a static private interrupt port to the class but it wont let me.

does anyone have any ideas why?


Doh, Forgot to add all of the correct references
Adam Clifford

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.