Example #1
0
        /// <summary>
        /// Enable frequency reporting on the given pin.
        /// </summary>
        /// <param name="pinNumber">The pin number</param>
        /// <param name="mode">The mode on which to increase the tick count. Typically rising or falling edge.</param>
        /// <param name="reportDelay">How often the value should update, in milliseconds. Must be positive and smaller than 2^14</param>
        public void EnableFrequencyReporting(int pinNumber, FrequencyMode mode, int reportDelay)
        {
            if (_frequencyReportingPin != -1)
            {
                throw new InvalidOperationException($"Frequency reporting is already enabled for pin {_frequencyReportingPin}. Only one pin is allowed in this mode.");
            }

            if (reportDelay <= 0 || reportDelay >= Math.Pow(2, 14))
            {
                throw new ArgumentOutOfRangeException(nameof(reportDelay), "Value must be between 1 and 2^14");
            }

            DisableFrequencyReporting(-1);

            if (!Board.SupportedPinConfigurations[pinNumber].PinModes.Contains(HandlesMode !))
            {
                throw new InvalidOperationException($"Pin {pinNumber} does not support mode {HandlesMode}");
            }

            Board.SetPinMode(pinNumber, SupportedMode.Frequency);

            var firstQuery = EnableFrequencyReportingInternal(pinNumber, mode, reportDelay);

            _lastFrequencyUpdateClock = firstQuery.TimeStamp;
            _lastFrequencyUpdateTicks = firstQuery.NewTicks;
            _frequencyReportingPin    = pinNumber;
        }
            public int GetFrequency(FrequencyMode mode)
            {
                switch (mode)
                {
                case FrequencyMode.Narrow:
                    return(8000);

                case FrequencyMode.Wide:
                    return(16000);

                case FrequencyMode.UltraWide:
                    return(32000);

                default:
                    return(16000);
                }
            }
Example #3
0
 public FrequencyModeCommand(FrequencyMode mode, int time)
     : base(time)
 {
     Mode = mode;
 }
Example #4
0
        private (int TimeStamp, int NewTicks, bool Success) EnableFrequencyReportingInternal(int pinNumber, FrequencyMode mode, int reportDelay)
        {
            if (reportDelay >= (1 << 14))
            {
                throw new ArgumentOutOfRangeException(nameof(reportDelay), "The maximum update delay is 16.383ms");
            }

            FirmataCommandSequence sequence = new();

            sequence.WriteByte((byte)FirmataSysexCommand.FREQUENCY_COMMAND);
            sequence.WriteByte(1);
            sequence.WriteByte((byte)pinNumber);
            sequence.WriteByte((byte)mode);
            sequence.WriteByte((byte)(reportDelay & 0x7f)); // lower 7 bits
            sequence.WriteByte((byte)((reportDelay >> 7) & 0x7f));
            sequence.WriteByte((byte)FirmataCommand.END_SYSEX);
            var reply = SendCommandAndWait(sequence);

            return(DecodeFrequencyReport(new Span <byte>(reply.ToArray())));
        }
Example #5
0
        public static List <RPCCommand> ProcessPCS(IList <Command> commands, ConvertOptions options)
        {
            List <RPCCommand> rpc = new List <RPCCommand>();

            bool          enabled   = false;
            int           starttime = -1;
            int           lasttime  = 0;
            FrequencyMode freqmode  = 0;

            int?setcountdown = null;

            var e = commands.GetEnumerator();

            while (e.MoveNext())
            {
                if (starttime == -1)
                {
                    starttime = e.Current.Time;
                }
                else
                {
                    int timedelta = e.Current.Time - lasttime;
                    if (timedelta > 0)
                    {
                        rpc.Add(RPCCommand.Delay(timedelta));
                    }
                }
                lasttime = e.Current.Time;
                if (e.Current is EnableCommand)
                {
                    enabled = ((EnableCommand)e.Current).Enable;
                    if (enabled == false)
                    {
                        rpc.Add(RPCCommand.ClearCountdown());
                    }
                    else if (setcountdown != null)
                    {
                        rpc.Add(new RPCCommand(RPCCommandType.SetCountdown, setcountdown.Value));
                        setcountdown = null;
                    }
                }
                else if (e.Current is FrequencyModeCommand)
                {
                    freqmode = ((FrequencyModeCommand)e.Current).Mode;
                }
                else if (e.Current is FrequencyByte1Command && freqmode != 0)
                {
                    if (freqmode == FrequencyMode.Countdown || freqmode == FrequencyMode.FrequencyDivider)
                    {
                        int countdown = ((FrequencyByte1Command)e.Current).Value;
                        e.MoveNext();
                        try{
                            countdown |= ((FrequencyByte2Command)e.Current).Value << 8;
                            //if(countdown != 0)
                            // /*if(enabled) */rpc.Add(new RPCCommand(RPCCommandType.SetCountdown, countdown));
                            if (enabled)
                            {
                                rpc.Add(new RPCCommand(RPCCommandType.SetCountdown, countdown));
                            }
                            else
                            {
                                setcountdown = countdown;
                            }
                        }catch (InvalidCastException)
                        {
                            Program.Error("Missing countdown pair for $42.");
                        }
                    }
                    else
                    {
                        Program.Warning("Unknown value {0:x} for port $42.", freqmode);
                    }
                }
                else
                {
                    Program.Warning("Unknown port ${0:x}.", e.Current.Port);
                }
            }

            return(rpc);
        }
Example #6
0
 public FrequencyModeCommand(int mode, int time) : base(time)
 {
     Mode = (FrequencyMode)mode;
 }
Example #7
0
 public FrequencyModeCommand(FrequencyMode mode, int time) : base(time)
 {
     Mode = mode;
 }
Example #8
0
 public static int GetFrequency(FrequencyMode mode)
 {
     return(FrequencyProvider.GetFrequency(mode));
 }