resources/libtiny/Servo.cpp
//=============================================================================
// FUNCTION:    void attach(uint8_t pin)
//
// DESCRIPTION: Attaches a servo motor to an i/o pin on Port B.
//
// INPUT:       pin - which pin on portB to attach to
//
// RETURNS:     The servo number of this servo.
//
//=============================================================================
void attach(uint8_t pin)
DESCRIPTION: Attaches a servo motor to an i/o pin on Port B.

which pin on portB to attach to

The servo number of this servo.
void attach ( uint8_t pin ) DESCRIPTION : Attaches a servo motor to an i / o pin on Port B . which pin on portB to attach to The servo number of this servo .
uint8_t Servo::attach(uint8_t pin)
{ 
  delay(1000);
    if(servoIndex == INVALID_SERVO)
  {
    servoIndex = ServoSequencer::registerServo();
    if(servoIndex == INVALID_SERVO)
    {
      return INVALID_SERVO;
    }
  }
  #if defined(__AVR_ATtinyX5__)
    if( pin <= 5 )
    {
        DDRB |= (1<<pin); 
        ServoSequencer::setServoPin(servoIndex, pin);
        ServoSequencer::enableDisableServo(servoIndex, true);
    }
  #elif defined(__AVR_ATtinyX61__)
    if (pin <= 16) {
      uint8_t bit = digitalPinToBitMask(pin);
      uint8_t port = digitalPinToPort(pin);
      volatile uint8_t *reg;
      if (port == NOT_A_PIN) return INVALID_SERVO;
      reg = portModeRegister(port);
      *reg |= bit;
      ServoSequencer::setServoPin(servoIndex, pin);
      ServoSequencer::enableDisableServo(servoIndex, true);
    }
  #else 
    #error "This part isn't supported - how did execution get here?"
  #endif
    else
    {
    }
    return servoIndex;
}
uint8_t Servo :: attach ( uint8_t pin ) { delay ( 1000 ) ; if ( servoIndex == INVALID_SERVO ) { servoIndex = ServoSequencer :: registerServo ( ) ; if ( servoIndex == INVALID_SERVO ) { return INVALID_SERVO ; } } #if defined ( __AVR_ATtinyX5__ ) if ( pin <= 5 ) { DDRB |= ( 1 << pin ) ; ServoSequencer :: setServoPin ( servoIndex , pin ) ; ServoSequencer :: enableDisableServo ( servoIndex , true ) ; } #elif defined ( __AVR_ATtinyX61__ ) if ( pin <= 16 ) { uint8_t bit = digitalPinToBitMask ( pin ) ; uint8_t port = digitalPinToPort ( pin ) ; volatile uint8_t * reg ; if ( port == NOT_A_PIN ) return INVALID_SERVO ; reg = portModeRegister ( port ) ; * reg |= bit ; ServoSequencer :: setServoPin ( servoIndex , pin ) ; ServoSequencer :: enableDisableServo ( servoIndex , true ) ; } #else #error  "This part isn't supported - how did execution get here?" #endif else { } return servoIndex ; }
FUNCTION:    void attach(uint8_t pin)
DESCRIPTION: Attaches a servo motor to an i/o pin on Port B.
FUNCTION : void attach ( uint8_t pin ) DESCRIPTION : Attaches a servo motor to an i / o pin on Port B .
//Do we need to register with the servo sequencer? //Yep, we do, so register and save our servo number. //We got an invalid servo number. That means the servo sequencer is full and can't handle any more servos. //valid pin values are between 0 and 5, inclusive. //set pin as output //set the servo pin //enable the servo to start outputing the pwm wave //enable the servo to start outputing the pwm wave //bad pin value. do nothing.