📄 dlg7290.cpp
字号:
// DLG7290.cpp : implementation file
//
#include "stdafx.h"
#include "Magic2410.h"
#include "DLG7290.h"
#include "I2C.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern HANDLE hFileIIC;
extern CRITICAL_SECTION critical_section_iic;
#define I2C_ADDR_7290 0x70 /* ZLG7290 的I2C地址 */
/////////////////////////////////////////////////////////////////////////////
// DLG7290 dialog
DLG7290::DLG7290(CWnd* pParent /*=NULL*/)
: CDialog(DLG7290::IDD, pParent)
{
//{{AFX_DATA_INIT(DLG7290)
m_strDisp = _T("");
m_strKeyValue = _T("");
m_strBusFreq = _T("");
//}}AFX_DATA_INIT
}
void DLG7290::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(DLG7290)
DDX_Text(pDX, IDC_DISP, m_strDisp);
DDX_Text(pDX, IDC_KEY_VALUE, m_strKeyValue);
DDX_Text(pDX, IDC_BUS_FREQ, m_strBusFreq);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(DLG7290, CDialog)
//{{AFX_MSG_MAP(DLG7290)
ON_BN_CLICKED(IDC_START_SCAN, OnStartScan)
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// DLG7290 message handlers
void DLG7290::OnStartScan()
{
// TODO: Add your control notification handler code here
CButton *Key;
Key = (CButton *)GetDlgItem(IDC_START_SCAN);
BYTE I2cSla = I2C_ADDR_7290, divid = 3;
BYTE buf[8] = {0x0A,0x00,0x01, 0x04,0x02,0x0C, 0x03,0x05 }; // S3C2410A //
DWORD actlen, ret, speed;
UpdateData(TRUE);
if (hFileIIC == NULL)
{
MessageBox(_T("I2C 驱动未打开!"));
Key->SetWindowText(_T("开始扫描")); /* 按键标题保持不变 */
Key->SetCheck(0); /* 按键保持为未按下 */
return;
}
// 设置 I2C 从机器件地址
ret = ::DeviceIoControl(hFileIIC, IOCTL_SET_I2C_SLAVE, &I2cSla, 1,
NULL, 0, NULL, NULL);
if (ret != TRUE)
{
CloseHandle(hFileIIC);
hFileIIC = NULL;
MessageBox(_T("设置 I2C 从机地址失败!"));
return;
}
// 设置 I2C 总线速度
ret = ::DeviceIoControl(hFileIIC, IOCTL_SET_SPEED_DIV, &divid, 1,
&speed, 1, &actlen, NULL);
if (ret != TRUE)
{
CloseHandle(hFileIIC);
hFileIIC = NULL;
MessageBox(_T("设置 I2C 总线速度失败!"));
return;
}
m_strBusFreq.Format(_T("%dHz"), speed); /* 显示I2C总线频率 */
UpdateData(FALSE);
zlg7290_disp(buf, 8); /* 显示 S3C2410A */
UINT state = Key->GetState();
if ((state & 0x0003) == 1) /* 按键按下 */
{
Key->SetWindowText(_T("停止扫描")); /* 按键标题改变, 提示下次操作 */
SetTimer(1, 20, NULL); /* 启动定时器, 进行采样 */
}
else /* 按键弹起 */
{
Key->SetWindowText(_T("开始扫描")); /* 按键标题复原 */
KillTimer(1); /* 取消定时 */
}
}
void DLG7290::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
ushort key;
char pcharbuff[8];
BYTE pdispbuff[8];
DWORD len;
BYTE I2cSla = I2C_ADDR_7290;
DWORD ret;
UpdateData(TRUE);
len = m_strDisp.GetLength();
if (len > 8) len = 8;
LPTSTR pStr = m_strDisp.GetBuffer(len);
if (len <= 8)
{
// 将 Unicode 的字符转换为字节型
WideCharToMultiByte(CP_ACP, 0, pStr, len, pcharbuff, len, NULL, NULL);
if (strtodata((BYTE *)pcharbuff, pdispbuff, len) != 0) /* ASCII转换为整数 */
{
KillTimer(1);
MessageBox(_T("输入字符含有非16进制数!"));
goto ERR;
}
}
// 设置 I2C 从机器件地址
EnterCriticalSection(&critical_section_iic);
ret = ::DeviceIoControl(hFileIIC, IOCTL_SET_I2C_SLAVE, &I2cSla, 1,
NULL, 0, NULL, NULL);
if (ret != TRUE)
{
CloseHandle(hFileIIC);
hFileIIC = NULL;
KillTimer(1);
MessageBox(_T("设置 I2C 从机地址失败!"));
return;
}
if (len == 8)
zlg7290_disp((BYTE *)pdispbuff, 8); /* 驱动数码管显示 */
if (zlg7290_getkey(&key) != 0) /* 读取按键键值 */
{
KillTimer(1);
MessageBox(_T("读取键值失败!"));
goto ERR;
}
if (zlg7290_sndcmd(0x70, 1 << (key - 1)) != 0) /* 闪烁 */
{
KillTimer(1);
MessageBox(_T("控制闪烁失败!"));
goto ERR;
}
LeaveCriticalSection(&critical_section_iic);
key &= 0X1F;
m_strKeyValue.Format(_T("0x%x"), key); /* 显示当前按键键值 */
int i;
for(i = 0; i < 16; i++)
((CStatic*)GetDlgItem(IDC_STATIC_KEY0+i))->SetBitmap (m_bitmap[0]);
if((key != 0) && (key < 17))
((CStatic*)GetDlgItem(IDC_STATIC_KEY0+key-1))->SetBitmap (m_bitmap[1]);
UpdateData(FALSE);
CDialog::OnTimer(nIDEvent);
return;
ERR: /* 出错处理代码 */
LeaveCriticalSection(&critical_section_iic);
CButton *Key = (CButton *)GetDlgItem(IDC_START_SCAN);
Key->SetWindowText(_T("开始扫描")); /* 按键标题保持不变 */
Key->SetCheck(0); /* 按键保持为未按下 */
CDialog::OnTimer(nIDEvent);
}
/***************************************************************
函数名称: chartoint()
功能描述: 将一个ASCII码转换为一个整数
输入参数: chr: 要转换的字符
输出参数: cint: 储存转换过来的数据
返 回: 0: 成功 1: 失败
***************************************************************/
int DLG7290::chartoint(unsigned char chr, unsigned char *cint)
{
unsigned char cTmp;
cTmp = chr - 48;
if(cTmp >= 0 && cTmp <= 9)
{
*cint = cTmp;
return 0;
}
cTmp = chr - 65;
if(cTmp >= 0 && cTmp <= 5)
{
*cint = (cTmp + 10);
return 0;
}
cTmp = chr - 97;
if(cTmp>=0 && cTmp <= 5)
{
*cint = (cTmp+10);
return 0;
}
return 1;
}
/***************************************************************
函数名称: strtodata()
功能描述: 将一串ASCII码转换为相应的整数
输入参数: unsigned char str: 要转换的字符
int len: 长度
输出参数: unsigned char data: 储存转换过来的数据
返 回: 0: 成功 1: 失败
***************************************************************/
int DLG7290::strtodata(unsigned char *str, unsigned char *data, int len)
{
unsigned char cTmp = 0;
int i = 0;
for(int j = 0;j < len; j++)
{
if(chartoint(str[i++], &cTmp))
return 1;
data[j] = cTmp;
}
return 0;
}
/*********************************************************************************************************
** Function name: zlg7290_disp
** Descriptions: control zlg7290 to driver led
** 控制zlg7290驱动led
** Input: unsigned char *buf: display buffer
unsigned char num: display number
** Output: NULL
** Created by: Ming Yuan Zheng 郑明远
** Created Date: 2006-01-09
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
void DLG7290::zlg7290_disp(unsigned char *buf, unsigned char num)
{
unsigned char i;
unsigned char temp[3];
DWORD actlen;
for(i=0; i<num; i++)
{
temp[0] = 0x07;
temp[1] = 0x60 + i;
temp[2] = *buf;
buf++;
::WriteFile(hFileIIC, temp, 3, &actlen, NULL);
Sleep(5);
}
}
/*********************************************************************************************************
** Function name: zlg7290_getkey
** Descriptions: get the key value from zlg7290
** 从zlg7290取得按键键值
** Input: unsigned short *key: the gotten key value
** Output: 0 : OK -1: fail
** Created by: Ming Yuan Zheng 郑明远
** Created Date: 2006-01-09
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int DLG7290::zlg7290_getkey(unsigned short *key)
{
unsigned char temp[2];
DWORD actlen;
temp[0] = 1;
temp[1] = 0;
if (::WriteFile(hFileIIC, temp, 1, &actlen, NULL) != TRUE) /* 发送子地址 */
return -1;
if (::ReadFile(hFileIIC, temp, 2, &actlen, NULL) != TRUE) /* 读取键值 */
return -1;
Sleep(2);
*key = temp[0] + (temp[1] * 256);
return 0;
}
/*********************************************************************************************************
** Function name: zlg7290_sndcmd
** Descriptions: send command to zlg7290
** 向zlg7290发送命令
** Input: unsigned char dat1: command word 1
unsigned char dat2: command word 2
** Output: 0 : OK -1: fail
** Created by: Ming Yuan Zheng 郑明远
** Created Date: 2006-01-09
**-------------------------------------------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------------------------------------------
********************************************************************************************************/
int DLG7290::zlg7290_sndcmd(unsigned char dat1, unsigned char dat2)
{
unsigned char temp[3];
DWORD actlen;
temp[0] = 0x07;
temp[1] = dat1;
temp[2] = dat2;
if (::WriteFile(hFileIIC, temp, 3, &actlen, NULL) != TRUE)
return -1;
Sleep(1);
return 0;
}
BOOL DLG7290::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_strKeyValue = "无键按下"; // 显示"无按键按下"
m_strBusFreq = "0Hz"; // 总线时钟为0
m_strDisp = "87654321"; // 数码管默认显示
m_bitmap[0].LoadBitmap(IDB_BITMAP_BLUE);
m_bitmap[1].LoadBitmap(IDB_BITMAP_RED);
UpdateData(FALSE); // 更新显示
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -