示例#1
0
        public bool TestAddress(int address)
        {
            var i2CAddress = new I2CSlaveAddress(address);
            var result     = _i2CBusService.Read(i2CAddress, new byte[1]);

            return(result.Status != I2CTransferStatus.SlaveAddressNotAcknowledged);
        }
示例#2
0
        public override void Execute(I2CSlaveAddress address, II2CBusService i2CBusService)
        {
            i2CBusService.Write(address, GenerateRegisterSensorPackage(), false);
            i2CBusService.Read(address, _buffer, false);

            ParseResponse();
        }
示例#3
0
        private II2CTransferResult Execute(I2CSlaveAddress address, Func <I2cDevice, I2cTransferResult> action, bool useCache = true)
        {
            lock (_deviceCache)
            {
                I2cDevice device = null;
                try
                {
                    device = GetDevice(address.Value, useCache);
                    var result = action(device);

                    if (result.Status != I2cTransferStatus.FullTransfer)
                    {
                        _log.Warning($"Transfer failed. Address={address.Value} Status={result.Status} TransferredBytes={result.BytesTransferred}");
                    }

                    return(WrapResult(result));
                }
                catch (Exception exception)
                {
                    // Ensure that the application will not crash if some devices are currently not available etc.
                    _log.Warning(exception, $"Error while accessing I2C device with address {address}.");
                    return(new I2CTransferResult(I2CTransferStatus.UnknownError, 0));
                }
                finally
                {
                    if (!useCache)
                    {
                        device?.Dispose();
                    }
                }
            }
        }
        public void Execute(I2CSlaveAddress address, Action <II2CDevice> action, bool useCache = true)
        {
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            lock (_syncRoot)
            {
                I2cDevice device = null;
                try
                {
                    device = GetI2CDevice(address.Value, useCache);
                    action(new I2CDeviceWrapper(device));
                }
                catch (Exception exception)
                {
                    // Ensure that the application will not crash if some devices are currently not available etc.
                    _logger.Warning("Error while accessing I2C device with address " + address + ". " + exception.Message);
                }
                finally
                {
                    if (device != null && !useCache)
                    {
                        device.Dispose();
                    }
                }
            }
        }
示例#5
0
 public II2CTransferResult WriteRead(I2CSlaveAddress address, byte[] writeBuffer, byte[] readBuffer, bool useCache = true)
 {
     LastUsedI2CSlaveAddress = address;
     LastWrittenBytes        = writeBuffer;
     Array.Copy(BufferForNextRead, readBuffer, readBuffer.Length);
     return(new I2CTransferResult(I2CTransferStatus.FullTransfer, writeBuffer.Length + readBuffer.Length));
 }
示例#6
0
        public IDevice CreateDevice(CCToolsDeviceType type, string id, int address)
        {
            if (id == null)
            {
                throw new ArgumentNullException(nameof(id));
            }

            var     i2CSlaveAddress = new I2CSlaveAddress(address);
            IDevice deviceInstance;

            switch (type)
            {
            case CCToolsDeviceType.HSPE16_InputOnly:
            {
                deviceInstance = new HSPE16InputOnly(id, i2CSlaveAddress, _i2CBusService, _log);
                break;
            }

            case CCToolsDeviceType.HSPE16_OutputOnly:
            {
                deviceInstance = new HSPE16OutputOnly(id, i2CSlaveAddress, _i2CBusService, _log);
                break;
            }

            case CCToolsDeviceType.HSPE8_InputOnly:
            {
                deviceInstance = new HSPE8InputOnly(id, i2CSlaveAddress, _i2CBusService, _log);
                break;
            }

            case CCToolsDeviceType.HSPE8_OutputOnly:
            {
                deviceInstance = new HSPE8OutputOnly(id, i2CSlaveAddress, _i2CBusService, _log);
                break;
            }

            case CCToolsDeviceType.HSRel5:
            {
                deviceInstance = new HSREL5(id, i2CSlaveAddress, _i2CBusService, _log);
                break;
            }

            case CCToolsDeviceType.HSRel8:
            {
                deviceInstance = new HSREL8(id, i2CSlaveAddress, _i2CBusService, _log);
                break;
            }

            case CCToolsDeviceType.HSRT16:
            {
                deviceInstance = new HSRT16(id, i2CSlaveAddress, _i2CBusService, _log);
                break;
            }

            default: throw new NotSupportedException();
            }

            return(deviceInstance);
        }
