public SpiConnection(ProcessorPin clock, ProcessorPin ss, ProcessorPin? miso, ProcessorPin? mosi, Endianness endianness)
        {
            this.clock = clock;
            this.ss = ss;
            this.miso = miso;
            this.mosi = mosi;
            this.endianness = endianness;

            driver = new MemoryGpioConnectionDriver();

            driver.Allocate(clock, PinDirection.Output);
            driver.Write(clock, false);

            driver.Allocate(ss, PinDirection.Output);
            driver.Write(ss, true);

            if (mosi.HasValue)
            {
                driver.Allocate(mosi.Value, PinDirection.Output);
                driver.Write(mosi.Value, false);
            }

            if (miso.HasValue)
                driver.Allocate(miso.Value, PinDirection.Input);
        }
Example #2
0
        private static void Main()
        {
            const ConnectorPin measurePin = ConnectorPin.P1Pin11;

            Console.WriteLine("DHT-22 Sample: measure humidity and temperature");
            Console.WriteLine();
            Console.WriteLine("\tMeasure pin: {0}", measurePin);
            Console.WriteLine();

            var driver = new MemoryGpioConnectionDriver();

            using (var pin = driver.InOut(measurePin))
            using (var DhtConnection = new Dht22Connection(pin))
            {
                int TotalRetries = 0;
                double measurements = 0;
                int errors = 0;
                int sumRetries = 0;
                while (!Console.KeyAvailable)
                {
                    DhtData data = null;
                    try {
                        data = DhtConnection.GetData();

                    } catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    TotalRetries += data.AttemptCount - 1;
                    measurements++;
                    sumRetries += data.AttemptCount -1;
                    if (data != null)
                        Console.WriteLine("{0}: Readings: {1:0.00}% humidity, {2:0.0}°C, Attempts {3}",
                            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), data.RelativeHumidity.Percent,
                            data.Temperature.DegreesCelsius, data.AttemptCount);
                    else
                    {
                        errors++;
                        Console.WriteLine("Unable to read data\n");
                    }
                    Console.WriteLine("\nMeasurements {0:0}, Errors {1}, Error rate {2:0.00}%, ",
                        measurements, errors, errors / measurements * 100);
                    Console.WriteLine("TotalRetries {0:0}, Mean retries per sample {1:0.000}\n",
                        TotalRetries, sumRetries/ measurements);

                    // DHT 22: producer hints that sample period should be at least 2 seconds
                    // Test that DhtXxConnection's code enforces the specification, by calling
                    // for a shorter wait
                    Timer.Sleep(TimeSpan.FromSeconds(2));
                }
            }
        }
Example #3
0
        static void Main()
        {
            const ConnectorPin adcClock = ConnectorPin.P1Pin12;
            const ConnectorPin adcMiso = ConnectorPin.P1Pin16;
            const ConnectorPin adcMosi = ConnectorPin.P1Pin18;
            const ConnectorPin adcCs = ConnectorPin.P1Pin22;

            Console.WriteLine("MCP-3008 Sample: Reading temperature on Channel 0 and luminosity on Channel 1");
            Console.WriteLine();
            Console.WriteLine("\tClock: {0}", adcClock);
            Console.WriteLine("\tCS: {0}", adcCs);
            Console.WriteLine("\tMOSI: {0}", adcMosi);
            Console.WriteLine("\tMISO: {0}", adcMiso);
            Console.WriteLine();

            const decimal voltage = 3.3m;

            var driver = new MemoryGpioConnectionDriver(); //GpioConnectionSettings.DefaultDriver;

            using (var adcConnection = new Mcp3008SpiConnection( 
                driver.Out(adcClock), 
                driver.Out(adcCs), 
                driver.In(adcMiso), 
                driver.Out(adcMosi)))
            using (var temperatureConnection = new Tmp36Connection(
                adcConnection.In(Mcp3008Channel.Channel0),
                voltage))
            using (var lightConnection = new VariableResistiveDividerConnection(
                adcConnection.In(Mcp3008Channel.Channel1), 
                ResistiveDivider.ForLowerResistor(10000)))
            {
                Console.CursorVisible = false;

                while (!Console.KeyAvailable)
                {
                    var temperature = temperatureConnection.GetTemperature();
                    decimal resistor = lightConnection.GetResistor();
                    var lux = resistor.ToLux();

                    Console.WriteLine("Temperature = {0,5:0.0} °C\tLight = {1,5:0.0} Lux ({2} ohms)", temperature, lux, (int) resistor);

                    Console.CursorTop--;

                    Thread.Sleep(1000);
                }
            }

            Console.CursorTop++;
            Console.CursorVisible = true;
        }
