[Libphidget-cvs-commits] CVS: libphidget/src/examples .cvsignore,NONE,1.1 Makefile.am,NONE,1.1 phidg
Status: Alpha
Brought to you by:
jstrohm
|
From: Vadim T. <vt...@us...> - 2002-09-07 21:56:58
|
Update of /cvsroot/libphidget/libphidget/src/examples
In directory usw-pr-cvs1:/tmp/cvs-serv4882/src/examples
Added Files:
.cvsignore Makefile.am phidget_c.c phidget_cpp.cc
Log Message:
Changed the directory layout and functionality:
- ./src/phidget++ now contains the libphidget++ library components
- ./src/examples now contains the, ahem, examples
--- NEW FILE: .cvsignore ---
Makefile
Makefile.in
.deps
.libs
phidget_c
phidget_cpp
--- NEW FILE: Makefile.am ---
bin_PROGRAMS = phidget_c phidget_cpp
phidget_c_SOURCES = phidget_c.c
phidget_c_LDADD = ../libphidget/libphidget.la
phidget_c_DEPENDENCIES = ../libphidget/libphidget.la
phidget_cpp_SOURCES = phidget_cpp.cc
phidget_cpp_LDADD = ../libphidget/libphidget.la ../phidget++/libphidget++.la
phidget_cpp_DEPENDENCIES = ../libphidget/libphidget.la ../phidget++/libphidget++.la
CFLAGS = -I../libphidget -L../libphidget
CXXFLAGS = -I../libphidget -I../phidget++ -L../libphidget -L../phidget++
clean-generic:
@RM@ -f *~ .*~
if DOXYGEN_ENABLED
all-local:
@ @ECHO@ "Doxygen is not being run in this directory yet"
#@DOXYGEN@
else
all-local:
@ @ECHO@ "*** Documentation is not created - install Doxygen"
endif
--- NEW FILE: phidget_c.c ---
/*
* libphidget library
* $Id: phidget_c.c,v 1.1 2002/09/07 21:56:54 vtt Exp $
*
* Copyright (c) 2002 Jack Strohm <js...@ja...>
*
* This library is covered by the LGPL, read LICENSE for details.
*/
#include "phidget.h"
#include <stdio.h>
/**
* 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);
}
--- NEW FILE: phidget_cpp.cc ---
/*
* phidget++ library
* $Id: phidget_cpp.cc,v 1.1 2002/09/07 21:56:54 vtt Exp $
*
* Copyright (c) 2002 Jack Strohm <js...@ja...>
*
* This library is covered by the LGPL, read LICENSE for details.
*/
#include "CPhidgetManager.h"
#include "CServo.h"
#include "CPhidget.h"
#include "CServoController.h"
#include <unistd.h>
// Some globals
CUID workingUID;
/*
class CMyServoController : public CServoController
{
public:
CMyServoController(const CUID &uid) :
CServoController(uid)
{
printf("Initializing My Servo Controller\n");
}
~CMyServoController()
{
printf("Destroying My Servo Controller\n");
}
void onAttach()
{
printf("My Servo Controller: Attach\n");
}
void onDetach()
{
printf("My Servo Controller: Detach\n");
}
};
class CMyServo : public CServo
{
public:
CMyServo(const CUID &uid) :
CServo(uid)
{
printf("Construction of my servo class.\n");
}
~CMyServo()
{
printf("Destruction of my servo class.\n");
}
void position(float f)
{
CServo::position(f);
printf("Rotating servo to %f\n",f);
}
};
// Discovery of available phidgets, creation of phidget objects, and discovery of all devices afterwards.
void example1()
{
printf("example 1 - Discovery of phidgets\n");
{
// See what the system has available
CPhidgetManager *manager=CPhidgetManager::getInstance();
CPhidgetManager::getInstance()->checkForEvents();
// This queries for any devices that are phidgets
vector< CUID > devices=manager->query(LP_ALL);
printf("All:%d\n",devices.size());
CPhidgetManager::getInstance()->checkForEvents();
// Print out how many we found
devices=manager->query(LP_PHIDGET);
printf("Phidgets:%d\n",devices.size());
CPhidgetManager::getInstance()->checkForEvents();
// Get the phidgets, this forces the manager to create them
// since they haven't already been created by the user.
for (int t=0;t<devices.size();t++)
{
CPhidget *phidg=dynamic_cast<CPhidget *>(manager->find(devices[t],true));
}
CPhidgetManager::getInstance()->checkForEvents();
// Now let's see how many devices are available
devices=manager->query(LP_ALL);
printf("All:%d\n",devices.size());
CPhidgetManager::getInstance()->checkForEvents();
}
// This makes the next example run like it's the first.
CPhidgetManager::release();
printf("Done\n");
}
// Just attacheding to a servo, don't have to worry about anything else
void example2()
{
printf("example 2 - Manipulating a servo\n");
{
// Create a servo
CServo servo(CUID(741,0));
servo.position(.5);
sleep(1);
CPhidgetManager::getInstance()->checkForEvents();
}
// This makes the next example run like it's the first.
CPhidgetManager::release();
printf("Done\n");
}
void example3()
{
printf("example 3 - Custom servo device\n");
{
// Create a servo
CMyServo servo(CUID(741,0));
servo.position(.2);
sleep(1);
CPhidgetManager::getInstance()->checkForEvents();
servo.position(.5);
sleep(1);
CPhidgetManager::getInstance()->checkForEvents();
servo.position(.8);
sleep(1);
CPhidgetManager::getInstance()->checkForEvents();
}
// This makes the next example run like it's the first.
CPhidgetManager::release();
printf("Done\n");
}
void example4()
{
printf("example 4 - Custom servo device\n");
{
CMyServoController controller(CUID(741));
CServo servo(CUID(741,0));
servo.position(.2);
sleep(1);
CPhidgetManager::getInstance()->checkForEvents();
servo.position(.5);
sleep(1);
CPhidgetManager::getInstance()->checkForEvents();
servo.position(.8);
sleep(1);
CPhidgetManager::getInstance()->checkForEvents();
}
// This makes the next example run like it's the first.
CPhidgetManager::release();
printf("Done\n");
}
void example5()
{
printf("example 5 - Custom servo device\n");
{
CMyServoController controller(CUID(741));
CMyServo servo(CUID(741,0));
servo.position(.2);
sleep(1);
CPhidgetManager::getInstance()->checkForEvents();
servo.position(.5);
sleep(1);
CPhidgetManager::getInstance()->checkForEvents();
servo.position(.8);
sleep(1);
CPhidgetManager::getInstance()->checkForEvents();
}
// This makes the next example run like it's the first.
CPhidgetManager::release();
printf("Done\n");
}
*/
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);
}
int main(int argn,char *argv[])
{
try
{
// What we are
printf("phidgets: a command line interface utility\n");
printf("version $Revision: 1.1 $\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);
}
|