worksgit add .ž

This commit is contained in:
[ Kristjan Komloši HomePC ]
2017-02-05 19:01:08 +01:00
parent 732776f734
commit fc7aa25c45
2 changed files with 174 additions and 128 deletions

Binary file not shown.

View File

@@ -2,19 +2,19 @@
#include <CyUSBCommon.h> #include <CyUSBCommon.h>
#include <ctype.h> #include <ctype.h>
#include <libusb.h> #include <libusb.h>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <stdint.h>
// Those are flags used for flow control inside the program // Those are flags used for flow control inside the program
uint8_t whichDev=177; // 177 values indicate unset variables. uint16_t whichDev = 0;
uint8_t interfaceNum=177; uint16_t interfaceNum = 0;
uint8_t writeFlag=0; uint8_t writeFlag = 0;
uint8_t readFlag=0; uint8_t readFlag = 0;
uint8_t exitFlag=1; uint8_t exitFlag = 0;
uint8_t pinNumber=177; uint16_t pinNumber = 0;
uint8_t valueToWrite=177; uint16_t valueToWrite = 0;
// those vars are to be used with locateDevice function // those vars are to be used with locateDevice function
const uint8_t maxDevs = 16; // 16 connected devices should be enough... const uint8_t maxDevs = 16; // 16 connected devices should be enough...
const CY_VID_PID deviceVidPid{UINT16(0x04b4), const CY_VID_PID deviceVidPid{UINT16(0x04b4),
@@ -25,7 +25,6 @@ CY_DEVICE_INFO deviceInfoList[maxDevs];
// vars for deviceOpen function // vars for deviceOpen function
CY_HANDLE deviceHandleList[maxDevs]; CY_HANDLE deviceHandleList[maxDevs];
uint8_t defaultInterfaceNum = 0; uint8_t defaultInterfaceNum = 0;
@@ -41,7 +40,7 @@ int verifyArgNum(
int initLib() // initializes the cypress library int initLib() // initializes the cypress library
{ {
printf("Initializing library..."); //printf("Initializing library...");
if (CyLibraryInit() != CY_SUCCESS) // if the init procedure fails if (CyLibraryInit() != CY_SUCCESS) // if the init procedure fails
{ {
puts("The library init failed."); puts("The library init failed.");
@@ -55,23 +54,27 @@ int initLib() // initializes the cypress library
initLib(); // if the library closed gracefully initLib(); // if the library closed gracefully
// try to reinitialize // try to reinitialize
} }
puts("OK"); //puts("OK");
return 0; return 0;
} }
void usage() { void usage() {
puts("Tinio test build 1"); puts("TinI/0 0.1");
puts("Usage:"); puts("Usage:");
//----TODO----// puts("tinio <options>");
// add a command that prints a puts("The supported options are:");
// usage summary. puts("-d<device number> - specifies the desired device - integer 0 to 15");
puts("-i<interface number - specifies the USB interface number - integer 0 to 255");
puts("-s<pin number> - sets the specified pin to the value specified with -v - integer 0 to 11");
puts("-r<pin number> - reads the specified pin's value and prints it to the stdout - integer 0 to 11");
puts("-v<value> - value for -s option - integer 0 to 255");
} }
int evalErrors(const CY_RETURN_STATUS error) { int evalErrors(const CY_RETURN_STATUS error) {
int errCast = int(error); int errCast = int(error);
switch (errCast) { // evaluates the return status of a function switch (errCast) { // evaluates the return status of a function
case 0: // everything goes OK case 0: // everything goes OK
puts("OK"); //puts("OK");
return 0; return 0;
break; break;
case 1: // access denied by the OS case 1: // access denied by the OS
@@ -118,7 +121,7 @@ int evalErrors(const CY_RETURN_STATUS error) {
int locateDevice() // locate the device and verify it's the right one int locateDevice() // locate the device and verify it's the right one
{ {
printf("Locating devices..."); //printf("Locating devices...");
try { try {
CY_RETURN_STATUS retVal; CY_RETURN_STATUS retVal;
retVal = CyGetDeviceInfoVidPid(deviceVidPid, deviceNumList, deviceInfoList, retVal = CyGetDeviceInfoVidPid(deviceVidPid, deviceNumList, deviceInfoList,
@@ -130,8 +133,6 @@ int locateDevice() // locate the device and verify it's the right one
return r; return r;
} }
if (deviceCount > 1) { if (deviceCount > 1) {
puts("More than one device found. The first one will be used unless told "
"otherwise.");
return 0; return 0;
} }
if (deviceCount == 0) { if (deviceCount == 0) {
@@ -153,9 +154,19 @@ int attachDevices() {
return 0; return 0;
} }
int setGPIO(int targetPin, uint8_t value) { int setGPIO() {
int retval = evalErrors(CySetGpioValue(deviceHandleList[whichDev-1], targetPin, value)); if(writeFlag==0)
return retval; return -1;
int errval = evalErrors(CySetGpioValue(deviceHandleList[whichDev], pinNumber, valueToWrite));
return errval;
}
int readGPIO() {
if(readFlag==0)
return -1;
uint8_t value;
int errval= evalErrors(CyGetGpioValue(deviceHandleList[whichDev], pinNumber, &value));
printf("%i\n", value);
return errval;
} }
int isstrdigit(const char *str) { int isstrdigit(const char *str) {
@@ -167,148 +178,183 @@ int isstrdigit(const char *str) {
return 1; return 1;
} }
void unknownOptarg(int option, char *argument) void unknownOptarg(int option, char *argument) {
{ printf("Error: Unknown option argument -- \'%s\' at option -%c \n", argument,
printf("Error: Unknown option argument -- \'%s\' at option -%c \n", argument, option); option);
} }
void pinNum2HiLo(int number) void pinNum2HiLo(int number) {
{ printf("Error: The specified pin number - %i is too high.\n", number);
printf("Error: The specified pin number - %i is too high.", number);
puts("Pin numbers must be between 0 and 11"); puts("Pin numbers must be between 0 and 11");
} }
void rwConflict() void rwConflict() { puts("Error: Can't write and read at the same time!"); }
{
puts("Error: Can't write and read at the same time!");
}
void uselessValue() void uselessValue() { puts("Error: -v option can't be used when reading!"); }
{
puts("Error: -v option can't be used when reading!");
}
void value2Hi(int number) void value2Hi(int number) {
{ printf("Error: The specified value - %i is too big/small.\n", number);
printf("Error: The specified value - %i is too big/small.", number);
puts("Values must be zero or positive integers, smaller than 255."); puts("Values must be zero or positive integers, smaller than 255.");
} }
void devNum2Hi(int number) void devNum2Hi(int number) {
{ printf(
printf("Error: The specified device number - %i is bigger than 15 or negative\n", number); "Error: The specified device number - %i is bigger than 15 or negative!\n",
puts("Note: The number of allowed devices can be adjusted by manipulating maxDevs"); number);
puts("Note: The number of allowed devices can be adjusted by manipulating "
"maxDevs");
puts("variable in the source code."); puts("variable in the source code.");
} }
void intNum2Hi(int number)
{
printf("Error: The specified interface number - %i is too big! \n", number);
puts("Interface numbers must be unsigned 8-bit integers \n (smaller than 256 and bigger or equal than 0).");
}
void test() { void test() {
printf("read: %i | write: %i | pinNumber: %i | valueToWrite: %i \n", readFlag, writeFlag, pinNumber, valueToWrite); printf("read: %i | write: %i | pinNumber: %i | valueToWrite: %i \n", readFlag,
writeFlag, pinNumber, valueToWrite);
printf("whichDev: %i | interfaceNum: %i \n \n", whichDev, interfaceNum); printf("whichDev: %i | interfaceNum: %i \n \n", whichDev, interfaceNum);
} }
int parseCmdLine(int acount, char **arglist) { int parseCmdLine(int acount, char **arglist) {
int opt; int opt;
opt = getopt(acount, arglist, "d:s:r:v:i:"); opt = getopt(acount, arglist, "d:s:r:v:i:");
while (opt != -1) { while (opt != -1) {
switch (opt) { switch (opt) {
case 'd': case 'd':
if(!isstrdigit(optarg)) if (!isstrdigit(optarg)) {
{ unknownOptarg(opt, optarg);
unknownOptarg(opt, optarg); }
} whichDev = atoi(optarg);
whichDev = atoi(optarg); if (whichDev > 15 || whichDev < 0) {
if(whichDev > 15 || whichDev < 0 ) devNum2Hi(whichDev);
{ whichDev = 0;
devNum2Hi(whichDev); exitFlag = 1;
whichDev=177; return -1;
return -1; }
} break;
break;
case 's': case 's':
if(!isstrdigit(optarg)) if (!isstrdigit(optarg)) {
{ unknownOptarg(opt, optarg);
unknownOptarg(opt, optarg); exitFlag = 1;
return -1; return -1;
} }
pinNumber=atoi(optarg); pinNumber = atoi(optarg);
if(pinNumber > 11) { if (pinNumber > 11 || pinNumber < 0) {
pinNum2HiLo(pinNumber); pinNum2HiLo(pinNumber);
pinNumber=177; pinNumber = 0;
return -1; exitFlag = 1;
} return -1;
if(readFlag == 1){ }
rwConflict(); if (readFlag == 1) {
pinNumber=177; rwConflict();
return -1; pinNumber = 0;
} exitFlag = 1;
writeFlag=1; return -1;
readFlag=0; }
break; writeFlag = 1;
readFlag = 0;
break;
case 'r': case 'r':
if(!isstrdigit(optarg)) if (!isstrdigit(optarg)) {
{ unknownOptarg(opt, optarg);
unknownOptarg(opt, optarg); exitFlag = 1;
return -1; return -1;
} }
pinNumber=atoi(optarg); pinNumber = atoi(optarg);
if(pinNumber > 11) { if (pinNumber > 11 || pinNumber < 0) {
pinNum2HiLo(pinNumber); pinNum2HiLo(pinNumber);
pinNumber=177; exitFlag = 1;
return -1; pinNumber = 0;
} return -1;
if(writeFlag == 1){ }
rwConflict(); if (writeFlag == 1) {
pinNumber=177; rwConflict();
return -1; pinNumber = 0;
} return -1;
readFlag=1; }
writeFlag=0; readFlag = 1;
break; writeFlag = 0;
break;
case 'i': case 'i':
if(!isstrdigit(optarg)) if (!isstrdigit(optarg)) {
{ unknownOptarg(opt, optarg);
unknownOptarg(opt, optarg); exitFlag = 1;
return -1; return -1;
} }
interfaceNum=atoi(optarg); interfaceNum = atoi(optarg);
break; if(interfaceNum >255 || interfaceNum < 0)
{
intNum2Hi(interfaceNum);
exitFlag=1;
interfaceNum=0;
return -1;
}
break;
case 'v': case 'v':
if(!isstrdigit(optarg)) if (!isstrdigit(optarg)) {
{ unknownOptarg(opt, optarg);
unknownOptarg(opt, optarg); valueToWrite = 0;
valueToWrite=177; exitFlag = 1;
writeFlag=0; writeFlag = 0;
return -1; return -1;
} }
if(readFlag==1) if (readFlag == 1) {
{ uselessValue();
uselessValue(); valueToWrite = 0;
valueToWrite=177; exitFlag = 1;
writeFlag=0; writeFlag = 0;
return -1; return -1;
} }
valueToWrite=atoi(optarg); valueToWrite = atoi(optarg);
if(valueToWrite > 255 || valueToWrite < 0) if (valueToWrite > 255 || valueToWrite < 0) {
{ value2Hi(valueToWrite);
value2Hi(valueToWrite); valueToWrite = 0;
valueToWrite=177; writeFlag = 0;
writeFlag=0; exitFlag = 1;
return -1; return -1;
} }
} }
opt = getopt(acount, arglist, "d:s:r:v:i:"); opt = getopt(acount, arglist, "d:s:r:v:i:");
} }
return 0;
} }
int exitOnError()
{
if(exitFlag==1)
{
puts("Exiting");
exit(-1);
return -1;
}
if((writeFlag | readFlag) == 0)
{
usage();
exit(-1);
return -1;
}
return 0;
}
int main(int argc, char **argv) { int main(int argc, char **argv) {
parseCmdLine(argc, argv);
//test();
exitOnError();
initLib(); initLib();
locateDevice(); locateDevice();
attachDevices(); attachDevices();
parseCmdLine(argc, argv); setGPIO();
test(); readGPIO();
return 0;
} }