📄 leddlg.cpp
字号:
// LEDDlg.cpp : implementation file
//
#include "stdafx.h"
#include "LED.h"
#include "LEDDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLEDDlg dialog
CLEDDlg::CLEDDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLEDDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLEDDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CLEDDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLEDDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLEDDlg, CDialog)
//{{AFX_MSG_MAP(CLEDDlg)
ON_BN_CLICKED(IDC_OPEN_PIO1, OnOpenPio1)
ON_BN_CLICKED(IDC_CLOSE_PIO1, OnClosePio1)
ON_BN_CLICKED(IDC_LED1_ON, OnLed1On)
ON_BN_CLICKED(IDC_LED1_OFF, OnLed1Off)
ON_BN_CLICKED(IDC_LED2_ON, OnLed2On)
ON_BN_CLICKED(IDC_LED3_ON, OnLed3On)
ON_BN_CLICKED(IDC_LED4_ON, OnLed4On)
ON_BN_CLICKED(IDC_LED2_OFF, OnLed2Off)
ON_BN_CLICKED(IDC_LED3_OFF, OnLed3Off)
ON_BN_CLICKED(IDC_LED4_OFF, OnLed4Off)
ON_WM_DESTROY()
ON_BN_CLICKED(IDC_BUTTON_LED1, OnButtonLed1)
ON_BN_CLICKED(IDC_BUTTON_LED2, OnButtonLed2)
ON_BN_CLICKED(IDC_STOP, OnStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLEDDlg message handlers
BOOL CLEDDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
/********************************************************************************
实验代码: LED 的控制
作 者: 范 海 帆
*********************************************************************************/
/*******************************************************************
定义GPIO编号
********************************************************************/
#define LED1 11 //定义
#define LED2 12
#define LED3 4
#define LED4 6
#define WATER 1
#define HOURSE 2
//包含命令码头文件
#include"gpio.h"
//文件句柄
HANDLE hFile=INVALID_HANDLE_VALUE;
HANDLE hThreadLed;
/************************************************
定义LED的亮灭
******************************************************/
BOOL SetLed(BYTE pinnum, BOOL status,HANDLE hfileled) //
{
BOOL ret;
switch(pinnum)
{
case LED1:
case LED2:
if(status)
ret = ::DeviceIoControl(hfileled, IOCTL_GPE_SET_PIN, &pinnum, 1, NULL, 0, NULL, NULL);
else
ret = ::DeviceIoControl(hfileled, IOCTL_GPE_CLR_PIN, &pinnum, 1, NULL, 0, NULL, NULL);
break;
case LED3:
case LED4:
if(status)
ret = ::DeviceIoControl(hfileled, IOCTL_GPH_SET_PIN, &pinnum, 1, NULL, 0, NULL, NULL);
else
ret = ::DeviceIoControl(hfileled, IOCTL_GPH_CLR_PIN, &pinnum, 1, NULL, 0, NULL, NULL);
break;
default:
ret = FALSE;
break;
}
return ret;
}
//“打开GPIO驱动”按钮单击事件代码
void CLEDDlg::OnOpenPio1()
{
// TODO: Add your control notification handler code here
BOOL ret_GPE11,ret_GPE12,ret_GPH4,ret_GPH6;
BYTE pinnum_GPE11=11,pinnum_GPE12=12,pinnum_GPH4=4,pinnum_GPH6=6;
hFile=CreateFile(TEXT("PIO1:"),GENERIC_READ|GENERIC_WRITE,0,
NULL,OPEN_EXISTING,0,0);
if (hFile == INVALID_HANDLE_VALUE)
{
MessageBox(_T("打开 GPIO 驱动失败!"));
return;
}
else
MessageBox(_T("打开 GPIO 驱动成功!"));
// 设置 GPE11 为输出口
ret_GPE11 = ::DeviceIoControl(hFile, IOCTL_GPE_SET_PIN_OUT, &pinnum_GPE11, 1, NULL, 0, NULL, NULL);
if (ret_GPE11 != TRUE)
{
OnClosePio1();
MessageBox(_T("设置 GPE11 引脚输出失败!"));
return;
}
// 设置 GPE12 为输出口
ret_GPE12 = ::DeviceIoControl(hFile, IOCTL_GPE_SET_PIN_OUT, &pinnum_GPE12, 1, NULL, 0, NULL, NULL);
if (ret_GPE12 != TRUE)
{
OnClosePio1();
MessageBox(_T("设置 GPE12 引脚输出失败!"));
return;
}
// 设置 GPH4 为输出口
ret_GPH4 = ::DeviceIoControl(hFile, IOCTL_GPH_SET_PIN_OUT, &pinnum_GPH4, 1, NULL, 0, NULL, NULL);
if (ret_GPH4 != TRUE)
{
OnClosePio1();
MessageBox(_T("设置 GPE12 引脚输出失败!"));
return;
}
// 设置 GPH6 为输出口
ret_GPH6 = ::DeviceIoControl(hFile, IOCTL_GPH_SET_PIN_OUT, &pinnum_GPH6, 1, NULL, 0, NULL, NULL);
if (ret_GPH6 != TRUE)
{
OnClosePio1();
MessageBox(_T("设置 GPE12 引脚输出失败!"));
return;
}
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_PIO1); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_PIO1);
pOpenButton->EnableWindow(FALSE); /* 禁止按键 */
pCloseButton->EnableWindow(TRUE); /* 使能按键 */
}
void CLEDDlg::OnClosePio1()
{
// TODO: Add your control notification handler code here
if (hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile); //关闭句柄
hFile = INVALID_HANDLE_VALUE;
}
CButton *pOpenButton = (CButton*)GetDlgItem(IDC_OPEN_PIO1); /* 取得控件指针 */
CButton *pCloseButton = (CButton*)GetDlgItem(IDC_CLOSE_PIO1);
pOpenButton->EnableWindow(TRUE); /* 使能按键 */
pCloseButton->EnableWindow(FALSE); /* 禁止按键 */
}
/****************************************************************
LED亮灭的控制
**********************************************************************/
void CLEDDlg::OnLed1On()
{
// TODO: Add your control notification handler code here
BOOL ret;
// 置 GPE11为高电平,LED1亮
ret = SetLed(LED1, TRUE,hFile);
if (ret != TRUE)
MessageBox(_T("设置LED1高电平失败"));
}
void CLEDDlg::OnLed1Off()
{
// TODO: Add your control notification handler code here
BOOL ret;
// 置 GPE11为低电平,LED1灭
ret = SetLed(LED1,FALSE,hFile);
if (ret != TRUE)
MessageBox(_T("设置LED1低电平失败"));
}
void CLEDDlg::OnLed2On()
{
// TODO: Add your control notification handler code here
BOOL ret;
// 置 GPE12为高电平,LED2亮
ret = SetLed(LED2,TRUE,hFile);
if (ret != TRUE)
MessageBox(_T("设置LED2高电平失败"));
}
void CLEDDlg::OnLed2Off()
{
// TODO: Add your control notification handler code here
BOOL ret;
// 置 GPE12为低电平,LED2灭
ret = SetLed(LED2,FALSE,hFile);
if (ret != TRUE)
MessageBox(_T("设置LED2低电平失败"));
}
void CLEDDlg::OnLed3On()
{
// TODO: Add your control notification handler code here
BOOL ret;
// 置 GPH4为高电平,LED3亮
ret = SetLed(LED3,TRUE,hFile);
if (ret != TRUE)
MessageBox(_T("设置LED3高电平失败"));
}
void CLEDDlg::OnLed3Off()
{
// TODO: Add your control notification handler code here
BOOL ret;
// 置 GPH4为低电平,LED3灭
ret = SetLed(LED3,FALSE,hFile);
if (ret != TRUE)
MessageBox(_T("设置LED3低电平失败"));
}
void CLEDDlg::OnLed4On()
{
// TODO: Add your control notification handler code here
BOOL ret;
// 置 GPH6为高电平,LED4亮
ret = SetLed(LED4,TRUE,hFile);
if (ret != TRUE)
MessageBox(_T("设置LED4高电平失败"));
}
void CLEDDlg::OnLed4Off()
{
// TODO: Add your control notification handler code here
BOOL ret;
// 置 GPH6为低电平,LED4灭
ret = SetLed(LED4,FALSE,hFile);
if (ret != TRUE)
MessageBox(_T("设置LED4低电平失败"));
}
void CLEDDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
OnClosePio1();
}
DWORD CLEDDlg::LedThread(PVOID arg)
{
CLEDDlg *pDlgBeep=(CLEDDlg *)arg;
int dly=100;
while(pDlgBeep->LedThread1)
{
if(pDlgBeep->LedStaly==WATER) //流水灯
{
dly=100;//定义延时100ms
SetLed(LED1,TRUE,hFile );
Sleep(dly);
SetLed(LED1,FALSE,hFile );
SetLed(LED2,TRUE,hFile );
Sleep(dly);
SetLed(LED2,FALSE,hFile );
SetLed(LED3,TRUE,hFile );
Sleep(dly);
SetLed(LED3,FALSE,hFile );
SetLed(LED4,TRUE,hFile );
Sleep(dly);
SetLed(LED4,FALSE,hFile );
}
else if(pDlgBeep->LedStaly == HOURSE) //跑马灯
{
dly=200;
SetLed(LED2,FALSE,hFile );
SetLed(LED4,FALSE,hFile );
SetLed(LED1,TRUE,hFile );
SetLed(LED3,TRUE,hFile );
Sleep(dly);
SetLed(LED1,FALSE,hFile );
SetLed(LED3,FALSE,hFile );
SetLed(LED2,TRUE,hFile );
SetLed(LED4,TRUE,hFile );
Sleep(dly);
}
else
Sleep(1000);
}
return (DWORD)0;
}
/**************************************************************************************
跑马灯
***************************************************************************************/
void CLEDDlg::OnButtonLed2()
{
// TODO: Add your control notification handler code here
DWORD dwThreadID;
LedStaly=HOURSE;
if(hThreadLed==NULL)
{
LedThread1=TRUE;
hThreadLed=CreateThread(NULL,NULL,LedThread,this,NULL,&dwThreadID);
if(hThreadLed==NULL)
MessageBox(_T("LED Thread Create Faile"));
}
}
/*********************************************************************************************
流水灯
*********************************************************************************************/
void CLEDDlg::OnButtonLed1()
{
// TODO: Add your control notification handler code here
DWORD dwThreadID;
LedStaly=WATER;
if(hThreadLed==NULL)
{
LedThread1=TRUE;
hThreadLed=CreateThread(NULL,NULL,LedThread,this,NULL,&dwThreadID);
if(hThreadLed==NULL)
MessageBox(_T("LED Thread Create Faile"));
}
}
/*************************************************************
停止
*************************************************************/
void CLEDDlg::OnStop()
{
// TODO: Add your control notification handler code here
LedThread1 = FALSE;
Sleep(100);
if(hThreadLed)
{
CloseHandle(hThreadLed);
hThreadLed=NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -