public virtual IEnumerator<ITask> SetLedHandler(SetLed ledStatus)
        {
            var ledStatusByte = BitConverter.GetBytes((int) ledStatus.Body.Status);

            var ledWrite = new LegoLSWrite
                {
                    Port = _state.SensorPort,
                    TXData = new byte[] {0x10, 0x51, ledStatusByte[0]},
                    ExpectedI2CResponseSize = 0
                };

            Activate(Arbiter.Choice(_legoBrickPort.SendNxtCommand(ledWrite),
                                    x => ledStatus.ResponsePort.Post(new DefaultUpdateResponseType()),
                                    f => ledStatus.ResponsePort.Post(f)));

            yield break;
        }
        public virtual IEnumerator<ITask> GetHandler(Get get)
        {
            var write = new LegoLSWrite
                {
                    Port = _state.SensorPort,
                    TXData = new byte[] {0x10, 0x07},
                    ExpectedI2CResponseSize = 15
                };

            Activate(Arbiter.Choice(_legoBrickPort.SendNxtCommand(write),
                                    EmptyHandler,
                                    f => LogInfo(f.ToException().InnerException + " " + f.ToException().Message)));

            Activate(Arbiter.Receive(false, TimeoutPort(500), EmptyHandler));

            var read = new LegoLSRead(_state.SensorPort);
            Activate(Arbiter.Choice(_legoBrickPort.SendNxtCommand(read),
                                    r =>
                                        {
                                            var response = LegoResponse.Upcast<I2CResponseSensorType>(r);
                                            if (response != null)
                                            {
                                                LogInfo(string.Format("Read response: {0} {1}", response.Manufacturer, response.SensorType));
                                                _state.ManufactureInfo = string.Format("{0}", response.SensorType);
                                                //_state.ManufactureInfo = string.Format("Hiya!");
                                                LogInfo(string.Format(_state.ManufactureInfo));
                                                get.ResponsePort.Post(_state);
                                            }
                                        },
                                    f =>
                                        {
                                            LogInfo(f.Detail.ToString());
                                            get.ResponsePort.Post(f);
                                        }));
            //get.ResponsePort.Post(_state);
            yield break;
        }
        public virtual IEnumerator<ITask> ReadFromI2cAddressHandler(ReadFromI2cAddress readRequest)
        {
            var write = new LegoLSWrite
            {
                Port = _state.SensorPort,
                TXData = readRequest.Body.TxData,
                ExpectedI2CResponseSize = readRequest.Body.ExpectedResponseSize
            };

            Activate(Arbiter.Choice(_legoBrickPort.SendNxtCommand(write),
                                    EmptyHandler,
                                    f => LogInfo(f.ToException().InnerException + " " + f.ToException().Message)));

            Activate(Arbiter.Receive(false, TimeoutPort(80), EmptyHandler));

            var read = new LegoLSRead(_state.SensorPort);
            Activate(Arbiter.Choice(_legoBrickPort.SendNxtCommand(read),
                                    r => readRequest.ResponsePort.Post(new ReadResponse {Bytes = r.CommandData}),
                                    f => readRequest.ResponsePort.Post(f)));
            yield break;
        }
コード例 #4
0
        /// <summary>
        /// Send Any Command, including a two-phase LowSpeed command (LSWrite)
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="priority"></param>
        /// <returns></returns>
        public PortSet<LegoResponse, Fault> SendAnyCommand(LegoCommand cmd, bool priority)
        {
            if (cmd.LegoCommandCode == LegoCommandCode.LSWrite)
            {
                LegoLSWrite cmdLsWrite = cmd as LegoLSWrite;
                if (cmdLsWrite == null)
                    cmdLsWrite = new LegoLSWrite(cmd);

                // Send Low Speed Command
                SendLowSpeedCommand sendCmd = new SendLowSpeedCommand(cmdLsWrite);
                _internalI2CPort.Post(sendCmd);
                return sendCmd.ResponsePort;
            }
            return _brickPort.SendCommand(cmd, priority);
        }