[Libphidget-cvs-commits] CVS: libphidget/src/examples phidget_c.c,1.1,1.2 phidget_cpp.cc,1.1,1.2
Status: Alpha
Brought to you by:
jstrohm
From: Vadim T. <vt...@us...> - 2002-09-08 18:59:57
|
Update of /cvsroot/libphidget/libphidget/src/examples In directory usw-pr-cvs1:/tmp/cvs-serv3113/src/examples Modified Files: phidget_c.c phidget_cpp.cc Log Message: Performed 'make indent' on the code - now it conforms to GNU coding style standard. In the future, don't forget to 'make indent' on the top level before doing 'cvs commit'. Otherwise, unwanted, irrelevant, and MASSIVE diffs will result. Index: phidget_c.c =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/examples/phidget_c.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** phidget_c.c 7 Sep 2002 21:56:54 -0000 1.1 --- phidget_c.c 8 Sep 2002 18:59:54 -0000 1.2 *************** *** 17,89 **** * Simply goes thru all phidgets, looking for servos and then moving them slowly. */ ! int main(int argn, char *argv[]) { ! int t,count; ! struct phidget **phidgets; ! ! printf("----------------------------------------\n"); ! printf("Phidgets Device driver test.\n\n\n"); ! // Initialize libphiget, here is where the devices are actually retrieved by the library ! // We want the library to handle signals (passing it 1) ! phidgetInit(1); ! // Get an array of pointers to phidgets and a count of available phidgets ! phidgets=phidgetGetPhidgets(&count); ! printf("Device count:%d\n",count); ! for (t=0;t<count;t++) ! { ! unsigned char buffer[6]; ! float k; ! struct phidget *dev=phidgetOpen(phidgets[t]); // Open the phidget #t ! // Make sure we opened it successfully ! if (dev==NULL) ! { ! printf("Can't open phidget:%s\n", phidgetErrorString(phidgetLastError())); ! exit(0); ! } ! // Print out some information about this phidget ! printf("Phidget Name:%s Vendor:0x0%x Product:0x0%x Serial#:%06d\n", ! phidgetTypeName(phidgetType(dev)), ! phidgetTypeVendorID(phidgetType(dev)), ! phidgetTypeProductID(phidgetType(dev)), ! phidgetSerial(dev) ! ); ! // Is it a servo controller ! if (phidgetTypeDeviceClass(phidgetType(dev))==LP_SERVO_CONTROLLER) ! { ! for (k=0;k<1;k+=.1) ! { ! int i; ! // Move a single servo (will move servo 0 of a 1 or 4 servo controller) ! phidgetSingleServo(dev,k); ! // Wait a bit ! sleep(1); ! // Process any events that have occured, like attach/detach ! phidgetEvents(); ! } ! } ! // Close the phidget ! phidgetClose(dev); ! printf("Finished\n"); - } ! // done using all phidgets, free's phidget memory ! phidgetDeinit(); ! ! printf("----------------------------------------\n"); ! return(0); } --- 17,88 ---- * Simply goes thru all phidgets, looking for servos and then moving them slowly. */ ! int ! main (int argn, char *argv[]) { ! int t, count; ! struct phidget **phidgets; ! printf ("----------------------------------------\n"); ! printf ("Phidgets Device driver test.\n\n\n"); ! // Initialize libphiget, here is where the devices are actually retrieved by the library ! // We want the library to handle signals (passing it 1) ! phidgetInit (1); ! // Get an array of pointers to phidgets and a count of available phidgets ! phidgets = phidgetGetPhidgets (&count); ! printf ("Device count:%d\n", count); ! for (t = 0; t < count; t++) ! { ! unsigned char buffer[6]; ! float k; ! struct phidget *dev = phidgetOpen (phidgets[t]); // Open the phidget #t ! // Make sure we opened it successfully ! if (dev == NULL) ! { ! printf ("Can't open phidget:%s\n", ! phidgetErrorString (phidgetLastError ())); ! exit (0); ! } ! // Print out some information about this phidget ! printf ! ("Phidget Name:%s Vendor:0x0%x Product:0x0%x Serial#:%06d\n", ! phidgetTypeName (phidgetType (dev)), ! phidgetTypeVendorID (phidgetType (dev)), ! phidgetTypeProductID (phidgetType (dev)), phidgetSerial (dev)); ! // Is it a servo controller ! if (phidgetTypeDeviceClass (phidgetType (dev)) == LP_SERVO_CONTROLLER) ! { ! for (k = 0; k < 1; k += .1) ! { ! int i; ! // Move a single servo (will move servo 0 of a 1 or 4 servo controller) ! phidgetSingleServo (dev, k); ! // Wait a bit ! sleep (1); ! // Process any events that have occured, like attach/detach ! phidgetEvents (); ! } ! } ! // Close the phidget ! phidgetClose (dev); ! printf ("Finished\n"); ! } + // done using all phidgets, free's phidget memory + phidgetDeinit (); ! printf ("----------------------------------------\n"); ! return (0); } Index: phidget_cpp.cc =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/examples/phidget_cpp.cc,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** phidget_cpp.cc 7 Sep 2002 21:56:54 -0000 1.1 --- phidget_cpp.cc 8 Sep 2002 18:59:54 -0000 1.2 *************** *** 195,382 **** */ ! void phidgets_ls() { ! CPhidgetManager *manager=CPhidgetManager::getInstance(); ! vector <CUID> phidgets=manager->query(LP_PHIDGET); ! printf("\n"); ! printf(" UID Name Misc\n"); ! printf("-------- -------------------------------- ----------------\n"); ! for (int t=0;t<phidgets.size();t++) ! { ! const CUID &uid=phidgets[t]; ! CPhidget *phidget=dynamic_cast<CPhidget *>(manager->find(uid,true)); ! if (phidget==NULL) ! throw runtime_error("obtained a phidget CUID that was not a phidget, this can't happen."); ! printf("%8d %32s ", ! phidget->UID().serial(), ! phidget->name() ! ); ! CServoController *sc=dynamic_cast<CServoController *>(phidget); ! if (sc!=NULL) ! { ! printf("Servos: %d\n",sc->servoCount()); ! } ! /* ! else ! if (ik!=NULL) ! { ! printf("DigitalIn: %d\n",ik->digitalIns().size()); ! printf(" DigitalOut: %d\n",ik->digitalOuts().size()); ! printf(" AnalogIns: %d\n",ik->analogIns().size()); ! } ! */ ! else ! printf("Unsupported\n"); ! } ! printf("-------- -------------------------------- ----------------\n"); ! printf("\n\n"); ! printf("Total phidgets:%d\n",phidgets.size()); ! printf("\n"); } ! 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); } --- 195,391 ---- */ ! void ! phidgets_ls () { ! CPhidgetManager *manager = CPhidgetManager::getInstance (); ! vector < CUID > phidgets = manager->query (LP_PHIDGET); ! printf ("\n"); ! printf (" UID Name Misc\n"); ! printf ("-------- -------------------------------- ----------------\n"); ! for (int t = 0; t < phidgets.size (); t++) ! { ! const CUID & uid = phidgets[t]; ! CPhidget *phidget = ! dynamic_cast < CPhidget * >(manager->find (uid, true)); ! if (phidget == NULL) ! throw ! runtime_error ! ("obtained a phidget CUID that was not a phidget, this can't happen."); ! printf ("%8d %32s ", phidget->UID ().serial (), phidget->name ()); ! CServoController *sc = dynamic_cast < CServoController * >(phidget); ! if (sc != NULL) ! { ! printf ("Servos: %d\n", sc->servoCount ()); ! } ! /* ! else ! if (ik!=NULL) ! { ! printf("DigitalIn: %d\n",ik->digitalIns().size()); ! printf(" DigitalOut: %d\n",ik->digitalOuts().size()); ! printf(" AnalogIns: %d\n",ik->analogIns().size()); ! } ! */ ! else ! printf ("Unsupported\n"); ! } ! printf ("-------- -------------------------------- ----------------\n"); ! printf ("\n\n"); ! printf ("Total phidgets:%d\n", phidgets.size ()); ! ! printf ("\n"); } ! 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); } *************** *** 384,412 **** ! int main(int argn,char *argv[]) { ! try ! { ! // What we are ! printf("phidgets: a command line interface utility\n"); ! printf("version $Revision$\n\n"); ! ! // 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); } --- 393,421 ---- ! int ! main (int argn, char *argv[]) { ! try ! { ! // What we are ! printf ("phidgets: a command line interface utility\n"); ! printf ("version $Revision$\n\n"); ! // 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); } |