📄 gpio.cpp
字号:
//**************************************************************
// GPIO.cpp
//
// Copyright 2007 by ITC, . All rights reserved.
//
// Author: Zhou Gang
//
// Date: 2007.7.27
//
// Description:
//
//**************************************************************
#include <iostream>
#include <bitset>
#include "stdafx.h"
#include "ezusbsys.h"
#include "usb100.h"
#include "gpio.h"
#pragma warning(disable:4996)
using namespace std;
GPIO :: GPIO()
{
m_handle = NULL;
}
GPIO :: ~GPIO()
{
}
int GPIO :: Initial(int Num)
{
int i = Num;
m_handle = NULL;
TCHAR m_buffer[32];
_stprintf(m_buffer,_T("\\\\.\\ezusb-%d"),i);
m_handle = CreateFile((LPCTSTR)m_buffer,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
NULL);
if (m_handle == INVALID_HANDLE_VALUE)
{
return (i+1);
}//IF
return 0;
}
int GPIO :: ControlGPIO(TCHAR gpioControlCode)
{
printf("Control Code : %2.2x\n", gpioControlCode);
int ErrorCode = Initial(0);//Start device
if(0 == ErrorCode)
{
_tprintf(_T("Initial USB Port %d SUCCESS\n"),0);
}
else
{
_tprintf(_T("Initial USB Port %d FAIL\n"),0);
return 1;
}
DWORD outleng=0;
TCHAR outbuffer[18];
tcPinBitStatus = 0;
//tcPinBitStatusLOW = 0;
HRESULT hr = DeviceIoControl(m_handle,IOCTL_Ezusb_GET_DEVICE_DESCRIPTOR,NULL,0,outbuffer,sizeof(outbuffer),&outleng,0);
if (0==hr)
{
return USBERROR;
}
PUSB_DEVICE_DESCRIPTOR devicedescriptor=(PUSB_DEVICE_DESCRIPTOR)outbuffer;
if((devicedescriptor->idProduct==0x0003)&&(devicedescriptor->idVendor==0xed02))
{
WORD getPipe[50];
hr = DeviceIoControl(m_handle,IOCTL_Ezusb_GET_PIPE_INFO,NULL,0,getPipe,sizeof(getPipe),&outleng,0);
if (0==hr)
{
return USBERROR;
}
TCHAR buffer1[MAXBUFFERSIZE];
TCHAR readbuffer[MAXBUFFERSIZE];
TCHAR writebuffer[MAXBUFFERSIZE];
memset(buffer1, gpioControlCode, sizeof(buffer1));
memset(readbuffer, 0, sizeof(readbuffer));
buffer1[sizeof(buffer1)-1] = 0;
_tcscpy(writebuffer,buffer1);
DWORD nBytes=0;
int bResult;
BULK_TRANSFER_CONTROL bulkControl;
for(int i=0;i<5;i++)
{
bulkControl.pipeNum= 1;
bResult=DeviceIoControl(m_handle,IOCTL_EZUSB_BULK_WRITE,&bulkControl,sizeof(BULK_TRANSFER_CONTROL),buffer1,MAXBUFFERSIZE,&nBytes,0);
if (0 == bResult)
return USBERROR;
bulkControl.pipeNum= 3;
bResult=DeviceIoControl(m_handle,IOCTL_EZUSB_BULK_READ,&bulkControl,sizeof(BULK_TRANSFER_CONTROL),readbuffer,MAXBUFFERSIZE,&nBytes,0);
if (0 == bResult)
return USBERROR;
}
tcPinBitStatus = readbuffer[1];
//tcPinBitStatusLOW = readbuffer[0];
}
if(0 == ErrorCode)
{
cout << "USB Port" << 0 << "PASS" << endl;
}
else
{
}
CloseHandle(m_handle);
return 0;
}
int GPIO::GetOutputPinStatus(OUT char *szOutputPinStatus)
{
char temp[32];
memset(temp, 0, sizeof(temp));
std::bitset<16> bOutput ( tcPinBitStatus );
strcpy(temp, bOutput.to_string().c_str());
strncpy(szOutputPinStatus, temp+1, 15);
return 0;
}
//int GPIO::GetInputPinStatus(OUT char *szInputPinStatus)
//{
// char temp[32];
// memset(temp, 0, sizeof(temp));
// std::bitset<8> bInput ( tcPinBitStatus );
//
// strcpy(temp, bInput.to_string().c_str());
// strncpy(szInputPinStatus, temp, 1);
// strncat(szInputPinStatus, temp+8, 8);
//
// return 0;
//}
int GPIO :: run(int UsbDeviceNum,int *UsbErrPort, int _argc, char **_argv)
{
int i;
int result = 0;
for(i=0;i<UsbDeviceNum;i++)
{
int ErrorCode = Initial(i);//Start device
if(0 == ErrorCode)
{
_tprintf(_T("Initial USB Port %d SUCCESS\n"),i);
}
else
{
_tprintf(_T("Initial USB Port %d FAIL\n"),i);
*(UsbErrPort+i) = i;
result = 1;
return result;
}
int n = _argc;
char **p = _argv;
if (n < 3 || n > 18)
{
menu();
return 1;
}
if (n == 3)
{
//printf("%s\t%s\n", *(p+1), *(p+2));
if (0 == stricmp("o", *(p+1)))
{
if (0 == stricmp("t", *(p+2)))
{
printf("set all\n");
ControlGPIO(OUTPUT_SET_ALL);
return 0;
}
else if (0 == stricmp("c", *(p+2)))
{
printf("clear all\n");
ControlGPIO(OUTPUT_CLEAR_ALL);
return 0;
}
else
{
menu();
return 1;
}
}
else if (0 == stricmp("i", *(p+1)))
{
if (0 == stricmp("t", *(p+2)))
{
ControlGPIO(INPUT_SET_ALL);
return 0;
}
else if (0 == stricmp("c", *(p+2)))
{
ControlGPIO(INPUT_CLEAR_ALL);
return 0;
}
else
{
menu();
return 1;
}
}
else
{
menu();
return 1;
}
}
int loop = n - 3;
if ((0 == stricmp("o", *(p+1))) && (0 == stricmp("t", *(p+2))))
{
for (int i = 0; i < loop; i++)
{
int j = atoi(*(p+3+i));
if ((j <= 0) || (j > 15) )
{
menu();
return 1;
}
ControlGPIO(OUTPUT_SET + j - 1);
}
return 0;
}
else if ((0 == stricmp("o", *(p+1))) && (0 == stricmp("c", *(p+2))))
{
for (int i = 0; i < loop; i++)
{
int j = atoi(*(p+3+i));
if ((j <= 0) || (j > 15) )
{
menu();
return 1;
}
ControlGPIO(OUTPUT_CLEAR + j - 1);
}
return 0;
}
else if ((0 == stricmp("i", *(p+1))) && (0 == stricmp("t", *(p+2))))
{
for (int i = 0; i < loop; i++)
{
int j = atoi(*(p+3+i));
if ((j <= 0) || (j > 9) )
{
menu();
return 1;
}
ControlGPIO(INPUT_SET + j - 1);
}
return 0;
}
else if ((0 == stricmp("i", *(p+1))) && (0 == stricmp("c", *(p+2))))
{
for (int i = 0; i < loop; i++)
{
int j = atoi(*(p+3+i));
if ((j <= 0) || (j > 9) )
{
menu();
return 1;
}
ControlGPIO(INPUT_CLEAR + j - 1);
}
return 0;
}
else
{
menu();
return 1;
}
if(0 == ErrorCode)
{
cout << "USB Port" << i << "PASS" << endl;
}
else
{
_tprintf(_T("USB Port %d BAUDFAIL\n"),i);
*(UsbErrPort+i) = i;
result = 1;
}
CloseHandle(m_handle);
}
return result;
}
void GPIO::menu()
{
printf("*********** USER GUIDE **********\n");
printf("gpio -o -t :Set all the output gpio pins to 1\n");
printf("gpio -o -t 1 3 12 :Set the 1st 3rd 12th output gpio pins to 1\n");
printf("gpio -o -c :Clear all the output gpio pins to 0\n");
printf("gpio -o -c 1 3 12 :Clear the 1st 3rd 12th output gpio pins to 1\n");
printf("gpio -i -t :Set all the input gpio pins to 1\n");
printf("gpio -i -t 1 3 12 :Set the 1st 3rd 12th input gpio pins to 1\n");
printf("gpio -i -c :Clear all the input gpio pins to 0\n");
printf("gpio -i -c 1 3 12 :Clear the 1st 3rd 12th input gpio pins to 1\n");
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -