Example #1
0
        static void Main()
        {
            const ConnectorPin adcClock = ConnectorPin.P1Pin23;
            const ConnectorPin adcMiso = ConnectorPin.P1Pin21;
            const ConnectorPin adcMosi = ConnectorPin.P1Pin19;
            const ConnectorPin adcCs = ConnectorPin.P1Pin24;

            Console.Clear();

            Console.WriteLine("MCP-3208 Sample: Reading ADC points in all channels");
            Console.WriteLine();
            Console.WriteLine("\tClock: {0}", adcClock);
            Console.WriteLine("\tCS: {0}", adcCs);
            Console.WriteLine("\tMOSI: {0}", adcMosi);
            Console.WriteLine("\tMISO: {0}", adcMiso);
            Console.WriteLine();

            var driver = new GpioConnectionDriver();

            {
                Console.CursorVisible = false;
                var adcConnection = new Mcp3208SpiConnection(
                    driver.Out(adcClock),
                    driver.Out(adcCs),
                    driver.In(adcMiso),
                    driver.Out(adcMosi));

                while (!Console.KeyAvailable)
                {
                    Console.CursorTop = 0;
                    Console.Clear();

                    Mcp3208Channel chan = Mcp3208Channel.Channel0;

                    for (int i = 0; i < 8; i++)
                    {
                        AnalogValue p = adcConnection.Read(chan);
                        decimal points = p.Value;
                        Console.WriteLine(i.ToString() + " ADC points " + points.ToString());
                        using (StreamWriter sw = File.AppendText(".\\prova.txt"))
                        {
                            sw.WriteLine(chan.ToString() + " ADC points " + points.ToString());
                        }
                        chan++; // enum increase sends to the next channel
                    }
                    Thread.Sleep(500);
                }
            }
            Console.CursorTop++;
            Console.CursorVisible = true;
        }
        /// <summary>
        /// Gets the best driver for the specified capabilities.
        /// </summary>
        /// <param name="capabilities">The capabilities.</param>
        /// <returns>The best driver, if found; otherwise, <c>null</c>.</returns>
        public static IGpioConnectionDriver GetBestDriver(GpioConnectionDriverCapabilities capabilities)
        {
            if ((GpioConnectionDriver.GetCapabilities() & capabilities) == capabilities)
            {
                return(new GpioConnectionDriver());
            }
            if ((MemoryGpioConnectionDriver.GetCapabilities() & capabilities) == capabilities)
            {
                return(new MemoryGpioConnectionDriver());
            }
            if ((FileGpioConnectionDriver.GetCapabilities() & capabilities) == capabilities)
            {
                return(new FileGpioConnectionDriver());
            }

            return(null);
        }
Example #3
0
        static void Main(string[] args)
        {
            var pin1 = ConnectorPin.P1Pin22.Input();

            var driver = new GpioConnectionDriver();

            var settings = new GpioConnectionSettings();

            settings.Driver = driver;

            using (var hans = new GpioConnection(settings))
            {
                hans.Add(pin1);

                while (true)
                {
                    Console.WriteLine(settings.Driver.Read(pin1.Pin));
                    Thread.Sleep(100);
                }
            }
        }
