Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

FlashIPAddrBehavior.cc

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

Tekkotsu v2.4.1
Generated Tue Aug 16 16:32:47 2005 by Doxygen 1.4.4