예제 #1
0
        public IObservable <TResult> Generate()
        {
            var source = Observable.Create <ONIManagedFrame <TData> >(async observer =>
            {
                var cd = await ONIContextManager.ReserveOpenContextAsync(DeviceAddress.HardwareSlot);

                var in_table = cd.Context.DeviceTable.TryGetValue((uint)DeviceAddress.Address, out var device);
                if (!in_table || device.ID != (int)ID)
                {
                    throw new WorkflowException("Selected device address is invalid.");
                }

                EventHandler <FrameReceivedEventArgs> frame_received = (sender, e) =>
                {
                    if (e.Frame.DeviceAddress == DeviceAddress.Address)
                    {
                        ONIManagedFrame <TData> frame = new ONIManagedFrame <TData>(e.Frame);
                        observer.OnNext(frame);
                    }
                };

                cd.Context.FrameReceived += frame_received;
                return(Disposable.Create(() =>
                {
                    cd.Context.FrameReceived -= frame_received;
                    cd.Dispose();
                }));
            });

            ulong frameOffset = FrameClockOffset;

            return(Process(source, frameOffset));
        }
예제 #2
0
 static public void WriteRegister(ONIXDeviceDescriptor descriptor, uint address, uint value)
 {
     using (var c = ONIContextManager.ReserveContext(descriptor.Address.HardwareSlot))
     {
         c.Context.WriteRegister((uint)descriptor.Address.Address, address, value);
     }
 }
예제 #3
0
 static public uint ReadRegister(ONIXDeviceDescriptor descriptor, uint address)
 {
     using (var c = ONIContextManager.ReserveContext(descriptor.Address.HardwareSlot))
     {
         return(c.Context.ReadRegister((uint)descriptor.Address.Address, address));
     }
 }
예제 #4
0
        public I2CConfiguration(ONIDeviceAddress address, DeviceID id, uint i2cAddress)
        {
            Valid         = ONIXDeviceDescriptor.IsValid(id, address);
            context       = ONIContextManager.ReserveContext(address.HardwareSlot);
            deviceAddress = address.Address;
            I2CAddress    = i2cAddress;

#if DEBUG
            Console.WriteLine("I2C context reserved by " + this.GetType());
#endif
        }
예제 #5
0
 public IObservable <TSource> Process(IObservable <TSource> source)
 {
     return(Observable.Using(
                cancellationToken => ONIContextManager.ReserveOpenContextAsync(DeviceAddress.HardwareSlot),
                (contextDisposable, cancellationToken) =>
     {
         return ONIXDeviceDescriptor.IsValid(ID, DeviceAddress)
                 ? Task.FromResult(source
                                   .Do(input => { OnNext(contextDisposable.Context, input); })
                                   .Finally(() => OnFinally(contextDisposable.Context)))
                 : throw new WorkflowException("Selected device address is invalid.");
     }
                ));
 }
