cmonitoring.~pas

来自「pipe类 pipe类 pipe类 pipe类 pipe类」· ~PAS 代码 · 共 542 行 · 第 1/2 页

~PAS
542
字号
    for i := 0 to KnownISASensors - 1 do
      if ISA_Sensors[i].ID = ChipID then
      begin
        result := i;
        exit;
      end;
  end;

var
  i, j: Word;
  ChipID, ChipIndex: Word;
begin
  for i:=Low(aISAChipDetectMethods) to High(aISAChipDetectMethods) do
  begin
    for j:=Low(ISABasePorts) to High(ISABasePorts) do
    begin
      WaitForBusReady(bISA);
      ChipID:=aISAChipDetectMethods[i].Method(ISABasePorts[j]);
      SetBusReady(bISA);

      ChipIndex:=GetISAChipIndex(ChipID);
      if (ChipIndex <> 0) then
        if not (ISA_Sensors[ChipIndex].Detected) then
        begin
          ISA_Sensors[ChipIndex].Detected:=true; //Preventing two times sensor detect
          AddISAChip(ChipIndex, ISABasePorts[j]);
        end;
    end;
  end;
end;

procedure tMonitoring.AddSMBChip(smbSensorNum: byte; smbHostNum: LongWord; smbDevice: Byte);
begin
  inc(fChipCount);
  SetLength(faChips, fChipCount);
  with faChips[fChipCount - 1], SMB_Devices[smbSensorNum] do
  begin
    Chip.SensorNum := smbSensorNum;
    Chip.smbHostNum := smbHostNum;
    Chip.smbDevice := smbDevice;
    Chip.Bus:=bSMB;
    Chip.ChipProp.Presets:=ChipProp.Presets;
    Chip.ChipProp.VoltagesCount:=ChipProp.VoltagesCount;
    Chip.ChipProp.TemperaturesCount:=ChipProp.TemperaturesCount;
    Chip.ChipProp.RPMsCount:=ChipProp.RPMsCount;
    Chip.ChipProp.PWMsCount:=ChipProp.PWMsCount;
    Chip.NameStr:=Name;
    Chip.BusAccessStr := 'SMBus ' +  format('%xh',[smbDevice]);
    GetTemperatureMethod := TM;
    GetVoltageMethod := VM;
    SetVoltageMethod := SM;
    GetFanMethod := FM;
  end;
end;

procedure tMonitoring.SMB_DevicesDetect;
var
  pdata: byte;
  i, j: Word;
  addr: Byte;
begin
  for i:=0 to SMB.HostsNum-1 do
  begin
    if i>SMB.HostsNum-1 then break;
    SMB.CurrentHost:=i;
    for addr:=$20 to $6F do
    begin
      if SMB.i2c_smbus_read_byte_data(addr, 0, pdata) <> 0 then continue;
      for j:=Low(SMB_Devices) to High(SMB_Devices) do
      begin
        if (addr in SMB_Devices[j].DevRange) and (Assigned(SMB_Devices[j].PM)) then
          if SMB_Devices[j].PM(i, addr) then
          if SMB_Devices[j].smbDevType in [SIO, Diode, VIDc] then AddSMBChip(j, i, addr);
      end;
    end;
  end;
end;

procedure tMonitoring.ImportSMBDevices;
begin
  //TO-DO Import SMBus Devices from registry
  //Importing will use AddSMBChip function to add sensor to faChips array
end;

procedure tMonitoring.ExportSMBDevices;
begin
  //TO-DO Export SMBus Devices to registry
end;

procedure tMonitoring.Detect(smbStat: Byte);
  procedure AdduGuruChip;
  begin
    inc(fChipCount);
    SetLength(faChips, fChipCount);
    with faChips[fChipCount - 1] do
    begin
      Chip.SensorNum := 0;
      Chip.isaBasePort := $0;
      Chip.Bus:=bUGURU;
      Chip.smbDevType:=SIO;
      Chip.ChipProp.VoltagesCount:=11;
      Chip.ChipProp.TemperaturesCount:=3;
      Chip.ChipProp.RPMsCount:=5;
      Chip.ChipProp.PWMsCount:=0;
      Chip.ChipProp.Presets:=1;
      Chip.NameStr:='Abit uGuru';
      Chip.BusAccessStr := 'uGuru Access Interface';
      GetTemperatureMethod := uGuruGetTemp;
      GetVoltageMethod := uGuruGetVoltage;
      SetVoltageMethod := nil;
      GetFanMethod := uGuruGetFan;
    end;
  end;
  procedure DetectAbitGuru;
  begin
    WaitForBusReady(bUGURU);
    if uGuruDetect then AdduGuruChip;
    SetBusReady(bUGURU);
  end;

begin
  ISA_SensorsDetect;
  DetectAbitGuru;

  case smbStat of
    SMB_ENABLE: SMB_DevicesDetect;
    SMB_IMPORT_DEVICES: ImportSMBDevices;
  end;
end;

constructor tMonitoring.Create(smbStat: Byte = SMB_DISABLE);
var
  count: byte;
