Exemplo n.º 1
0
        private static void SpiRaspiTestWithSoftwareCs(Board raspi)
        {
            Console.WriteLine("MCP3008 SPI Software CS Test");

            // Runs a test communication against an MCP3008. The CS pin 8 is controlled explicitly by software (this binding
            // fails to have support for this)
            SpiConnectionSettings spiSettings = new SpiConnectionSettings(0, -1)
            {
                ChipSelectLineActiveState = PinValue.Low
            };

            using SpiDevice dev       = raspi.CreateSpiDevice(spiSettings);
            using Mcp3008 mcp         = new Mcp3008(dev);
            using GpioController ctrl = raspi.CreateGpioController();

            // Note that if we have only a single device attached to the SPI bus, we could just as well pull the CS line
            // hard to low, but this is to show the concept.
            ctrl.OpenPin(8, PinMode.Output);
            ctrl.Write(8, PinValue.High);
            while (!Console.KeyAvailable)
            {
                for (int i = 0; i < 8; i++)
                {
                    ctrl.Write(8, PinValue.Low);
                    int value = mcp.Read(i);
                    ctrl.Write(8, PinValue.High);
                    Console.WriteLine($"Channel {i} has value {value}.");
                    Thread.Sleep(100);
                }

                Thread.Sleep(500);
            }

            Console.ReadKey(true);
        }
Exemplo n.º 2
0
        public void DoubleDispose_DoesNotThrow()
        {
            Mcp3008 adc = CreateAdc();

            adc.Dispose();
            adc.Dispose();
        }
Exemplo n.º 3
0
 public FsrWithAdcSample()
 {
     // Create a ADC convertor instance you are using depending how you wired ADC pins to controller
     // in this example used ADC Mcp3008 with "bit-banging" wiring method.
     // please refer https://github.com/dotnet/iot/tree/master/src/devices/Mcp3008/samples for more information
     _adcConvertor = new Mcp3008(18, 23, 24, 25);
 }
Exemplo n.º 4
0
 public RtdProbe(SpiDevice spi, IConfiguration config)
 {
     _adc = new Mcp3008(spi);
     _probeResistances = new ConcurrentQueue <double>();
     _offset           = int.Parse(config["offset"]);
     _adcReadTask      = ReadAdc();
 }
Exemplo n.º 5
0
        public void PWM_DutyCycleIsSetCorrectly()
        {
            using (PwmChannel pwm = CreatePwmChannel(dutyCycle: 0))
                using (Mcp3008 adc = CreateAdc())
                {
                    for (int n = 0; n < 2; n++)
                    {
                        for (int i = 0; i <= 10; i++)
                        {
                            pwm.DutyCycle = i * 0.1;

                            // Settling time is ~1.1ms (when going from GND to max)
                            // R=4.7k ohm
                            // C=0.1uF
                            // f=10k Hz
                            // peak to peak is ~0.18V (5.5% VCC)
                            //   in this scenario is 2 * error
                            Thread.Sleep(3);

                            int expected = (int)Math.Round(pwm.DutyCycle * 1023.0);
                            AdcValueAround(expected, adc.Read(0));
                        }
                    }
                }
        }
Exemplo n.º 6
0
 /// <inheritdoc/>
 public override void Initialize()
 {
     this.LogStartInitialization();
     this.adc           = new Mcp3008(this.spiDevice);
     this.IsInitialized = true;
     this.LogStartSuccess();
 }
Exemplo n.º 7
0
        public static void TestSpi(ArduinoBoard board)
        {
            const double          vssValue = 5; // Set this to the supply voltage of the arduino. Most boards have 5V, some newer ones run at 3.3V.
            SpiConnectionSettings settings = new SpiConnectionSettings(0, 10);

            using (var spi = board.CreateSpiDevice(settings))
                using (Mcp3008 mcp = new Mcp3008(spi))
                {
                    Console.WriteLine("SPI Device open");
                    while (!Console.KeyAvailable)
                    {
                        double vdd    = mcp.Read(5);
                        double vss    = mcp.Read(6);
                        double middle = mcp.Read(7);
                        Console.WriteLine($"Raw values: VSS {vss} VDD {vdd} Average {middle}");
                        vdd    = vssValue * vdd / 1024;
                        vss    = vssValue * vss / 1024;
                        middle = vssValue * middle / 1024;
                        Console.WriteLine($"Converted values: VSS {vss:F2}V, VDD {vdd:F2}V, Average {middle:F2}V");
                        Thread.Sleep(200);
                    }
                }

            Console.ReadKey();
        }