示例#7
0
        public HSPE16InputOnly(string id, I2CSlaveAddress address, II2CBusService i2CBusService, ILogger log)
            : base(id, new MAX7311Driver(address, i2CBusService), log)
        {
            byte[] setupAsInputs = { 0x06, 0xFF, 0xFF };
            i2CBusService.Write(address, setupAsInputs);

            FetchState();
        }
示例#8
0
        public HSRT16 RegisterHSRT16(string id, I2CSlaveAddress address)
        {
            var device = new HSRT16(id, address, _i2CBusService, _deviceMessageBrokerService, _log);

            _deviceService.AddDevice(device);

            return(device);
        }
示例#9
0
        public HSPE8InputOnly RegisterHSPE8InputOnly(string id, I2CSlaveAddress i2CAddress)
        {
            var device = new HSPE8InputOnly(id, i2CAddress, _i2CBusService, _deviceMessageBrokerService, _log);

            _deviceService.AddDevice(device);

            return(device);
        }
示例#10
0
 public HSREL5(DeviceId id, I2CSlaveAddress i2CAddress, II2CBusService bus)
     : base(id, new PCF8574Driver(i2CAddress, bus))
 {
     // Ensure that all relays are off by default. The first 5 ports are hardware inverted! The other ports are not inverted but the
     // connected relays are inverted.
     SetState(new byte[] { 0xFF });
     CommitChanges(true);
 }
示例#11
0
 public HSREL5(string id, I2CSlaveAddress i2CAddress, II2CBusService i2CBusService, IDeviceMessageBrokerService deviceMessageBrokerService, ILogger log)
     : base(id, new PCF8574Driver(i2CAddress, i2CBusService), deviceMessageBrokerService, log)
 {
     // Ensure that all relays are off by default. The first 5 ports are hardware inverted! The other ports are not inverted but the
     // connected relays are inverted.
     SetState(new byte[] { 0xFF });
     CommitChanges(true);
 }
示例#12
0
        public HSREL8 CreateHSREL8(Enum id, I2CSlaveAddress i2CAddress)
        {
            var device = new HSREL8(DeviceIdFactory.CreateIdFrom(id), i2CAddress, _i2CBus, _httpApi, _log);

            _controller.AddDevice(device);

            return(device);
        }
示例#13
0
        public HSPE16OutputOnly CreateHSPE16OutputOnly(Enum id, I2CSlaveAddress address)
        {
            var device = new HSPE16OutputOnly(DeviceIdFactory.CreateIdFrom(id), address, _i2CBus, _httpApi, _log);

            _controller.AddDevice(device);

            return(device);
        }
示例#14
0
        public HSPE16InputOnly(string id, I2CSlaveAddress address, II2CBusService i2cBusService, IDeviceMessageBrokerService deviceMessageBrokerService, ILogger log)
            : base(id, new MAX7311Driver(address, i2cBusService), deviceMessageBrokerService, log)
        {
            byte[] setupAsInputs = { 0x06, 0xFF, 0xFF };
            i2cBusService.Execute(address, b => b.Write(setupAsInputs));

            FetchState();
        }
示例#15
0
 public HSREL5(DeviceId id, I2CSlaveAddress i2CAddress, II2CBus bus, IHttpRequestController httpApi, ILogger logger)
     : base(id, new PCF8574Driver(i2CAddress, bus), httpApi, logger)
 {
     // Ensure that all relays are off by default. The first 5 ports are hardware inverted! The other ports are not inverted but the
     // connected relays are inverted.
     SetState(new byte[] { 0xFF });
     CommitChanges(true);
 }
示例#16
0
        public HSPE16InputOnly(DeviceId id, I2CSlaveAddress address, II2CBus i2cBus, IHttpRequestController httpApi, ILogger logger)
            : base(id, new MAX7311Driver(address, i2cBus), httpApi, logger)
        {
            byte[] setupAsInputs = { 0x06, 0xFF, 0xFF };
            i2cBus.Execute(address, b => b.Write(setupAsInputs));

            FetchState();
        }
示例#17
0
        public HSPE8InputOnly CreateHSPE8InputOnly(Enum id, I2CSlaveAddress i2CAddress)
        {
            var device = new HSPE8InputOnly(DeviceIdFactory.CreateIdFrom(id), i2CAddress, _i2CBus);

            _controller.AddDevice(device);

            return(device);
        }
示例#18
0
        public HSRT16 CreateHSRT16(Enum id, I2CSlaveAddress address)
        {
            var device = new HSRT16(DeviceIdFactory.CreateIdFrom(id), address, _i2CBus);

            _controller.AddDevice(device);

            return(device);
        }
