Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Blinker.cpp
00001 #include "stdafx.h" 00002 00003 #include "mbed.h" 00004 #include "Blinker.h" 00005 00006 #define EYE_CLOSED 0.25 00007 #define EYE_OPEN 0.85 00008 00009 extern Logger pcSerial; 00010 00011 Blinker::Blinker( ServoMinder *servoMinder ) 00012 { 00013 m_servoMinder = servoMinder; 00014 m_servoMinder->moveTo( EYE_CLOSED ); 00015 00016 m_sleepiness = 0; 00017 m_boredom = 0; 00018 00019 m_tickTime = 0.1; 00020 m_blinkTimer = 0; 00021 00022 m_nextMove = -1; 00023 00024 00025 m_ticker.attach( this, &Blinker::tick, m_tickTime ); 00026 } 00027 00028 void Blinker::setBoredom( float boredom ) 00029 { 00030 m_boredom = boredom; 00031 } 00032 00033 void Blinker::setSleepiness( float sleepiness ) 00034 { 00035 float diff = fabs( m_sleepiness - sleepiness ); 00036 m_sleepiness = sleepiness; 00037 00038 if( diff > 0.01 ) 00039 open(); 00040 00041 } 00042 00043 float Blinker::speedForSleepiness() 00044 { 00045 // for sleepiness 0->1, return speed 2->0.5 00046 return 2 - (1.5 * m_sleepiness); 00047 } 00048 00049 float Blinker::openPosForSleepiness() 00050 { 00051 // for sleepiness 0->1, return EYE_OPEN ->( )/2 00052 return EYE_OPEN + ( (EYE_CLOSED-EYE_OPEN) * 0.8 * m_sleepiness); 00053 } 00054 00055 void Blinker::close() 00056 { 00057 m_servoMinder->setSpeed( speedForSleepiness() ); 00058 m_servoMinder->moveTo( EYE_CLOSED ); 00059 00060 } 00061 00062 void Blinker::open() 00063 { 00064 m_servoMinder->setSpeed( speedForSleepiness() ); 00065 m_servoMinder->moveTo( openPosForSleepiness() ); 00066 } 00067 00068 void Blinker::blink() 00069 { 00070 m_servoMinder->setSpeed( speedForSleepiness() ); 00071 m_servoMinder->moveTo( EYE_CLOSED ); 00072 m_nextMove = openPosForSleepiness(); 00073 } 00074 00075 void Blinker::tick() 00076 { 00077 00078 if( ! m_servoMinder->isMoving()) 00079 if( m_nextMove >= 0 ) 00080 { 00081 m_servoMinder->moveTo( m_nextMove ); 00082 m_nextMove = -1; 00083 } 00084 00085 m_blinkTimer ++; 00086 if( m_blinkTimer > 100 ) // 10 secs 00087 { 00088 m_blinkTimer = 0; 00089 blink(); 00090 } 00091 00092 if( m_boredom < m_sleepiness ) 00093 setSleepiness( m_boredom ); // wake up quickly 00094 else 00095 setSleepiness( m_sleepiness + (m_boredom - m_sleepiness) / 200 ); // fall asleep slowly 00096 } 00097
Generated on Wed Jul 27 2022 03:18:56 by
1.7.2