/* Going to select the highest power object that we are compatible with */ for (i = 0; i < port->SrcCapsHeaderReceived.NumDataObjects; i++) { switch (port->SrcCapsReceived[i].PDO.SupplyType) { case pdoTypeFixed: /* pdoTypeFixed */ objVoltage = port->SrcCapsReceived[i].FPDOSupply.Voltage * 50; if (objVoltage > port->PortConfig.SinkRequestMaxVoltage) { /* If the voltage is greater than our limit... */ continue; } else { /* Calculate the power for comparison */ objCurrent = port->SrcCapsReceived[i].FPDOSupply.MaxCurrent * 10; objPower = (objVoltage * objCurrent) / 1000; } break; case pdoTypeVariable: case pdoTypeBattery: case pdoTypeAugmented: PPSAPDO = i + 1; objVoltage = port->SrcCapsReceived[i].PPSAPDO.MaxVoltage * 100; if (objVoltage > port->PortConfig.SinkRequestMaxVoltage) { /* If the voltage is greater than our limit... */ continue; } else { /* Calculate the power for comparison */ objCurrent = port->SrcCapsReceived[i].PPSAPDO.MaxCurrent * 50; objPower = (objVoltage * objCurrent) / 1000; } break; default: /* Ignore other supply types for now */ objPower = 0; break; } /* Look for highest power */ if (objPower >= MaxPower) { MaxPower = objPower; SelVoltage = objVoltage; reqPos = i + 1; } }代码分析
时间: 2024-04-04 07:29:35 浏览: 95
What-is-the-highest-score.rar_What Is the What_华为OJ
这段代码是一个 USB PD(Power Delivery)协议的实现,用于在连接的设备之间确定电源供应类型和电源参数,以便为设备提供所需的电能。这段代码的作用是在所有可供选择的电源中,选择与当前设备兼容并输出最高功率的电源。
具体来说,代码通过遍历接收到的源电源容量数据对象,分别计算每个电源的电压和电流,从而计算出该电源可以提供的最大功率。对于不同类型的电源,代码使用不同的方法计算电流和电压。
代码中使用变量 MaxPower 和 SelVoltage 记录当前选择的最大功率和电压,并且使用 reqPos 记录所选电源的位置。最后,代码返回选择的电源位置和所需电压,以便设备可以向所选择的电源发出请求。
阅读全文