示例#19
0
        public HSPE16InputOnly(DeviceId id, I2CSlaveAddress address, II2CBusService i2cBus)
            : base(id, new MAX7311Driver(address, i2cBus))
        {
            byte[] setupAsInputs = { 0x06, 0xFF, 0xFF };
            i2cBus.Execute(address, b => b.Write(setupAsInputs));

            FetchState();
        }
示例#20
0
        public HSRT16 RegisterHSRT16(Enum id, I2CSlaveAddress address)
        {
            var device = new HSRT16(DeviceIdFactory.CreateIdFrom(id), address, _i2CBusService);

            _deviceService.AddDevice(device);

            return(device);
        }
示例#21
0
        public HSPE8InputOnly RegisterHSPE8InputOnly(Enum id, I2CSlaveAddress i2CAddress)
        {
            var device = new HSPE8InputOnly(DeviceIdFactory.CreateIdFrom(id), i2CAddress, _i2CBusService);

            _deviceService.AddDevice(device);

            return(device);
        }
示例#22
0
        public II2CTransferResult Read(I2CSlaveAddress address, byte[] buffer, bool useCache = true)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException(nameof(buffer));
            }

            return(Execute(address, d => d.ReadPartial(buffer), useCache));
        }
示例#23
0
        public MAX7311Driver(I2CSlaveAddress address, II2CBus i2CBus)
        {
            if (i2CBus == null)
            {
                throw new ArgumentNullException(nameof(i2CBus));
            }

            _address = address;
            _i2CBus  = i2CBus;
        }
示例#24
0
        public HSPE16InputOnly RegisterHSPE16InputOnly(Enum id, I2CSlaveAddress address)
        {
            var device = new HSPE16InputOnly(DeviceIdFactory.CreateIdFrom(id), address, _i2CBusService)
            {
                AutomaticallyFetchState = true
            };

            _deviceService.AddDevice(device);

            return(device);
        }
示例#25
0
        public HSPE16InputOnly CreateHSPE16InputOnly(Enum id, I2CSlaveAddress address)
        {
            var device = new HSPE16InputOnly(DeviceIdFactory.CreateIdFrom(id), address, _i2CBus)
            {
                AutomaticallyFetchState = true
            };

            _controller.AddDevice(device);

            return(device);
        }
示例#26
0
        public HSPE16InputOnly RegisterHSPE16InputOnly(string id, I2CSlaveAddress address)
        {
            var device = new HSPE16InputOnly(id, address, _i2CBusService, _deviceMessageBrokerService, _log)
            {
                AutomaticallyFetchState = true
            };

            _deviceService.AddDevice(device);

            return(device);
        }
示例#27
0
        public I2CHardwareBridge(string id, I2CSlaveAddress address, II2CBusService i2CBusService, ISchedulerService schedulerService)
        {
            if (schedulerService == null)
            {
                throw new ArgumentNullException(nameof(schedulerService));
            }
            _address       = address;
            _i2CBusService = i2CBusService ?? throw new ArgumentNullException(nameof(i2CBusService));

            Id            = id ?? throw new ArgumentNullException(nameof(id));
            DHT22Accessor = new DHT22Accessor(this, schedulerService);
        }
示例#28
0
        public PCF8574Driver(I2CSlaveAddress address, II2CBusService i2CBus)
        {
            if (address == null)
            {
                throw new ArgumentNullException(nameof(address));
            }
            if (i2CBus == null)
            {
                throw new ArgumentNullException(nameof(i2CBus));
            }

            _address = address;
            _i2CBus  = i2CBus;
        }
示例#29
0
        public override void Execute(I2CSlaveAddress address, II2CBusService i2CBusService)
        {
            i2CBusService.Write(address, GenerateClearSignalCachePackage(), false);

            var offset = 0;

            while (offset < _signal.Length)
            {
                var buffer = _signal.Skip(offset).Take(30).ToArray();
                offset += buffer.Length;

                i2CBusService.Write(address, GenerateFillSignalCachePackage(buffer), false);
            }

            i2CBusService.Write(address, GenerateSendCachedSignalPackage(), false);
        }
        public I2CHardwareBridge(I2CSlaveAddress address, II2CBus i2cBus, IHomeAutomationTimer timer)
        {
            if (i2cBus == null)
            {
                throw new ArgumentNullException(nameof(i2cBus));
            }
            if (timer == null)
            {
                throw new ArgumentNullException(nameof(timer));
            }

            _address = address;
            _i2CBus  = i2cBus;

            DHT22Accessor = new DHT22Accessor(this, timer);
        }