test-BeoChipQt.C

00001 /*! @file Qt/test-BeoChipQt.C Qt4 interface for BeoChip control */
00002 
00003 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Qt4/test-BeoChipQt.C $
00004 // $Id: test-BeoChipQt.C 12277 2009-12-17 07:39:05Z itti $
00005 
00006 #include <Qt/qapplication.h>
00007 #include "Qt4/BeoChipMainForm.qt.H"
00008 #include "QtUtil/Util.H"
00009 
00010 // ######################################################################
00011 //! Our own little BeoChipListener
00012 class MyBeoChipListener : public BeoChipListener
00013 {
00014 public:
00015   MyBeoChipListener() :
00016     itsForm(NULL)
00017   { }
00018 
00019   virtual ~MyBeoChipListener()
00020   { }
00021 
00022   virtual void setQtForm(BeoChipMainForm *form)
00023   { itsForm = form; }
00024 
00025   virtual void event(const BeoChipEventType t, const int valint,
00026                      const float valfloat)
00027   {
00028     // Note how here we only store the values and do not immediately
00029     // update the Qt displays. This is because events may come at a
00030     // very high rate. Rather, we will update the displays at a lower
00031     // rate from a thread that reads the current values once in a
00032     // while.
00033     LDEBUG("Event: %d val = %d, fval = %f", int(t), valint, valfloat);
00034     switch(t)
00035       {
00036       case NONE:
00037         break;
00038 
00039       case PWM0:
00040         if (itsForm) itsForm->showPWM(0, valint);
00041         break;
00042 
00043       case PWM1:
00044         if (itsForm) itsForm->showPWM(1, valint);
00045         break;
00046 
00047       case KBD:
00048         if (itsForm)
00049           for (int i = 0; i < 5; i ++)
00050             if (valint & (1 << i)) itsForm->showDin(i, true);
00051             else itsForm->showDin(i, false);
00052         break;
00053 
00054       case ADC0:
00055         if (itsForm) itsForm->showAnalog(0, valint);
00056         break;
00057 
00058       case ADC1:
00059         if (itsForm) itsForm->showAnalog(1, valint);
00060         break;
00061 
00062       case RESET:
00063         if (itsForm) itsForm->beoChipReset();
00064         LERROR("BeoChip RESET occurred!");
00065         break;
00066 
00067       case ECHOREP:
00068         LINFO("BeoChip Echo reply received.");
00069         break;
00070 
00071       case INOVERFLOW:
00072         LERROR("BeoChip input overflow!");
00073         break;
00074 
00075       case SERIALERROR:
00076         LERROR("BeoChip serial error!");
00077         break;
00078 
00079       case OUTOVERFLOW:
00080         LERROR("BeoChip output overflow!");
00081         break;
00082 
00083       default:
00084         LERROR("Unknown event %d received!", int(t));
00085         break;
00086       }
00087   }
00088 
00089 protected:
00090   BeoChipMainForm *itsForm; //!< our Qt form
00091 };
00092 
00093 // ######################################################################
00094 //! GUI to play with the BeoChip
00095 int main( int argc, const char ** argv )
00096 {
00097   // instantiate a model manager:
00098   ModelManager manager("test-BeoChipQt");
00099 
00100   // instantiate our various ModelComponents:
00101   nub::soft_ref<BeoChip> bc(new BeoChip(manager));
00102   manager.addSubComponent(bc);
00103 
00104   // let's register our listener:
00105   rutz::shared_ptr<MyBeoChipListener> lis(new MyBeoChipListener);
00106   rutz::shared_ptr<BeoChipListener> lis2; lis2.dynCastFrom(lis); // cast down
00107   bc->setListener(lis2);
00108 
00109   // Parse command-line:
00110   if (manager.parseCommandLine(argc, argv, "<serdev>", 1, 1) == false)
00111     return(1);
00112 
00113   // let's configure our serial device:
00114   bc->setModelParamVal("BeoChipDeviceName", manager.getExtraArg(0));
00115 
00116   // Let's get going:
00117   manager.start();
00118 
00119   // get the Qt form up and going:
00120   QApplication a(argc, argv2qt(argc, argv));
00121   BeoChipMainForm *w = new BeoChipMainForm;
00122 
00123   // let our BeoChipListner know:
00124   lis->setQtForm(w);
00125 
00126   // get going (will reset the BeoChip):
00127   w->init(&manager, bc);
00128 
00129   // pass control on to Qt:
00130   w->show();
00131   a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
00132 
00133   // run the form until the user quits it:
00134   int retval = a.exec();
00135 
00136   // close down the manager:
00137   manager.stop();
00138 
00139   return retval;
00140 }
Generated on Sun May 8 08:41:16 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3