示例#1
0
 internal void EnableSpi()
 {
     _spiEnabled++;
     if (_spiEnabled == 1)
     {
         Firmata.EnableSpi();
     }
 }
示例#2
0
        /// <summary>
        /// Returns the current assignment of the given pin
        /// </summary>
        /// <param name="pinNumber">Pin number to query</param>
        /// <returns>A value of the <see cref="PinUsage"/> enumeration</returns>
        public override PinUsage DetermineCurrentPinUsage(int pinNumber)
        {
            SupportedMode mode = Firmata.GetPinMode(pinNumber);

            switch (mode)
            {
            case SupportedMode.AnalogInput:
                return(PinUsage.AnalogIn);

            case SupportedMode.DigitalInput:
                return(PinUsage.Gpio);

            case SupportedMode.DigitalOutput:
                return(PinUsage.Gpio);

            case SupportedMode.Pwm:
                return(PinUsage.Pwm);

            case SupportedMode.Servo:
                break;

            case SupportedMode.Shift:
                break;

            case SupportedMode.I2c:
                return(PinUsage.I2c);

            case SupportedMode.OneWire:
                break;

            case SupportedMode.Stepper:
                break;

            case SupportedMode.Encoder:
                break;

            case SupportedMode.Serial:
                return(PinUsage.Uart);

            case SupportedMode.InputPullup:
                return(PinUsage.Gpio);

            case SupportedMode.Spi:
                return(PinUsage.Spi);

            case SupportedMode.Sonar:
                break;

            case SupportedMode.Tone:
                break;

            case SupportedMode.Dht:
                return(PinUsage.Gpio);
            }

            return(PinUsage.Unknown);
        }
示例#3
0
 /// <summary>
 /// Sets a single pixel to the given color values
 /// </summary>
 /// <param name="red">The amount of red to set</param>
 /// <param name="green">The amount of green to set</param>
 /// <param name="blue">The amount of blue to set</param>
 private void SetPixel(byte pixel, byte red, byte green, byte blue)
 {
     Firmata.beginSysex(NEOPIXEL_SET_COMMAND);
     Firmata.appendSysex(pixel);
     Firmata.appendSysex(red);
     Firmata.appendSysex(green);
     Firmata.appendSysex(blue);
     Firmata.endSysex();
 }
示例#4
0
        internal void DisableSpi()
        {
            if (_spiEnabled <= 0)
            {
                throw new InvalidOperationException("Internal reference counting error: Spi ports already closed");
            }

            _spiEnabled--;
            if (_spiEnabled == 0)
            {
                Firmata.DisableSpi();
            }
        }
示例#5
0
        /// <summary>
        /// Special function to read DHT sensor, if supported
        /// </summary>
        /// <param name="pinNumber">Pin Number</param>
        /// <param name="dhtType">Type of DHT Sensor: 11 = DHT11, 22 = DHT22, etc.</param>
        /// <param name="temperature">Temperature</param>
        /// <param name="humidity">Relative humidity</param>
        /// <returns>True on success, false otherwise</returns>
        public bool TryReadDht(int pinNumber, int dhtType, out Temperature temperature, out RelativeHumidity humidity)
        {
            Initialize();

            if (!_supportedPinConfigurations[pinNumber].PinModes.Contains(SupportedMode.Dht))
            {
                temperature = default;
                humidity    = default;
                return(false);
            }

            return(Firmata.TryReadDht(pinNumber, dhtType, out temperature, out humidity));
        }
示例#6
0
        /// <summary>
        /// Returns the current assignment of the given pin
        /// </summary>
        /// <param name="pinNumber">Pin number to query</param>
        /// <returns>An instance of <see cref="SupportedMode"/> from the list of known modes (or a new instance for an unknown mode)</returns>
        /// <remarks>Thi is the opposite of <see cref="SetPinMode"/>. See there for usage limitations.</remarks>
        public SupportedMode GetPinMode(int pinNumber)
        {
            byte mode = Firmata.GetPinMode(pinNumber);

            _commandHandlersLock.EnterReadLock();
            try
            {
                var m = _knownSupportedModes.FirstOrDefault(x => x.Value == mode);
                if (m == null)
                {
                    return(new SupportedMode(mode, $"Unknown mode {mode}"));
                }

                return(m);
            }
            finally
            {
                _commandHandlersLock.ExitReadLock();
            }
        }
