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 tc77 = new TC77(nusbio, NusbioGpio.Gpio0, NusbioGpio.Gpio1, NusbioGpio.Gpio2, NusbioGpio.Gpio4 ); var secondTimeout = new TimeOut(1000); while (nusbio.Loop()) { if (secondTimeout.IsTimeOut()) { ConsoleEx.WriteLine(0, 3, string.Format("{0:0.00}C {1:0.00}F", tc77.GetTemperature(), tc77.GetTemperature(AnalogTemperatureSensor.TemperatureType.Fahrenheit)) , ConsoleColor.DarkCyan ); } if (Console.KeyAvailable) { var k = Console.ReadKey(true).Key; if (k == ConsoleKey.W) { } if (k == ConsoleKey.Q) { break; } Cls(nusbio); } } } Console.Clear(); }
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(); }