예제 #6
0
        public static bool IsValid(DeviceID id, ONIDeviceAddress address)
        {
            if (string.IsNullOrEmpty(address.HardwareSlot.Driver) || !address.Address.HasValue)
            {
                return(false);
            }

            using (var c = ONIContextManager.ReserveContext(address.HardwareSlot))
            {
                if (!(c.Context.DeviceTable[(uint)address.Address].ID == (uint)id))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #7
0
 public override IObservable <ONIContextTask> Generate()
 {
     return(Observable.Using(
                () =>
     {
         var c = ONIContextManager.ReserveContext(ContextConfiguration.Slot, true, true);
         c.Context.BlockReadSize = ContextConfiguration.ReadSize;
         c.Context.BlockWriteSize = ContextConfiguration.WriteSize;
         c.Context.Start();
         return c;
     },
                c =>
     {
         return Observable
         .Return(c.Context)
         .Concat(Observable.Never(c.Context));
     }));
 }
예제 #8
0
        public ONIXDeviceDescriptor(DeviceID id, ONIDeviceAddress address)
        {
            if (string.IsNullOrEmpty(address.HardwareSlot.Driver))
            {
                throw new ArgumentException("Invalid hardware driver.");
            }

            if (!address.Address.HasValue)
            {
                throw new ArgumentException("Invalid device address.");
            }

            using (var c = ONIContextManager.ReserveContext(address.HardwareSlot))
            {
                if (!(c.Context.DeviceTable[(uint)address.Address].ID == (uint)id))
                {
                    throw new ArgumentException("Address " + address.ToString() + " does not contain a device with ID " + id);
                }
            }

            ID      = id;
            Address = address;
        }
예제 #9
0
        public IObservable <TResult> Generate(IObservable <TSource> source)
        {
            return(Observable.Create <TResult>(async observer =>
            {
                var cd = await ONIContextManager.ReserveOpenContextAsync(DeviceAddress.HardwareSlot);

                var in_table = cd.Context.DeviceTable.TryGetValue((uint)DeviceAddress.Address, out var device);
                if (!in_table || device.ID != (int)ID)
                {
                    throw new WorkflowException("Selected device address is invalid.");
                }

                var source_sub = source.Subscribe(
                    input =>
                {
                    try
                    {
                        Write(cd.Context, input);
                    }
                    catch (Exception ex)
                    {
                        observer.OnError(ex);
                    }
                },
                    observer.OnError,
                    () => { });

                return new CompositeDisposable(
                    Generate().SubscribeSafe(observer),
                    Disposable.Create(() =>
                {
                    cd.Dispose();
                    source_sub.Dispose();
                })
                    );
            }));
        }
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            if (context != null)
            {
                var workflowBuilder = (WorkflowBuilder)context.GetService(typeof(WorkflowBuilder));
                if (workflowBuilder != null)
                {
                    var hw_slots = (from builder in workflowBuilder.Workflow.Descendants()
                                    let ctx_config = ExpressionBuilder.GetWorkflowElement(builder) as ONIContext
                                                     where ctx_config != null && !string.IsNullOrEmpty(ctx_config.ContextConfiguration.Slot.Driver)
                                                     select ctx_config.ContextConfiguration.Slot)
                                   .Concat(ONIContextManager.LoadConfiguration())
                                   .Distinct()
                                   .ToList();

                    // This device
                    var      deviceattribute = context.PropertyDescriptor.ComponentType.GetCustomAttributes(typeof(ONIXDeviceIDAttribute), true).FirstOrDefault() as ONIXDeviceIDAttribute;
                    DeviceID deviceID        = deviceattribute == null ? DeviceID.Null : deviceattribute.deviceID;

                    // To fill after inspecting hardware
                    var deviceAddresses = new List <ONIDeviceAddress>();

                    foreach (var hw in hw_slots)
                    {
                        try
                        {
                            using (var c = ONIContextManager.ReserveContext(hw))
                            {
                                // Find valid device indices
                                var dev_matches = c.Context.DeviceTable
                                                  //.Where(dev => dev.Value.ID == (uint)device.ID)
                                                  .Where(dev =>
                                {
                                    return(deviceID == DeviceID.Null ?
                                           dev.Value.ID != (uint)DeviceID.Null :
                                           dev.Value.ID == (uint)deviceID);
                                })
                                                  .Select(x =>
                                {
                                    var d = new ONIDeviceAddress
                                    {
                                        HardwareSlot = hw,
                                        Address      = x.Key
                                    };
                                    return(d);
                                }).ToList();

                                deviceAddresses = deviceAddresses.Concat(dev_matches).ToList();
                            }
                        }
                        catch (InvalidProgramException) // Bad context initialization
                        {
                            return(base.GetStandardValues(context));
                        }
                        catch (oni.ONIException) // Something happened during hardware init
                        {
                            return(base.GetStandardValues(context));
                        }
                    }

                    return(deviceAddresses.Count == 0 ?
                           base.GetStandardValues(context) :
                           new StandardValuesCollection(deviceAddresses));
                }
            }

            return(base.GetStandardValues(context));
        }