[Libphidget-cvs-commits] CVS: libphidget/src/examples phidget_cpp.cc,1.7,1.8
Status: Alpha
Brought to you by:
jstrohm
From: Jack S. <js...@us...> - 2002-12-15 05:05:31
|
Update of /cvsroot/libphidget/libphidget/src/examples In directory sc8-pr-cvs1:/tmp/cvs-serv20168/examples Modified Files: phidget_cpp.cc Log Message: Added support for TextLCD in the phidget++ Index: phidget_cpp.cc =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/examples/phidget_cpp.cc,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** phidget_cpp.cc 13 Dec 2002 05:01:09 -0000 1.7 --- phidget_cpp.cc 15 Dec 2002 05:05:28 -0000 1.8 *************** *** 12,15 **** --- 12,16 ---- #include "CPhidget.h" #include "CServoController.h" + #include "CTextLCD.h" #include <unistd.h> *************** *** 242,404 **** } ! void ! readUID(const string & arg) { ! bool colon_found = false; ! string temp; ! char last_char; ! int serial = -1; ! int id = -1; ! for (int t = 0; t < arg.length(); t++) { ! last_char = arg[t]; ! if (arg[t] == ':') { ! if (colon_found) ! throw runtime_error("More than one colon found in UID\n"); ! colon_found = true; ! serial = atoi(temp.c_str()); ! temp = ""; ! } else if (arg[t] >= '0' && arg[t] <= '9') { ! temp += arg.substr(t, 1); ! } else ! throw runtime_error("Invalid character in UID:" + arg); ! } ! if (colon_found && temp.length() > 0 && last_char != ':') ! id = atoi(temp.c_str()); ! else if (!colon_found) ! serial = atoi(temp.c_str()); ! workingUID = CUID (serial, id); } ! void ! query() { ! if (workingUID.serial() == -1) ! throw runtime_error("Must use parameter -u with query command\n"); ! CPhidgetManager *manager = CPhidgetManager::getInstance(); ! CUniqueDevice *dev = manager->find(workingUID, true); ! printf("%-10s ", workingUID.asString().c_str()); ! printf(">> "); ! // What type of device is this? ! CPhidget *phidget; ! if ((phidget = dynamic_cast < CPhidget *>(dev)) !=NULL) { ! printf("Phidget [%s] ", phidget->name()); ! } else { ! if (dynamic_cast < CServo *>(dev) != NULL) ! printf("Servo "); ! } ! printf("\n"); } ! void ! moveServo(const string & parm) { ! float p = atof(parm.c_str()); ! CServo servo(workingUID); ! printf("Moving servo to position %f\n", p); ! servo.position(p); ! sleep(1); } ! void ! help() { ! printf("Arguments:\n\n"); ! printf(" -l (default) list all phidets \n"); ! printf(" -u {UID} set the working UID\n"); ! printf(" -q query the working UID\n"); ! printf(" -h help\n"); ! printf(" -p {percent} adjust the position of a servo referenced by UID\n"); ! printf("\n"); } ! int ! processArgs(int argn, char *argv[]) { ! if (argn == 0) ! return (0); ! // Read an argument ! string arg = argv[0]; ! if (arg == "-u") // The unique device ID to work with ! { ! if (argn < 2) ! throw runtime_error("Expected argument after -u"); ! string parm = argv[1]; ! readUID(parm); ! return (processArgs(argn - 2, argv + 2)); ! } else if (arg == "-q") { ! query(); ! return (processArgs(argn - 1, argv + 1)); ! } else if (arg == "-h") { ! help(); ! return (processArgs(argn - 1, argv + 1)); ! } else if (arg == "-l") { ! phidgets_ls(); ! return (processArgs(argn - 1, argv + 1)); ! } else if (arg == "-p") { ! if (argn < 2) ! throw runtime_error("Expected argument after -p"); ! string parm = argv[1]; ! moveServo(parm); ! return (processArgs(argn - 2, argv + 2)); ! } ! throw runtime_error("Invalid argument:" + arg); } ! int ! main(int argn, char *argv[]) ! { ! try { ! char adjusted_version[64]; ! memcpy(adjusted_version,revision+11,strlen(revision)-11-2); ! adjusted_version[strlen(revision)-11-2]=0; ! // What we are ! printf("phidgets: a command line interface utility\n"); ! // Get the version number ! printf("Version %s\n",adjusted_version); ! // No arguments ! if (argn <= 1) { ! phidgets_ls(); ! return (0); } - // Recursive argument parser, remove the executable name - return (processArgs(argn - 1, argv + 1)); - - } - catch(const exception & e) { - printf("Exception: %s\n", e.what()); - return (-1); - } ! return (0); } --- 243,438 ---- } ! void readUID(const string & arg) { ! bool colon_found = false; ! string temp; ! char last_char; ! int serial = -1; ! int id = -1; ! for (int t = 0; t < arg.length(); t++) ! { ! last_char = arg[t]; ! if (arg[t] == ':') ! { ! if (colon_found) ! throw runtime_error("More than one colon found in UID\n"); ! colon_found = true; ! serial = atoi(temp.c_str()); ! temp = ""; ! } else ! if (arg[t] >= '0' && arg[t] <= '9') ! { ! temp += arg.substr(t, 1); ! } else ! throw runtime_error("Invalid character in UID:" + arg); ! } ! if (colon_found && temp.length() > 0 && last_char != ':') ! id = atoi(temp.c_str()); ! else ! if (!colon_found) ! serial = atoi(temp.c_str()); ! workingUID = CUID (serial, id); } ! void query() { ! if (workingUID.serial() == -1) ! throw runtime_error("Must use parameter -u with query command\n"); ! CPhidgetManager *manager = CPhidgetManager::getInstance(); ! CUniqueDevice *dev = manager->find(workingUID, true); ! printf("%-10s ", workingUID.asString().c_str()); ! printf(">> "); ! // What type of device is this? ! CPhidget *phidget; ! if ((phidget = dynamic_cast < CPhidget *>(dev)) !=NULL) ! { ! printf("Phidget [%s] ", phidget->name()); ! } ! else ! { ! if (dynamic_cast < CServo *>(dev) != NULL) ! printf("Servo "); ! } ! printf("\n"); } ! void moveServo(const string & parm) { ! float p = atof(parm.c_str()); ! CServo servo(workingUID); ! printf("Moving servo to position %f\n", p); ! servo.position(p); ! sleep(1); } ! void sendText(const string & parm) { ! CTextLCD text(workingUID); ! ! printf("Sending text to %s\n", parm.c_str()); ! ! text.sendText(parm.c_str(),0,0); ! ! sleep(1); ! } ! ! void help() ! { ! printf("Arguments:\n\n"); ! printf(" -l (default) list all phidets \n"); ! printf(" -u {UID} set the working UID\n"); ! printf(" -q query the working UID\n"); ! printf(" -h help\n"); ! printf(" -p {percent} adjust the position of a servo referenced by UID\n"); ! printf(" -t {string} send a string to a Text LCD phidget\n"); ! printf("\n"); } ! int processArgs(int argn, char *argv[]) { ! if (argn == 0) ! return (0); ! // Read an argument ! string arg = argv[0]; ! if (arg == "-u") // The unique device ID to work with ! { ! if (argn < 2) ! throw runtime_error("Expected argument after -u"); ! string parm = argv[1]; ! readUID(parm); ! return (processArgs(argn - 2, argv + 2)); ! } else ! if (arg == "-q") ! { ! query(); ! return (processArgs(argn - 1, argv + 1)); ! } else ! if (arg == "-h") ! { ! help(); ! return (processArgs(argn - 1, argv + 1)); ! } else ! if (arg == "-l") ! { ! phidgets_ls(); ! return (processArgs(argn - 1, argv + 1)); ! } else ! if (arg == "-t") ! { ! if (argn < 2) ! throw runtime_error("Expected argument after -t"); ! string parm = argv[1]; ! ! sendText(parm); ! return (processArgs(argn - 2, argv + 2)); ! } else ! if (arg == "-p") ! { ! if (argn < 2) ! throw runtime_error("Expected argument after -p"); ! string parm = argv[1]; + moveServo(parm); + return (processArgs(argn - 2, argv + 2)); + } ! throw runtime_error("Invalid argument:" + arg); } + int main(int argn, char *argv[]) + { + try + { + char adjusted_version[64]; + memcpy(adjusted_version,revision+11,strlen(revision)-11-2); + adjusted_version[strlen(revision)-11-2]=0; ! // What we are ! printf("phidgets: a command line interface utility\n"); ! // Get the version number ! printf("Version %s\n",adjusted_version); ! // No arguments ! if (argn <= 1) ! { ! phidgets_ls(); ! return (0); ! } ! // Recursive argument parser, remove the executable name ! return (processArgs(argn - 1, argv + 1)); ! } ! catch(const exception & e) ! { ! printf("Exception: %s\n", e.what()); ! return (-1); } ! return (0); } |