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