예제 #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;
            }

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

                /*
                 *  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();
                analogTempSensor.ReferenceVoltage = 5.05; // If you can measure your voltage out of the Nusbio VCC

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

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

                lightSensor.Begin();

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

                        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}, Volt:{2:000.000})    ",
                                                                lightSensor.CalibratedValue.PadRight(18),
                                                                lightSensor.AnalogValue,
                                                                lightSensor.Voltage), 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}, 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)
                        //{
                        //    ConsoleEx.Write(0, 8, string.Format("Motion Sensor     : {0,-20} (ADValue:{1:000.000}, Volt:{2:000.000})", motionType, analogMotionSensor.AnalogValue, analogMotionSensor.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();
        }