Example #4
0
        public static void Main()
        {
            Console.Title = "Raspberry-LED Domotica client";
            if (!Helpers.IsLinux)
            {
                Console.WriteLine("Sorry, almost everything in this script can only run on the Raspberry Pi.");
                Console.WriteLine("Press enter to close the script.");
                Console.Read();
                Environment.Exit(0);
            }
            Console.CancelKeyPress += delegate
            {
                Console.WriteLine("Stopping the program");
                HubConnection.Closed -= StartHubConnection;
                HubConnection.Closed += null;
                HubConnection.Stop();
                Console.WriteLine("Stopped SignalR communication");
                for (var i = 0; i == 32;)
                {
                    string str = $"gpio{i}";
                    if (Directory.Exists(Path.Combine("/sys/class/gpio", str)))
                    {
                        driver.Release((ProcessorPin) i);
                    }
                    i++;
                }
                gpio.Close();
                Console.WriteLine("Stopped driver allocating");
                Thread.Sleep(1000);
                Environment.Exit(0);
            };
            // Connection to the signalr hub
            HubConnection = new HubConnection("http://192.168.1.100");
            RaspberryHub = HubConnection.CreateHubProxy("Raspberry");
            // If the server decides to close the connection we need to start it again
            HubConnection.Closed += StartHubConnection;
            // Starts the connection
            StartHubConnection();

            gpio = new GpioConnection();
            driver = new GpioConnectionDriver();
            I2CDriver = new I2cDriver(ConnectorPin.P1Pin3.ToProcessor(), ConnectorPin.P1Pin5.ToProcessor());
            ArduinoConnection = I2CDriver.Connect(0x04);
            var switchButton = ConnectorPin.P1Pin13.Input().Revert().OnStatusChanged(x =>
            {
                Console.WriteLine(x);
                RaspberryHub.Invoke("SendChangedValue", ConnectorPin.P1Pin37, x ? "On" : "Off");
            });
            var doorSensor = ConnectorPin.P1Pin7.Input().PullUp().OnStatusChanged(x =>
            {
                RaspberryHub.Invoke("SendChangedValue", ConnectorPin.P1Pin7, x ? "Open" : "Closed");
            });
            var motionSensor = ConnectorPin.P1Pin13.Input().OnStatusChanged(x =>
            {
                RaspberryHub.Invoke("SendChangedValue", ConnectorPin.P1Pin11, x ? "Detected" : "Not detected");
                Console.WriteLine( DateTime.Now + ":Motion {0}", x ? "Detected" : "Not detected");
            });
            //            gpio.Add(switchButton);
            //            gpio.Add(doorSensor);
            //            gpio.Add(motionSensor);

            RaspberryHub.On<string>("ChangePiLed", pinnumber =>
            {
                int ledid = int.Parse(pinnumber);
                var procpin = ((ConnectorPin) ledid).ToProcessor();

                string str = string.Format("gpio{0}",procpin.ToString().Replace("Pin0","").Replace("Pin",""));
                if (!Directory.Exists(Path.Combine("/sys/class/gpio", str)))
                {
                    Console.WriteLine($"OutputPin {procpin} is not allocated!\nAllocating now.");
                    driver.Allocate(procpin, PinDirection.Output);
                    Console.WriteLine("Pin allocated");
                }
                driver.Write(procpin, !driver.Read(procpin));
                RaspberryHub.Invoke("SendChangedValue", pinnumber, driver.Read(procpin) ? "On" : "Off");
            });

            RaspberryHub.On<string, string>("SetupConfig", (pinnumber, type) =>
            {
                int pin = int.Parse(pinnumber);
                if (pin > 7)
                {
                    Action<bool> onstatusaction =
                        b =>
                        {
                            RaspberryHub.Invoke("SendChangedValue", pin,
                                driver.Read(((ConnectorPin) pin).ToProcessor()) ? "On" : "Off");
                        };
                    string str = string.Format("gpio{0}", ((ConnectorPin)pin).ToProcessor().ToString().Replace("Pin0", "").Replace("Pin", ""));
                    Console.WriteLine(str);
                    if (!Directory.Exists(Path.Combine("/sys/class/gpio", str)))
                    {
                        Console.WriteLine("Adding button");
                        var button = CreatePinConfig.CreateOutputPinConfiguration((ConnectorPin) pin, onstatusaction, "Button");
                        gpio.Add(button);
                    }
                }
            });

            RaspberryHub.On<int, string>("GetPinStatus", (pin, type) =>
            {
                driver.Read(((ConnectorPin) pin).ToProcessor());
                string status = string.Empty;
                if (type.Equals("Button"))
                {
                    status = driver.Read(((ConnectorPin) pin).ToProcessor()) ? "Pressed" : "Not pressed";
                }
                if (type.Equals("LED"))
                {
                    status = driver.Read(((ConnectorPin) pin).ToProcessor()) ? "On" : "Off";
                }
                if (type.Equals("Door sensor"))
                {
                    status = driver.Read(((ConnectorPin)pin).ToProcessor()) ? "Open" : "Closed";
                }

                RaspberryHub.Invoke("SendChangedValue", pin, status);
            });

            ServerWorkThread objThread = new ServerWorkThread();
            SendToArduino(1);
            while (true)
            {
                Thread.Sleep(50);
                ReadFromArduino();
                //objThread.HandleConnection(objThread.mySocket.Accept());
            }
        }