Example #4
0
        public static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Starting application");
                var driver = new MemoryGpioConnectionDriver();

                var lcdSpiSettings = new SpiConnectionSettings();
                lcdSpiSettings.BitsPerWord = 8;
                lcdSpiSettings.MaxSpeed = 3932160;
                lcdSpiSettings.Delay = 0;
                lcdSpiSettings.Mode = SpiMode.Mode0;

                var adsSpiSettings = new SpiConnectionSettings();
                adsSpiSettings.BitsPerWord = 8;
                adsSpiSettings.MaxSpeed = 3932160;
                adsSpiSettings.Delay = 0;
                adsSpiSettings.Mode = SpiMode.Mode1;

                var spi0 = new NativeSpiConnection("/dev/spidev0.0", lcdSpiSettings);
                var spi1 = new NativeSpiConnection("/dev/spidev0.1", adsSpiSettings);

                var lcdRegisterSelectGpio = ConnectorPin.P1Pin11;
                driver.In(lcdRegisterSelectGpio).Read();
                var lcdRegisterSelectOut = driver.Out(lcdRegisterSelectGpio);

                var lcdResetGpio = ConnectorPin.P1Pin16;
                var lcdResetOut = driver.Out(lcdResetGpio);

                using (var deviceConnection = new Ti430BoostAds1118Connection(spi0, spi1, lcdRegisterSelectOut, lcdResetOut))
                {
                    deviceConnection.InitializeLcd();
                    deviceConnection.DisplayStringOnLcd(LcdLine.FirstLine, "Hello!");
                    Thread.Sleep(500);

                    deviceConnection.ClearLcd();

                    var temp = deviceConnection.GetMeasurement();
                    deviceConnection.DisplayStringOnLcd(LcdLine.SecondLine, string.Format("TEMP: {0} C", temp));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception caught!");
                Console.WriteLine("Exception Message: {0}", ex.Message);
                Console.WriteLine("Stack Trace: {0}", ex.StackTrace);
            }
        }
 public override void Initialization()
 {
     logger.Debug("Humidity_Temperature_Air_DHT22|Initialization_00");
     try
     {
         if (!Simulation)
         {
             logger.Debug("Humidity_Temperature_Air_DHT22|Initialization_05");
             MemoryGpioConnectionDriver driver = new MemoryGpioConnectionDriver();
             logger.Debug("Humidity_Temperature_Air_DHT22|Initialization_06");
             const ConnectorPin measurePin = ConnectorPin.P1Pin11;
             logger.Debug("Humidity_Temperature_Air_DHT22|Initialization_07");
             IInputOutputBinaryPin pin = driver.InOut(measurePin);
             logger.Debug("Humidity_Temperature_Air_DHT22|Initialization_10");
             dht22 = new Dht22Connection(pin);
             logger.Debug("Humidity_Temperature_Air_DHT22|Initialization_11");
         }
         else
         {
             // do nothing
         }
         // define measurements list
         DateTime instant = DateTime.Now;
         Measurement t = new Measurement()
         {
             Value = MinValue,
             Unit = "[°C]",
             DisplayFormat = "0.00",
             SampleTime = instant,
             Name = "DHT22-Temperature",
         };
         LastMeasurements.Add(t);
         Measurement rh = new Measurement()
         {
             Value = MinValue,
             Unit = "[RH%]",
             DisplayFormat = "0",
             SampleTime = instant,
             Name = "DHT22-Relative Humidity"
         };
         LastMeasurements.Add(rh);
     }
     catch (Exception ex)
     {
         logger.Error("Humidity_Temperature_Air_DHT22|Initialization_20 " + ex.Message);
     }
     logger.Debug("Humidity_Temperature_Air_DHT22|Initialization_99");
 }
        /// <summary>
        /// Gets the best driver for the specified capabilities.
        /// </summary>
        /// <param name="capabilities">The capabilities.</param>
        /// <returns>The best driver, if found; otherwise, <c>null</c>.</returns>
        public static IGpioConnectionDriver GetBestDriver(GpioConnectionDriverCapabilities capabilities)
        {
            if ((GpioConnectionDriver.GetCapabilities() & capabilities) == capabilities)
            {
                return(new GpioConnectionDriver());
            }
            if ((MemoryGpioConnectionDriver.GetCapabilities() & capabilities) == capabilities)
            {
                return(new MemoryGpioConnectionDriver());
            }
            if ((FileGpioConnectionDriver.GetCapabilities() & capabilities) == capabilities)
            {
                return(new FileGpioConnectionDriver());
            }

            return(null);
        }
Example #7
0
        public void Initialize(ConnectorPin adcClock, ConnectorPin adcMiso, ConnectorPin adcMosi, ConnectorPin adcCs)
        {
            AdcClock = adcClock;
            AdcMiso = adcMiso;
            AdcMosi = adcMosi;
            AdcCs = adcCs;

            Console.WriteLine("MCP-3008 Sample: Reading temperature on Channel 0 and luminosity on Channel 1");
            Console.WriteLine();
            Console.WriteLine("\tClock: {0}", adcClock);
            Console.WriteLine("\tCS: {0}", adcCs);
            Console.WriteLine("\tMOSI: {0}", adcMosi);
            Console.WriteLine("\tMISO: {0}", adcMiso);
            Console.WriteLine();

            _driver = new MemoryGpioConnectionDriver();
            _adcConnection = new Mcp3008SpiConnection(
                _driver.Out(AdcClock),
                _driver.Out(AdcCs),
                _driver.In(AdcMiso),
                _driver.Out(AdcMosi));
        }