Exemplo n.º 1
0
/*
 * @brief Should take an integer 0-255 and create a 500Hz PWM signal with a duty cycle from 0-100%.
 * On Photon, DAC1 and DAC2 act as true analog outputs(values: 0 to 4095) using onchip DAC peripheral
 */
void analogWrite(pin_t pin, uint16_t value)
{
    // Safety check
    if (!pinAvailable(pin))
    {
        return;
    }

    if (HAL_Validate_Pin_Function(pin, PF_DAC) == PF_DAC)
    {
        HAL_DAC_Write(pin, value);
    }
    else if (HAL_Validate_Pin_Function(pin, PF_TIMER) == PF_TIMER)
    {
        PinMode mode = HAL_Get_Pin_Mode(pin);

        if (mode != OUTPUT && mode != AF_OUTPUT_PUSHPULL)
        {
            return;
        }

        HAL_PWM_Write(pin, value);
    }
}
Exemplo n.º 2
0
/*
 * @brief Should take an integer 0-255 and create a PWM signal with a duty cycle from 0-100%
 * and frequency from 1 to 65535 Hz.
 */
void analogWrite(pin_t pin, uint16_t value, uint16_t pwm_frequency)
{
    // Safety check
    if (!pinAvailable(pin))
    {
        return;
    }

    if (HAL_Validate_Pin_Function(pin, PF_TIMER) == PF_TIMER)
    {
        PinMode mode = HAL_Get_Pin_Mode(pin);

        if (mode != OUTPUT && mode != AF_OUTPUT_PUSHPULL)
        {
            return;
        }

        HAL_PWM_Write_With_Frequency(pin, value, pwm_frequency);
    }
}
Exemplo n.º 3
0
/*
 * @brief Read the analog value of a pin.
 * Should return a 16-bit value, 0-65536 (0 = LOW, 65536 = HIGH)
 * Note: ADC is 12-bit. Currently it returns 0-4095
 */
int32_t analogRead(pin_t pin)
{
  // Allow people to use 0-7 to define analog pins by checking to see if the values are too low.
  if(pin < FIRST_ANALOG_PIN)
  {
    pin = pin + FIRST_ANALOG_PIN;
  }

  // Safety check
  if( !pinAvailable(pin) ) {
    return LOW;
  }

  if(HAL_Validate_Pin_Function(pin, PF_ADC)!=PF_ADC)
  {
    return LOW;
  }

  return HAL_ADC_Read(pin);
}
Exemplo n.º 4
0
void noTone(uint8_t pin)
{
    if (pinAvailable(pin) && HAL_Validate_Pin_Function(pin, PF_TIMER)==PF_TIMER) {
        HAL_Tone_Stop(pin);
    }
}
Exemplo n.º 5
0
void tone(uint8_t pin, unsigned int frequency, unsigned long duration)
{
    if (pinAvailable(pin) && HAL_Validate_Pin_Function(pin, PF_TIMER)==PF_TIMER) {
        HAL_Tone_Start(pin, frequency, duration);
    }
}