public async Task Initialize() { try { var i2Init = _i2CConnector.Initialize(); var spiInit = _spiConnector.Initialize(); await spiInit; await i2Init; var factory = new GeneralFactory(_i2CConnector, _spiConnector, _driverLogger); foreach (var foundGroupId in _spiConnector.GetFoundGroupIds()) { var board = factory.CreateNeuronBoard(foundGroupId); if (board == null) { continue; } _driverLogger.LogInformation(this, "Found Board " + board.BoardSystemInformation?.HardwareName + " on group " + foundGroupId); _boards.Add(board); } OnInitializationFinish?.Invoke(); } catch (Exception exception) { _driverLogger.LogException(this, exception); } }
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()); } }
private void PollInformation(object state) { if (_pollInProgess) { return; } _pollInProgess = true; try { lock (_observeLocker) { foreach (var keyValuePair in _registersToObserve) { var registerNumber = keyValuePair.Key; var result = SpiConnector.GetSingleRegisterValue(NeuronGroup, registerNumber); if (result == null) { continue; } if (keyValuePair.Value == null) { continue; } foreach (var observedObject in keyValuePair.Value) { observedObject.SetRegisterValue(registerNumber, result.Value); } } } } catch (Exception e) { Logger.LogException(this, e); } _pollInProgess = false; }
internal static BoardInformation GetBoardInfoFromRegisters(SpiRegisterSet registerSet, DriverLogger logger, NeuronGroup neuronGroup) { try { if (registerSet == null || registerSet.Count != 5) { return(null); } if (!registerSet.ContainsRegister(1000) || !registerSet.ContainsRegister(1001) || !registerSet.ContainsRegister(1002) || !registerSet.ContainsRegister(1003) || !registerSet.ContainsRegister(1004)) { logger.LogError(null, "i got the wrong registerSet to create the system information!"); return(null); } var configRegisters = registerSet.ToRegisterValueArray(); int swSubver; int hwVersion; int hwSubver; var swVersion = configRegisters[0] >> 8; var diCount = configRegisters[1] >> 8; var doCount = configRegisters[1] & 0xff; var aiCount = configRegisters[2] >> 8; var aoCount = (configRegisters[2] & 0xff) >> 4; var uledCount = 0; if (swVersion < 4) { swSubver = 0; hwVersion = (configRegisters[0] & 0xff) >> 4; hwSubver = configRegisters[0] & 0xf; } else { swSubver = configRegisters[0] & 0xff; hwVersion = configRegisters[3] >> 8; hwSubver = configRegisters[3] & 0xff; if (hwSubver < 3) { } if (hwVersion == 0) { if (configRegisters[0] != 0x0400) { uledCount = 4; } } } var firmwareVersion = new Version(swVersion, swSubver); var digitalInputCount = diCount; var digitalOutputCount = doCount; var analogInputCount = aiCount; var analogOutputCount = aoCount; var userLedCount = uledCount; var hardwareVersion = new Version(hwVersion, hwSubver); return(new BoardInformation(firmwareVersion, hardwareVersion, digitalInputCount, digitalOutputCount, analogInputCount, analogOutputCount, userLedCount, hwVersion, neuronGroup)); } catch (Exception e) { logger.LogException(null, e); return(null); } }
internal INeuronGroupBoard CreateNeuronBoard(NeuronGroup neuronGroup) { try { var boardInformationRegisterSet = _spiConnector.GetRegisterValues(neuronGroup, 1000, 5); var boardInformation = GetBoardInfoFromRegisters(boardInformationRegisterSet, _logger, neuronGroup); if (boardInformation != null) { switch (boardInformation.BoardType) { case BoardType.UnknownBoard: _logger.LogInformation(this, "Board " + boardInformation.BoardType + " not supported!"); break; case BoardType.B10001: return(new B10001GroupBoard(neuronGroup, boardInformation, _spiConnector, _i2CConnector, _logger)); case BoardType.E8Di8Ro1: return(new UniversalNeuronGroupDiRoBoard(8, 8, neuronGroup, boardInformation, _spiConnector, _i2CConnector, _logger)); case BoardType.E14Ro1: return(new UniversalNeuronGroupDiRoBoard(0, 16, neuronGroup, boardInformation, _spiConnector, _i2CConnector, _logger)); case BoardType.E16Di1: return(new UniversalNeuronGroupDiRoBoard(16, 0, neuronGroup, boardInformation, _spiConnector, _i2CConnector, _logger)); case BoardType.E8Di8Ro1P11DiMb485: _logger.LogInformation(this, "Board " + boardInformation.BoardType + " not supported!"); break; case BoardType.E14Ro1P11DiR4851: _logger.LogInformation(this, "Board " + boardInformation.BoardType + " not supported!"); break; case BoardType.E16Di1P11DiR4851: _logger.LogInformation(this, "Board " + boardInformation.BoardType + " not supported!"); break; case BoardType.E14Ro1U14Ro1: _logger.LogInformation(this, "Board " + boardInformation.BoardType + " not supported!"); break; case BoardType.E16Di1U14Ro1: return(new UniversalNeuronGroupDiRoBoard(16, 14, neuronGroup, boardInformation, _spiConnector, _i2CConnector, _logger)); case BoardType.E14Ro1U14Di1: return(new UniversalNeuronGroupDiRoBoard(14, 14, neuronGroup, boardInformation, _spiConnector, _i2CConnector, _logger)); case BoardType.E16Di1U14Di1: _logger.LogInformation(this, "Board " + boardInformation.BoardType + " not supported!"); break; default: throw new ArgumentOutOfRangeException(); } } } catch (Exception e) { _logger.LogException(this, e); return(null); } return(null); }
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); } }