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

📄 lightlampdlg.cpp

📁 基于ZIGBEE协议通讯的开关程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*
** ============================================================================
**
** FILE
**  lightlampDlg.cpp
**
** DESCRIPTION
**  Lighting Lamp Implementation File
**
** CREATED
**  I.A.Marsden Integration UK Ltd
**
** COPYRIGHT
** Copyright 2005 Integration Associates Inc.  All rights reserved.
**
** LIMITED USE LICENSE.  By using this software, the user agrees to the terms of the 
**                       following license.  If the user does not agree to these terms, 
**                       then this software should be returned within 30 days and a full 
**                       refund of the purchase price or license fee will provided.  
**                       Integration Associates hereby grants a license to the user on the 
**                       following terms and conditions:  The user may use, copy, modify, 
**                       revise, translate, abridge, condense, expand, collect, compile, 
**                       link, recast, distribute, transform or adapt this software solely 
**                       in connection with the development of products incorporating 
**                       integrated circuits sold by Integration Associates.  Any other use 
**                       for any other purpose is expressly prohibited with the prior written 
**                       consent of Integration Associates.
**
** Any copy or modification made must satisfy the following conditions:
** 
** 1. Both the copyright notice and this permission notice appear in all copies of the software, 
**    derivative works or modified versions, and any portions thereof, and that both notices 
**    appear in supporting documentation.
**
** 2. All copies of the software shall contain the following acknowledgement: "Portions of this 
**    software are used under license from Integration Associates Inc. and are copyrighted."
**
** 3  Neither the name of Integration Associates Inc. nor any of its subsidiaries may be used 
**    to endorse or promote products derived from this software without specific prior written 
**    permission.
**
** THIS SOFTWARE IS PROVIDED BY "AS IS" AND ALL WARRANTIES OF ANY KIND, INCLUDING THE IMPLIED 
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR USE, ARE EXPRESSLY DISCLAIMED.  THE DEVELOPER 
** SHALL NOT BE LIABLE FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.  
** THIS SOFTWARE MAY NOT BE USED IN PRODUCTS INTENDED FOR USE IN IMPLANTATION OR OTHER DIRECT 
** LIFE SUPPORT APPLICATIONS WHERE MALFUNCTION MAY RESULT IN THE DIRECT PHYSICAL HARM OR INJURY 
** TO PERSONS.  ALL SUCH IS USE IS EXPRESSLY PROHIBITED.
** ============================================================================
*/
#include "stdafx.h"
#include "lightlamp.h"
#include "lightlampDlg.h"
#include "ConfigureDlg.h"
#include "ZBDLLApi.h"
#include "ZigBeeIf.h"
#include "bisync_tokens.h"
#include "ZigBeeStructures.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

/*
** ============================================================================
** Function Prototypes
** ============================================================================
*/
ClightlampDlg* m_lightlampDlg = NULL;

// CAboutDlg dialog used for App About

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

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

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

// Implementation
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()

/*
** ============================================================================
**
** FUNCTION NAME:
**  ClightlampDlg
**
** DESCRIPTION
**  Constructor
**
** AUTHOR
**  Ian Marsden
**
** ============================================================================
*/
ClightlampDlg::ClightlampDlg(CWnd* pParent /*=NULL*/)
	: CDialog(ClightlampDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  m_lightlampDlg = this; // The single instantiation
}

/*
** ============================================================================
**
** FUNCTION NAME:
**  DoDataExchange
**
** DESCRIPTION
**  Load / Unload the controls
**
** AUTHOR
**  Ian Marsden
**
** ============================================================================
*/
void ClightlampDlg::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, IDC_STATIC_STATUS, m_Status);
  DDX_Control(pDX, IDC_STATIC_LAMP, m_Lamp);
}

