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

📄 lightlampdlg.cpp

📁 基于ZIGBEE协议通讯的开关程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

/*
** ============================================================================
**
** FUNCTION NAME:
**  OnQueryDragIcon
**
** DESCRIPTION
**  The system calls this function to obtain the cursor to display while the user drags
**  the minimized window.
**
** AUTHOR
**  Ian Marsden
**
** ============================================================================
*/
HCURSOR ClightlampDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

/*
** ============================================================================
**
** FUNCTION NAME:
**  OnDestroy
**
** DESCRIPTION
**  Close the ZigBee Stack and device
**
** AUTHOR
**  Ian Marsden
**
** ============================================================================
*/
void ClightlampDlg::OnDestroy()
{
  if ( hZigBee == NULL ) return; // If no stack operational just return
  hZigBee->ZBIFDisconnect(); // Disconnect
}

/*
** ============================================================================
**
** FUNCTION NAME:
**  OnBnClickedButtonBind
**
** DESCRIPTION
**  Action the Bind Button
**
** AUTHOR
**  Ian Marsden
**
** ============================================================================
*/
void ClightlampDlg::OnBnClickedButtonBind()
{
	UCHAR cl1[2] = { 0, 1 }; // Output Cluster List
	UCHAR cl2[1] = { 0 }; // Input Cluster List

  if ( hZigBee == NULL ) return; // If no stack operational just return

	m_Status.SetWindowText( "Bind in progress" ); // Tell the user

  // Send Bind Request
  hZigBee->ZDO_END_DEVICE_BIND_request (
    	0,					// LocalCoordinator,
    	1,					// DEP,
    	0x104,			// ProfileID,
    	1,          // NumInClusters,
      cl2,				// InClusterList,
    	2,					// NumOutClusters,
    	cl1					// OutClusterList
	);

}

/*
** ============================================================================
**
** FUNCTION NAME:
**  ZigBee ZDO_SET_Confirm Callback
**
** ============================================================================
*/
LRESULT ClightlampDlg::ZDO_SET_confirm( WPARAM wParam, LPARAM lParam)
{
  unsigned char* msg;
  unsigned char msglength = (unsigned char) wParam;
  msg = (unsigned char*) lParam;
  /* ============== Create Parameters ============== */
  unsigned char   Status = msg[2];
  unsigned char   Attribute = msg[3];
  /* ============== Created Parameters ============== */

  UCHAR buffer[11];

  if ( Status != 0 )
  {
    // SET Error 
    m_lightlampDlg->OperatingState = STATE_FAIL;
   	m_lightlampDlg->m_Status.SetWindowText( "Set Failed" ); // Tell the user
  }
  else
  {
    switch ( Attribute )
    {
    case ZIB_CONFIG_PANID:
      // Have successfully Set the PANId.
      // Build the node descriptor
      buffer[0] = m_lightlampDlg->DevType; // Device Type
      buffer[1] = 0x40; // Frequency Band
      buffer[2] = 0x4F; // MAC Capabilities
      buffer[3] = 0; // Manufacturers Code
      buffer[4] = 0; // Manufacturers Code
      buffer[5] = 0x7F; // Max Buffer
      buffer[6] = 0; // Max Transfer
      buffer[7] = 0; // Max Transfer
		  m_lightlampDlg->hZigBee->ZDO_SET_request( ZIB_CONFIG_NODE_DESCRIPTOR, 8, buffer );
     	m_lightlampDlg->m_Status.SetWindowText( "Setting the Node Descriptor" ); // Tell the user
      break;
    case ZIB_CONFIG_NODE_DESCRIPTOR:
      // Have successfully Set the Node Descriptor.
      // Build the mode & params descriptor
      buffer[0] = m_lightlampDlg->ScanChannels[0]; // Scan Channels
      buffer[1] = m_lightlampDlg->ScanChannels[1]; // Scan Channels
      buffer[2] = m_lightlampDlg->ScanChannels[2]; // Scan Channels
      buffer[3] = m_lightlampDlg->ScanChannels[3]; // Scan Channels
      buffer[4] = 5; // Scan Duration
      buffer[5] = 1; // Protocol Version
      buffer[6] = 1; // Stack Profile
      buffer[7] = 0xF; // Beacon Order
      buffer[8] = 0xF; // Superframe Order
      buffer[9] = 0; // Battery Life Extension
      buffer[10] = 0; // Security Level
      m_lightlampDlg->hZigBee->ZDO_SET_request( ZIB_CONFIG_NWK_MODE_AND_PARAMS, 11, buffer );
    	m_lightlampDlg->m_Status.SetWindowText( "Setting the Mode Descriptor" ); // Tell the user
      break;
    case ZIB_CONFIG_NWK_MODE_AND_PARAMS:
      // Have successfully Set the Mode & Params Descriptor.
      // Build the simple descriptor
		  buffer[0] = 1;	    // gives 1 for endpoint
		  buffer[1] = 4;      // app profileid = 0x0104
		  buffer[2] = 1;      // app profileid = 0x0104			
		  buffer[3] = 2;			// device id light = 0x0002
		  buffer[4] = 0;			// device id light = 0x0002
		  buffer[5] = 0;			// version flags
      buffer[6] = 2;	    // light input cluster count
			buffer[7] = 0;	    // cluster 0 is input
			buffer[8] = 1;      // cluster 1 is input
			buffer[9] = 1;      // light output cluster count
			buffer[10] = 0;     // cluster 0 is output
		  m_lightlampDlg->hZigBee->ZDO_SET_request( ZIB_CONFIG_SIMPLE_DESCRIPTOR, 11, buffer );
    	m_lightlampDlg->m_Status.SetWindowText( "Setting the Simple Descriptor" ); // Tell the user
      break;
    case ZIB_CONFIG_SIMPLE_DESCRIPTOR:
      // Have successfully Set the Simple Descriptor.
      // Now start the stack
		  m_lightlampDlg->hZigBee->ZDO_START_request(  );
      m_lightlampDlg->OperatingState = STATE_START;
      if (m_lightlampDlg->DevType == 0)	m_lightlampDlg->m_Status.SetWindowText( "Starting a Network" );
      else	m_lightlampDlg->m_Status.SetWindowText( "Joining a Network" ); // Tell the user
      break;
    default:
      break;
    }
  }
  delete [] msg; // Tidy up the memory associated with this message
  return 0;
}

