Beispiel #1
0
        /// <summary>
        /// Starts the Treehopper's SPI interface.
        /// </summary>
        /// <param name="Mode">Configures clock polarity and phase</param>
        /// <param name="RateMHz"></param>
        /// <param name="ChipSelect"></param>
        /// <param name="ChipSelectPolarity"></param>
        /// <param name="ChipSelectDelayMilliseconds"></param>
        /// <param name="InputMode">Chooses whether incoming data is sampled in the middle of the valid data period, or at the end. Most modern sensors produce valid output data in the middle of the waveform period.</param>
        public void Start(SPIMode Mode, double RateMHz, Pin ChipSelect = null, PinPolarity ChipSelectPolarity = PinPolarity.ActiveLow, int ChipSelectDelayMilliseconds = 0, SPISampleMode InputMode = SPISampleMode.Middle)
        {
            this.ChipSelect         = ChipSelect;
            this.ChipSelectPolarity = ChipSelectPolarity;
            if (this.ChipSelect != null)
            {
                this.ChipSelect.MakeDigitalOutput();
                if (ChipSelectPolarity == PinPolarity.ActiveLow)
                {
                    this.ChipSelect.DigitalValue = true;
                }
                else
                {
                    this.ChipSelect.DigitalValue = false;
                }
            }

            double SSPADD = (120.0 / RateMHz - 1);

            if (SSPADD > 255)
            {
                throw new Exception("SPI Rate out of limits. Valid rate is 46.875 kHz - 12 MHz");
            }
            byte[] dataToSend = new byte[4];
            dataToSend[0] = (byte)DeviceCommands.SPIConfig;
            dataToSend[1] = (byte)Mode;
            dataToSend[2] = (byte)InputMode;
            dataToSend[3] = (byte)SSPADD;
            device.sendCommsConfigPacket(dataToSend);
        }
Beispiel #2
0
        /// <summary>
        /// Start I2C communication with the given mode and rate. SDA is on Pin 10, SCL is on pin 11
        /// </summary>
        /// <param name="mode">Master or slave mode</param>
        /// <param name="rate">Communication rate, in kHz.</param>
        public void Start(double rate = 100.0)
        {
            double SSPADD = (12000.0 / rate - 1);

            if (SSPADD < 3 || SSPADD > 255)
            {
                throw new Exception("Rate out of limits. Valid rate is 46.875 kHz - 3000 kHz (3 MHz)");
            }
            byte[] dataToSend = new byte[3];
            dataToSend[0] = (byte)DeviceCommands.I2CConfig;
            dataToSend[1] = 0; // this is hard-coded until the API can be updated with slave support.
            dataToSend[2] = (byte)SSPADD;
            device.sendCommsConfigPacket(dataToSend);
        }
        private void UpdateConfig()
        {
            // timerVal = 0 --> 54.25 us
            // timerVal = 50 --> 45.8 us
            // timerVal = 100 --> 37.5 us
            // timerVal = 150 --> 29.17 us
            // timerVal = 200 --> 20.83 us
            if (pins.Count > 0)
            {
                double timer = Math.Round(268.0934 - 6.22568 * this.resolution);

                if (timerVal < 0)
                {
                    throw new Exception("Bad timer config");
                }

                timerVal = (byte)timer;

                byte[] periodBytes = BitConverter.GetBytes(period);

                byte[] config = new byte[5 + pins.Count]; // { , (byte)pins.Count, timerVal };
                config[0] = (byte)DeviceCommands.SoftPwmConfig;
                config[1] = (byte)pins.Count;
                config[2] = (byte)timerVal;
                periodBytes.CopyTo(config, 3);
                int i = 5;
                foreach (KeyValuePair <int, SoftPwmPinConfig> entry in pins)
                {
                    config[i++] = (byte)entry.Key;
                }
                board.sendCommsConfigPacket(config);
            }
            else
            {
                // turn off the SoftPWM interrupt
                board.sendCommsConfigPacket(new byte[] { (byte)DeviceCommands.SoftPwmConfig, 0 });
            }
        }