unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, JvComponentBase, JvTrayIcon, CoolTrayIcon, TextTrayIcon,
  JvExControls, JvComponent, JvDialButton, StdCtrls, JvgCheckBox,
  JvgDigits, JvFormPlacement, JvAppStorage, JvAppRegistryStorage, ComCtrls,
  ExtCtrls, GiveIO;

const PORT_INDEX   = $295;
const PORT_DATA    = $296;

const REG_BANK_SET = $4e; 
const REG_CPU_TEMP = $50; 
const REG_MB_TEMP  = $67; 

  
type
  TForm1 = class(TForm)
    TextTrayIcon1: TTextTrayIcon;
    GroupBox1: TGroupBox;
    Label2: TLabel;
    Label3: TLabel;
    JvAppRegistryStorage1: TJvAppRegistryStorage;
    JvFormStorage1: TJvFormStorage;
    dgCurrentCPUTemp: TJvgDigits;
    dgCurrentMotherboardTemp: TJvgDigits;
    cbBoosted: TJvgCheckBox;
    Label4: TLabel;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    dgBoostOnTemp: TJvgDigits;
    Label5: TLabel;
    dgBoostOffTemp: TJvgDigits;
    udBoostOnTemp: TUpDown;
    udBoostOffTemp: TUpDown;
    Timer1: TTimer;
    Button1: TButton;
    procedure Timer1Timer(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure udBoostOffTempClick(Sender: TObject; Button: TUDBtnType);
    procedure udBoostOnTempClick(Sender: TObject; Button: TUDBtnType);
    procedure Button1Click(Sender: TObject);
    procedure TextTrayIcon1Click(Sender: TObject);
    procedure FormHide(Sender: TObject);
  private
    { Private declarations }
   procedure UpdateStatus();
   procedure UpdateSettings();
   procedure SetBoost(b: boolean);
  public
    { Public declarations }

   boosted: boolean; 
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
 UpdateStatus();
end;

Function CPUTemperature():Integer;
begin
 OutPort(PORT_INDEX,REG_BANK_SET);
 OutPort(PORT_DATA,1);
 OutPort(PORT_INDEX,REG_CPU_TEMP);
 Result:=(InPort(PORT_DATA));
end;

Function MBTemperature():Integer;
begin
 OutPort(PORT_INDEX,REG_MB_TEMP);
 Result:=(InPort(PORT_DATA));
end;

procedure TForm1.SetBoost(b: boolean);
begin
 try
  if b then OutPort($3f2,0)
       else OutPort($3f2,$1c-8);
  boosted:=b;
  cbBoosted.checked:=b;
 except
  InitDriver();
  Sleep(1000);
 end;      
end;


procedure TForm1.UpdateStatus();
begin
 try
  dgCurrentCPUTemp.value:=CPUTemperature();
  TextTrayIcon1.Text:=inttostr(CPUTemperature())+char($B0);
  dgCurrentMotherboardTemp.value:=MBTemperature();
 except
  //Reinit driver. Can happend after hibernation.
  InitDriver;
  Sleep(1000);
 end; 

 if not boosted then
  begin
   SetBoost(dgCurrentCPUTemp.value>=udBoostOnTemp.position);
  end
   else
  begin
   SetBoost(dgCurrentCPUTemp.value>udBoostOffTemp.position);
  end; 
end;


procedure TForm1.FormCreate(Sender: TObject);
begin
 boosted:=true;
 InitDriver; 
 UpdateStatus();
end;

procedure TForm1.FormShow(Sender: TObject);
begin
 jvFormStorage1.RestoreFormPlacement();
 UpdateSettings();
end;

procedure TForm1.UpdateSettings();
begin
 dgBoostOffTemp.value:=udBoostOffTemp.position;
 dgBoostOnTemp.value:=udBoostOnTemp.position;

 if udBoostOffTemp.position>=udBoostOnTemp.position then
  begin
   udBoostOffTemp.position:=udBoostOnTemp.position-1;
   dgBoostOffTemp.value:=udBoostOffTemp.position;
  end;
end;  

procedure TForm1.udBoostOffTempClick(Sender: TObject; Button: TUDBtnType);
begin
 UpdateSettings();
end;

procedure TForm1.udBoostOnTempClick(Sender: TObject; Button: TUDBtnType);
begin
 UpdateSettings();
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 Hide();
end;

procedure TForm1.TextTrayIcon1Click(Sender: TObject);
begin
 if not visible then Show() else Hide();
end;

procedure TForm1.FormHide(Sender: TObject);
begin
 jvFormStorage1.SaveFormPlacement();
end;

end.
