private async Task <Pack> DetermineGeometry() { // Read all cell voltages in the whole chain await this.Connection.ExecuteCommand(CommandId.StartCellConversion(ConversionMode.Normal, false, 0)).ConfigureAwait(false); await Task.Delay(3).ConfigureAwait(false); // Read voltages var cellVoltageA = (await this.Connection.ReadRegister(CommandId.ReadCellRegisterA, 6).ConfigureAwait(false)).ToArray(); var cellVoltageB = (await this.Connection.ReadRegister(CommandId.ReadCellRegisterB, 6).ConfigureAwait(false)).ToArray(); var cellVoltageC = (await this.Connection.ReadRegister(CommandId.ReadCellRegisterC, 6).ConfigureAwait(false)).ToArray(); var cellVoltageD = (await this.Connection.ReadRegister(CommandId.ReadCellRegisterD, 6).ConfigureAwait(false)).ToArray(); this.CheckChainLength("determining pack geometry", cellVoltageA, cellVoltageB, cellVoltageC, cellVoltageD); // Build a series pack for each IC in chain var chainPacks = new List <ChipPack>(); for (int chainIndex = 0; chainIndex < this.ChainLength; chainIndex++) { // Decode cell voltages var voltageRegister = CellVoltageRegister.FromGroups(cellVoltageA[chainIndex], cellVoltageB[chainIndex], cellVoltageC[chainIndex], cellVoltageD[chainIndex]); var cellVoltages = Enumerable.Range(1, 12) .Select(x => Tuple.Create(x, voltageRegister.GetCellVoltage(x))); // Detect connected cells var packCells = cellVoltages .Where(x => x.Item2 > MinConnectedCellVoltage) .ToDictionary(x => x.Item1, x => { var cell = new SingleCell(); cell.Actuals().Voltage = x.Item2; return(cell); }); var chipPack = new ChipPack(chainIndex, packCells); chainPacks.Add(chipPack); } this.logger.LogDebug(new TraceBuilder() .AppendLine("A LTC6804 daisy chain with {0} chips recognized with following geometry:", this.ChainLength) .Indent() .ForEach(chainPacks, (tb, p) => tb .AppendLine("Chain index {0} with {1} connected cells:", p.ChainIndex, p.ConnectedCells.Count) .Indent() .AppendLineForEach(p.ConnectedCells, "Channel {0} => {1} V", x => x.Key, x => x.Value.Actuals().Voltage) .Unindent() .AppendLine()) .Trace()); // Connect all chip packs into a series stack Pack pack = chainPacks.Count > 1 ? new SeriesPack(chainPacks) : chainPacks[0]; return(pack); }
private async Task <CellVoltageRegister[]> ReadCellVoltages() { var cellVoltageA = (await this.Connection.ReadRegister(CommandId.ReadCellRegisterA, 6).ConfigureAwait(false)).ToArray(); var cellVoltageB = (await this.Connection.ReadRegister(CommandId.ReadCellRegisterB, 6).ConfigureAwait(false)).ToArray(); var cellVoltageC = (await this.Connection.ReadRegister(CommandId.ReadCellRegisterC, 6).ConfigureAwait(false)).ToArray(); var cellVoltageD = (await this.Connection.ReadRegister(CommandId.ReadCellRegisterD, 6).ConfigureAwait(false)).ToArray(); this.CheckChainLength("reading cell voltages", cellVoltageA, cellVoltageB, cellVoltageC, cellVoltageD); var voltageRegisters = Enumerable.Range(0, this.ChainLength) .Select(x => CellVoltageRegister.FromGroups(cellVoltageA[x], cellVoltageB[x], cellVoltageC[x], cellVoltageD[x])) .ToArray(); return(voltageRegisters); }