Exemplo n.º 8
0
        private static void SpiRaspiTestWithHardwareCs(Board raspi)
        {
            Console.WriteLine("MCP3008 SPI Hardware CS Test");

            // Runs a test communication against an MCP3008. The CS pin 8 is expected to be controlled by the driver, as
            // we specify it here (SPI0_CE0 is BCM pin 8 on ALT0)
            SpiConnectionSettings spiSettings = new SpiConnectionSettings(0, 0)
            {
                ChipSelectLineActiveState = PinValue.Low
            };

            using SpiDevice dev = raspi.CreateSpiDevice(spiSettings);
            using Mcp3008 mcp   = new Mcp3008(dev);
            while (!Console.KeyAvailable)
            {
                for (int i = 0; i < 8; i++)
                {
                    int value = mcp.Read(i);
                    Console.WriteLine($"Channel {i} has value {value}.");
                    Thread.Sleep(100);
                }

                Thread.Sleep(500);
            }

            Console.ReadKey(true);
        }
Exemplo n.º 9
0
 public void InvalidChannel_Throws()
 {
     using (Mcp3008 adc = CreateAdc())
     {
         Assert.Throws <ArgumentOutOfRangeException>(() => adc.Read(-1));
         Assert.Throws <ArgumentOutOfRangeException>(() => adc.Read(8));
     }
 }
Exemplo n.º 10
0
        public RtdArray(SpiDevice spi)
        {
            _adc = new Mcp3008(spi);
            _grillResistances = new ConcurrentQueue <double>();
            _probeResistances = new ConcurrentQueue <double>();

            _adcReadTask = ReadAdc();
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Mcp3008!");

            // This sample implements two different ways of accessing the MCP3008.
            // The SPI option is enabled in the sample by default, but you can switch
            // to the GPIO bit-banging option by switching which one is commented out.
            // The sample uses local functions to make it easier to switch between
            // the two implementations.

            // SPI implementation
            Mcp3008 GetMcp3008WithSpi()
            {
                Console.WriteLine("Using SPI protocol.");

                var connection = new SpiConnectionSettings(0, 0)
                {
                    ClockFrequency = 1000000,
                    Mode           = SpiMode.Mode0
                };

                var spi     = new UnixSpiDevice(connection);
                var mcp3008 = new Mcp3008(spi);

                return(mcp3008);
            }

            // GPIO (via bit banging) implementation
            Mcp3008 GetMcp3008WithGpio()
            {
                Console.WriteLine("Using GPIO pins.");
                var mcp3008 = new Mcp3008(18, 23, 24, 25);

                return(mcp3008);
            }

            Mcp3008 mcp = GetMcp3008WithSpi();

            // Uncomment next line to use GPIO instead.
            // Mcp3008 mcp = GetMcp3008WithGpio();

            using (mcp)
            {
                while (true)
                {
                    double value = mcp.Read(0, Mcp3008.InputConfiguration.SingleEnded);
                    value = value / 10.24;
                    value = Math.Round(value);
                    Console.WriteLine(value);
                    Thread.Sleep(500);
                }
            }
        }
Exemplo n.º 12
0
    public static Volume EnableVolume()
    {
        var connection = new SpiConnectionSettings(0, 0);

        connection.ClockFrequency = 1000000;
        connection.Mode           = SpiMode.Mode0;
        var spi     = SpiDevice.Create(connection);
        var mcp3008 = new Mcp3008(spi);
        var volume  = new Volume(mcp3008);

        volume.Init();
        return(volume);
    }
Exemplo n.º 13
0
        public void Read()
        {
            var hardwareSpiSettings = new SpiConnectionSettings(0, 0)
            {
                ClockFrequency = 1000000
            };

            using SpiDevice spi = SpiDevice.Create(hardwareSpiSettings);
            using Mcp3008 mcp   = new Mcp3008(spi);
            while (true)
            {
                double value = mcp.Read(0);
                value = value / 10.24;
                value = Math.Round(value);
                Console.WriteLine($"{value}%");
                Thread.Sleep(500);
            }
        }
Exemplo n.º 14
0
        public void SPI_Mcp3008CanRead()
        {
            using (Mcp3008 adc = CreateAdc())
            {
                // We don't care about specific value for the first 5 channels
                for (int i = 0; i <= 4; i++)
                {
                    Assert.InRange(adc.Read(i), MinAdc, MaxAdc);
                }

                // VCC
                Assert.InRange(adc.Read(5), MaxAdc - 5, MaxAdc);

                // GND
                Assert.InRange(adc.Read(6), MinAdc, MinAdc + 5);

                // Voltage divider with equal resistors (50% VCC)
                AdcValueAround(HalfAdc, adc.Read(7));
            }
        }
Exemplo n.º 15
0
        public void ChangingFrequency_UpdatesDutyCycle()
        {
            // choice of frequencies is not random
            // 9k -> 20k makes sure that 0.5 duty cycle must be updated
            // otherwise it will go out of range in the driver (at least on Linux)

            // 9k is because DACs (or PWM + low pass filter)
            // settling time calculation was done for 10k and 9k was close enough
            // to not change calculations significantly
            using (PwmChannel pwm = CreatePwmChannel(9000))
                using (Mcp3008 adc = CreateAdc())
                {
                    // Let the analog value to settle
                    Thread.Sleep(3);
                    AdcValueAround(HalfAdc, adc.Read(0));

                    pwm.Frequency = 20000;
                    Thread.Sleep(3);
                    AdcValueAround(HalfAdc, adc.Read(0));
                }
        }
