Beispiel #1
0
        /// <summary>
        /// Plays a tone based on frequency for given duration and at a given volume and at certain interval
        /// </summary>
        /// <param name="volume">Specify volume for playback, [0 - 100]</param>
        /// <param name="frequency">Specify frequency, [250 - 10000</param>
        /// <param name="duration">Specify duration in milliseconds [1-n]</param>
        /// <param name="numberOfLoops">Specify number of loops [1-n] if 1 will play single tone.</param>
        /// <param name="timeOut">Time in milliseconds between tones [1-n]</param>
        /// <exception cref="ArgumentOutOfRangeException">volume, frequency, duration, numberOfLoops or timeOut out of range</exception>
        public async void ToneLoop(int volume, int frequency, int duration, int numberOfLoops, int timeOut)
        {
            if (timeOut < 1)
            {
                throw new ArgumentOutOfRangeException("Time out must > 0 ms", "timeOut");
            }
            if (numberOfLoops < 1)
            {
                throw new ArgumentOutOfRangeException("Amount must be > 0", "numberOfLoops");
            }

            if (numberOfLoops == 1)
            {
                await SoundMethods.Tone(Brick.Socket, volume, frequency, duration);
            }
            else
            {
                int time = timeOut + duration;
                CancellationToken token = Brick.Socket.CancellationToken;
                await Task.Factory.StartNew(
                    async() =>
                {
                    for (int i = 0; i < numberOfLoops; i++)
                    {
                        await SoundMethods.Tone(Brick.Socket, volume, frequency, duration);
                        if (i + 1 < numberOfLoops)
                        {
                            if (token.WaitHandle.WaitOne(time))
                            {
                                break;
                            }
                        }
                    }
                }, token, TaskCreationOptions.LongRunning, TaskScheduler.Default);
            }
        }
Beispiel #2
0
 /// <summary>
 /// Plays a tone based on frequency for given duration and at a given volume
 /// </summary>
 /// <param name="volume">Specify volume for playback, [0 - 100]</param>
 /// <param name="frequency">Specify frequency, [250 - 10000]</param>
 /// <param name="duration">Specify duration in milliseconds [1-n]</param>
 /// <exception cref="ArgumentOutOfRangeException">volume, frequency or duration out of range</exception>
 public async void Tone(int volume, int frequency, int duration)
 {
     await SoundMethods.Tone(Brick.Socket, volume, frequency, duration);
 }
