现在的位置: 首页 > 综合 > 正文

control battery chg &dischg

2013年05月15日 ⁄ 综合 ⁄ 共 10018字 ⁄ 字号 评论关闭

// BatteryToolDlg.cpp : 龟郎
//
#include "stdafx.h"
#include "BatteryTool.h"
#include "BatteryToolDlg.h"
#include <WinUser.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define IOCTL_NBDEVCTL_PORT_IO_READ /
 CTL_CODE(FILE_DEVICE_UNKNOWN, 0x703, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_NBDEVCTL_PORT_IO_WRITE /
 CTL_CODE(FILE_DEVICE_UNKNOWN, 0x704, METHOD_BUFFERED, FILE_ANY_ACCESS)

// 癸 App About ㄏノ CAboutDlg 癸杠よ遏

class CAboutDlg : public CDialog
{
public:
 CAboutDlg();

// 癸杠よ遏戈
 enum { IDD = IDD_ABOUTBOX };

 protected:
 virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV や穿

// 祘Α絏龟
protected:
 DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()

// CBatteryToolDlg 癸杠よ遏

CBatteryToolDlg::CBatteryToolDlg(CWnd* pParent /*=NULL*/)
 : CDialog(CBatteryToolDlg::IDD, pParent)
 , LowerLimit(0)
 ,UpperLimit(100)
 , iBTPowPercent(0)
 ,start(false)
 ,bCharging(true)
 ,bMesageboxOpen(true)
{
 //m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBatteryToolDlg::DoDataExchange(CDataExchange* pDX)
{
 CDialog::DoDataExchange(pDX);
 DDX_Text(pDX, IDC_LowerLimit, LowerLimit);
 DDV_MinMaxInt(pDX, LowerLimit, 0, 100);
 DDX_Text(pDX, IDC_UpperLimit, UpperLimit);
 DDV_MinMaxInt(pDX, UpperLimit, 0, 100);
 DDX_Text(pDX, IDC_BatteryPowPercent, iBTPowPercent);
 DDV_MinMaxInt(pDX, iBTPowPercent, 0, 100);
}

BEGIN_MESSAGE_MAP(CBatteryToolDlg, CDialog)
 ON_WM_SYSCOMMAND()
 ON_WM_PAINT()
 ON_WM_QUERYDRAGICON()
 ON_WM_TIMER()
 //}}AFX_MSG_MAP
 ON_BN_CLICKED(IDOK, &CBatteryToolDlg::OnBnClickedOk)
 ON_BN_CLICKED(IDC_BUTTON_Stop, &CBatteryToolDlg::OnBnClickedButtonStop)
END_MESSAGE_MAP()

// CBatteryToolDlg 癟矪瞶盽Α

BOOL CBatteryToolDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

 // 盢 [闽...] ╰参
 
 // IDM_ABOUTBOX ゲ斗╰参㏑絛瞅ぇい
 ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
 ASSERT(IDM_ABOUTBOX < 0xF000);

 CMenu* pSysMenu = GetSystemMenu(FALSE);
 if (pSysMenu != NULL)
 {
  CString strAboutMenu;
  strAboutMenu.LoadString(IDS_ABOUTBOX);
  if (!strAboutMenu.IsEmpty())
  {
   pSysMenu->AppendMenu(MF_SEPARATOR);
   pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  }
 }

 // 砞﹚癸杠よ遏瓜ボ讽莱ノ祘Α跌怠ぃ琌癸杠よ遏
 // 琜穦笆眖ㄆ穨
 SetIcon(m_hIcon, TRUE);   // 砞﹚瓜ボ
 SetIcon(m_hIcon, FALSE);  // 砞﹚瓜ボ

 // TODO: 肂﹍砞﹚

 //*****Command argument*****
 CString CommandLine= theApp.m_lpCmdLine;
 CommandLine.MakeLower();

 int index=CommandLine.Find("/l");
 if (index!=-1)
 {
  CString cNum;
  cNum=CommandLine.Mid( index+2,4);
  LowerLimit=atoi(cNum);
 }
 index=CommandLine.Find("/u");
 if (index!=-1)
 {
  CString cNum;
  cNum=CommandLine.Mid( index+2,4);
  UpperLimit=atoi(cNum);
 }

 if (UpperLimit >100 | LowerLimit<0 | UpperLimit<LowerLimit)
 {
  MessageBox("Command argument error! Please input correct type:/r/n Batterytool.exe /u N /l L /r/nwhere N and L  are  numer between 0 and 100 .");
  ExitProcess(0);
 }
 else if(CommandLine=="")
 {
  UpperLimit=90;
  LowerLimit=70;
 }

 UpdateData(false);
 //*****Command argument*****

 iBTPowPercent=EcIO.GetBatteryStatus();
 SetDlgItemInt(IDC_BatteryPowPercent,iBTPowPercent);
 SetTimer(1,5000,(TIMERPROC) NULL);
 
 if (EcIO.GetACStatus() && EcIO.GetChargeStatus() && (iBTPowPercent<100))
  SetDlgItemText(IDC_BatteryStatus,"AC mode and Charging");
 else if (iBTPowPercent==100)
  SetDlgItemText(IDC_BatteryStatus,"AC mode");
 else
 {
  SetDlgItemText(IDC_BatteryStatus,"Discharging");
  bCharging=false;
 }

 for (int i=0;i<2;i++)
  hThread[i] = CreateThread( NULL, 0,WhileThread,0,CREATE_SUSPENDED,0);

 //test report
 
 BTStatus.Open("BatteryStatus.txt",CFile::modeCreate | CFile::modeReadWrite | CFile::typeText | CFile::shareDenyNone);
 BTStatus.SeekToBegin();
 Result=1;
 BTStatus.Write("1",1);
 
 
 BTStatus.Close();

 report.Open("BatteryReport.txt",CFile::modeCreate  | CFile::modeReadWrite);
 report.SeekToEnd();
 report.Write("Battery Report : /r/n",20);
 SetTimer(2,60000,(TIMERPROC) NULL);

 OnBnClickedOk();
 return TRUE;  // 肚 TRUE埃獶眤癸北兜砞﹚礘翴
}

void CBatteryToolDlg::OnTimer(UINT_PTR nIDEvent)
{
 iBTPowPercent=EcIO.GetBatteryStatus();
 SetDlgItemInt(IDC_BatteryPowPercent,iBTPowPercent);
 /*SetDlgItemInt(IDC_3E,EcIO.GetBatteryError1());
 SetDlgItemInt(IDC_3F,EcIO.GetBatteryError2());
 SetDlgItemInt(IDC_E8,EcIO.GetBatteryError3());*/
 bCharging=EcIO.GetACStatus();
 
 /*if (EcIO.GetACStatus() && EcIO.GetChargeStatus() && (iBTPowPercent<100))
   SetDlgItemText(IDC_BatteryStatus,"AC mode and Charging");
 else if (EcIO.GetACStatus() && EcIO.GetChargeStatus())
  SetDlgItemText(IDC_BatteryStatus,"AC mode");
 else
  SetDlgItemText(IDC_BatteryStatus,"Discharging");*/
 if (EcIO.GetACStatus()  && (iBTPowPercent<100))
   SetDlgItemText(IDC_BatteryStatus,"AC mode and Charging");
 else if (EcIO.GetACStatus() )
  SetDlgItemText(IDC_BatteryStatus,"AC mode");
 else
  SetDlgItemText(IDC_BatteryStatus,"Discharging");

 BTStatus.Open("BatteryStatus.txt",CFile::modeReadWrite | CFile::typeText | CFile::shareDenyNone);
 if (UpperLimit >=iBTPowPercent && LowerLimit <=iBTPowPercent)
 {
   SetDlgItemText(IDC_Result,"PASS!");
   if (bMesageboxOpen)
    CloseErrorMessagebox();
   if (Result==1)
   {
    BTStatus.SeekToBegin();
    Result=0;
    BTStatus.Write("0",1);
   }
 }
 else
 {
  SetDlgItemText(IDC_Result,"Not ready");
  
  if (Result==0)
  {
   BTStatus.SeekToBegin();
   Result=1;
   BTStatus.Write("1",1);
   
  }
 }

 BTStatus.Close();
 if (start)
 {
  if (UpperLimit <iBTPowPercent && bCharging)
  {
   EcIO.SetChargeStatus(false);
   for (int i=0;i<2;i++)
   {
    while (ResumeThread(hThread[i])>1)
     ;
   }
   bCharging=EcIO.GetACStatus();
  }
  else if (LowerLimit >iBTPowPercent && !bCharging)
  {
   EcIO.SetChargeStatus(true);
   for (int i=0;i<2;i++)
    SuspendThread(hThread[i]);
   
   if(EcIO.GetACStatus())
   {
    bCharging=true;
   }
   else
   {
    start=false;
    /*GetDlgItem(IDC_BUTTON_Stop)->EnableWindow(false);
    GetDlgItem(IDOK)->EnableWindow();
    GetDlgItem(IDC_LowerLimit)->EnableWindow();
    GetDlgItem(IDC_UpperLimit)->EnableWindow();*/
    bMesageboxOpen=true;
    MessageBox("Please connect AC power and try again.","Battery Capcity Error",MB_ICONERROR);
    start=true;
   }
  }

  if (UpperLimit <iBTPowPercent && bCharging)
  {
   start=false;
   /*GetDlgItem(IDC_BUTTON_Stop)->EnableWindow(false);
   GetDlgItem(IDOK)->EnableWindow();
   GetDlgItem(IDC_LowerLimit)->EnableWindow();
   GetDlgItem(IDC_UpperLimit)->EnableWindow();*/
   bMesageboxOpen=true;
   MessageBox("Please remove AC power and try again.","Battery Capcity Error",MB_ICONERROR);
   start=true;
  }

 }

 if (iBTPowPercent>=LowerLimit && iBTPowPercent<=UpperLimit)
 {
   if (!bCharging)
   {
    for (int i=0;i<2;i++)
     SuspendThread(hThread[i]);
   }
 }

 if (nIDEvent==2)
 {
  CString batteryreport;
  CTime time=CTime::GetCurrentTime();  
  
  batteryreport.Format("%d/%d %d:%d 3E=%X , 3F=%X , E8=%X./r/n",time.GetMonth(),time.GetDay(),time.GetHour(),time.GetMinute(),EcIO.GetBatteryError1(),EcIO.GetBatteryError2(),EcIO.GetBatteryError3());
  report.Write(batteryreport,batteryreport.GetAllocLength());
 }
 
}

void CBatteryToolDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
 if ((nID & 0xFFF0) == IDM_ABOUTBOX)
 {
  CAboutDlg dlgAbout;
  dlgAbout.DoModal();
 }
 else
 {
  CDialog::OnSysCommand(nID, lParam);
 }
}

// 狦盢程て秙眤癸杠よ遏眤惠璶祘Α絏
// 獽酶籹瓜ボ癸ㄏノゅン/浪跌家Α MFC 莱ノ祘Α
// 琜穦笆ЧΘ穨

void CBatteryToolDlg::OnPaint()
{
 if (IsIconic())
 {
  CPaintDC dc(this); // 酶籹杆竚ず甧

  SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

  // 盢瓜ボ竚いノめ狠痻
  int cxIcon = GetSystemMetrics(SM_CXICON);
  int cyIcon = GetSystemMetrics(SM_CYICON);
  CRect rect;
  GetClientRect(&rect);
  int x = (rect.Width() - cxIcon + 1) / 2;
  int y = (rect.Height() - cyIcon + 1) / 2;

  // 磞酶瓜ボ
  dc.DrawIcon(x, y, m_hIcon);
 }
 else
 {
  CDialog::OnPaint();
 }
}

// 讽ㄏノ╈Σ程て跌怠
// ╰参㊣硂眔村夹陪ボ
HCURSOR CBatteryToolDlg::OnQueryDragIcon()
{
 return static_cast<HCURSOR>(m_hIcon);
}

void CBatteryToolDlg::OnBnClickedOk()
{
 // TODO: 北兜矪瞶盽Α祘Α絏
 UpdateData();
 
 if (LowerLimit >=0 && LowerLimit <=UpperLimit && UpperLimit<=100)
 {
  GetDlgItem(IDC_LowerLimit)->EnableWindow(false);
  GetDlgItem(IDC_UpperLimit)->EnableWindow(false);
  start=true;
  iBTPowPercent=EcIO.GetBatteryStatus();
  SetDlgItemInt(IDC_BatteryPowPercent,iBTPowPercent);
  GetDlgItem(IDC_BUTTON_Stop)->EnableWindow();
  GetDlgItem(IDOK)->EnableWindow(false);

  if (UpperLimit <iBTPowPercent)
  {
   EcIO.SetChargeStatus(false);
   for (int i=0;i<2;i++)
   {
    while (ResumeThread(hThread[i])>1)
     ;
   }
   bCharging=false;
   SetDlgItemText(IDC_Result,"Not ready");
  }
  else if (LowerLimit >iBTPowPercent)
  {
   EcIO.SetChargeStatus(true);
   if(EcIO.GetACStatus())
   {
    bCharging=true;
    SetDlgItemText(IDC_Result,"Not ready");
   }
   else
   {
    start=false;
    /*GetDlgItem(IDC_BUTTON_Stop)->EnableWindow(false);
    GetDlgItem(IDOK)->EnableWindow();
    GetDlgItem(IDC_LowerLimit)->EnableWindow();
    GetDlgItem(IDC_UpperLimit)->EnableWindow();*/
    bMesageboxOpen=true;
    MessageBox("Please connect AC power and try again.","Battery Capcity Error",MB_ICONERROR);
    start=true;
   }
  }
  else
  {
   SetDlgItemText(IDC_Result,"PASS!");
  }
 }
 if (LowerLimit >=UpperLimit)
  AfxMessageBox("LowerLimit must less than UpperLimit");
}

void CBatteryToolDlg::GetShutdownPrivilege()
{
 HANDLE htoken;
 TOKEN_PRIVILEGES tp;
 LUID luid;

 OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&htoken);
 if ( !LookupPrivilegeValue(
  NULL,            // lookup privilege on local system
  SE_SHUTDOWN_NAME,   // privilege to lookup
  &luid ) )        // receives LUID of privilege
 {
   EcIO.ShowError( GetLastError() );
 }

 tp.PrivilegeCount = 1;
 tp.Privileges[0].Luid = luid;
 tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

 // Enable the privilege or disable all privileges.

 if ( !AdjustTokenPrivileges(
  htoken,
  FALSE,
  &tp,
  sizeof(TOKEN_PRIVILEGES),
  (PTOKEN_PRIVILEGES) NULL,
  (PDWORD) NULL) )
 {
   EcIO.ShowError( GetLastError() );
 }
}

void CBatteryToolDlg::OnBnClickedButtonStop()
{
 start=false;
 EcIO.SetChargeStatus(true);
 for (int i=0;i<2;i++)
  SuspendThread(hThread[i]);
 GetDlgItem(IDC_BUTTON_Stop)->EnableWindow(false);
 GetDlgItem(IDOK)->EnableWindow();
 GetDlgItem(IDC_LowerLimit)->EnableWindow();
 GetDlgItem(IDC_UpperLimit)->EnableWindow();
}

DWORD WINAPI WhileThread( LPVOID lpParam )
{
 while (true)
  ;
 return 0;
}

 

void CBatteryToolDlg::OnCancel( )
{
 EcIO.SetChargeStatus(true);
 BTStatus.Close();
 CDialog::OnCancel();
}

void CBatteryToolDlg::CloseErrorMessagebox()
{
 CWnd * MessageH= FindWindow(NULL,"Battery Capcity Error");
 if (MessageH != NULL)
 {
  ::PostMessage( MessageH->m_hWnd,WM_CLOSE,0,0);
  bMesageboxOpen=false;
 }
}

抱歉!评论已关闭.