Exemplo n.º 1
0
        public void SetPercentValue(double percent)
        {
            _logger.LogDebug(this, "set Percent value " + percent);
            var voltage = 10.0 / 100.0 * percent;

            SetVoltageValue(voltage);
        }
Exemplo n.º 2
0
        internal SpiRegisterSet GetRegisterValues(NeuronGroup neuronGroup, ushort register, byte numberOfRegisters)
        {
            var result = new SpiRegisterSet();

            var returnMessage = new byte[0];

            try
            {
                var bytesToSendPhaseOne = ByteArrayForReadRegisterMessagePhase1(NeuronSpiCommand.ReadRegister, register, numberOfRegisters);
                var bytesToSendPhaseTwo = ByteArrayForReadRegisterMessagePhase2(NeuronSpiCommand.ReadRegister, register, numberOfRegisters);

                returnMessage = TwoPhaseCommunication(neuronGroup, bytesToSendPhaseOne, bytesToSendPhaseTwo);

                if (returnMessage == null)
                {
                    return(result);
                }

                var firstRegister = 4;

                for (ushort i = 0; i < numberOfRegisters; i++)
                {
                    var completeRegister = new[] { returnMessage[firstRegister++], returnMessage[firstRegister++] };
                    var registerValue    = BitConverter.ToUInt16(completeRegister, 0);
                    var registerNumber   = Convert.ToUInt16(i + register);
                    result.SetRegisterWithValue(registerNumber, registerValue);
                }

                return(result);
            }
            catch (Exception e)
            {
                _driverLogger.LogException(this, e);
                _driverLogger.LogDebug(this, "SPI Return Value:" + string.Join("-", returnMessage));
                return(new SpiRegisterSet());
            }
        }
Exemplo n.º 3
0
        public void SendCommand(byte[] romNumber, int waitInMilliSecAfterCommand, bool enableStrongPullUp = false, params byte[] commands)
        {
            lock (_controllerLock)
            {
                _logger.LogMonitor(this, "sending command " + string.Join("-", commands) + " to device with rom number " + string.Join("-", romNumber));

                _logger.LogDebug(this, "reset one wire bus");
                _neuronConnectedOneWireDevice.OneWireResetBus();

                _logger.LogDebug(this, "writing rom command match");
                _neuronConnectedOneWireDevice.OneWireWriteByte(0x55);

                _logger.LogDebug(this, "write romNumber " + string.Join("-", romNumber) + " for rom command match");
                foreach (var item in romNumber)
                {
                    _neuronConnectedOneWireDevice.OneWireWriteByte(item);
                }

                if (enableStrongPullUp)
                {
                    _logger.LogDebug(this, "enable strong pullup");
                    _neuronConnectedOneWireDevice.EnableStrongPullup();
                }

                _logger.LogDebug(this, "writing command " + string.Join("-", commands));
                _neuronConnectedOneWireDevice.OneWireWriteByte(commands);



                if (waitInMilliSecAfterCommand > 0)
                {
                    _logger.LogDebug(this, "waiting for command finish");

                    Task.Delay(waitInMilliSecAfterCommand)?.Wait();
                }



                _logger.LogDebug(this, "command finish");
            }
        }
Exemplo n.º 4
0
        internal async Task Initialize()
        {
            try
            {
                _logger.LogDebug(this, "Initializing...");

                var deviceSelector = I2cDevice.GetDeviceSelector("I2C1");

                if (deviceSelector == null)
                {
                    throw new Exception("No deviceSelector for I2C found!");
                }

                var devices = DeviceInformation.FindAllAsync(deviceSelector);

                if (devices == null)
                {
                    throw new Exception("Find All Async failed");
                }

                var unipiI2CDevices = Enum.GetValues(typeof(UnipiI2CDevice));

                if (unipiI2CDevices == null)
                {
                    throw new Exception("No I2C Devices in Configuration Enumn");
                }

                foreach (UnipiI2CDevice unipiI2CDevice in unipiI2CDevices)
                {
                    var found = false;

                    await devices;

                    var deviceInformationCollection = devices.GetResults();

                    if (deviceInformationCollection == null)
                    {
                        throw new Exception("No devices found!");
                    }

                    foreach (var deviceInformation in deviceInformationCollection.Where(deviceInformation => deviceInformation != null))
                    {
                        _logger.LogDebug(this, "searching for device " + unipiI2CDevice + " on device " + deviceInformation.Id);

                        var i2CSettings = new I2cConnectionSettings((int)unipiI2CDevice)
                        {
                            BusSpeed = I2cBusSpeed.FastMode
                        };

                        var device = I2cDevice.FromIdAsync(deviceInformation.Id, i2CSettings);

                        if (device == null)
                        {
                            throw new Exception("Find All Async failed");
                        }

                        await device;

                        _logger.LogDebug(this, "device " + unipiI2CDevice + " found");

                        lock (_busLock)
                        {
                            _devices.Add(unipiI2CDevice, device.GetResults());
                        }


                        found = true;

                        break;
                    }

                    if (!found)
                    {
                        _logger.LogError(this, "device " + unipiI2CDevice + " not found!");
                    }
                }
            }
            catch (Exception exception)
            {
                _logger.LogException(this, exception);
            }
        }