⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 umain.pas

📁 这个程序的主要目的是为了提供一个具有通用性的 AVR Bootloader
💻 PAS
📖 第 1 页 / 共 5 页
字号:

procedure TfrmMain.btnExitClick(Sender: TObject);
begin
  //avoid reiteration event
  if Idle then
  begin
    Idle := False;
    try
      Close;
    finally
      Idle := True;
    end;
  end;
end;

procedure TfrmMain.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  case Key of
    Ord('C'):
      if Shift = [ssAlt] then
        mnComPortSetupClick(Sender);
    Ord('M'):
      if Shift = [ssAlt] then
        mnMCUClick(Sender);
    Ord('X'):
      if Shift = [ssAlt] then
        btnExitClick(Sender);
    Ord('O'):
      if Shift = [ssCtrl] then
        btnLoadClick(Sender);
    Ord('S'):
      begin
        if Shift = [ssCtrl] then
          btnSaveClick(Sender);
        if Shift = [ssAlt] then
          mnSystemClick(Sender);
      end;
    VK_F1:
      if Shift = [] then
        mnContentClick(Sender);
    VK_F9:
      if Shift = [] then
        btnDownloadClick(Sender);
    VK_F11:
      begin
        //Autocode
        if Shift = [] then
          btnAutoCodeClick(Sender);
        //toggle debug mode
        if Shift = [ssAlt] then
        begin
          frmAbout.imgAVRUBDMouseDown(Sender, mbLeft, [ssLeft, ssShift], 0, 0);
        end;
        if Shift = [ssAlt, ssShift] then
        begin
          if CompareText(cfg.Language, 'Chinese') = 0 then
          begin
            mmoHex.Text := #13#10 + tipcn[Random(MAXTIPCN)];
          end
          else
          begin
            mmoHex.Text := #13#10 + tipen[Random(MAXTIPEN)];
          end;
        end;
      end;
    VK_SCROLL: //reload default value
      begin
        if Shift = [ssAlt] then
        begin
          if DeleteFile(ExtractFilePath(Application.ExeName) + 'avrubd.ini') then
            loadcfg;
        end;
      end;
    Ord('A'):
      if Shift = [ssAlt] then
        btnAboutClick(Sender);
  end;
end;

procedure TfrmMain.mnMCUClick(Sender: TObject);
begin
  HWD := frmOption.Handle;
  frmOption.pgctOption.ActivePageIndex := 0;
  btnOptionClick(Sender);
  HWD := frmMain.Handle;
end;

procedure TfrmMain.mnSystemClick(Sender: TObject);
begin
  HWD := frmOption.Handle;
  frmOption.pgctOption.ActivePageIndex := 1;
  btnOptionClick(Sender);
  HWD := frmMain.Handle;
end;

procedure TfrmMain.mnComPortSetupClick(Sender: TObject);
begin
  HWD := frmOption.Handle;
  frmOption.pgctOption.ActivePageIndex := 2;
  btnOptionClick(Sender);
  HWD := frmMain.Handle;
end;

procedure TfrmMain.mnContentClick(Sender: TObject);
var
  hlp: string;
begin
  hlp := ExtractFilePath(Application.ExeName) + cfg.Language + '.hlp';
  if not FileExists(hlp) then
  begin
    hlp := ExtractFilePath(Application.ExeName) + 'English.hlp';
    if not FileExists(hlp) then
      Exit;
  end;
  Application.HelpFile := hlp;
  Application.HelpCommand(HELP_CONTENTS, 0);
end;

procedure TfrmMain.pmLangEnglishClick(Sender: TObject);
begin
  if Sender is TMenuItem then
  begin
    cfg.Language := TMenuItem(Sender).Caption;
    loadlanguage(cfg.Language);
  end;
end;

procedure TfrmMain.HisFileClick(Sender: TObject);
var
  FileName: string;
begin
  FileName := pmHisFile.Items[TMenuItem(Sender).MenuIndex].Caption;
  if FileExists(FileName) then
  begin
    OpenFile(FileName);
    dlgOpen.FileName := FileName;
  end;
end;

procedure TfrmMain.tmrSmartMouseTimer(Sender: TObject);
var
  mousekey: Byte;
  rect: TRect;
