//--------
        //-------- Constructors
        //--------

        /// <summary>
        /// Construct the state of the U brick with the defaults
        /// </summary>
        public UAdapterState(OneWireState newOneWireState)
        {
            // get a pointer to the OneWire state object
            oneWireState = newOneWireState;

            // set the defaults
            ubaud                   = BAUD_9600;
            uSpeedMode              = USPEED_FLEX;
            revision                = 0;
            inCommandMode           = true;
            streamBits              = true;
            streamBytes             = true;
            streamSearches          = true;
            streamResets            = false;
            programVoltageAvailable = false;
            longAlarmCheck          = false;
            lastAlarmCount          = 0;

            // create the three speed logical parameter settings
            uParameters    = new UParameterSettings[4];
            uParameters[0] = new UParameterSettings();
            uParameters[1] = new UParameterSettings();
            uParameters[2] = new UParameterSettings();
            uParameters[3] = new UParameterSettings();

            // adjust flex time
            uParameters[DSPortAdapter.SPEED_FLEX].pullDownSlewRate = UParameterSettings.SLEWRATE_0p83Vus;
            uParameters[DSPortAdapter.SPEED_FLEX].write1LowTime    = UParameterSettings.WRITE1TIME_12us;
            uParameters[DSPortAdapter.SPEED_FLEX].sampleOffsetTime = UParameterSettings.SAMPLEOFFSET_TIME_10us;
        }
Example #2
0
        public DecoderOutput[] Process(Dictionary <string, Array> inputWaveforms, Dictionary <string, object> parameters, double samplePeriod)
        {
            //name input waveforms for easier usage
            bool[] input = (bool[])inputWaveforms["Input"];

            //initialize output structure
            List <DecoderOutput> decoderOutputList = new List <DecoderOutput>();

            //init counters and flags
            OneWireState state = OneWireState.Idle;

            int startIndex = 0;

            //brute-force decoding of incoming bits
            for (int i = 1; i < input.Length; i++)
            {
                bool risingEdge  = input[i] && !input[i - 1];
                bool fallingEdge = !input[i] && input[i - 1];

                switch (state)
                {
                case OneWireState.Idle:
                {
                    if (fallingEdge)
                    {
                        state      = OneWireState.ResetMPulse;
                        startIndex = i;
                    }
                }
                break;

                case OneWireState.ResetMPulse:
                {
                    if (risingEdge)
                    {
                        int    elapsedSamples = i - startIndex;
                        double elapsedTime    = (double)(elapsedSamples) * samplePeriod;
                        if (elapsedTime > resetMPulseMin)
                        {
                            state = OneWireState.ResetSWait;
                            decoderOutputList.Add(new DecoderOutputEvent(startIndex, i, DecoderOutputColor.Green, "Reset"));
                        }
                        else
                        {
                            state = OneWireState.Idle;
                            decoderOutputList.Add(new DecoderOutputEvent(startIndex, i, DecoderOutputColor.Red, "ResetNotLongEnough"));
                        }
                        startIndex = i;
                    }
                }
                break;

                case OneWireState.ResetSWait:
                {
                    if (fallingEdge)
                    {
                        int    elapsedSamples = i - startIndex;
                        double elapsedTime    = (double)(elapsedSamples) * samplePeriod;
                        if (elapsedTime > resetWaitMin && elapsedTime < resetWaitMax)
                        {
                            state = OneWireState.ResetSPulse;
                        }
                        else
                        {
                            state = OneWireState.Idle;
                            decoderOutputList.Add(new DecoderOutputEvent(startIndex, i, DecoderOutputColor.Red, "UntimelyResponse"));
                        }
                        startIndex = i;
                    }
                }
                break;

                case OneWireState.ResetSPulse:
                {
                    if (risingEdge)
                    {
                        int    elapsedSamples = i - startIndex;
                        double elapsedTime    = (double)(elapsedSamples) * samplePeriod;
                        if (elapsedTime > resetSPulseMin && elapsedTime < resetSPulseMax)
                        {
                            state = OneWireState.Active;
                            decoderOutputList.Add(new DecoderOutputEvent(startIndex, i, DecoderOutputColor.Purple, "R_ACK"));
                        }
                        else
                        {
                            state = OneWireState.Idle;
                            decoderOutputList.Add(new DecoderOutputEvent(startIndex, i, DecoderOutputColor.Red, "WrongAckWidth"));
                        }
                        startIndex = i;
                    }
                }
                break;

                case OneWireState.Active:
                {
                    if (fallingEdge)
                    {
                        state      = OneWireState.ActiveFallen;
                        startIndex = i;
                    }
                }
                break;

                case OneWireState.ActiveFallen:
                {
                    if (risingEdge)
                    {
                        int    elapsedSamples = i - startIndex;
                        double elapsedTime    = (double)(elapsedSamples) * samplePeriod;
                        if (elapsedTime < masterWriteOneMax)
                        {
                            state = OneWireState.One;
                        }
                        else if (elapsedTime < timeslot)
                        {
                            state = OneWireState.MasterReadingZero;
                        }
                        else if (elapsedTime > timeslot)
                        {
                            state = OneWireState.MasterWritingZero;
                        }
                        // startIndex = i; don't reset here, as the output needs to span both falling and rising edge period
                    }
                }
                break;

                case OneWireState.One:
                {
                    if (fallingEdge)
                    {
                        state = OneWireState.ActiveFallen;
                        decoderOutputList.Add(new DecoderOutputValueNumeric(startIndex, i, DecoderOutputColor.Purple, 1, "W/R", 1));
                        startIndex = i;
                    }
                }
                break;

                case OneWireState.MasterWritingZero:
                {
                    if (fallingEdge)
                    {
                        state = OneWireState.ActiveFallen;
                        decoderOutputList.Add(new DecoderOutputValueNumeric(startIndex, i, DecoderOutputColor.Blue, 0, "W", 1));
                        startIndex = i;
                    }
                }
                break;

                case OneWireState.MasterReadingZero:
                {
                    if (fallingEdge)
                    {
                        state = OneWireState.ActiveFallen;
                        decoderOutputList.Add(new DecoderOutputValueNumeric(startIndex, i, DecoderOutputColor.Green, 0, "R", 1));
                        startIndex = i;
                    }
                }
                break;
                }

                //watchdog
                if ((state == OneWireState.ActiveFallen) && ((double)(i - startIndex) * samplePeriod > 100 * timeslot))
                {
                    state = OneWireState.Idle;
                    decoderOutputList.Add(new DecoderOutputEvent(startIndex, i, DecoderOutputColor.Red, "TimeOut"));
                }
            }

            return(decoderOutputList.ToArray());
        }