Example #1
0
 void ServoRotator::rotate(uint16_t angle)
 {
     if (angle > maxAngle)
     {
         PC_EXCEPTION(OutOfRangeException, "Angle value is too big.");
     }
     
     if (maxDuty <= minDuty)
     {
         PC_EXCEPTION(IncorrectParamException, 
                 "Maximal duty should be greater than minimal duty.");
     }
     
     uint32_t duty = minDuty + (angle * (maxDuty - minDuty)) / 
             maxAngle;
     
     pwm.setPulseParams(duty, impPeriod);
     pwm.init();
 }
Example #2
0
    std::shared_ptr<PWMImpl> PWMFactory::createPWM(PWM_CHANNEL po)
    {
        std::lock_guard<std::mutex> l(dataMutex);
        
        if (pwmMap.find(po) != pwmMap.end())
        {
            PC_EXCEPTION(PinLockedException, "Selected PWM pin already "
                    "locked.");
        }
        
        auto gpioInstance = GPIOManager::getInstance();
        if (!gpioInstance)
        {
            PC_EXCEPTION(InternalErrorException, "Instance of GPIO manager "
                    "object is NULL.");
        }
                
        if (po < PWM_CHANNEL::PWM_0 || po >= PWM_CHANNEL::PWM_MAX)
        {
            PC_EXCEPTION(IncorrectParamException, "Invalid PWM pin index.");
        }
        
        gpioInstance->aquire(contactMap.at(po));

        std::shared_ptr<PWMImpl> sp;
        
        try
        {
            sp = std::shared_ptr<PWMImpl>(new PWMImpl(po));
        }
        catch (Exception& e)
        {
            gpioInstance->release(contactMap.at(po));
            throw e;
        }
        
        pwmMap[po] = sp;
        return sp;
    }
Example #3
0
    void PWMFactory::releasePWM(PWM_CHANNEL po)
    {
        std::lock_guard<std::mutex> l(dataMutex);
        
        auto gpioInstance = GPIOManager::getInstance();
        if (!gpioInstance)
        {
            PC_EXCEPTION(InternalErrorException, "Instance of GPIO manager "
                    "object is NULL.");
        }

        gpioInstance->release(contactMap.at(po));
        pwmMap.erase(po);
    }