begin
  inherited Create;

  fMutexISA := CreateMutex(nil, false, 'Global\Access_ISABUS.HTP.Method');
  fMutexSMB := CreateMutex(nil, false, 'Global\Access_SMBUS.HTP.Method');
  fMutexUGURU:= CreateMutex(nil, false, 'Access_ABIT_UGURU');
  oHWIO := GetInstance;

  fChipCount := 0;
  fCurrentChip := 0;

  if SMB = nil then SMB:=cSMBus.Create;

  Detect(smbStat);

  if fChipCount <> 0 then
    SetCurrentChip(1);
end;

function tMonitoring.GetChipCount: Byte;
begin
  result:=fChipCount;
end;

function tMonitoring.GetCurrentChip: Byte;
begin
  result:=fCurrentChip;
end;

procedure tMonitoring.SetCurrentChip(Number: byte);
begin
  if (Number > 0 ) and (Number <= fChipCount) then
    fCurrentChip := Number - 1;
end;

function tMonitoring.WaitForBusReady(Bus: tBus = bUnknown): boolean;
const TimeOut = INFINITE {1000};
begin
  result := false;
  if Bus = bUnknown then Bus := bISA;
  case Bus of
    bISA: result := (WaitforSingleObject(fMutexISA, TimeOut) = WAIT_OBJECT_0);
    bSMB: result := (WaitforSingleObject(fMutexSMB, TimeOut) = WAIT_OBJECT_0);
    bUGURU: result := (WaitforSingleObject(fMutexUGURU, TimeOut) = WAIT_OBJECT_0);
  end;
end;

procedure tMonitoring.SetBusReady(Bus: tBus = bUnknown);
begin
  if Bus = bUnknown then Bus := bISA;
  case Bus of
    bISA: ReleaseMutex(fMutexISA);
    bSMB: ReleaseMutex(fMutexSMB);
    bUGURU: ReleaseMutex(fMutexUGURU);
  end;
end;

function tMonitoring.GetChipProp:tChipProp;
begin
  result.Presets:=faChips[fCurrentChip].Chip.ChipProp.Presets;
  result.VoltagesCount:=faChips[fCurrentChip].Chip.ChipProp.VoltagesCount;
  result.TemperaturesCount:=faChips[fCurrentChip].Chip.ChipProp.TemperaturesCount;
  result.RPMsCount:=faChips[fCurrentChip].Chip.ChipProp.RPMsCount;
  result.PWMsCount:=faChips[fCurrentChip].Chip.ChipProp.PWMsCount;
end;

function tMonitoring.GetTemperature(Index: word): single;
begin
  result := -1;
  if Assigned(faChips[fCurrentChip].GetTemperatureMethod) then
  begin
    if not WaitForBusReady(faChips[fCurrentChip].Chip.Bus) then exit;
    try
      if not faChips[fCurrentChip].GetTemperatureMethod(Index, faChips[fCurrentChip].Chip, result) then
        result := -1;
    finally
      SetBusReady(faChips[fCurrentChip].Chip.Bus);
    end;
  end;
  if result = -200 then result := 0;
end;

function tMonitoring.GetVoltage(index: word; preset: byte): single;
begin
  result := -1;
  if Assigned(faChips[fCurrentChip].GetVoltageMethod) then
  begin
    if not WaitForBusReady(faChips[fCurrentChip].Chip.Bus) then exit;
    try
      if not faChips[fCurrentChip].GetVoltageMethod(Index, faChips[fCurrentChip].Chip, result, preset) then
        result := -1;
    finally
      SetBusReady(faChips[fCurrentChip].Chip.Bus);
    end;
  end;
end;

function tMonitoring.GetFan(index: word): tFANstatus;
begin
  FillChar(result, SizeOf(result), 0);
  if Assigned(faChips[fCurrentChip].GetFanMethod) then
  begin

    if not WaitForBusReady(faChips[fCurrentChip].Chip.Bus) then exit;
    try
      if not faChips[fCurrentChip].GetFanMethod(Index, faChips[fCurrentChip].Chip, result) then
      FillChar(result, SizeOf(result), 0);
    finally
      SetBusReady(faChips[fCurrentChip].Chip.Bus);
    end;
  end;
end;

function tMonitoring.GetNameStr: shortstring;
begin
  result := '';
  if (fCurrentChip >= Low(faChips)) and (fCurrentChip <= High(faChips)) then
    result := faChips[fCurrentChip].Chip.NameStr;
end;

function tMonitoring.GetBusAccessStr: shortstring;
begin
  result := '';
  if (fCurrentChip >= Low(faChips)) and (fCurrentChip <= High(faChips)) then
    result := faChips[fCurrentChip].Chip.BusAccessStr;
end;

destructor tMonitoring.Destroy;
begin
  if fMutexUGURU <> 0 then CloseHandle(fMutexUGURU);
  if fMutexSMB <> 0 then CloseHandle(fMutexSMB);
  if fMutexISA <> 0 then CloseHandle(fMutexISA);
  SetLength(faChips, 0);
  inherited Destroy;
end;

end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?