worked on the old monolythic design

This commit is contained in:
[ Kristjan Komloši HomePC ]
2017-01-15 17:59:58 +01:00
parent ac8aed99e3
commit 46c7fd06c5
2 changed files with 93 additions and 104 deletions
+3 -1
View File
@@ -1,2 +1,4 @@
tinio: dynamic:
g++ -lcyusbserial -lusb-1.0 main.cpp g++ -lcyusbserial -lusb-1.0 main.cpp
monolyth:
g++ -lcyusbserial -lusb-1.0 old.cpp
+90 -103
View File
@@ -1,54 +1,52 @@
// Tinio - the Tiny I/O tool // Tinio - the Tiny I/O tool
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <CyUSBCommon.h> #include <CyUSBCommon.h>
#include <libusb.h> #include <libusb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//those vars are to be used with locateDevice function // those vars are to be used with locateDevice function
const UINT8 devInfoListLen = 200; const uint8_t devInfoListLen = 200;
const CY_VID_PID deviceVidPid {UINT16(0x04b4), UINT16(0x0002)}; const CY_VID_PID deviceVidPid{UINT16(0x04b4), UINT16(0x0002)};
UINT8 deviceNumList[devInfoListLen]; uint8_t deviceNumList[devInfoListLen];
UINT8 deviceCount; uint8_t deviceCount;
CY_DEVICE_INFO deviceInfoList[devInfoListLen]; CY_DEVICE_INFO deviceInfoList[devInfoListLen];
//vars for deviceOpen function // vars for deviceOpen function
CY_HANDLE deviceHandle; CY_HANDLE deviceHandle;
UINT8 intNum; uint8_t defaultInterfaceNum = 0;
UINT8 devNum;
//----ACTUAL CODE BELOW //----ACTUAL CODE BELOW
int verifyArgNum(const int num) // verifies the number of arguments. minimum is 2 int verifyArgNum(
const int num) // verifies the number of arguments. minimum is 2
{ {
if (num < 2) if (num < 2) {
{
return 0; return 0;
} }
return 1; return 1;
} }
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("Restarting...");
if (CyLibraryExit() !=
CY_SUCCESS) // try to close the possibly running library
{ {
puts("The library init failed."); return 1; // if even that fails, abort.
puts("Restarting...");
if (CyLibraryExit() != CY_SUCCESS) //try to close the possibly running library
{
return 1; //if even that fails, abort.
}
puts("OK");
initLib(); //if the library closed gracefully
//try to reinitialize
} }
puts("OK");
initLib(); // if the library closed gracefully
// try to reinitialize
}
puts("OK"); puts("OK");
return 0;
} }
void usage() void usage() {
{
puts("Tinio test build 1"); puts("Tinio test build 1");
puts("Usage:"); puts("Usage:");
//----TODO----// //----TODO----//
@@ -56,106 +54,95 @@ void usage()
// usage summary. // usage summary.
} }
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
puts("Acces denied. Consider sudoing."); puts("Acces denied. Consider sudoing.");
return 1; return 1;
break; break;
case 2: //driver init failed. library's fault probably case 2: // driver init failed. library's fault probably
puts("Library failure. Try to rerun the tinio utility or reinstall the library"); puts("Library failure. Try to rerun the tinio utility or reinstall the "
return 2; "library");
break; return 2;
case 3: //CY_DEVICE_INFO_FETCH_FAILED error break;
puts("Device info fetch failed."); case 3: // CY_DEVICE_INFO_FETCH_FAILED error
puts("This should not happen."); puts("Device info fetch failed.");
puts("Please send me a bug report to: kristjan.komlosi@gmail.com"); puts("This should not happen.");
return 3; puts("Please send me a bug report to: kristjan.komlosi@gmail.com");
break; return 3;
case 4: break;
puts("Libusb can't open the device."); //another weird driver error case 4:
return 4; puts("Libusb can't open the device."); // another weird driver error
break; return 4;
case 5: //parameter error. shouldn't happen, but who knows... break;
puts("Parameter error!"); case 5: // parameter error. shouldn't happen, but who knows...
puts("This should not happen."); puts("Parameter error!");
puts("Please send me a bug report to: kristjan.komlosi@gmail.com"); puts("This should not happen.");
return 5; puts("Please send me a bug report to: kristjan.komlosi@gmail.com");
break; return 5;
case 6: //library request failed. The cypress or libusb lib may be uninitialized break;
puts("Request failed. Is library installed?"); case 6: // library request failed. The cypress or libusb lib may be
return 6; // uninitialized
break; puts("Request failed. Is library installed?");
case 10: //device not found. nothing special, just exit return 6;
puts("Device not found. Is tinio conected?"); break;
return 10; case 10: // device not found. nothing special, just exit
break; puts("Device not found. Is tinio conected?");
default: return 10;
printf("Error no. %i!\n", errCast); break;
puts("This should not happen."); default:
puts("Please send me a bug report to: kristjan.komlosi@gmail.com"); printf("Error no. %i!\n", errCast);
puts("This should not happen.");
puts("Please send me a bug report to: kristjan.komlosi@gmail.com");
return errCast;
} }
} }
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, &deviceCount, devInfoListLen); retVal = CyGetDeviceInfoVidPid(deviceVidPid, deviceNumList, deviceInfoList,
&deviceCount, devInfoListLen);
throw retVal; throw retVal;
} } catch (CY_RETURN_STATUS errVal) {
catch(CY_RETURN_STATUS errVal)
{
int r = evalErrors(errVal); int r = evalErrors(errVal);
if( r != 0 ) if (r != 0) {
{
return r; return r;
} }
if (deviceCount > 1) if (deviceCount > 1) {
{ puts("More than one device found. The first one will be used unless told "
puts("More than one device found. The first one will be used unless told otherwise."); "otherwise.");
return 0; return 0;
} }
return 0; return 0;
} }
} }
int selectDevice(const UINT8 whichDev = 1) int selectDevice(const uint8_t whichDev = 0) {
{ if (deviceCount == 0) {
if (deviceCount == 0)
{
puts("No devices found!"); puts("No devices found!");
return 10; return 10;
} }
if (whichDev > deviceCount) if (whichDev > deviceCount) {
{
puts("The requested device doesn't exist."); puts("The requested device doesn't exist.");
return 5; return 5;
} }
int errval = CyOpen(13, 0, &deviceHandle); CY_RETURN_STATUS retval =
printf("errrval: %i, device %i\n", errval, deviceNumList[whichDev]); CyOpen(deviceNumList[whichDev], defaultInterfaceNum, &deviceHandle);
if (!evalErrors(retval)) {
return retval;
}
return -1;
} }
int main(int argc, char const *argv[]) {
int main(int argc, char const *argv[])
{
initLib(); initLib();
locateDevice(); locateDevice();
for (size_t i = 0; i < 16; i++) {
selectDevice(i); /* code */
}
} }