[Libphidget-cvs-commits] CVS: libphidget/src/examples phidget_c.c,1.4,1.5 servo_example.cc,1.4,1.5
Status: Alpha
Brought to you by:
jstrohm
From: Jack S. <js...@us...> - 2002-09-16 06:45:45
|
Update of /cvsroot/libphidget/libphidget/src/examples In directory usw-pr-cvs1:/tmp/cvs-serv32501 Modified Files: phidget_c.c servo_example.cc Log Message: Added better error checking to phidget_c.c (ignores warnings, or positive errors). Also added example4 to servo_example which shows how to derive a CServoController that can detect attach/detach events. Index: phidget_c.c =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/examples/phidget_c.c,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** phidget_c.c 14 Sep 2002 01:25:16 -0000 1.4 --- phidget_c.c 16 Sep 2002 06:45:42 -0000 1.5 *************** *** 62,66 **** phidgetTypeProductID (phidgetType (dev)), phidgetSerial (dev)); ! if (phidgetLastError()!=LPE_NONE) { printf("error - %s\n",phidgetErrorString(phidgetLastError())); --- 62,66 ---- phidgetTypeProductID (phidgetType (dev)), phidgetSerial (dev)); ! if (phidgetLastError()<0) { printf("error - %s\n",phidgetErrorString(phidgetLastError())); *************** *** 77,81 **** // Move a single servo (will move servo 0 of a 1 or 4 servo controller) ! if (phidgetSingleServo (dev, k)!=LPE_NONE) { printf("error - %s\n",phidgetErrorString(phidgetLastError())); --- 77,81 ---- // Move a single servo (will move servo 0 of a 1 or 4 servo controller) ! if (phidgetSingleServo (dev, k)<0) { printf("error - %s\n",phidgetErrorString(phidgetLastError())); *************** *** 92,96 **** // Close the phidget ! if (phidgetClose (dev)!=LPE_NONE) { printf("error - %s\n",phidgetErrorString(phidgetLastError())); --- 92,96 ---- // Close the phidget ! if (phidgetClose (dev)<0) { printf("error - %s\n",phidgetErrorString(phidgetLastError())); Index: servo_example.cc =================================================================== RCS file: /cvsroot/libphidget/libphidget/src/examples/servo_example.cc,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** servo_example.cc 12 Sep 2002 03:19:48 -0000 1.4 --- servo_example.cc 16 Sep 2002 06:45:42 -0000 1.5 *************** *** 130,133 **** --- 130,192 ---- }; + /* + * A derived class that is a servo controller + */ + void example4() + { + class CMyServoController : public CServoController + { + public: + CMyServoController(const CUID &uid) : + CServoController(uid) + { + printf(" CMyServoController: constructed\n"); + } + ~CMyServoController() + { + printf(" CMyServoController: destructor\n"); + } + virtual void onAttach() + { + CServoController::onAttach(); + printf(" CMyServoController: attach\n"); + } + virtual void onDetach() + { + CServoController::onDetach(); + printf(" CMyServoController: deattach\n"); + } + }; + + printf("example 4 - Bit more complex, construct our own derived servo controller\n"); + + // We have this brace to make sure all the stack objects get destroyed before we call cleanup, + // cleanup will throw an exception when it is called and objects are still out in the "world" + { + printf(" Request servo #0 from the servo controller\n"); + CMyServoController servoController(servoPhidgetID); + + printf(" Name:%s\n",servoController.name()); + printf(" Servo Count:%d\n",servoController.servoCount()); + + printf(" Randomly moving all servos.\n"); + for (int t=0;t<servoController.servoCount();t++) + { + servoController.servo(t,true)->position((rand()%1000)/1000.0); + } + + printf(" Wait for them to move. this will delay for 10 seconds, good time to attach/detach it.\n"); + time_t start=time(NULL); + + while ((time(NULL)-start)<10) + { + CPhidgetManager::getInstance()->checkForEvents(); + } + } + + cleanup(); + }; + + /** *************** *** 152,155 **** --- 211,215 ---- example2(); example3(); + example4(); } catch(const exception &e) |