Exemplo n.º 16
0
        static void Main(string[] args)
        {
            var hardwareSpiSettings = new SpiConnectionSettings(0, 0)
            {
                ClockFrequency = 1000000
            };

            using (SpiDevice spi = new SoftwareSpi(clk: 6, miso: 23, mosi: 5, cs: 24))
                // For hardware implementation replace it with following
                // using (SpiDevice spi = SpiDevice.Create(hardwareSpiSettings))
                using (Mcp3008 mcp = new Mcp3008(spi))
                {
                    while (true)
                    {
                        double value = mcp.Read(0);
                        value = value / 10.24;
                        value = Math.Round(value);
                        Console.WriteLine($"{value}%");
                        Thread.Sleep(500);
                    }
                }
        }
Exemplo n.º 17
0
 public Volume(Mcp3008 mcp3008)
 {
     _mcp3008 = mcp3008;
 }
Exemplo n.º 18
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");
            var serialNumber = Nusbio.Detect();
            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("Nusbio not detected");
                return;
            }

            using (var nusbio = new Nusbio(serialNumber))
            {
                Cls(nusbio);
                var halfSeconds = new TimeOut(500);
                /*
                    Mcp300X - SPI Config
                    gpio 0 - CLOCK
                    gpio 1 - MOSI
                    gpio 2 - MISO
                    gpio 3 - SELECT
                */
                ad = new Mcp3008(nusbio, 
                    selectGpio: NusbioGpio.Gpio3, mosiGpio:  NusbioGpio.Gpio1, 
                     misoGpio:  NusbioGpio.Gpio2, clockGpio: NusbioGpio.Gpio0);
                ad.Begin();

                var analogTempSensor = new Tmp36AnalogTemperatureSensor(nusbio);
                analogTempSensor.Begin();

                var analogMotionSensor = new AnalogMotionSensor(nusbio, 4);
                analogMotionSensor.Begin();

                var lightSensor      = new AnalogLightSensor(nusbio);
                lightSensor.AddCalibarationValue("Dark", 0, 100);
                lightSensor.AddCalibarationValue("Office Night", 101, 299);
                lightSensor.AddCalibarationValue("Office Day", 300, 400);
                lightSensor.AddCalibarationValue("Outdoor Sun Light", 401, 1000);
                lightSensor.Begin();

                while(nusbio.Loop())
                {
                    if (halfSeconds.IsTimeOut())
                    {
                        const int lightSensorAnalogPort       = 7;
                        const int motionSensorAnalogPort      = 6;
                        const int temperatureSensorAnalogPort = 5;


                        ConsoleEx.WriteLine(0, 2, string.Format("{0,-20}", DateTime.Now, lightSensor.AnalogValue), ConsoleColor.Cyan);

                        lightSensor.SetAnalogValue(ad.Read(lightSensorAnalogPort));
                        ConsoleEx.WriteLine(0, 4, string.Format("Light Sensor      : {0} (ADValue:{1:000.000})", lightSensor.CalibratedValue.PadRight(18), lightSensor.AnalogValue), ConsoleColor.Cyan);

                        analogTempSensor.SetAnalogValue(ad.Read(temperatureSensorAnalogPort));
                        ConsoleEx.WriteLine(0, 6, string.Format("Temperature Sensor: {0:00.00}C, {1:00.00}F     (ADValue:{2:0000})    ",  analogTempSensor.GetTemperature(AnalogTemperatureSensor.TemperatureType.Celsius), analogTempSensor.GetTemperature(AnalogTemperatureSensor.TemperatureType.Fahrenheit), analogTempSensor.AnalogValue), ConsoleColor.Cyan);

                        analogMotionSensor.SetAnalogValue(ad.Read(motionSensorAnalogPort));
                        var motionType = analogMotionSensor.MotionDetected();
                        if (motionType == MotionSensorPIR.MotionDetectedType.MotionDetected || motionType == MotionSensorPIR.MotionDetectedType.None)
                        {
                            ConsoleEx.Write(0, 8, string.Format("Motion Sensor     : {0,-20} (ADValue:{1:000})", motionType, analogMotionSensor.AnalogValue), ConsoleColor.Cyan);
                        }
                    }

                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;

                        if (k == ConsoleKey.C)
                        {
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.Q) {
                            
                            break;
                        }
                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }
Exemplo n.º 19
0
 public FsrWithAdcSample()
 {
     // Create a ADC convertor instance you are using depending how you wired ADC pins to controller
     // in this example used ADC Mcp3008 with software spi method. If you want to do hardware spi, then call SoftwareSpi.Create()
     _adcConvertor = new Mcp3008(new SoftwareSpi(18, 23, 24, 25));
 }
Exemplo n.º 20
0
 public ThermometerService()
 {
     this.mcp      = new Mcp3008(0);
     this.initTask = this.mcp.InitializeAsync();
 }