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

            var clockPin   = NusbioGpio.None;
            var dataOutPin = NusbioGpio.None;
            var useAdafruitI2CAdapterForNusbio = false;

            if (useAdafruitI2CAdapterForNusbio)
            {
                clockPin   = NusbioGpio.Gpio1; // Clock should be zero, but on the Adafruit MCP9808 SCL and SDA are inversed compared to the Adafruit LED matrix
                dataOutPin = NusbioGpio.Gpio0;
            }
            else
            {
                clockPin   = NusbioGpio.Gpio0;
                dataOutPin = NusbioGpio.Gpio1;
            }

            using (var nusbio = new Nusbio(serialNumber))
            {
                _MCP9808_TemperatureSensor = new MCP9808_TemperatureSensor(nusbio, dataOutPin, clockPin);
                if (!_MCP9808_TemperatureSensor.Begin())
                {
                    Console.WriteLine("MCP9808 not detected on I2C bus. Hit any key to retry");
                    var kk = Console.ReadKey();
                    if (!_MCP9808_TemperatureSensor.Begin())
                    {
                        return;
                    }
                }

                Cls(nusbio);

                var everySecond = new TimeOut(1000);

                while (nusbio.Loop())
                {
                    if (everySecond.IsTimeOut())
                    {
                        double celsius = 0;
                        for (var i = 0; i < 3; i++)
                        {
                            celsius = _MCP9808_TemperatureSensor.GetTemperature(TemperatureType.Celsius);
                        }

                        ConsoleEx.WriteLine(1, 2,
                                            string.Format("Temperature Celsius:{0:000.00}, Fahrenheit:{1:000.00}, Kelvin:{2:00000.00}",
                                                          celsius,
                                                          _MCP9808_TemperatureSensor.CelsiusToFahrenheit(celsius),
                                                          _MCP9808_TemperatureSensor.CelsiusToKelvin(celsius)
                                                          ), ConsoleColor.Cyan);
                    }
                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;
                        if (k == ConsoleKey.T)
                        {
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.D0)
                        {
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.C)
                        {
                            Cls(nusbio);
                        }
                        if (k == ConsoleKey.Q)
                        {
                            break;
                        }
                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }
예제 #2
0
        public static void ComboWith6LedPanel(Nusbio nusbio, APA102LEDStrip ledStrip0, APA102LEDStrip ledStrip1 = null)
        {
            Console.Clear();
            ConsoleEx.TitleBar(0, "Rainbow Demo", ConsoleColor.White, ConsoleColor.DarkBlue);
            ConsoleEx.WriteMenu(-1, 6, "Q)uit");

            int wait       = 50;
            var quit       = false;
            var maxStep    = 256;
            var brightness = 5;
            int jStep      = 8;

            ledStrip0.AllOff();
            ledStrip1.AllOff();

            var ledIndex        = -1;
            var maxLedIndex     = 4;
            var ledIndexTimeOut = new TimeOut(wait * 50);

            while (!quit)
            {
                var j2 = maxStep;
                for (var j = 0; j < maxStep; j += jStep)
                {
                    ledStrip0.Reset();
                    ledStrip1.Reset();

                    for (var i = 0; i < ledStrip0.MaxLed; i++)
                    {
                        ledStrip0.AddRGBSequence(false, brightness, RGBHelper.Wheel(((i * maxStep / ledStrip0.MaxLed) + j)));
                    }

                    for (var i = 0; i < ledStrip1.MaxLed; i++)
                    {
                        ledStrip1.AddRGBSequence(false, brightness, RGBHelper.Wheel(((i * maxStep / ledStrip0.MaxLed) + j2)));
                    }

                    foreach (var bkColor in ledStrip0.LedColors)
                    {
                        ConsoleEx.WriteLine(1, 2, String.Format("Strip 0 - Color:{0}, Html:{1}, Dec:{2}", bkColor.Name, APA102LEDStrip.ToHexValue(bkColor), APA102LEDStrip.ToDecValue(bkColor)), ConsoleColor.DarkCyan);
                    }
                    foreach (var bkColor in ledStrip1.LedColors)
                    {
                        ConsoleEx.WriteLine(1, 3, String.Format("Strip 1 - Color:{0}, Html:{1}, Dec:{2}", bkColor.Name, APA102LEDStrip.ToHexValue(bkColor), APA102LEDStrip.ToDecValue(bkColor)), ConsoleColor.DarkCyan);
                    }
                    j2 -= jStep;
                    if (j2 < 0)
                    {
                        j2 = maxStep;
                    }

                    ledStrip0.Show();
                    ledStrip1.Show().Wait(wait);

                    /*
                     * // Turn on and off led connected to gpio 0,1,2,3
                     * if (ledIndexTimeOut.IsTimeOut())
                     * {
                     *  if (ledIndex != -1)
                     *      nusbio[ledIndex].Low();
                     *  ledIndex++;
                     *  if (ledIndex == maxLedIndex)
                     *      ledIndex = 0;
                     *  nusbio[ledIndex].High();
                     * }
                     */

                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;
                        if (k == ConsoleKey.Q)
                        {
                            quit = true;
                            break;
                        }
                    }
                }
            }
            ledStrip0.AllOff();
            ledStrip1.AllOff();
        }
