Пример #1
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);

                const int motionSensorAnalogPort      = 0;
                const int temperatureSensorAnalogPort = 1;
                const int multiButtonPort             = 3;
                const int lightSensorAnalogPort       = 6; // Has a pull down resistor

                var halfSeconds = new TimeOut(200);

                /*
                 *  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, 5);
                analogTempSensor.Begin();

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

                var lightSensor = CalibrateLightSensor(new AnalogLightSensor(nusbio), AnalogLightSensor.LightSensorType.CdsPhotoCell_3mm_45k_140k);
                lightSensor.Begin();

                AnalogSensor multiButton = null;
                //multiButton = new AnalogSensor(nusbio, multiButtonPort);
                //multiButton.Begin();

                ulong loopCounter = 0;

                while (nusbio.Loop())
                {
                    if (halfSeconds.IsTimeOut())
                    {
                        ConsoleEx.WriteLine(0, 2, string.Format("{0,-20}, {1}", DateTime.Now, loopCounter++), ConsoleColor.Cyan);

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

                        // There is an issue with the TMP36 and the ADC MCP3008
                        // The correction is to add a 2k resistors between the out of the temperature sensor
                        // and the AD port and increase the value by 14%
                        const int TMP36_AccuracyCorrection = 14;
                        analogTempSensor.SetAnalogValue(ad.Read(temperatureSensorAnalogPort, TMP36_AccuracyCorrection));
                        ConsoleEx.WriteLine(0, 6, string.Format("Temperature Sensor : {0:00.00}C, {1:00.00}F     (ADValue:{2:0000}, Volt:{3:000.000})      ",
                                                                analogTempSensor.GetTemperature(AnalogTemperatureSensor.TemperatureType.Celsius),
                                                                analogTempSensor.GetTemperature(AnalogTemperatureSensor.TemperatureType.Fahrenheit),
                                                                analogTempSensor.AnalogValue,
                                                                analogTempSensor.Voltage), ConsoleColor.Cyan);

                        analogMotionSensor.SetAnalogValue(ad.Read(motionSensorAnalogPort));
                        var motionType = analogMotionSensor.MotionDetected();
                        if (motionType == DigitalMotionSensorPIR.MotionDetectedType.MotionDetected ||
                            motionType == DigitalMotionSensorPIR.MotionDetectedType.None ||
                            motionType == DigitalMotionSensorPIR.MotionDetectedType.SameMotionDetected)
                        {
                            ConsoleEx.Write(0, 8, string.Format("Motion Sensor      : {0,-18} (ADValue:{1:000.000}, Volt:{2:000.000})", motionType, analogMotionSensor.AnalogValue, analogMotionSensor.Voltage), ConsoleColor.Cyan);
                        }

                        if (multiButton != null)
                        {
                            multiButton.SetAnalogValue(ad.Read(multiButtonPort));
                            ConsoleEx.Write(0, 10, string.Format("Multi Button       : {0,-18} (ADValue:{1:000.000}, Volt:{2:000.000})",
                                                                 multiButton.AnalogValue > 2 ? "Down" : "Up  ",
                                                                 multiButton.AnalogValue,
                                                                 multiButton.Voltage), 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();
        }
Пример #2
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;
            }

            var lightSensorAnalogPort  = 2;
            var motionSensorAnalogPort = 0;
            var buttonSensorAnalogPort = 1;
            var ledGpio = NusbioGpio.Gpio5;

            using (var nusbio = new Nusbio(serialNumber))
            {
                Cls(nusbio);

                var halfSeconds = new TimeOut(333);

                // Mcp300X Analog To Digital - SPI Config
                ad = new MCP3008(nusbio,
                                 selectGpio: NusbioGpio.Gpio3,
                                 mosiGpio:   NusbioGpio.Gpio1,
                                 misoGpio:   NusbioGpio.Gpio2,
                                 clockGpio:  NusbioGpio.Gpio0);
                ad.Begin();

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

                var button = new AnalogButton(nusbio);

                var lightSensor = CalibrateLightSensor(new AnalogLightSensor(nusbio), AnalogLightSensor.LightSensorType.CdsPhotoCell_3mm_45k_140k);
                lightSensor.Begin();

                // Analog Port 5, 6, 7 are only available in
                // Analog Extension PCBv2
                const int    multiButtonPort = 5;
                AnalogSensor multiButton     = null;
                //multiButton = new AnalogSensor(nusbio, multiButtonPort);
                //multiButton.Begin();

                // TC77 Temperature Sensor SPI
                var tc77 = new TC77(nusbio,
                                    clockGpio:  NusbioGpio.Gpio0,
                                    mosiGpio:   NusbioGpio.Gpio1,
                                    misoGpio:   NusbioGpio.Gpio2,
                                    selectGpio: NusbioGpio.Gpio4
                                    );
                tc77.Begin();

                if (nusbio.Type == NusbioType.NusbioType1_Light)
                {
                    tc77._spi.SoftwareBitBangingMode     = true;
                    ad._spiEngine.SoftwareBitBangingMode = true;
                }

                while (nusbio.Loop())
                {
                    if (halfSeconds.IsTimeOut())
                    {
                        nusbio[ledGpio].AsLed.ReverseSet();

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

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

                        analogMotionSensor.SetAnalogValue(ad.Read(motionSensorAnalogPort));
                        var motionType = analogMotionSensor.MotionDetected();
                        if (motionType == DigitalMotionSensorPIR.MotionDetectedType.MotionDetected || motionType == DigitalMotionSensorPIR.MotionDetectedType.None)
                        {
                            ConsoleEx.Write(0, 6, string.Format("Motion Sensor      : {0,-18} (ADValue:{1:000.000}, Volt:{2:0.00})    ",
                                                                motionType, analogMotionSensor.AnalogValue, analogMotionSensor.Voltage), ConsoleColor.Cyan);
                        }

                        ConsoleEx.WriteLine(0, 8, string.Format("Temperature Sensor : {0:0.00}C {1:0.00}F    ", tc77.GetTemperature(),
                                                                tc77.GetTemperature(AnalogTemperatureSensor.TemperatureType.Fahrenheit)), ConsoleColor.Cyan);

                        button.SetAnalogValue(ad.Read(buttonSensorAnalogPort));
                        ConsoleEx.WriteLine(0, 10, string.Format("Button             : {0,-18} [{1:0000}, {2:0.00}V]   ",
                                                                 button.Down ? "Down" : "Up", button.AnalogValue, button.Voltage), ConsoleColor.Cyan);

                        if (multiButton != null)
                        {
                            multiButton.SetAnalogValue(ad.Read(multiButtonPort));
                            ConsoleEx.Write(0, 12, string.Format("Multi Button       : {0,-18} (ADValue:{1:000.000}, Volt:{2:000.000})",
                                                                 multiButton.AnalogValue > 2 ? "Down" : "Up  ",
                                                                 multiButton.AnalogValue,
                                                                 multiButton.Voltage), 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();
        }
Пример #3
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();
        }