コード例 #1
0
ファイル: Program.cs プロジェクト: spacepope/PiFaceSharp
        static void Main(string[] args)
        {
            var settings = Properties.Settings.Default;

            // initialise the PiFace device to bind the server to
            var device = default(IPiFaceDevice);

            switch (settings.DeviceType)
            {
            case "Physical":
                device = new PiFaceDevice();
                break;

            case "Emulated":
                device = new PiFaceEmulator();
                break;

            default:
                throw new System.Configuration.ConfigurationErrorsException("The 'DeviceType' setting in app.config must be either 'Physical' or 'Emulated'.");
            }

            // get the address to run the server on
            var address = default(IPAddress);

            if (string.IsNullOrEmpty(settings.ServerAddress))
            {
                address = PiFaceTcpHelper.GetLocalIPAddress();
            }
            else
            {
                address = IPAddress.Parse(settings.ServerAddress);
            }

            // get the port to run the server on
            var port = (uint)0;

            if (!uint.TryParse(settings.ServerPort, out port))
            {
                throw new System.Configuration.ConfigurationErrorsException("The 'ServerPort' setting in app.config must be an unsigned integer.");
            }

            // start the server
            var endpoint = new IPEndPoint(address, (int)port);
            var server   = new PiFaceTcpServer(device, endpoint);

            server.MessageReceived += Program.PiFaceTcpServer_MessageReceived;
            server.ResponseSent    += Program.PiFaceTcpServer_ResponseSent;
            server.Start();

            // wait around while the server runs
            while (true)
            {
                System.Threading.Thread.Sleep(1000);
            }
        }
コード例 #2
0
        static void Main(string[] args)
        {
            // get reference to default piface device
            var piface = new PiFaceEmulator();

            // toggle each output pin on and off 50 times
            for (byte pin = 0; pin < 8; pin++)
            {
                Console.WriteLine("toggling input pin {0}", pin);
                var state = true;
                for (var i = 0; i < 50; i++)
                {
                    piface.SetOutputPinState(pin, state);
                    System.Threading.Thread.Sleep(25);
                    state = !state;
                }
            }

            // read the current state of each input pin
            for (byte pin = 0; pin < 8; pin++)
            {
                Console.WriteLine("input pin {0} = {1}", pin, piface.GetInputPinState(pin));
            }
        }