Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

FlashIPAddrBehavior.cc

Go to the documentation of this file.
00001 #include "Shared/RobotInfo.h"
00002 #ifdef TGT_HAS_LEDS
00003 
00004 #include "Shared/ERS210Info.h"
00005 #include "Shared/ERS220Info.h"
00006 #include "Shared/ERS7Info.h"
00007 
00008 #include "FlashIPAddrBehavior.h"
00009 #include "Events/EventRouter.h"
00010 #include "Motion/MMAccessor.h"
00011 #include "Motion/LedEngine.h"
00012 #include "Shared/WorldState.h"
00013 #include "Shared/Config.h"
00014 #include "Sound/SoundManager.h"
00015 #include "Wireless/Wireless.h"
00016 
00017 void FlashIPAddrBehavior::DoStart() {
00018   BehaviorBase::DoStart(); // do this first
00019   if(config->behaviors.flash_on_start) {
00020     setupSequence();
00021     loadSounds();
00022     ms_id = motman->addPrunableMotion(ms,MotionManager::kEmergencyPriority+1);
00023     erouter->addListener(this,EventBase::motmanEGID,ms_id,EventBase::deactivateETID);
00024   }
00025 #ifdef TGT_HAS_BUTTONS
00026   erouter->addListener(this,EventBase::buttonEGID,button1);
00027   erouter->addListener(this,EventBase::buttonEGID,button2);
00028 #endif
00029 }
00030 
00031 void FlashIPAddrBehavior::DoStop() {
00032   erouter->removeListener(this);
00033   motman->removeMotion(ms_id);
00034   ms_id=MotionManager::invalid_MC_ID;
00035   releaseSounds();
00036   BehaviorBase::DoStop(); // do this last
00037 }
00038 
00039 void FlashIPAddrBehavior::processEvent(const EventBase& e) {
00040   if(e.getGeneratorID()==EventBase::timerEGID) {
00041 
00042     if(e.getSourceID()==ACTIVATE_TIMER) {
00043       //buttons have been held down long enough, time to run display
00044       if(ms_id!=MotionManager::invalid_MC_ID) {
00045         //there's already one running, have to check it out to clear it
00046         MMAccessor<MSMC_t> ms_acc(ms_id);
00047         setupSequence();
00048       } else
00049         setupSequence();
00050       loadSounds();
00051       ms_id = motman->addPrunableMotion(ms);
00052       erouter->addListener(this,EventBase::motmanEGID,ms_id,EventBase::deactivateETID);
00053         
00054     } else { //its time to play a digit sound file
00055       //the source id was set to correspond to an element of the sounds vector
00056       if(e.getSourceID()>=sounds.size())
00057         serr->printf("ERROR: %s received invalid timer event %s\n",getName().c_str(),e.getName().c_str());
00058       else {
00059         sndman->playFile(sounds[e.getSourceID()]);
00060         if(e.getSourceID()==sounds.size()-1)
00061           releaseSounds();
00062       }
00063         
00064     }
00065 
00066 #ifdef TGT_HAS_BUTTONS
00067   } else if(e.getGeneratorID()==EventBase::buttonEGID) {
00068     //if it's an activate, start a timer to expire in a few seconds
00069     //if it's a deactivate, cancel that timer
00070     if(e.getTypeID()==EventBase::activateETID) {
00071       if(state->buttons[button1] && state->buttons[button2])
00072         erouter->addTimer(this,ACTIVATE_TIMER,2000,false);
00073     } else if(e.getTypeID()==EventBase::deactivateETID)
00074       erouter->removeTimer(this,ACTIVATE_TIMER);
00075 #endif
00076 
00077   } else if(e.getGeneratorID()==EventBase::motmanEGID) {
00078     // display has completed, mark it as such
00079     if(e.getSourceID()!=ms_id)
00080       serr->printf("WARNING: %s received event %s, doesn't match ms_id (%d)\n",getName().c_str(),e.getName().c_str(),ms_id);
00081     ms_id=MotionManager::invalid_MC_ID;
00082     erouter->removeListener(this,EventBase::motmanEGID);
00083       
00084   }
00085 }
00086 
00087 void FlashIPAddrBehavior::loadSounds() {
00088   for(unsigned int i=0; i<sounds.size(); i++)
00089     sndman->loadFile(sounds[i]);
00090 }
00091 
00092 void FlashIPAddrBehavior::releaseSounds() {
00093   for(unsigned int i=0; i<sounds.size(); i++)
00094     sndman->releaseFile(sounds[i]);
00095   sounds.clear();
00096 }
00097 
00098 void FlashIPAddrBehavior::setupSequence() {
00099   const unsigned int DISP_TIME=600;
00100   const unsigned int GROUP_TIME=500;
00101   const unsigned int DOT_TIME=400;
00102   const unsigned int FADE_TIME=1;
00103   const unsigned int BLANK_TIME=100-FADE_TIME*2;
00104   erouter->removeTimer(this);
00105   ms->clear();
00106   releaseSounds();
00107   unsigned int a=wireless->getIPAddress();
00108   unsigned int n=config->behaviors.flash_bytes;
00109   if(n>4)
00110     n=4;
00111   LedEngine disp;
00112   for(unsigned int i=n-1; i!=-1U; i--) {
00113     unsigned int byte=(a>>(i*8))&0xFF;
00114     unsigned int digits=1;
00115     if(byte>=10)
00116       digits++;
00117     if(byte>=100)
00118       digits++;
00119     //cout << "byte " << i << " is " << byte << " -- " << digits << " digits" << endl;
00120     //cout << "Setting LEDs: ";
00121     for(unsigned int d=0; d<digits; d++) {
00122       unsigned int digit=byte;
00123       for(unsigned int j=d;j<digits-1;j++)
00124         digit/=10;
00125       digit-=(digit/10)*10;
00126       disp.displayNumber(digit,LedEngine::onedigit);
00127       std::string soundfile="numbers/";
00128       soundfile+=(digit+'0');
00129       soundfile+=".wav";
00130       erouter->addTimer(this,sounds.size(),ms->getTime()+delay,false);
00131       sounds.push_back(soundfile);
00132       for(unsigned int j=0; j<NumLEDs; j++)
00133         if(FaceLEDMask&(1<<j)) {
00134           //if(disp.getValue(static_cast<LEDOffset_t>(LEDOffset+j)))
00135           //cout << j << ' ';
00136           ms->setOutputCmd(LEDOffset+j,disp.getValue(static_cast<LEDOffset_t>(LEDOffset+j)));
00137         }
00138       ms->advanceTime(DISP_TIME);
00139       for(unsigned int j=0; j<NumLEDs; j++)
00140         if(FaceLEDMask&(1<<j))
00141           ms->setOutputCmd(LEDOffset+j,disp.getValue(static_cast<LEDOffset_t>(LEDOffset+j)));
00142       ms->advanceTime(FADE_TIME);
00143       for(unsigned int j=0; j<NumLEDs; j++)
00144         if(FaceLEDMask&(1<<j))
00145           ms->setOutputCmd(LEDOffset+j,0);
00146       ms->advanceTime(BLANK_TIME);
00147       if(d==digits-1)
00148         ms->advanceTime(GROUP_TIME);
00149       for(unsigned int j=0; j<NumLEDs; j++)
00150         if(FaceLEDMask&(1<<j))
00151           ms->setOutputCmd(LEDOffset+j,0);
00152       ms->advanceTime(FADE_TIME);
00153     }
00154     //cout << endl;
00155     if(i!=0) {
00156       LEDBitMask_t dot=LedEngine::defaultCountNumMasks[10]; //default in case we don't recognize model
00157 #ifdef TGT_HAS_LED_PANEL
00158       if(RobotName == ERS210Info::TargetName) {
00159         dot=LedEngine::defaultMimicNumMasks[10];
00160       } else if(RobotName == ERS220Info::TargetName) {
00161         dot=LedEngine::ERS220numMasks[10];
00162       } else if(RobotName == ERS7Info::TargetName) {
00163         dot=LedEngine::ERS7numMasks[10];
00164       }
00165 #endif
00166       erouter->addTimer(this,sounds.size(),ms->getTime()+delay,false);
00167       sounds.push_back("numbers/dot.wav");
00168       for(unsigned int j=0; j<NumLEDs; j++)
00169         if(FaceLEDMask&(1<<j))
00170           ms->setOutputCmd(LEDOffset+j,(dot>>j)&1);
00171       ms->advanceTime(DOT_TIME);
00172       for(unsigned int j=0; j<NumLEDs; j++)
00173         if(FaceLEDMask&(1<<j))
00174           ms->setOutputCmd(LEDOffset+j,(dot>>j)&1);
00175       ms->advanceTime(FADE_TIME);
00176       for(unsigned int j=0; j<NumLEDs; j++)
00177         if(FaceLEDMask&(1<<j))
00178           ms->setOutputCmd(LEDOffset+j,0);
00179       ms->advanceTime(BLANK_TIME);
00180       for(unsigned int j=0; j<NumLEDs; j++)
00181         if(FaceLEDMask&(1<<j))
00182           ms->setOutputCmd(LEDOffset+j,0);
00183       ms->advanceTime(FADE_TIME);
00184     }
00185   }
00186   ms->play();
00187 }
00188 
00189 #endif
00190 
00191 /*! @file
00192  * @brief Implements FlashIPAddrBehavior, which displays IP address by flashing a series of numbers on the LED face panel
00193  * @author ejt (Creator)
00194  *
00195  * $Author: ejt $
00196  * $Name: tekkotsu-4_0 $
00197  * $Revision: 1.5 $
00198  * $State: Exp $
00199  * $Date: 2007/08/05 16:16:04 $
00200  */

Tekkotsu v4.0
Generated Thu Nov 22 00:54:53 2007 by Doxygen 1.5.4