/*
** ============================================================================
**
** FUNCTION NAME:
**  ClightlampDlg MESSAGE MAP
**
** AUTHOR
**  Ian Marsden
**
** ============================================================================
*/
BEGIN_MESSAGE_MAP(ClightlampDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
  ON_WM_DESTROY()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
  ON_BN_CLICKED(IDC_BUTTON_BIND, OnBnClickedButtonBind)
  ON_MESSAGE(WM_USER+ZDO_SET_CONFIRM, ZDO_SET_confirm )
  ON_MESSAGE(WM_USER+ZDO_RESET_CONFIRM, ZDO_RESET_confirm )
  ON_MESSAGE(WM_USER+ZDO_START_CONFIRM, ZDO_START_confirm )
  ON_MESSAGE(WM_USER+ZDO_END_DEVICE_BIND_CONFIRM, ZDO_END_DEVICE_BIND_confirm )
  ON_MESSAGE(WM_USER+AF_INDIRECT_INDICATION, AF_INDIRECT_indication )
  ON_MESSAGE(WM_USER+ZDO_NLME_SYNC_CONFIRM, ZDO_NLME_SYNC_confirm )
END_MESSAGE_MAP()

/*
** ============================================================================
**
** FUNCTION NAME:
**  OnInitDialog
**
** DESCRIPTION
**  Initialise the dialog
**
** AUTHOR
**  Ian Marsden
**
** ============================================================================
*/
BOOL ClightlampDlg::OnInitDialog()
{
  int retval;
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	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);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

  nBitmapOff.LoadBitmap( IDB_BITMAP_BULBOFF ); // Load bitmaps
  nBitmapOn.LoadBitmap( IDB_BITMAP_BULBON );
  m_Lamp.SetBitmap( nBitmapOff.operator HBITMAP() ); // Set the starting picture to be off
  m_lightlampDlg->m_Status.SetWindowText( "Lamp Off" ); // Set the text too

  hZigBee = new CZigBeeIf(); // The ZigBee If
  if ( hZigBee == NULL )
  {
    // Error creating the ZigBee interface
    ::MessageBox( m_hWnd, "Error Creating ZigBee Interface", "ZigBee Light Lamp", MB_ICONSTOP | MB_OK );
    return FALSE;
  }

  retval = hZigBee->ZBIFConnect( m_hWnd ); // Find some hardware
  if (retval != STK_SUCCESS)
  {
    // Error 
    ::MessageBox( m_hWnd, "Error Opening ZigBee Interface", "ZigBee Light Lamp", MB_ICONSTOP | MB_OK );
    return FALSE;
  }

  CConfigureDlg hConfigureDlg; // The Config Dialog
  hConfigureDlg.s_Channel = "00020000"; // Initial dialog settings
  hConfigureDlg.s_PANId = "1AAA";
  hConfigureDlg.b_ZC = 1;
  hConfigureDlg.b_ZR = 0;
  hConfigureDlg.b_ZED = 0;
	INT_PTR nResponse = hConfigureDlg.DoModal(); // Display the dialog
  if (nResponse == IDCANCEL)
	{
    return FALSE; // Cancelled Config so just leave
	}

  // get the device type
  if ( hConfigureDlg.b_ZC != 0 ) DevType = 0;
  else if ( hConfigureDlg.b_ZR != 0 ) DevType = 1;
  else DevType = 2;
  
  CHAR scanbuf[6];
  scanbuf[2] = '\0';

  // Get the channel mask
  int i = hConfigureDlg.s_Channel.Find( 'x' );
  if ( i != -1 )
  {
    hConfigureDlg.s_Channel.Delete( 0, i+1 ); // Strip off the 0x
  }
  i = hConfigureDlg.s_Channel.GetLength();
  if ( i < 8 )
  { 
    hConfigureDlg.s_Channel = "FFFFFFFF"; // Too few digits, so overwrite
  }
  for ( i = 4 ; i > 0 ; i-- )
  {
    scanbuf[0] = hConfigureDlg.s_Channel[0];
    scanbuf[1] = hConfigureDlg.s_Channel[1];
    hConfigureDlg.s_Channel.Delete( 0, 2 );
    sscanf( scanbuf, "%02x", &scanbuf[0] );
    ScanChannels[i-1] = scanbuf[0];
  }

  // Get the PANId
  i = hConfigureDlg.s_PANId.Find( 'x' );
  if ( i != -1 )
  {
    hConfigureDlg.s_PANId.Delete( 0, i+1 ); // Strip off the 0x
  }
  i = hConfigureDlg.s_PANId.GetLength();
  if ( i < 4 )
  { 
    hConfigureDlg.s_PANId = "1AAA"; // Too few digits, so overwrite
  }
  for ( i = 2 ; i > 0 ; i-- )
  {
    scanbuf[0] = hConfigureDlg.s_PANId[0];
    scanbuf[1] = hConfigureDlg.s_PANId[1];
    hConfigureDlg.s_PANId.Delete( 0, 2 );
    sscanf( scanbuf, "%02x", &scanbuf[0] );
    PANId[i-1] = scanbuf[0];
  }

  // Subscribe to relevant Callbacks
  hZigBee->ZBIFSubscribe( ZDO_SET_CONFIRM );
  hZigBee->ZBIFSubscribe( ZDO_RESET_CONFIRM );
  hZigBee->ZBIFSubscribe( ZDO_START_CONFIRM );
  hZigBee->ZBIFSubscribe( ZDO_END_DEVICE_BIND_CONFIRM );
  hZigBee->ZBIFSubscribe( AF_INDIRECT_INDICATION );
  hZigBee->ZBIFSubscribe( ZDO_NLME_SYNC_CONFIRM );

  Sleep( 1000 ); // Let the stack start and the world sort itself out

  hZigBee->ZDO_RESET_request( 0 ); // Send a reset into the stack
  OperatingState = STATE_START; // We are now starting up the device
 	m_Status.SetWindowText( "Resetting Stack" ); // Tell the user
  
  return TRUE;  // return TRUE  unless you set the focus to a control
}

/*
** ============================================================================
**
** FUNCTION NAME:
**  OnSysCommand
**
** DESCRIPTION
**  System Command Menu
**
** AUTHOR
**  Ian Marsden
**
** ============================================================================
*/
void ClightlampDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

/*
** ============================================================================
**
** FUNCTION NAME:
**  OnPaint
**
** DESCRIPTION
**  Handle the paint request
**
** AUTHOR
**  Ian Marsden
**
** ============================================================================
*/
void ClightlampDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

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

		// Center icon in client rectangle
		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;

⌨️ 快捷键说明

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