Exemplo n.º 1
0
upm_result_t speaker_set_frequency(const speaker_context dev,
                                   unsigned int freq)
{
    assert(dev != NULL);

    if (!dev->is_pwm)
        return UPM_ERROR_NO_RESOURCES;

    if (freq < 50 || freq > 32000)
    {
        printf("%s: freq must be between 50 and 32000.\n", __FUNCTION__);

        return UPM_ERROR_OPERATION_FAILED;
    }

    float period = 1.0 / (float)freq;

    // printf("PERIOD: %f (freq %d)\n", period, freq);

    if (period >= 0.001) // ms range
    {
        if (mraa_pwm_period(dev->pwm, period))
        {
            printf("%s: mraa_pwm_period() failed.\n", __FUNCTION__);

            return UPM_ERROR_OPERATION_FAILED;
        }
    }
    else // us range.  With a max of 32KHz, no less than 3.125 us.
    {
        if (mraa_pwm_period_us(dev->pwm, (int)(period * 1000000)))
        {
            printf("%s: mraa_pwm_period_us() failed.\n", __FUNCTION__);

            return UPM_ERROR_OPERATION_FAILED;
        }
    }

    // save it for later if needed
    dev->default_freq = freq;

    // A 10% duty cycle enables better results at high frequencies
    mraa_pwm_write(dev->pwm, 0.1);

    return UPM_SUCCESS;
}
Exemplo n.º 2
0
 /**
  * Set the PWM period as seconds represented in a float
  *
  * @param period Period represented as a float in seconds
  * @return Result of operation
  */
 mraa_result_t
 period(float period)
 {
     return mraa_pwm_period(m_pwm, period);
 }