/// <summary>
        /// Plays a tone based on frequency for given duration and at a given volume.
        /// </summary>
        /// <param name="socket">socket for executing command to brick</param>
        /// <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"></exception>
        /// <remarks>
        /// Instruction opSound (CMD, …)
        /// Opcode 0x94
        /// Arguments (Data8) CMD => Specific command parameter documented below
        /// Dispatch status Unchanged
        /// Description Sound control entry
        /// CMD: TONE = 0x01
        /// Arguments
        /// (Data8) VOLUME – Specify volume for playback, [0 - 100]
        /// (Data16) FREQUENCY – Specify frequency, [250 - 10000]
        /// (Data16) DURATION – Specify duration in millisecond
        /// </remarks>
        internal static async Task Tone(ISocket socket, int volume, int frequency, int duration)
        {
            if (volume < 0 || volume > 100)
            {
                throw new ArgumentOutOfRangeException("Volume must be between 0 and 100", "volume");
            }
            if (frequency < 250 || frequency > 10000)
            {
                throw new ArgumentOutOfRangeException("Frequency must be between 250 and 10000", "frequency");
            }
            if (duration < 1)
            {
                throw new ArgumentOutOfRangeException("Duration must be longer than 0", "duration");
            }

            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(CommandType.DIRECT_COMMAND_NO_REPLY))
            {
                cb.OpCode(OP.opSOUND);
                cb.Raw((byte)SOUND_SUBCODE.TONE);
                cb.PAR8(volume);
                cb.PAR16(frequency);
                cb.PAR16(duration);
                cmd = cb.ToCommand();
            }
            await socket.Execute(cmd);
        }
示例#2
0
        /// <summary>
        /// This function enables synchonizing two motors.
        /// Synchonization should be used when motors should run as synchrone as possible,
        /// </summary>
        /// <param name="socket">socket for executing command to brick</param>
        /// <param name="layer">Specify chain layer number [0 - 3]</param>
        /// <param name="ports">Output bit field [0x00 – 0x0F]</param>
        /// <param name="speed">Speed level, [-100 – 100]</param>
        /// <param name="turnRatio">Turn ratio, [-200 - 200]
        /// 0 : Motor will run with same power
        /// 100 : One motor will run with specified power while the other will be close to zero
        /// 200: One motor will run with specified power forward while the other will run in the opposite direction at the same power level.
        /// </param>
        /// <param name="time">Time in milliseconds, 0 = Infinite</param>
        /// <param name="brake">Specify break level, [0: Float, 1: Break]</param>
        /// <remarks>
        /// Instruction opOutput_Time_Sync (LAYER, NOS, SPEED, TURN, STEP, BRAKE)
        /// Opcode 0xB1
        /// Arguments (Data8) LAYER – Specify chain layer number [0 - 3]
        /// (Data8) NOS – Output bit field [0x00 – 0x0F]
        /// (Data8) SPEED – Power level, [-100 – 100]
        /// (Data16) TURN – Turn ratio, [-200 - 200], see documentation below
        /// (Data32) TIME – Time in milliseconds, 0 = Infinite
        /// (Data8) BRAKE - Specify break level [0: Float, 1: Break]
        /// Dispatch status Unchanged
        /// Description This function enables synchonizing two motors. Synchonization should be used when motors should run as synchrone as possible,
        /// </remarks>
        public static async Task TimeSync(ISocket socket, ChainLayer layer, OutputPortFlag ports, int speed, int turnRatio, int time, Brake brake = Brake.Float, bool requireReply = true)
        {
            if (time < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(time), ">=0");
            }
            if (speed < -100 || speed > 100)
            {
                throw new ArgumentOutOfRangeException(nameof(speed), "[-100,100]");
            }
            if (turnRatio < -200 || turnRatio > 200)
            {
                throw new ArgumentOutOfRangeException(nameof(turnRatio), "[-200,200]");
            }

            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(requireReply ? CommandType.DIRECT_COMMAND_REPLY : CommandType.DIRECT_COMMAND_NO_REPLY))
            {
                cb.OpCode(OP.opOUTPUT_TIME_SYNC);
                cb.SHORT((int)layer);
                cb.SHORT((int)ports);
                cb.PAR8(speed);
                cb.PAR16(turnRatio);
                cb.PAR32(time);
                cb.PAR8((byte)brake);
                cmd = cb.ToCommand();
            }
            await socket.Execute(cmd);
        }
        //CMD: BMPFILE = 0x1C
        //Arguments
        //(DATA8) Color – Specify either black or white, [0: White, 1: Black]
        //(Data16) X0 – Specify X start point, [0 - 177]
        //(Data16) Y0 – Specify Y start point, [0 - 127]
        //(Data8) NAME – First character in filename(Character string)
        //Description
        //Enable displaying BMP file from icon file within running project.
        internal static async Task BMPFile(ISocket socket, string path, int x = 0, int y = 0, UIColor color = UIColor.Black)
        {
            Command cmd = null;

            using (CommandBuilder cb = new CommandBuilder(CommandType.DIRECT_COMMAND_NO_REPLY))
            {
                cb.OpCode(OP.opUI_DRAW);
                cb.Raw((byte)UI_DRAW_SUBCODE.BMPFILE);
                cb.PAR8((int)color);
                cb.PAR16(x);
                cb.PAR16(y);
                cb.PARS(path);
                cb.OpCode(OP.opUI_DRAW);
                cb.Raw((byte)UI_DRAW_SUBCODE.UPDATE);
                cmd = cb.ToCommand();
            }
            await socket.Execute(cmd);
        }