示例#1
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);
     }
 }
示例#2
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));
     }
 }
示例#3
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
        }
        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);
        }
示例#5
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));
     }));
 }
        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;
        }
        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));
        }