示例#7
0
        /// <summary>
        /// Returns the current assignment of the given pin
        /// </summary>
        /// <param name="pinNumber">Pin number to query</param>
        /// <returns>A value of the <see cref="PinUsage"/> enumeration</returns>
        public override PinUsage DetermineCurrentPinUsage(int pinNumber)
        {
            byte mode = Firmata.GetPinMode(pinNumber);

            _commandHandlersLock.EnterReadLock();
            try
            {
                var m = _knownSupportedModes.FirstOrDefault(x => x.Value == mode);
                if (m == null)
                {
                    return(PinUsage.Unknown);
                }

                return(m.PinUsage);
            }
            finally
            {
                _commandHandlersLock.ExitReadLock();
            }
        }
示例#8
0
        /// <summary>
        /// Configures the sampling interval for analog input pins (when an event callback is enabled)
        /// </summary>
        /// <param name="timeSpan">Timespan between updates. Default ~20ms</param>
        public void SetAnalogPinSamplingInterval(TimeSpan timeSpan)
        {
            Initialize();

            Firmata.SetAnalogInputSamplingInterval(timeSpan);
        }
示例#9
0
 /// <summary>
 /// Tells the NeoPixel strip to update its displayed colors.
 /// This function must be called before any colors set to pixels will be displayed.
 /// </summary>
 /// <param name="red">The amount of red to set</param>
 /// <param name="green">The amount of green to set</param>
 /// <param name="blue">The amount of blue to set</param>
 private void UpdateStrip()
 {
     Firmata.beginSysex(NEOPIXEL_SHOW_COMMAND);
     Firmata.endSysex();
 }
示例#10
0
 /// <summary>
 /// Performs a software reset of the Arduino firmware
 /// </summary>
 public void SoftwareReset()
 {
     Initialize();
     Firmata.SendSoftwareReset();
     Firmata.QueryCapabilities();
 }
示例#11
0
 /// <summary>
 /// Sets the internal pin mode to the given value, if supported.
 /// </summary>
 /// <param name="pin">The pin to configure</param>
 /// <param name="arduinoMode">The mode to set</param>
 /// <exception cref="TimeoutException">The mode was not updated, either because the command was not understood or
 /// the mode is unknown by the firmware</exception>
 /// <remarks>This method is intended for use by <see cref="ExtendedCommandHandler"/> instances. Users should not
 /// call this method directly. It is the responsibility of the command handler to use the capabilities table to check
 /// that the mode is actually supported</remarks>
 public void SetPinMode(int pin, SupportedMode arduinoMode)
 {
     Firmata.SetPinMode(pin, arduinoMode);
 }
示例#12
0
        public override void Run(string param)
        {
            string[] vars = param.Split(';');
            int      power, a, b = -1;
            bool     on, astate, bstate = false;

            if (vars.Length == 5)
            {
                if (!int.TryParse(vars[1], out power) ||
                    !int.TryParse(vars[2], out a))
                {
                    Console.WriteLine("A pin was not a number.");
                    return;
                }
                if (!ParseBool(vars[3], out on) ||
                    !ParseBool(vars[4], out astate))
                {
                    Console.WriteLine("A state was not boolean.");
                    return;
                }
            }
            else if (vars.Length == 7)
            {
                if (!int.TryParse(vars[1], out power) ||
                    !int.TryParse(vars[2], out a) ||
                    !int.TryParse(vars[3], out b))
                {
                    Console.WriteLine("A pin was not a number.");
                    return;
                }
                if (!ParseBool(vars[4], out on) ||
                    !ParseBool(vars[5], out astate) ||
                    !ParseBool(vars[6], out bstate))
                {
                    Console.WriteLine("A state was not boolean.");
                    return;
                }
            }
            else
            {
                Console.WriteLine("Incorrect number of parameters.");
                return;
            }

            Firmata board;

            try {
                board = new Firmata(vars[0]);
            } catch (UnauthorizedAccessException) {
                Console.WriteLine("Access is denied to the port.");
                return;
            } catch (ArgumentException) {
                Console.WriteLine("The port name does not begin with \"COM\", or the file type of the port is not supported.");
                return;
            } catch (InvalidOperationException) {
                Console.WriteLine("The specified port is used by another application.");
                return;
            } catch {
                Console.WriteLine("The port couldn't be opened.");
                return;
            }

            board.SetPin(a, !astate);
            if (b != -1)
            {
                board.SetPin(b, !bstate);
            }
            board.SetPin(power, on);
            board.Dispose();
        }