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

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:39 2016 by Doxygen 1.6.3