Beispiel #3
0
        /// <summary>
        /// Checks if all devices are properly connected to the brick. False on any device/port error forcing brick to disconnect
        /// If true will beep once for OK, twice for OK and devices found (autoConnectDevices = true), Three times ERROR for device/port error(s)
        /// Test runs on setting PowerUpSelfTest  <see cref="Configuration.PowerUpSelfTestOptions"/>
        /// Brick must be connected.
        /// </summary>
        /// <returns>False on Error, otherwise true</returns>
        private async Task <bool> PowerUpSelfTest()
        {
            //do not stop on error detected to enable logging for all errors.
            bool errorDetected        = false;
            bool autoConnectDevices   = Options.PowerUpSelfTest.AutoConnectDevices;
            bool autoDevicesConnected = false;

            IEnumerable <PortInfo> list = await InputMethods.PortScan(Socket);

            List <PortInfo> devices = new List <PortInfo>();

            devices.AddRange(list);

            for (int layer = 0; layer < 4; layer++)
            {
                for (int portNumber = 0; portNumber < 4; portNumber++)
                {
                    int        index;
                    PortInfo   entry;
                    PortStatus status;
                    string     message;

                    #region Input Ports

                    index = (layer * 4) + portNumber;
                    entry = devices[index];

                    InputPort inputPort = IOPort.Input.Ports[index];

                    message = $"Brick {(ChainLayer)layer} Port {inputPort.Name}";

                    switch (entry.Status)
                    {
                    case PortStatus.OK:
                    {
                        status = inputPort.CheckDevice(entry.Device.Value, Options.PowerUpSelfTest.AutoConnectDevices);
                        switch (status)
                        {
                        case PortStatus.Error:
                        {
                            Logger.LogWarning($"Invalid device {inputPort.Device.Type} {message}");
                            inputPort.Status = PortStatus.Error;
                            errorDetected    = true;
                            break;
                        }

                        case PortStatus.Initializing:
                        {
                            autoDevicesConnected = true;
                            break;
                        }
                        }
                        break;
                    }

                    case PortStatus.Empty:
                    {
                        if (inputPort.Status == PortStatus.OK)
                        {
                            Logger.LogWarning($"Device {inputPort.Device.Type} is not connected {message}");
                            inputPort.Status = PortStatus.Error;
                            //unconnected device!
                            errorDetected = true;
                        }
                        break;
                    }

                    case PortStatus.Error:
                    {
                        Logger.LogWarning($"{message} Device {entry.Device}");
                        inputPort.Status = PortStatus.Error;
                        // Output device connected to InputPort
                        errorDetected = true;
                        break;
                    }

                    default:
                    {
                        Logger.LogWarning($"{message} Device {entry.Device}");
                        inputPort.Status = PortStatus.Error;
                        errorDetected    = true;
                        break;
                    }
                    }

                    if (!errorDetected && inputPort.Status == PortStatus.OK)
                    {
                        await inputPort.InitializeDevice();
                    }


                    #endregion

                    #region Output Ports

                    index = 16 + (layer * 4) + portNumber;
                    entry = devices[index];

                    OutputPort outputPort = IOPort.Output.Ports[index];

                    message = $"Brick {(ChainLayer)layer} Port {outputPort.Name}";

                    switch (entry.Status)
                    {
                    case PortStatus.OK:
                    {
                        status = outputPort.CheckDevice(entry.Device.Value, autoConnectDevices);
                        switch (status)
                        {
                        case PortStatus.Error:
                        {
                            Logger.LogWarning($"Invalid device {outputPort.Device.Type} {message}");
                            outputPort.Status = PortStatus.Error;
                            errorDetected     = true;
                            break;
                        }

                        case PortStatus.Initializing:
                        {
                            autoDevicesConnected = true;
                            break;
                        }
                        }
                        break;
                    }

                    case PortStatus.Empty:
                    {
                        if (outputPort.Status == PortStatus.OK)
                        {
                            Logger.LogWarning($"Device {outputPort.Device.Type} is not connected {message}");
                            outputPort.Status = PortStatus.Error;
                            //unconnected device!
                            errorDetected = true;
                        }
                        break;
                    }

                    case PortStatus.Error:
                    {
                        Logger.LogWarning($"{message} Device {entry.Device}");
                        outputPort.Status = PortStatus.Error;
                        // InputPort device connected to Outputport
                        errorDetected = true;
                        break;
                    }

                    default:
                    {
                        Logger.LogWarning($"{message} Device {entry.Device}");
                        outputPort.Status = PortStatus.Error;
                        errorDetected     = true;
                        break;
                    }
                    }

                    if (!errorDetected && outputPort.Status == PortStatus.OK)
                    {
                        await outputPort.InitializeDevice();
                    }

                    #endregion
                }
            }


            int numberOfLoops = 1; // beep once for OK
            if (errorDetected)
            {
                numberOfLoops = 3;                //beep three times
            }
            else if (autoDevicesConnected)
            {
                numberOfLoops = 2;                            //beep two times devices found, but all is still well :-)
            }
            bool beep = false;
            switch (numberOfLoops)
            {
            case 1:
            {
                beep = Options.PowerUpSelfTest.BeepOnOK;
                break;
            }

            case 2:
            {
                // all is still well so beep once if set anyhow.
                beep = (Options.PowerUpSelfTest.BeepOnAutoConnect) ? true : Options.PowerUpSelfTest.BeepOnOK;
                break;
            }

            case 3:
            {
                beep = Options.PowerUpSelfTest.BeepOnError;
                break;
            }
            }

            if (beep)
            {
                CancellationToken token = Socket.CancellationToken;
                await Task.Run(async() =>
                {
                    for (int i = 0; i < numberOfLoops; i++)
                    {
                        await SoundMethods.Tone(Socket, 50, 850, 150);
                        await Task.Delay(400, token);
                    }
                }, token);
            }

            if (errorDetected && Options.PowerUpSelfTest.DisconnectOnError)
            {
                await Disconnect();
            }

            Logger.LogInformation($"PowerUpSelfTest: {((errorDetected) ? "ERROR" : "OK")}");
            return(!errorDetected);
        }