begin
  GetCursorPos(mp);
  if (Abs(msx - mp.X) > 2) or (Abs(msy - mp.Y) > 2) then
  begin
    msx := mp.X;
    msy := mp.Y;
    tmrSmartMouse.Tag := 990 div tmrSmartMouse.Interval;
    Exit;
  end;

  if tmrSmartMouse.Tag > 0 then
    tmrSmartMouse.Tag := tmrSmartMouse.Tag - 1;
  //get mouse status
  mousekey := 0;
  if (GetKeyState(VK_LBUTTON) and $80) = $80 then
    mousekey := mousekey or $01;
  if (GetKeyState(VK_RBUTTON) and $80) = $80 then
    mousekey := mousekey or $02;
  if (GetKeyState(VK_MBUTTON) and $80) = $80 then
    mousekey := mousekey or $04;
  //No mouse button pressed
  if mousekey <> 0 then
    tmrSmartMouse.Tag := 0;

  if tmrSmartMouse.Tag = 1 then
  begin
    //If active window is used form
    if GetForegroundWindow <> HWD then
      Exit;
    //Get form size
    GetWindowRect(HWD, rect);
    if (msx < rect.Left) or (msx > rect.Right) or (msy < rect.Top) or (msy > rect.Bottom) then
      Exit;

    mouse_event(MOUSEEVENTF_LEFTDOWN or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
  end;
end;

procedure TfrmMain.tmrClockTimer(Sender: TObject);
begin
  tmrClock.Tag := tmrClock.Tag + 1;
  if tmrClock.Tag > 4 then
  begin
    tmrClock.Tag := 0;
    ClockCnt := ClockCnt + 1;
    if (not Debug) and (ClockCnt > 4) then
      ClockCnt := 0;
  end;

  case ClockCnt of
    1: //Date
      stat.Panels[1].Text := FormatDateTime('hh:nn:ss', Now - TS);
    2: //elapse time
      stat.Panels[1].Text := FormatDateTime('yyyy-mmm-dd', Now);
    3: //Down counter
      stat.Panels[1].Text := Format('%d/%d, %d/%d', [StaSum.sum - StaSum.fail, StaSum.sum, StaToday.sum - StaToday.fail, StaToday.sum]);
    4: //version
      stat.Panels[1].Text := 'AVRUBD v4';
    5: //run counter
      stat.Panels[1].Text := IntToStr(cfg.run);
    6: //comport configure
      stat.Panels[1].Text := ComPort.Port + ',' + BaudRateToStr(ComPort.BaudRate) + ',' + DataBitsToStr(ComPort.DataBits) + ',' + StopBitsToStr(ComPort.StopBits);
    7: //Font
      stat.Panels[1].Text := cfg.FontName + ',' + IntToStr(cfg.FontSize);
    8: //Language
      stat.Panels[1].Text := cfg.Language;
  else
    //Time
    stat.Panels[1].Text := FormatDateTime('hh:nn:ss', Now);
    ClockCnt := 0;
  end;
end;

//click panel[1] will change to next status

procedure TfrmMain.statMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if X > (stat.Panels[0].Width + 1 + 2 + 1) then
  begin
    if Button = mbLeft then
      ClockCnt := ClockCnt + 1;
    if Button = mbRight then
    begin
      ClockCnt := ClockCnt - 1;
    end;
    if Debug then
    begin
      if ClockCnt > 8 then
        ClockCnt := 0;
      if ClockCnt < 0 then
        ClockCnt := 8;
    end
    else
    begin
      if ClockCnt > 4 then
        ClockCnt := 0;
      if ClockCnt < 0 then
        ClockCnt := 4;
    end;
    if Button = mbMiddle then
      tmrClock.Tag := -2147483647
    else
      tmrClock.Tag := -5;
    tmrClockTimer(Sender);
  end
  else
    tbMenuMouseDown(Sender, Button, Shift, X, Y);
end;

procedure TfrmMain.OpenFile(FileName: string);
var
  i, j: Integer;
  addr, len: Integer;
  s: string;
  hf: TMenuItem;
  buf: CBuf;
  stream: TMemoryStream;
  aub: TFastIniFile;
begin
  //avoid reload
  mmoBin.Tag := 12;

  sr.Name := FileName;
  FileModify(sr);

  //Disable download function
  btnDownload.Enabled := False;
  mnDownLoad.Enabled := False;
  btnEncrypt.Enabled := False;
  btnDecrypt.Enabled := False;
  DataSize := 0;

  //fill buffer with config value
  if cfg.fillunused then
  begin
    FormatHexStr(cfg.filldata, buf);
    len := (Length(cfg.filldata) + 2) div 3;
    for i := 0 to 1024 * 1024 - 1 do
    begin
      DataBuf[i] := buf[i mod len];
    end;
  end
  else
  begin
    for i := 0 to 1024 * 1024 - 1 do
      DataBuf[i] := $FF;
  end;

  mmoHex.Clear;
  mmoBin.DataSize := 0;
  stream := TMemoryStream.Create;
  try
    if CompareText('.aub', ExtractFileExt(FileName)) = 0 then
    begin
      aub := TFastIniFile.Create(FileName);
      try
        s := aub.ReadString('target', 'id', '');
        if s <> 'aub' then
        begin
          Application.MessageBox(PChar(lang.msgFmtErr + ': [ ' + FileName + ' ]'), PChar(lang.msgError), MB_OK + MB_ICONSTOP);
          Exit;
        end;
        s := aub.ReadString('buf', 'data', '');
        DataSize := Length(s) div 2;
        for i := 0 to DataSize - 1 do
        begin
          DataBuf[i] := CharToByte(s[i * 2 + 1], s[i * 2 + 2]);
        end;
        mmoBin.LoadFromBuffer(DataBuf, DataSize);

        cfg.Protocol := aub.ReadInteger('cfg', 'protocol', 0);
        cfg.FrameLen := aub.ReadInteger('cfg', 'FrameLen', 128);
        cfg.SendTime := aub.ReadInteger('cfg', 'Timeout', 500);
        cfg.BootCnt := aub.ReadInteger('cfg', 'BootCnt', 10);
        cfg.FlashSize := aub.ReadInteger('cfg', 'FlashSize', 2);
        cfg.keyMode := aub.ReadInteger('cfg', 'KeyMode', 1);
        cfg.keyASC := aub.ReadString('cfg', 'KeyASC', 'd');
        cfg.keyHEX := aub.ReadString('cfg', 'KeyHEX', '64');
        cfg.filldata := aub.ReadString('cfg', 'FillData', 'FF CF');
        cfg.fillunused := aub.ReadBoolean('cfg', 'Fill', True);
        cfg.AutoSize := aub.ReadBoolean('cfg', 'AutoSize', True);
        cfg.SendRST := aub.ReadBoolean('cfg', 'SendRST', False);
        cfg.CmdRST := aub.ReadString('cfg', 'CmdRST', '7E');
        cfg.RTS := aub.ReadBoolean('cfg', 'RTS', False);
        cfg.DTR := aub.ReadBoolean('cfg', 'DTR', False);
        ComPort.BaudRate := StrToBaudRate(aub.ReadString('cfg', 'BaudRate', '19200'));
        ComPort.DataBits := StrToDataBits(aub.ReadString('cfg', 'DataBits', '8'));
        ComPort.StopBits := StrToStopBits(aub.ReadString('cfg', 'StopBits', '1'));
        ComPort.Parity.Bits := StrToParity(aub.ReadString('cfg', 'Parity', 'None'));
        ComPort.FlowControl.FlowControl := StrToFlowControl(aub.ReadString('cfg', 'FlowControl', 'None'));

        pgctMain.ActivePage := tsBin;
      finally
        aub.Free;
      end;
    end
    else
    begin
      if CompareText('.hex', ExtractFileExt(FileName)) = 0 then
      begin
        //HEX
        mmoHex.Lines.LoadFromFile(FileName);
        if not UpdateBin(FileName) then
          Exit;
        pgctMain.ActivePage := tsHex;
      end
      else
      begin
        //BIN
        mmoBin.LoadFromFile(FileName);
        UpdateHex;
      end;
    end;
    loginfo('');
    s := Format('V %-25s [%s]', [lang.msgLoadFileSuccess + ':', FileName]);
    loginfo(s);
    s := Format('> %-25s 0x%.04X/%d', [lang.msgFileLength + ':', DataSize, DataSize]);
    loginfo(s);
    s := Format('> %-25s 0x%.04X/%d', [lang.msgFlashSize + ':', FlashSize[cfg.FlashSize], FlashSize[cfg.FlashSize]]);
    loginfo(s);

    btnDownload.Enabled := True;
    mnDownLoad.Enabled := True;
    btnEncrypt.Enabled := True;
    btnDecrypt.Enabled := True;

    if not (CompareText('.aub', ExtractFileExt(FileName)) = 0) then
    begin
      //make buffer size is multiple frame size
      if (DataSize mod cfg.FrameLen) <> 0 then
      begin
        DataSize := ((DataSize + cfg.FrameLen - 1) div cfg.FrameLen) * cfg.FrameLen;
      end;

      //even frame
      if ((DataSize div cfg.FrameLen) mod 2) = 1 then
      begin
        DataSize := DataSize + cfg.FrameLen;
      end;

    end;
    s := Format('> %-25s 0x%.04X/%d', [lang.msgBufferSize + ':', mmoBin.DataSize, mmoBin.DataSize]);
    loginfo(s);

    //change Caption
    Caption := 'AVRUBD - [' + FileName + ']';

    for i := 0 to pmHisFile.Items.Count - 1 do
      pmHisFile.Items[i].Checked := False;
    if pmHisFile.Items.Find(FileName) <> nil then
    begin
      pmHisFile.Items.Find(FileName).Free;
    end;

    //History file list
    if pmHisFile.Items.Count >= MaxHisFileCount then
      pmHisFile.Items.Delete(pmHisFile.Items.Count - 1);
    hf := TMenuItem.Create(Self);
    hf.OnClick := HisFileClick;
    hf.Caption := FileName;
    hf.RadioItem := True;
    hf.Checked := True;
    s := ExtractFileExt(FileName);
    if CompareText(s, '.hex') = 0 then
      hf.ImageIndex := 14
    else
      if CompareText(s, '.aub') = 0 then
        hf.ImageIndex := 17
      else
        hf.ImageIndex := 15;
    pmHisFile.Items.Insert(0, hf);
  finally
    stream.Free;
    mmoBin.Tag := 0;

⌨️ 快捷键说明

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