public static AnalogLightSensor CalibrateLightSensor(AnalogLightSensor lightSensor, AnalogLightSensor.LightSensorType type) { switch (type) { case AnalogLightSensor.LightSensorType.CdsPhotoCell_3mm_45k_140k: lightSensor.AddCalibarationValue("Dark", 000, 025); lightSensor.AddCalibarationValue("Office Night", 026, 050); lightSensor.AddCalibarationValue("Office Day", 051, 085); lightSensor.AddCalibarationValue("Outdoor Sun Light", 086, 1024); break; case AnalogLightSensor.LightSensorType.Unknown: case AnalogLightSensor.LightSensorType.CdsPhotoCell_5mm_5k_200k: lightSensor.AddCalibarationValue("Dark", 0, 100); lightSensor.AddCalibarationValue("Office Night", 101, 299); lightSensor.AddCalibarationValue("Office Day", 300, 400); lightSensor.AddCalibarationValue("Outdoor Sun Light", 401, 1024); break; } return(lightSensor); }
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(); }