private void setPulseWidthInClocks(IOIOProtocolOutgoing outBound, float pulseWidthInClocks) { if (pulseWidthInClocks > this.CalculatedPeriod_) { pulseWidthInClocks = this.CalculatedPeriod_; } int pulseWidth; int fraction; pulseWidthInClocks -= 1; // period parameter is one less than the actual period length // yes, there is 0 and then 2 (no 1) - this is not a bug, that // is how the hardware PWM module works. if (pulseWidthInClocks < 1) { pulseWidth = 0; fraction = 0; } else { pulseWidth = (int)pulseWidthInClocks; fraction = ((int)pulseWidthInClocks * 4) & 0x03; } outBound.setPwmDutyCycle(this.PwmDef.PwmNumber, pulseWidth, fraction); }
public void CreateDigitalOutputTo_ToggleLED() { IOIOConnection ourConn = this.CreateGoodSerialConnection(); this.CreateCaptureLogHandlerSet(); // add our handlers on to p of default so we don't have to grovel around in the internal variables IOIOProtocolIncoming fooIn = new IOIOProtocolIncoming(ourConn.GetInputStream(), HandlerObservable_); IOIOProtocolOutgoing fooOut = new IOIOProtocolOutgoing(ourConn.GetOutputStream()); System.Threading.Thread.Sleep(100); // wait for us to get the hardware ids DigitalOutputSpec ledSpec = new DigitalOutputSpec(Spec.LED_PIN); IDigitalOutputConfigureCommand commandSetup = new DigitalOutputConfigureCommand( ledSpec, false); IDigitalOutputValueSetCommand commandOn = new DigitalOutputSetValueCommand(ledSpec, true); IDigitalOutputValueSetCommand commandOff = new DigitalOutputSetValueCommand(ledSpec, false); //// TODO should use the hardware from the captured connection IResourceManager rManager = new ResourceManager(Hardware.IOIO0003); commandSetup.Alloc(rManager); commandOn.Alloc(rManager); commandOff.Alloc(rManager); commandSetup.ExecuteMessage(fooOut); for (int i = 0; i < 8; i++) { System.Threading.Thread.Sleep(200); commandOn.ExecuteMessage(fooOut); System.Threading.Thread.Sleep(200); commandOff.ExecuteMessage(fooOut); } Assert.IsTrue(true, "there is no status to check"); }
public void UartTest_LoopbackOut31In32() { //// TODO should use the hardware from the captured connection IResourceManager rManager = new ResourceManager(Hardware.IOIO0003); IOIOConnection ourConn = this.CreateGoodSerialConnection(); this.CreateCaptureLogHandlerSet(); ObserverTxStatusUart bufferObserver = new ObserverTxStatusUart(); this.HandlerObservable_.Subscribe(bufferObserver); // add our own handler so we don't have to grovel aroudn in there IOIOProtocolIncoming fooIn = new IOIOProtocolIncoming(ourConn.GetInputStream(), HandlerObservable_); IOIOProtocolOutgoing fooOut = new IOIOProtocolOutgoing(ourConn.GetOutputStream()); System.Threading.Thread.Sleep(100); // wait for us to get the hardware ids UartConfigureCommand commandCreate = new UartConfigureCommand( new DigitalInputSpec(32), new DigitalOutputSpec(31), 38400, UartParity.NONE, UartStopBits.ONE); commandCreate.Alloc(rManager); commandCreate.ExecuteMessage(fooOut); System.Threading.Thread.Sleep(10); string helloWorld = "Hello World"; byte[] helloWorldBytes = System.Text.Encoding.ASCII.GetBytes(helloWorld); LOG.Debug("Sending Hello World"); UartSendDataCommand commandSend = new UartSendDataCommand(commandCreate.UartDef, helloWorldBytes); commandSend.Alloc(rManager); commandSend.ExecuteMessage(fooOut); System.Threading.Thread.Sleep(50); LOG.Debug("Closing Uart"); UartCloseCommand commandClose = new UartCloseCommand(commandCreate.UartDef); commandClose.Alloc(rManager); commandClose.ExecuteMessage(fooOut); System.Threading.Thread.Sleep(50); // IUartFrom is the parent interface for all messages coming from the UARt Assert.AreEqual(1 + 1 + helloWorldBytes.Count() + 1, this.CapturedSingleQueueAllType_.OfType <IUartFrom>().Count()); Assert.AreEqual(1, this.CapturedSingleQueueAllType_.OfType <IUartOpenFrom>().Count(), "didn't get IUartOpenFrom"); Assert.AreEqual(1, this.CapturedSingleQueueAllType_.OfType <IUartReportTxStatusFrom>().Count(), "didn't get IUartReportTXStatusFrom"); IEnumerable <IUartDataFrom> readValues = this.CapturedSingleQueueAllType_.OfType <IUartDataFrom>(); Assert.AreEqual(helloWorldBytes.Count(), readValues.Count(), "Didn't find the number of expected IUartFrom: " + readValues.Count()); // logging the messages with any other string doesn't show the messages themselves !? LOG.Debug("Captured " + +this.CapturedSingleQueueAllType_.Count()); LOG.Debug(this.CapturedSingleQueueAllType_.GetEnumerator()); Assert.AreEqual(1, this.CapturedSingleQueueAllType_.OfType <IUartCloseFrom>().Count()); // should verify close command in the resource }
public void IOIOProtocolOutgoing_CheckInterfaceVersionWrites() { this.CreateCaptureLogHandlerSet(); MemoryStream fakeStream = new MemoryStream(); IOIOProtocolOutgoing fooOut = new IOIOProtocolOutgoing(fakeStream); fooOut.checkInterfaceVersion(); Assert.AreEqual(9, fakeStream.Length, "expected to send 8 characters when checking interface version"); // could seek to 0 and read the byte from the buffer but this is easier byte[] streamBuffer = fakeStream.ToArray(); Assert.AreEqual((int)IOIOProtocolCommands.CHECK_INTERFACE, streamBuffer[0]); }
public void IOIOProtocolOutgoing_CheckInterfaceVersion() { IOIOConnection ourConn = this.CreateGoodSerialConnection(); this.CreateCaptureLogHandlerSet(); LOG.Debug("Setup Complete"); IOIOProtocolIncoming fooIn = new IOIOProtocolIncoming(ourConn.GetInputStream(), HandlerObservable_); IOIOProtocolOutgoing fooOut = new IOIOProtocolOutgoing(ourConn.GetOutputStream()); System.Threading.Thread.Sleep(100); // wait for us to get the hardware ids fooOut.checkInterfaceVersion(); // wait for reply System.Threading.Thread.Sleep(2000); Assert.IsTrue(CapturedConnectionState_.Supported_.IsSupported, " the HandlerContainer_ returned not supported interface."); }
public void ServoTest_SimpleServoTest() { //// TODO should use the hardware from the captured connection IResourceManager rManager = new ResourceManager(Hardware.IOIO0003); IOIOConnection ourConn = this.CreateGoodSerialConnection(); this.CreateCaptureLogHandlerSet(); IOIOProtocolIncoming fooIn = new IOIOProtocolIncoming(ourConn.GetInputStream(), HandlerObservable_); IOIOProtocolOutgoing fooOut = new IOIOProtocolOutgoing(ourConn.GetOutputStream()); System.Threading.Thread.Sleep(20); // wait for us to get the hardware ids int pwmFrequency = 100; DigitalOutputSpec pwmPinSpec = new DigitalOutputSpec(3, DigitalOutputSpecMode.NORMAL); // configure for servo PwmOutputConfigureCommand commandCreatePWM = new PwmOutputConfigureCommand(pwmPinSpec, pwmFrequency); commandCreatePWM.Alloc(rManager); commandCreatePWM.ExecuteMessage(fooOut); System.Threading.Thread.Sleep(100); for (int i = 0; i < 4; i++) { // change it after settling PwmOutputUpdateCommand lowValue = new PwmOutputUpdatePulseWidthCommand(commandCreatePWM.PwmDef, 600); lowValue.Alloc(rManager); lowValue.ExecuteMessage(fooOut); System.Threading.Thread.Sleep(500); // change it after settling PwmOutputUpdateCommand highValue = new PwmOutputUpdatePulseWidthCommand(commandCreatePWM.PwmDef, 2000); highValue.Alloc(rManager); highValue.ExecuteMessage(fooOut); System.Threading.Thread.Sleep(500); } PwmOutputCloseCommand commandReleasePwm = new PwmOutputCloseCommand(commandCreatePWM.PwmDef); commandReleasePwm.Alloc(rManager); commandReleasePwm.ExecuteMessage(fooOut); System.Threading.Thread.Sleep(500); //IEnumerable<IReportAnalogPinValuesFrom> readValues = this.HandlerSingleQueueAllType_.CapturedMessages_ // .OfType<IReportAnalogPinValuesFrom>(); //Assert.IsTrue(readValues.Count() >= 1, "Didn't find the number of expected IReportAnalogPinValuesFrom: " + readValues.Count()); // logging the messages with any other string doesn't show the messages themselves !? LOG.Debug("Captured " + +this.CapturedSingleQueueAllType_.Count()); LOG.Debug(this.CapturedSingleQueueAllType_.GetEnumerator()); // should verify close command }
public void CreateAnalogInputOutputTo_AnalogLoopbackOut31In32() { //// TODO should use the hardware from the captured connection IResourceManager rManager = new ResourceManager(Hardware.IOIO0003); IOIOConnection ourConn = this.CreateGoodSerialConnection(); this.CreateCaptureLogHandlerSet(); IOIOProtocolIncoming fooIn = new IOIOProtocolIncoming(ourConn.GetInputStream(), HandlerObservable_); IOIOProtocolOutgoing fooOut = new IOIOProtocolOutgoing(ourConn.GetOutputStream()); System.Threading.Thread.Sleep(100); // wait for us to get the hardware ids AnalogInputConfigureCommand commandCreateIn = new AnalogInputConfigureCommand(31, true); commandCreateIn.Alloc(rManager); commandCreateIn.ExecuteMessage(fooOut); System.Threading.Thread.Sleep(10); DigitalOutputSpec pwmPinSpec = new DigitalOutputSpec(32, DigitalOutputSpecMode.NORMAL); // set analog "voltage" PwmOutputConfigureCommand commandCreatePWM = new PwmOutputConfigureCommand(pwmPinSpec, 1000, 0.3f); commandCreatePWM.Alloc(rManager); commandCreatePWM.ExecuteMessage(fooOut); System.Threading.Thread.Sleep(100); // change it after settling PwmOutputUpdateCommand commandChangePWM = new PwmOutputUpdateDutyCycleCommand(commandCreatePWM.PwmDef, 0.7f); commandChangePWM.Alloc(rManager); commandChangePWM.ExecuteMessage(fooOut); System.Threading.Thread.Sleep(100); PwmOutputCloseCommand commandReleasePwm = new PwmOutputCloseCommand(commandCreatePWM.PwmDef); commandReleasePwm.Alloc(rManager); commandReleasePwm.ExecuteMessage(fooOut); System.Threading.Thread.Sleep(50); IEnumerable <IReportAnalogPinValuesFrom> readValues = this.CapturedSingleQueueAllType_ .OfType <IReportAnalogPinValuesFrom>(); Assert.IsTrue(readValues.Count() >= 1, "Didn't find the number of expected IReportAnalogPinValuesFrom: " + readValues.Count()); // logging the messages with any other string doesn't show the messages themselves !? LOG.Debug("Captured " + this.CapturedSingleQueueAllType_.Count()); LOG.Debug(this.CapturedSingleQueueAllType_.GetEnumerator()); // should verify close command }
public virtual bool ExecuteMessage(IOIOProtocolOutgoing outBound) { // calculate the period and scale even if not setting frequency because needed by duty cycle CalculatePeriodAndScale(this.RequestedFrequency); if (this.RequestedFrequency != PwmDef.Frequency) { outBound.setPwmPeriod(this.PwmDef.PwmNumber, this.CalculatedPeriod_, this.CalculatedScale_); // update the Pwm spec to show the current settings this.PwmDef = new PwmOutputSpec(this.PwmDef.PinSpec, this.PwmDef.PwmNumber, this.RequestedFrequency); } if (!float.IsNaN(this.DutyCycle)) { setPulseWidthInClocks(outBound, this.CalculatedPeriod_ * this.DutyCycle); } if (!float.IsNaN(this.PulseWidthUSec)) { setPulseWidthInClocks(outBound, PulseWidthUSec / BaseUSec_); } return(true); }
public void IOIOProtocolOutgoing_ToggleLED() { IOIOConnection ourConn = this.CreateGoodSerialConnection(); this.CreateCaptureLogHandlerSet(); LOG.Debug("Setup Complete"); IOIOProtocolIncoming fooIn = new IOIOProtocolIncoming(ourConn.GetInputStream(), HandlerObservable_); IOIOProtocolOutgoing fooOut = new IOIOProtocolOutgoing(ourConn.GetOutputStream()); System.Threading.Thread.Sleep(100); // wait for us to get the hardware ids fooOut.setPinDigitalOut(Spec.LED_PIN, false, DigitalOutputSpecMode.NORMAL); for (int i = 0; i < 8; i++) { System.Threading.Thread.Sleep(200); fooOut.setDigitalOutLevel(Spec.LED_PIN, true); System.Threading.Thread.Sleep(200); fooOut.setDigitalOutLevel(Spec.LED_PIN, false); } Assert.IsTrue(true, "there is no status to check"); }
public bool ExecuteMessage(IOIOProtocolOutgoing outBound) { outBound.setPinDigitalOut(this.PwmDef.PinSpec.Pin, false, this.PwmDef.PinSpec.Mode); outBound.setPinPwm(this.PwmDef.PinSpec.Pin, this.PwmDef.PwmNumber, this.Enable); IPwmOutputUpdateCommand updateCommand; // this should accepted periodUSec also if (!float.IsNaN(this.DutyCycle)) { updateCommand = new PwmOutputUpdateDutyCycleCommand(this.PwmDef, this.RequestedFrequency, this.DutyCycle); } else { updateCommand = new PwmOutputUpdateCommand(this.PwmDef, this.RequestedFrequency); } updateCommand.ExecuteMessage(outBound); // retain any frequency change done by the update command this.PwmDef = updateCommand.PwmDef; return(true); }
public void IOIOProtocolOutgoing_DigitalLoopbackOut31In32() { IOIOConnection ourConn = this.CreateGoodSerialConnection(); this.CreateCaptureLogHandlerSet(); LOG.Debug("Setup Complete"); IOIOProtocolIncoming fooIn = new IOIOProtocolIncoming(ourConn.GetInputStream(), HandlerObservable_); IOIOProtocolOutgoing fooOut = new IOIOProtocolOutgoing(ourConn.GetOutputStream()); System.Threading.Thread.Sleep(50); // receive the HW ID LOG.Info("This test requires Pin 31 and 32 be shorted together"); fooOut.setPinDigitalIn(31, DigitalInputSpecMode.FLOATING); // request to be told of state change. system will acknowledge this fooOut.setChangeNotify(31, true); // first change that will be captured... fooOut.setPinDigitalOut(32, false, DigitalOutputSpecMode.NORMAL); // second change that is captured fooOut.setDigitalOutLevel(32, true); // we could wait until our acknowledgements are received System.Threading.Thread.Sleep(300); // all log methods contain method name which is in the interface so this is reasonably safe // we get one change event as soon as the Pin input Pin is configured + 2 changes in test int matchingLogs = this.CapturedLogs_.CapturedLogs_.Count(s => s.Contains(typeof(ReportDigitalInStatusFrom).Name)); // sometimes we get 3 and sometimes 4 (!?) Assert.IsTrue(3 == matchingLogs || 4 == matchingLogs, "Should have captured 3 or 4 input changes, not " + matchingLogs + ". Are pins 31 and 32 shorted together"); // verify the system acknowledged our request to be notified of state change Assert.AreEqual(1, this.CapturedSingleQueueAllType_ .OfType <ISetChangeNotifyMessageFrom>().Where(m => m.Pin == 31).Count() , "Unexpected count for IReportDigitalInStatusFrom"); // verify we got Pin state changes for 31 Assert.AreEqual(3, this.CapturedSingleQueueAllType_ .OfType <IReportDigitalInStatusFrom>().Where(m => m.Pin == 31).Count() , "Unexpected count for IReportDigitalInStatusFrom"); }
/// <summary> /// This will pretty much always generate a I2cResultFrom response (ack) /// even if you set the expected response length to 0 /// </summary> /// <param name="outBound"></param> /// <returns>true if executes</returns> public bool ExecuteMessage(IOIOProtocolOutgoing outBound) { outBound.i2cWriteRead(TwiDef.TwiNum, this.IsTenBitAddress, this.Address, this.Data.Length, this.NumBytesRead, this.Data); return(true); }
public bool ExecuteMessage(IOIOProtocolOutgoing outBound) { throw new NotImplementedException(); }
public bool ExecuteMessage(IOIOProtocolOutgoing outBound) { // luckily? outbound ignores resource allcoation outBound.i2cClose(TwiDef.TwiNum); return(true); }
public bool ExecuteMessage(IOIOProtocolOutgoing outBound) { outBound.setPinDigitalIn(BoundPin, DigitalInputSpecMode.FLOATING); return(true); }
public bool ExecuteMessage(IOIOProtocolOutgoing outBound) { outBound.i2cConfigureMaster(this.TwiDef.TwiNum, this.TwiDef.Rate, this.TwiDef.SmBus); return(true); }