예제 #3
0
        public static void Run(string[] args)
        {
            Console.WriteLine("Nusbio initialization");
            var serialNumber = Nusbio.Detect();

            //var serialNumber = "LD2Ub9pAg";
            if (serialNumber == null) // Detect the first Nusbio available
            {
                Console.WriteLine("nusbio not detected");
                return;
            }

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

                var _4DIGITS7SEGMENTS_ADDR = 0x70 + 2;
                var clockPin   = NusbioGpio.Gpio0;             // White
                var dataOutPin = NusbioGpio.Gpio1;             // Green
                _4digits = new _4Digits7Segments(nusbio, dataOutPin, clockPin);
                _4digits.Begin(_4DIGITS7SEGMENTS_ADDR);
                _4digits.Clear(true);

                var oneSecondTimeOut = new TimeOut(1000);

                while (nusbio.Loop())
                {
                    if (oneSecondTimeOut.IsTimeOut())  // Make the colon blink at a 1 second rate

                    {
                        var t = DateTime.Now;

                        _4digits.Clear();
                        _4digits.Write(string.Format("{0}{1}", t.Minute.ToString("00"), t.Second.ToString("00")));

                        //if(twoSecondTimeOut.Counter % 2 == 0)
                        //    _4digits.Write(string.Format("{0}{1}", t.Minute.ToString("00"), t.Second.ToString("00")));
                        //else
                        //    _4digits.Write(string.Format("{0}{1}", t.Hour.ToString("00"), t.Minute.ToString("00")));

                        _4digits.WriteDisplay();
                        _4digits.DrawColon(!_4digits.ColonOn);
                    }

                    if (Console.KeyAvailable)
                    {
                        var k = Console.ReadKey(true).Key;
                        if (k == ConsoleKey.D1)
                        {
                            Demo1();
                        }
                        if (k == ConsoleKey.D2)
                        {
                            Demo1To100();
                        }
                        if (k == ConsoleKey.D3)
                        {
                            DemoScrollNumber();
                        }
                        if (k == ConsoleKey.D4)
                        {
                            Demo1To10000();
                        }
                        if (k == ConsoleKey.C)
                        {
                            Cls(nusbio);
                            _4digits.Clear(true);
                        }
                        if (k == ConsoleKey.Q)
                        {
                            _4digits.Clear(true);
                            break;
                        }
                        Cls(nusbio);
                    }
                }
            }
            Console.Clear();
        }
예제 #4
0
        private static void FadeInOutLEDWithGpioAnd100mfCapacitorResistorTransistor(Nusbio nusbio, NusbioGpio regLedGpio, NusbioGpio blueLedGpio)
        {
            Console.Clear();
            ConsoleEx.TitleBar(0, "Fade In Out LED With Gpio With Capacitor/Resistor/Transistor");
            ConsoleEx.WriteMenu(-1, 4, "Q)uit");

            const int wait                     = 100;
            const int redLedChargeTime         = wait * 8;
            const int redLedDischargeTime      = wait * 46;
            const int blueLedChargeTime        = wait * 12; // 8
            const int blueLedDischargeTime     = wait * 60; // 42
            const int capacitorPreChargingTime = wait * 24;

            ConsoleEx.WriteLine(1, 7, string.Format("Pre charging capacitors for {0} ms-seconds ", capacitorPreChargingTime), ConsoleColor.Yellow);
            nusbio[regLedGpio].High();
            nusbio[blueLedGpio].High();
            Thread.Sleep(capacitorPreChargingTime);
            ConsoleEx.WriteLine(1, 7, "".PadRight(64), ConsoleColor.Cyan);

            var redLedTimeout  = new MadeInTheUSB.TimeOut(redLedChargeTime);
            var blueLedTimeout = new MadeInTheUSB.TimeOut(blueLedDischargeTime);

            // Turn blue LED off, red led stay on
            nusbio[blueLedGpio].DigitalWrite(PinState.Low);

            while (true)
            {
                if (blueLedTimeout.IsTimeOut())
                {
                    if (blueLedTimeout.Duration == blueLedChargeTime)
                    {
                        ConsoleEx.WriteLine(1, 7, "Blue Off".PadRight(64), ConsoleColor.Cyan);
                        nusbio[blueLedGpio].Low();
                        blueLedTimeout = new TimeOut(blueLedDischargeTime);
                    }
                    else
                    {
                        ConsoleEx.WriteLine(1, 7, "Blue On ".PadRight(64), ConsoleColor.Cyan);
                        nusbio[blueLedGpio].High();
                        blueLedTimeout = new TimeOut(blueLedChargeTime);
                    }
                }
                if (redLedTimeout.IsTimeOut())
                {
                    if (redLedTimeout.Duration == redLedChargeTime)
                    {
                        ConsoleEx.WriteLine(1, 8, "Red  Off".PadRight(64), ConsoleColor.Cyan);
                        nusbio[regLedGpio].Low();
                        redLedTimeout = new TimeOut(redLedDischargeTime);
                    }
                    else
                    {
                        ConsoleEx.WriteLine(1, 8, "Red  On ".PadRight(64), ConsoleColor.Cyan);
                        nusbio[regLedGpio].High();
                        redLedTimeout = new TimeOut(redLedChargeTime);
                    }
                }
                if (Console.KeyAvailable)
                {
                    if (Console.ReadKey().Key == ConsoleKey.Q)
                    {
                        break;
                    }
                }
                Thread.Sleep(wait);
            }
            nusbio[regLedGpio].DigitalWrite(PinState.Low);
            nusbio[blueLedGpio].DigitalWrite(PinState.Low);
        }