/*
** ============================================================================
**
** FUNCTION NAME:
**  ZigBee ZDO_RESET_Confirm Callback
**
** ============================================================================
*/
LRESULT ClightlampDlg::ZDO_RESET_confirm( WPARAM wParam, LPARAM lParam)
{
  unsigned char* msg;
  unsigned char msglength = (unsigned char) wParam;
  msg = (unsigned char*) lParam;
  /* ============== Create Parameters ============== */
  unsigned char   Status = msg[2];
  /* ============== Created Parameters ============== */

  if ( Status != 0 )
  {
    // Reset Error 
    m_lightlampDlg->OperatingState = STATE_FAIL;
   	m_lightlampDlg->m_Status.SetWindowText( "Reset Failed" ); // Tell the user
  }
  else
  {
    // Next Set the PANId
   	m_lightlampDlg->m_Status.SetWindowText( "Setting the PANID" ); // Tell the user
    m_lightlampDlg->hZigBee->ZDO_SET_request( ZIB_CONFIG_PANID, 2, m_lightlampDlg->PANId );
  }

  delete [] msg; // Tidy up the memory associated with this message
  return 0;
}

/*
** ============================================================================
**
** FUNCTION NAME:
**  ZigBee ZDO_START_Confirm Callback
**
** ============================================================================
*/
LRESULT ClightlampDlg::ZDO_START_confirm( WPARAM wParam, LPARAM lParam)
{
  unsigned char* msg;
  unsigned char msglength = (unsigned char) wParam;
  msg = (unsigned char*) lParam;
  /* ============== Create Parameters ============== */
  unsigned char   Status = msg[2];
  unsigned short   ShortAddr = (msg[4]<<8) & msg[3];
  /* ============== Created Parameters ============== */

  if ( Status != 0 )
  {
    // Start Error 
    m_lightlampDlg->OperatingState = STATE_FAIL;
    CString strError;
    strError.Format( "Start Failed %02x", Status );
   	m_lightlampDlg->m_Status.SetWindowText( strError ); // Tell the user
  }
  else
  {
    // We are operational
    m_lightlampDlg->OperatingState = STATE_RUNNING;
   	m_lightlampDlg->m_Status.SetWindowText( "Device Started" );

    if (m_lightlampDlg->DevType == 2)
    {
      // End device so poll for data
      m_lightlampDlg->hZigBee->ZDO_NLME_SYNC_request( 1 ); // Sync & Track
    }

  }

  delete [] msg; // Tidy up the memory associated with this message
  return 0;
}

/*
** ============================================================================
**
** FUNCTION NAME:
**  ZigBee AF_INDIRECT_indication Callback
**
** ============================================================================
*/
LRESULT ClightlampDlg::AF_INDIRECT_indication( WPARAM wParam, LPARAM lParam)
{
  unsigned char* msg;
  unsigned char msglength = (unsigned char) wParam;
  msg = (unsigned char*) lParam;
  /* ============== Create Parameters ============== */
  unsigned char   DstEndpoint = msg[2];
  unsigned char   ClusterId = msg[3];
  unsigned char   AfduLength = msg[4];
  unsigned char*  Afdu = &msg[5];
  unsigned char   WasBroadcast = msg[5+AfduLength];
  unsigned char   SecurityStatus = msg[6+AfduLength];
  /* ============== Created Parameters ============== */

  if ( ( Afdu[0] == 0x21 ) &&
      ( Afdu[2] == 4 ) &&
      ( Afdu[3] == 1 ) &&
      ( Afdu[4] == 0 ) &&
      ( Afdu[5] == 0 ))
  {
    // Lighting Move to Level Command
    if ( Afdu[6] == 0 )
    {
      // Switch Off
      m_lightlampDlg->m_Lamp.SetBitmap( nBitmapOff.operator HBITMAP() ); 
   	  m_lightlampDlg->m_Status.SetWindowText( "Lamp Off" ); // Tell user
    }
    else
    {
      // Switch On
      m_lightlampDlg->m_Lamp.SetBitmap( nBitmapOn.operator HBITMAP() ); 
   	  m_lightlampDlg->m_Status.SetWindowText( "Lamp On" ); // Tell user
    }
  }
  else
  {
 	  m_lightlampDlg->m_Status.SetWindowText( "Indirect Data Mismatch" ); // Tell user
  }
  delete [] msg; // Tidy up the memory associated with this message
  return 0;
}

/*
** ============================================================================
**
** FUNCTION NAME:
**  ZigBee ZDO_END_DEVICE_BIND_confirm Callback
**
** ============================================================================
*/
LRESULT ClightlampDlg::ZDO_END_DEVICE_BIND_confirm( WPARAM wParam, LPARAM lParam)
{
  unsigned char* msg;
  unsigned char msglength = (unsigned char) wParam;
  msg = (unsigned char*) lParam;
  /* ============== Create Parameters ============== */
  unsigned char   Status = msg[2];
  /* ============== Created Parameters ============== */

  if ( Status != 0 )
  {
    // Bind Error 
   	m_lightlampDlg->m_Status.SetWindowText( "Bind Failed" ); // Tell user
  }
  else
  {
    // BIND OK 
    m_lightlampDlg->m_Status.SetWindowText( "Bind Succeeded" ); // Tell user
  }
  delete [] msg; // Tidy up the memory associated with this message
  return 0;
}

/*
** ============================================================================
**
** FUNCTION NAME:
**  ZigBee ZDO_NLME_SYNC_confirm Callback
**
** ============================================================================
*/
LRESULT ClightlampDlg::ZDO_NLME_SYNC_confirm( WPARAM wParam, LPARAM lParam)
{
  unsigned char* msg;
  unsigned char msglength = (unsigned char) wParam;
  msg = (unsigned char*) lParam;
  /* ============== Create Parameters ============== */
  unsigned char   Status = msg[2];
  /* ============== Created Parameters ============== */

  if ( Status != 0 )
  {
    // Sync Error 
   	m_lightlampDlg->m_Status.SetWindowText( "Sync Failed" ); // Tell user
  }
  delete [] msg; // Tidy up the memory associated with this message
  return 0;
}

⌨️ 快捷键说明

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